From 205445f95157341f77bcbd35b3a6ce7aabba4baa Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Mon, 8 Jul 2024 11:34:32 +0200 Subject: [PATCH 1/4] Update boot.php --- boot.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/boot.php b/boot.php index 4a2fb03..b99e3c8 100644 --- a/boot.php +++ b/boot.php @@ -79,3 +79,20 @@ function ($a) { /* Wenn quick_navigation installiert, dann */ ButtonRegistry::registerButton(new QuickNavigationButton(), 5); + +if(rex::isBackend()) { + $addon = rex_addon::get('wildcard'); + $page = $addon->getProperty('page'); + + $_csrf_key = \rex_yform_manager_table::get('rex_wildcard')->getCSRFKey(); + $token = \rex_csrf_token::factory($_csrf_key)->getUrlParams(); + + $params = array(); + $params['table_name'] = 'wildcard'; // Tabellenname anpassen + $params['rex_yform_manager_popup'] = '0'; + $params['_csrf_token'] = $token['_csrf_token']; + $params['func'] = 'add'; + + $page['title'] .= ' +'; + $addon->setProperty('page', $page); +} From 1709c98af0271f4e826b813d2487e31c524de711 Mon Sep 17 00:00:00 2001 From: alxndr-w Date: Mon, 8 Jul 2024 09:38:32 +0000 Subject: [PATCH 2/4] Apply php-cs-fixer changes --- boot.php | 50 ++++++++++--------- .../backend/WildcardQuicknavigationButton.php | 8 +-- install.php | 39 +++++++-------- lib/FragmentScanner.php | 49 +++++++----------- lib/QuickNavigationButton.php | 12 +++-- lib/Sync.php | 50 ++++++++++--------- lib/rex_api_call_wildcard.php | 8 +-- lib/wildcard.php | 14 +++--- pages/docs.php | 5 +- pages/settings.php | 1 - 10 files changed, 114 insertions(+), 122 deletions(-) diff --git a/boot.php b/boot.php index b99e3c8..1e4853a 100644 --- a/boot.php +++ b/boot.php @@ -2,12 +2,17 @@ namespace Alexplusde\Wildcard; +use FriendsOfRedaxo\QuickNavigation\Button\ButtonRegistry; use rex; use rex_addon; use rex_config; +use rex_csrf_token; use rex_extension; +use rex_package; +use rex_url; +use rex_view; use rex_yform_manager_dataset; -use FriendsOfRedaxo\QuickNavigation\Button\ButtonRegistry; +use rex_yform_manager_table; if (rex_addon::get('yform')->isAvailable() && !rex::isSafeMode()) { rex_yform_manager_dataset::setModelClass( @@ -16,7 +21,7 @@ ); } -if(rex::isBackend() && rex::isDebugMode() && rex_config::get('wildcard', 'sync', true)) { +if (rex::isBackend() && rex::isDebugMode() && rex_config::get('wildcard', 'sync', true)) { // FragmentScanner::scan(); Sync::fileToDb(); // Sync::dbToFile(); @@ -35,38 +40,37 @@ /* Darstellung im Backend der Datalist ändern */ if (rex::isBackend()) { - rex_extension::register('YFORM_DATA_LIST', function ($ep) { - if ($ep->getParam('table')->getTableName() == 'rex_wildcard') { + rex_extension::register('YFORM_DATA_LIST', static function ($ep) { + if ('rex_wildcard' == $ep->getParam('table')->getTableName()) { $list = $ep->getSubject(); $list->setColumnFormat( 'package', 'custom', - function ($a) { + static function ($a) { /* get the icon of the package.yml of the addon */ - $packageIcon = \rex_package::get($a['list']->getValue('package'))->getProperty('page')['icon'] ?? 'rex-icon-package'; - return '
 '.$a['list']->getValue('package').'
'; - } + $packageIcon = rex_package::get($a['list']->getValue('package'))->getProperty('page')['icon'] ?? 'rex-icon-package'; + return '
 ' . $a['list']->getValue('package') . '
'; + }, ); $list->setColumnFormat( 'Funktion ', 'custom', - function ($a) { - - if($a['list']->getValue('package') != 'project' || $a['list']->getValue('package') == '') { + static function ($a) { + if ('project' != $a['list']->getValue('package') || '' == $a['list']->getValue('package')) { return ''; } return $a['subject']; - } + }, ); $list->setColumnFormat( 'wildcard', 'custom', - function ($a) { - $value = rex_config::get('wildcard', 'opentag') . $a['list']->getValue('wildcard') . rex_config::get('wildcard', 'closetag'); - return '
'.$a['list']->getValue('wildcard') . '
'; - } + static function ($a) { + $value = rex_config::get('wildcard', 'opentag') . $a['list']->getValue('wildcard') . rex_config::get('wildcard', 'closetag'); + return '
' . $a['list']->getValue('wildcard') . '
'; + }, ); } }); @@ -74,25 +78,25 @@ function ($a) { /* Javascript-Asset laden */ if (rex::isBackend() && rex::getUser()) { - \rex_view::addJsFile($this->getAssetsUrl('js/backend.js')); + rex_view::addJsFile($this->getAssetsUrl('js/backend.js')); } /* Wenn quick_navigation installiert, dann */ ButtonRegistry::registerButton(new QuickNavigationButton(), 5); -if(rex::isBackend()) { +if (rex::isBackend()) { $addon = rex_addon::get('wildcard'); $page = $addon->getProperty('page'); - - $_csrf_key = \rex_yform_manager_table::get('rex_wildcard')->getCSRFKey(); - $token = \rex_csrf_token::factory($_csrf_key)->getUrlParams(); - $params = array(); + $_csrf_key = rex_yform_manager_table::get('rex_wildcard')->getCSRFKey(); + $token = rex_csrf_token::factory($_csrf_key)->getUrlParams(); + + $params = []; $params['table_name'] = 'wildcard'; // Tabellenname anpassen $params['rex_yform_manager_popup'] = '0'; $params['_csrf_token'] = $token['_csrf_token']; $params['func'] = 'add'; - $page['title'] .= ' +'; + $page['title'] .= ' +'; $addon->setProperty('page', $page); } diff --git a/fragments/wildcard/backend/WildcardQuicknavigationButton.php b/fragments/wildcard/backend/WildcardQuicknavigationButton.php index 9f4aecb..d663e14 100644 --- a/fragments/wildcard/backend/WildcardQuicknavigationButton.php +++ b/fragments/wildcard/backend/WildcardQuicknavigationButton.php @@ -7,8 +7,8 @@ diff --git a/install.php b/install.php index 2509564..4b3fa9d 100644 --- a/install.php +++ b/install.php @@ -4,8 +4,7 @@ $addon = rex_addon::get('wildcard'); if (rex_addon::get('yform')->isAvailable() && !rex::isSafeMode()) { - - // Laden Sie die JSON-Datei + // Laden Sie die JSON-Datei $tableset = rex_file::get(rex_path::addon($addon->getName(), 'install/rex_wildcard.tableset.json')); // Konvertieren Sie die JSON-Datei in ein PHP-Array @@ -13,30 +12,29 @@ // Durchsuchen Sie das Array nach dem Feld mit dem Namen "wildcard" und der Priorität 2 foreach ($data['rex_wildcard']['fields'] as $index => $field) { - if ($field['name'] === 'wildcard' && $field['prio'] === 2) { + if ('wildcard' === $field['name'] && 2 === $field['prio']) { foreach (rex_clang::getAll() as $clang) { - // Erstellen Sie das neue Feld $newField = [ - "table_name" => "rex_wildcard", - "prio" => 3, - "type_id" => "value", - "type_name" => "textarea", - "db_type" => "text", - "list_hidden" => 0, - "search" => 1, - "name" => "text_".$clang->getCode(), - "label" => "translate:wildcard_text_".$clang->getCode(), - "not_required" => "", - "attributes" => "", - "default" => "", - "no_db" => "0", - "notice" => "", + 'table_name' => 'rex_wildcard', + 'prio' => 3, + 'type_id' => 'value', + 'type_name' => 'textarea', + 'db_type' => 'text', + 'list_hidden' => 0, + 'search' => 1, + 'name' => 'text_' . $clang->getCode(), + 'label' => 'translate:wildcard_text_' . $clang->getCode(), + 'not_required' => '', + 'attributes' => '', + 'default' => '', + 'no_db' => '0', + 'notice' => '', ]; // Fügen Sie das neue Feld direkt nach dem gefundenen Feld ein array_splice($data['rex_wildcard']['fields'], $index + 1, 0, [$newField]); - } + } // Sobald das neue Feld hinzugefügt wurde, beenden Sie die Schleife break; } @@ -49,7 +47,6 @@ rex_yform_manager_table::deleteCache(); } - /* Zusätzliche Eigenschaften an der Tabelle direkt setzen, z.B. Index-Felder zur Performance-Optimierung */ $table = rex_sql_table::get(rex::getTable('wildcard')); @@ -57,7 +54,7 @@ $table = $table->ensureColumn(new rex_sql_column('package', 'varchar(191)', false, 'project')); $table = $table->ensureColumn(new rex_sql_column('wildcard', 'varchar(191)', false, '')); foreach (rex_clang::getAll() as $clang) { - $table = $table->ensureColumn(new rex_sql_column('text_' . \rex_string::normalize($clang->getCode()), 'text', true)); + $table = $table->ensureColumn(new rex_sql_column('text_' . rex_string::normalize($clang->getCode()), 'text', true)); } $table = $table->ensureColumn(new rex_sql_column('createdate', 'datetime')); $table = $table->ensureColumn(new rex_sql_column('createuser', 'varchar(191)', false, '')); diff --git a/lib/FragmentScanner.php b/lib/FragmentScanner.php index 40eec4d..b3523d8 100644 --- a/lib/FragmentScanner.php +++ b/lib/FragmentScanner.php @@ -5,47 +5,43 @@ use rex_file; use rex_path; +use function array_key_exists; + +use const DIRECTORY_SEPARATOR; +use const GLOB_BRACE; +use const JSON_PRETTY_PRINT; + class FragmentScanner { - /** - * @var string - */ + /** @var string */ private $packageDir; - /** - * @var string - */ + /** @var string */ private $wildcardFile; - /** - * @var array - */ + /** @var array */ private $wildcardContent; /** * FragmentScanner constructor. - * - * @param string $packageName */ public function __construct(string $packageDir) { - if(!is_dir($packageDir)) { + if (!is_dir($packageDir)) { return; } $this->packageDir = $packageDir; - $this->wildcardFile = $packageDir . 'wildcard'.\DIRECTORY_SEPARATOR.'translations.json'; + $this->wildcardFile = $packageDir . 'wildcard' . DIRECTORY_SEPARATOR . 'translations.json'; $this->wildcardContent = $this->getWildcardContent(); } /** * Liefert alle Fragment-Dateien, die Platzhalter enthalten könnten. - * - * @return array */ public function getFiles(): array { return array_merge( - glob($this->packageDir . 'fragments' . \DIRECTORY_SEPARATOR . '*.php', GLOB_BRACE), - glob($this->packageDir . 'fragments' . \DIRECTORY_SEPARATOR . '**' . \DIRECTORY_SEPARATOR . '*.php', GLOB_BRACE) + glob($this->packageDir . 'fragments' . DIRECTORY_SEPARATOR . '*.php', GLOB_BRACE), + glob($this->packageDir . 'fragments' . DIRECTORY_SEPARATOR . '**' . DIRECTORY_SEPARATOR . '*.php', GLOB_BRACE), ); } @@ -61,8 +57,6 @@ private function ensureWildcardFileExists(): void /** * Gibt den Inhalt der Wildcard-Datei als PHP-Array zurück. - * - * @return array */ private function getWildcardContent(): ?array { @@ -71,13 +65,10 @@ private function getWildcardContent(): ?array /** * Überprüft, ob der Key bereits in Wildcard vorhanden ist. - * - * @param string $key - * @return bool */ public function keyExists(string $key): bool { - if(!isset($this->wildcardContent['wildcards'])) { + if (!isset($this->wildcardContent['wildcards'])) { return false; } return array_key_exists($key, $this->wildcardContent['wildcards']); @@ -85,15 +76,13 @@ public function keyExists(string $key): bool /** * Fügt den Key zu den Wildcards hinzu. - * - * @param string $key */ public function addKey(string $key): void { if (!$this->keyExists($key)) { $this->wildcardContent['wildcards'][$key] = [ 'timestamp' => date('Y-m-d H:i:s'), - 'translations' => [] + 'translations' => [], ]; } } @@ -109,18 +98,16 @@ public function writeWildcardFile(): void /** * Führt die Scan-Funktion für alle Addons oder ein spezifisches Addon aus und aktualisiert die Wildcard-Dateien. - * - * @param string $dir */ - public static function scan(string $packageName = null): void + public static function scan(?string $packageName = null): void { $dirs = glob(rex_path::addon('*')); - if($packageName) { + if ($packageName) { $dirs = [rex_path::addon($packageName)]; } foreach ($dirs as $dir) { - $scanner = new FragmentScanner($dir); + $scanner = new self($dir); $files = $scanner->getFiles(); foreach ($files as $file) { $content = rex_file::get($file); diff --git a/lib/QuickNavigationButton.php b/lib/QuickNavigationButton.php index 725c3e3..84204e5 100644 --- a/lib/QuickNavigationButton.php +++ b/lib/QuickNavigationButton.php @@ -1,11 +1,15 @@ parse('wildcard/backend/WildcardQuicknavigationButton.php'); } } diff --git a/lib/Sync.php b/lib/Sync.php index 1d42aea..174442b 100644 --- a/lib/Sync.php +++ b/lib/Sync.php @@ -2,19 +2,25 @@ namespace Alexplusde\Wildcard; -use rex_config; +use rex_file; use rex_path; +use function dirname; + +use const DIRECTORY_SEPARATOR; +use const JSON_PRETTY_PRINT; + class Sync { /* Synchronisiere die Wildcard-Dateien mit der Datenbank. */ - private static function getWildcardFiles(string $packageName = null) :array + private static function getWildcardFiles(?string $packageName = null): array { - if($packageName) { - return [rex_path::addon($packageName) . 'wildcard'. \DIRECTORY_SEPARATOR .'translations.json']; + if ($packageName) { + return [rex_path::addon($packageName) . 'wildcard' . DIRECTORY_SEPARATOR . 'translations.json']; } - return glob(rex_path::src('addons') . \DIRECTORY_SEPARATOR . '*' . \DIRECTORY_SEPARATOR . 'wildcard'. \DIRECTORY_SEPARATOR .'translations.json'); + return glob(rex_path::src('addons') . DIRECTORY_SEPARATOR . '*' . DIRECTORY_SEPARATOR . 'wildcard' . DIRECTORY_SEPARATOR . 'translations.json'); } + public static function fileToDb() { // Hole alle Wildcard-Dateien. @@ -22,62 +28,58 @@ public static function fileToDb() foreach ($wildcardFiles as $wildcardFile) { // Lese die Datei ein. - $wildcardData = json_decode(\rex_file::get($wildcardFile), true); - $packageName = basename(dirname(dirname($wildcardFile))); + $wildcardData = json_decode(rex_file::get($wildcardFile), true); + $packageName = basename(dirname($wildcardFile, 2)); // Trage alle Übersetzungen in die Datenbank ein. - if(!isset($wildcardData['wildcards'])) { + if (!isset($wildcardData['wildcards'])) { continue; } - foreach($wildcardData['wildcards'] as $wildcard => $data) { - - if(!$wildcard || !$data) { + foreach ($wildcardData['wildcards'] as $wildcard => $data) { + if (!$wildcard || !$data) { continue; } $wildcard_dataset = wildcard::findByWildcard($packageName, $wildcard); - if($wildcard_dataset && ($wildcard_dataset->getUpdatedate() <= $data['timestamp'])) { + if ($wildcard_dataset && ($wildcard_dataset->getUpdatedate() <= $data['timestamp'])) { // continue; } - if(!$wildcard_dataset) { + if (!$wildcard_dataset) { /** @var Wildcard $yform_wildcard */ $wildcard_dataset = Wildcard::create(); $wildcard_dataset->setValue('package', $packageName); $wildcard_dataset->setValue('wildcard', $wildcard); $wildcard_dataset->setValue('createuser', '🎴'); } - if(isset($data['translations'])) { - foreach($data['translations'] as $lang => $text) { + if (isset($data['translations'])) { + foreach ($data['translations'] as $lang => $text) { $wildcard_dataset->setValue('text_' . $lang, $text); } } $wildcard_dataset->setValue('updatedate', $data['timestamp']); $wildcard_dataset->save(); - } - } } + public static function dbToFile() { // Hole alle Wildcard-Dateien. $wildcardFiles = self::getWildcardFiles(); foreach ($wildcardFiles as $wildcardFile) { - $packageName = basename(dirname(dirname($wildcardFile))); + $packageName = basename(dirname($wildcardFile, 2)); $wildcardData = ['wildcards' => []]; $wildcards = Wildcard::query()->where('package', $packageName)->find(); - foreach($wildcards as $wildcard) { + foreach ($wildcards as $wildcard) { $wildcardData['wildcards'][$wildcard->getValue('wildcard')] = [ 'timestamp' => $wildcard->getUpdatedate(), - 'translations' => - ['de_DE' => $wildcard->getValue('text_de_DE'), - 'en_GB' => $wildcard->getValue('text_en_GB')] + 'translations' => ['de_DE' => $wildcard->getValue('text_de_DE'), + 'en_GB' => $wildcard->getValue('text_en_GB')], ]; } - \rex_file::put($wildcardFile, json_encode($wildcardData, JSON_PRETTY_PRINT)); + rex_file::put($wildcardFile, json_encode($wildcardData, JSON_PRETTY_PRINT)); } } - } diff --git a/lib/rex_api_call_wildcard.php b/lib/rex_api_call_wildcard.php index e1fa291..97c422a 100644 --- a/lib/rex_api_call_wildcard.php +++ b/lib/rex_api_call_wildcard.php @@ -1,5 +1,7 @@ whereRaw( - "`package` LIKE '%".$search."%' OR `wildcard` LIKE '%".$search."%' OR `text_de_DE` LIKE '%".$search."%'" - )->limit(10)->orderBy('package')->orderBy('wildcard')->find(['search' => $search]); + $result = Wildcard::query()->whereRaw( + "`package` LIKE '%" . $search . "%' OR `wildcard` LIKE '%" . $search . "%' OR `text_de_DE` LIKE '%" . $search . "%'", + )->limit(10)->orderBy('package')->orderBy('wildcard')->find(['search' => $search]); $wildcards = []; foreach ($result as $item) { $wildcards[$item->wildcard] = ['de_DE' => $item->text_de_DE]; diff --git a/lib/wildcard.php b/lib/wildcard.php index 0378cc6..7273975 100644 --- a/lib/wildcard.php +++ b/lib/wildcard.php @@ -6,6 +6,7 @@ use rex_clang; use rex_config; use rex_extension_point; +use rex_package; use rex_sql_table; use rex_user; use rex_yform_manager_dataset; @@ -17,12 +18,12 @@ public static function getCurrentFieldName($field = 'text', $separator = '_') return $field . $separator . rex_clang::getCurrentId(); } - public static function findByWildcard(string $package = '', string $wildcard): ?self + public static function findByWildcard(string $package, string $wildcard): ?self { - return self::query() - ->where('wildcard', $wildcard) - ->where('package', $package) - ->findOne(); + return self::query() + ->where('wildcard', $wildcard) + ->where('package', $package) + ->findOne(); } public static function findWildcard(string $wildcard, mixed $clang_code = null) @@ -38,7 +39,6 @@ public static function findWildcard(string $wildcard, mixed $clang_code = null) return ''; } - public static function parse(string $text, ?int $clang_code = null) { $open_tag = self::getOpenTag(); @@ -193,7 +193,7 @@ public static function addClangColumn(rex_extension_point $ep): void public static function packageChoices(): array { $choices = []; - foreach (\rex_package::getRegisteredPackages() as $package) { + foreach (rex_package::getRegisteredPackages() as $package) { $choices[$package->getName()] = $package->getName(); } return $choices; diff --git a/pages/docs.php b/pages/docs.php index 3105372..82fb108 100644 --- a/pages/docs.php +++ b/pages/docs.php @@ -1,4 +1 @@ -addOption(rex_i18n::msg('wildcard_config_sync_true'), true); $select->addOption(rex_i18n::msg('wildcard_config_sync_false'), false); - $fragment = new rex_fragment(); $fragment->setVar('class', 'edit', false); $fragment->setVar('title', $addon->i18n('wildcard_config'), false); From 0448934b97c37943a037d89e4735ba96e1efe1fe Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Sat, 27 Jul 2024 19:04:45 +0200 Subject: [PATCH 3/4] Update boot.php --- boot.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/boot.php b/boot.php index 1e4853a..fcac66f 100644 --- a/boot.php +++ b/boot.php @@ -84,19 +84,25 @@ static function ($a) { /* Wenn quick_navigation installiert, dann */ ButtonRegistry::registerButton(new QuickNavigationButton(), 5); -if (rex::isBackend()) { + +if (rex::isBackend() && \rex_addon::get('wildcard') && \rex_addon::get('wildcard')->isAvailable() && !rex::isSafeMode()) { $addon = rex_addon::get('wildcard'); $page = $addon->getProperty('page'); - $_csrf_key = rex_yform_manager_table::get('rex_wildcard')->getCSRFKey(); - $token = rex_csrf_token::factory($_csrf_key)->getUrlParams(); + if(!rex::getConsole()) { + $_csrf_key = rex_yform_manager_table::get('rex_wildcard')->getCSRFKey(); + + $token = rex_csrf_token::factory($_csrf_key)->getUrlParams(); + + $params = []; + $params['table_name'] = 'rex_wildcard'; // Tabellenname anpassen + $params['rex_yform_manager_popup'] = '0'; + $params['_csrf_token'] = $token['_csrf_token']; + $params['func'] = 'add'; - $params = []; - $params['table_name'] = 'wildcard'; // Tabellenname anpassen - $params['rex_yform_manager_popup'] = '0'; - $params['_csrf_token'] = $token['_csrf_token']; - $params['func'] = 'add'; + $href = rex_url::backendPage('wildcard/entry', $params); - $page['title'] .= ' +'; - $addon->setProperty('page', $page); + $page['title'] .= ' +'; + $addon->setProperty('page', $page); + } } From a31aaad71278eb066cb7c303346cb9f390b0119b Mon Sep 17 00:00:00 2001 From: alxndr-w Date: Sat, 27 Jul 2024 17:05:05 +0000 Subject: [PATCH 4/4] Apply php-cs-fixer changes --- boot.php | 7 +++---- pages/docs.php | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/boot.php b/boot.php index fcac66f..6a13d6a 100644 --- a/boot.php +++ b/boot.php @@ -84,14 +84,13 @@ static function ($a) { /* Wenn quick_navigation installiert, dann */ ButtonRegistry::registerButton(new QuickNavigationButton(), 5); - -if (rex::isBackend() && \rex_addon::get('wildcard') && \rex_addon::get('wildcard')->isAvailable() && !rex::isSafeMode()) { +if (rex::isBackend() && rex_addon::get('wildcard') && rex_addon::get('wildcard')->isAvailable() && !rex::isSafeMode()) { $addon = rex_addon::get('wildcard'); $page = $addon->getProperty('page'); - if(!rex::getConsole()) { + if (!rex::getConsole()) { $_csrf_key = rex_yform_manager_table::get('rex_wildcard')->getCSRFKey(); - + $token = rex_csrf_token::factory($_csrf_key)->getUrlParams(); $params = []; diff --git a/pages/docs.php b/pages/docs.php index 82fb108..638714f 100644 --- a/pages/docs.php +++ b/pages/docs.php @@ -1 +1 @@ -