ver. 0.278: Settings + Languages frontend migration, bug fix get_single_settings_value
- Add cached frontend methods to existing Domain repositories (allSettings, getSingleValue, defaultLanguage, activeLanguages, translations) - Convert front\factory\Settings and Languages to facades delegating to Domain repositories - Fix get_single_settings_value() - was hardcoded to 'firm_name', now uses $param correctly - Add CacheHandler stub methods (get/set/exists) to test bootstrap - Establish architectural rule: Domain classes are shared between admin and frontend Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -332,6 +332,85 @@ class LanguagesRepository
|
||||
return $translationId;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca ID domyslnego jezyka (z flaga start=1) z cache Redis.
|
||||
*/
|
||||
public function defaultLanguage(): string
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = 'Domain\Languages\LanguagesRepository::defaultLanguage';
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
if ($objectData) {
|
||||
return unserialize($objectData);
|
||||
}
|
||||
|
||||
$results = $this->db->query(
|
||||
'SELECT id FROM pp_langs WHERE status = 1 ORDER BY start DESC, o ASC LIMIT 1'
|
||||
)->fetchAll();
|
||||
|
||||
$defaultLanguage = $results[0][0] ?? 'pl';
|
||||
|
||||
$cacheHandler->set($cacheKey, $defaultLanguage);
|
||||
|
||||
return $defaultLanguage;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca liste aktywnych jezykow z cache Redis.
|
||||
*/
|
||||
public function activeLanguages(): array
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = 'Domain\Languages\LanguagesRepository::activeLanguages';
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
if ($objectData) {
|
||||
return unserialize($objectData);
|
||||
}
|
||||
|
||||
$activeLanguages = $this->db->select(
|
||||
'pp_langs',
|
||||
['id', 'name'],
|
||||
['status' => 1, 'ORDER' => ['o' => 'ASC']]
|
||||
);
|
||||
|
||||
if (!is_array($activeLanguages)) {
|
||||
$activeLanguages = [];
|
||||
}
|
||||
|
||||
$cacheHandler->set($cacheKey, $activeLanguages);
|
||||
|
||||
return $activeLanguages;
|
||||
}
|
||||
|
||||
/**
|
||||
* Zwraca tlumaczenia dla danego jezyka z cache Redis.
|
||||
*/
|
||||
public function translations(string $language = 'pl'): array
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "Domain\Languages\LanguagesRepository::translations:$language";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
if ($objectData) {
|
||||
return unserialize($objectData);
|
||||
}
|
||||
|
||||
$translations = ['0' => $language];
|
||||
|
||||
$results = $this->db->select('pp_langs_translations', ['text', $language]);
|
||||
if (is_array($results)) {
|
||||
foreach ($results as $row) {
|
||||
$translations[$row['text']] = $row[$language];
|
||||
}
|
||||
}
|
||||
|
||||
$cacheHandler->set($cacheKey, $translations);
|
||||
|
||||
return $translations;
|
||||
}
|
||||
|
||||
private function sanitizeLanguageId(string $languageId): ?string
|
||||
{
|
||||
$languageId = strtolower(trim($languageId));
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
namespace Domain\Settings;
|
||||
|
||||
/**
|
||||
* Repozytorium ustawien panelu administratora.
|
||||
* Repozytorium ustawien — wspolne dla admin i frontendu.
|
||||
*/
|
||||
class SettingsRepository
|
||||
{
|
||||
@@ -141,6 +141,58 @@ class SettingsRepository
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobranie wszystkich ustawien z cache Redis.
|
||||
*
|
||||
* @param bool $skipCache Pomija cache (np. przy generowaniu htaccess)
|
||||
*/
|
||||
public function allSettings(bool $skipCache = false): array
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = 'Domain\Settings\SettingsRepository::allSettings';
|
||||
|
||||
if (!$skipCache) {
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
if ($objectData) {
|
||||
return unserialize($objectData);
|
||||
}
|
||||
}
|
||||
|
||||
$results = $this->db->select('pp_settings', '*');
|
||||
$settings = [];
|
||||
|
||||
if (is_array($results)) {
|
||||
foreach ($results as $row) {
|
||||
$settings[$row['param']] = $row['value'];
|
||||
}
|
||||
}
|
||||
|
||||
$cacheHandler->set($cacheKey, $settings);
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pobranie pojedynczej wartosci ustawienia po nazwie parametru.
|
||||
*/
|
||||
public function getSingleValue(string $param): string
|
||||
{
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "Domain\Settings\SettingsRepository::getSingleValue:$param";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
if ($objectData) {
|
||||
return unserialize($objectData);
|
||||
}
|
||||
|
||||
$value = $this->db->get('pp_settings', 'value', ['param' => $param]);
|
||||
$value = (string)($value ?? '');
|
||||
|
||||
$cacheHandler->set($cacheKey, $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
private function isEnabled($value): bool
|
||||
{
|
||||
if (is_bool($value)) {
|
||||
|
||||
@@ -1,78 +1,29 @@
|
||||
<?php
|
||||
namespace front\factory;
|
||||
|
||||
/**
|
||||
* Fasada delegujaca do Domain\Languages\LanguagesRepository.
|
||||
*/
|
||||
class Languages
|
||||
{
|
||||
public static function default_language()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\front\factory\Languages::default_language";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if ( !$objectData )
|
||||
public static function default_language()
|
||||
{
|
||||
$results = $mdb -> query( 'SELECT id FROM pp_langs WHERE status = 1 ORDER BY start DESC, o ASC LIMIT 1' ) -> fetchAll();
|
||||
$default_language = $results[0][0];
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $default_language );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize($objectData);
|
||||
}
|
||||
return $default_language;
|
||||
}
|
||||
|
||||
public static function active_languages()
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\front\factory\Languages::active_languages";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
{
|
||||
$active_languages = $mdb -> select( 'pp_langs', [ 'id', 'name' ], [ 'status' => 1, 'ORDER' => [ 'o' => 'ASC' ] ] );
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $active_languages );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
global $mdb;
|
||||
$repo = new \Domain\Languages\LanguagesRepository($mdb);
|
||||
return $repo->defaultLanguage();
|
||||
}
|
||||
|
||||
return $active_languages;
|
||||
}
|
||||
|
||||
public static function lang_translations( $language = 'pl' )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\front\factory\Languages::lang_translations:$language";
|
||||
|
||||
$objectData = $cacheHandler -> get( $cacheKey );
|
||||
|
||||
if ( !$objectData )
|
||||
public static function active_languages()
|
||||
{
|
||||
$translations[ '0' ] = $language;
|
||||
|
||||
$results = $mdb -> select( 'pp_langs_translations', [ 'text', $language ] );
|
||||
if ( is_array( $results ) ) foreach ( $results as $row )
|
||||
$translations[ $row['text'] ] = $row[ $language ];
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $translations );
|
||||
}
|
||||
else
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
global $mdb;
|
||||
$repo = new \Domain\Languages\LanguagesRepository($mdb);
|
||||
return $repo->activeLanguages();
|
||||
}
|
||||
|
||||
return $translations;
|
||||
}
|
||||
public static function lang_translations($language = 'pl')
|
||||
{
|
||||
global $mdb;
|
||||
$repo = new \Domain\Languages\LanguagesRepository($mdb);
|
||||
return $repo->translations($language);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,43 +1,22 @@
|
||||
<?php
|
||||
namespace front\factory;
|
||||
|
||||
/**
|
||||
* Fasada delegujaca do Domain\Settings\SettingsRepository.
|
||||
*/
|
||||
class Settings
|
||||
{
|
||||
public static function settings_details( $admin = false )
|
||||
{
|
||||
global $mdb;
|
||||
|
||||
$cacheHandler = new \CacheHandler();
|
||||
$cacheKey = "\front\factory\Settings::settings_details";
|
||||
|
||||
$objectData = $cacheHandler->get($cacheKey);
|
||||
|
||||
if ( !$objectData or $admin )
|
||||
public static function settings_details($admin = false)
|
||||
{
|
||||
$results = $mdb -> select( 'pp_settings', '*' );
|
||||
if ( is_array( $results ) ) foreach ( $results as $row )
|
||||
$settings[ $row['param'] ] = $row['value'];
|
||||
|
||||
$cacheHandler -> set( $cacheKey, $settings );
|
||||
global $mdb;
|
||||
$repo = new \Domain\Settings\SettingsRepository($mdb);
|
||||
return $repo->allSettings($admin);
|
||||
}
|
||||
else
|
||||
|
||||
public static function get_single_settings_value($param)
|
||||
{
|
||||
return unserialize( $objectData );
|
||||
global $mdb;
|
||||
$repo = new \Domain\Settings\SettingsRepository($mdb);
|
||||
return $repo->getSingleValue($param);
|
||||
}
|
||||
|
||||
return $settings;
|
||||
}
|
||||
|
||||
static public function get_single_settings_value( $param ) {
|
||||
|
||||
global $mdb;
|
||||
|
||||
if ( !$value = \Cache::fetch( "get_single_settings_value:$param" ) ) {
|
||||
|
||||
$value = $mdb -> get( 'pp_settings', 'value', [ 'param' => 'firm_name' ] );
|
||||
\Cache::store( "get_single_settings_value:$param", $value );
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user