From d9887892253a59f8651e3354a097fd035e865261 Mon Sep 17 00:00:00 2001 From: Alexander Walther Date: Sat, 25 May 2024 20:47:53 +0200 Subject: [PATCH 1/2] initial commit --- README.md | 13 ++- assets/js/backend.js | 85 +++++++++++++++++++ boot.php | 42 +++++++++ .../backend/WildcardQuicknavigationButton.php | 26 ++++++ install.php | 40 ++++----- lang/de_de.lang | 13 ++- lib/QuickNavigationButton.php | 11 +++ lib/Sync.php | 56 ++++++++++-- lib/rex_api_call_wildcard.php | 22 +++++ lib/wildcard.php | 9 ++ package.yml | 6 +- pages/docs.php | 4 + pages/index.php | 1 + pages/settings.php | 2 +- pages/wildcard.php | 2 +- 15 files changed, 293 insertions(+), 39 deletions(-) create mode 100644 assets/js/backend.js create mode 100644 fragments/wildcard/backend/WildcardQuicknavigationButton.php create mode 100644 lib/QuickNavigationButton.php create mode 100644 lib/rex_api_call_wildcard.php create mode 100644 pages/docs.php diff --git a/README.md b/README.md index 0398794..843cb06 100644 --- a/README.md +++ b/README.md @@ -1,13 +1,18 @@ -# Wildcard - Ersetzungen von Sprach-Platzhaltern in REDAXO neu gedacht +# 🎴 Wildcard - Ersetzungen von Sprach-Platzhaltern in REDAXO neu gedacht Wildcard lässt dich Platzhalter nutzen, die du in deinem Addon oder Theme definierst. Redakteure können dadurch deine Sprachübersetzungen nutzen oder mit eigenen überschreiben. Die Änderungen bleiben dann auch bei einem Update deines Addons erhalten. -## Geplante Features +## Features +* Ersetzung von Platzhaltern in jeder Sprache * Einfache Verwaltung durch YForm: Exportieren und importieren von Sprachdateien -* Automatische Erkennung von Sprachdateien in Addons (Verzeichnis: `package/wildcard/de_de.lang`) +* Synchronisation von Wildcard-Dateien von Addons in das Projekt +* Einfaches Kopieren und Einfügen von Platzhaltern + +## Geplante Features + +* Automatische Erkennung von Sprachdateien in Addons (Verzeichnis: `src/addons//wildcard/translate.json`) * Kombination mit YForm_Inline-Addon für eine einfachere Bearbeitung -* Unterstützung von Cloud-Übersetzungstools ## Voraussetzungen diff --git a/assets/js/backend.js b/assets/js/backend.js new file mode 100644 index 0000000..c05f593 --- /dev/null +++ b/assets/js/backend.js @@ -0,0 +1,85 @@ +function data_copy(event) { + // Holt das aktuelle Element, auf das geklickt wurde + var element = event.currentTarget; + // Fügt die Klasse "copied" zum aktuellen Element hinzu + element.classList.add('copied'); + // Sucht das icon-Element im aktuellen Element + var iconElement = element.querySelector('i'); + // Entfernt die Klasse "fa-clone" vom i-Element + iconElement.classList.remove('fa-clone'); + // Fügt die Klasse "fa-check" zum i-Element hinzu + iconElement.classList.add('fa-check'); + // Kopiert den Wert des data-wildcard-copy Attributs in die Zwischenablage + navigator.clipboard.writeText(element.getAttribute('data-wildcard-copy')); +}; + +$(document).on("rex:ready", function(wildcard, container) { + + // Fügt den Click-Event-Listener zu allen div-Elementen mit dem data-wildcard-copy Attribut hinzu + document.querySelectorAll('div[data-wildcard-copy]').forEach(function(el) { + el.addEventListener('click', data_copy) + }) + + // Warte auf QuickNavigation - Event an das Formular erst nach 2 Sekunden + setTimeout(function() { + + // Fügt den Submit-Event-Listener zum Formular mit der ID "wildcard_search" hinzu + $('#wildcard_search').on('submit', function(e) { + // Verhindert das Absenden des Formulars + e.preventDefault(); + + // Sendet eine AJAX-Anfrage + $.ajax({ + url: '/', // URL, an die die Anfrage gesendet wird + type: 'GET', // Methode der Anfrage + data: { + "rex-api-call": 'wildcard_search', // Daten, die an den Server gesendet werden + "q": $('input[name="q"]').val() // Wert des Eingabefelds + }, + success: function(response) { + // Erstellt ein leeres table-Element und fügt Klassen hinzu + var table = $('
'); + table.addClass('table table-striped table-hover'); + // Erstellt ein leeres tbody-Element + var tbody = $(''); + + // Durchläuft jedes Element im Antwortobjekt + $.each(response, function(key, value) { + // Erstellt ein neues tr-Element und fügt das gewünschte HTML hinzu + var tr = $(''); + var th = $(''); + var div = $('
').attr('data-wildcard-copy', key).attr('role', 'button'); + var i = $('').addClass('fa fa-clone').attr('aria-hidden', 'true'); + var code = $('').text(key); + div.append(i); + div.append(code); + th.append(div); + var td = $('').text("⫸ " + value['de_DE']); + tr.append(th); + tr.append(td); + + // Fügt das tr-Element zum tbody-Element hinzu + tbody.append(tr); + // Fügt das tbody-Element zum table-Element hinzu + table.append(tbody); + }); + + // Fügt das table-Element in das div mit der ID "wildcardSearchResults" ein + $('#wildcardSearchResults').html(table); + + // Fügt den Click-Event-Listener zu allen neuen div-Elementen mit dem data-wildcard-copy Attribut hinzu + document.querySelectorAll('div[data-wildcard-copy]').forEach(function(el) { + el.addEventListener('click', data_copy) + }) + + }, + error: function() { + // Zeigt eine Fehlermeldung an, wenn ein Fehler auftritt + $('#wildcardSearchResults').html('Ein Fehler ist aufgetreten.'); + } + }); + }); + + }, 2000); + +}); diff --git a/boot.php b/boot.php index 2cf16c9..52e7a1c 100644 --- a/boot.php +++ b/boot.php @@ -4,8 +4,10 @@ use rex; use rex_addon; +use rex_config; use rex_extension; use rex_yform_manager_dataset; +use FriendsOfRedaxo\QuickNavigation\Button\ButtonRegistry; if (rex_addon::get('yform')->isAvailable() && !rex::isSafeMode()) { rex_yform_manager_dataset::setModelClass( @@ -14,6 +16,11 @@ ); } +if(rex::isBackend() && rex::isDebugMode() && rex_addon::get('developer') && rex_addon::get('developer')->isAvailable()) { + FragmentScanner::scan('sh_neues'); + Sync::fileToDb(); +} + require_once __DIR__ . '/functions/wildcard.php'; if (rex::isFrontend()) { @@ -24,3 +31,38 @@ rex_extension::register('CLANG_ADDED', 'Alexplusde\Wildcard\Wildcard::addClangColumn'); rex_extension::register('CLANG_DELETED', 'Alexplusde\Wildcard\Wildcard::removeClangColumn'); } + +/* Darstellung im Backend der Datalist ändern */ +if (rex::isBackend()) { + rex_extension::register('YFORM_DATA_LIST', function ($ep) { + if ($ep->getParam('table')->getTableName() == 'rex_wildcard') { + $list = $ep->getSubject(); + + $list->setColumnFormat( + 'package', + 'custom', + 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').'
'; + } + ); + $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') . '
'; + } + ); + } + }); +} + +/* Javascript-Asset laden */ +if (rex::isBackend() && rex::getUser()) { + \rex_view::addJsFile($this->getAssetsUrl('js/backend.js')); +} + +/* Wenn quick_navigation installiert, dann */ +ButtonRegistry::registerButton(new QuickNavigationButton(), 5); diff --git a/fragments/wildcard/backend/WildcardQuicknavigationButton.php b/fragments/wildcard/backend/WildcardQuicknavigationButton.php new file mode 100644 index 0000000..9f4aecb --- /dev/null +++ b/fragments/wildcard/backend/WildcardQuicknavigationButton.php @@ -0,0 +1,26 @@ + + diff --git a/install.php b/install.php index af71d65..2509564 100644 --- a/install.php +++ b/install.php @@ -4,7 +4,8 @@ $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 @@ -12,29 +13,30 @@ // 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 ('wildcard' === $field['name'] && 2 === $field['prio']) { + if ($field['name'] === 'wildcard' && $field['prio'] === 2) { 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; } @@ -47,6 +49,7 @@ 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')); @@ -54,8 +57,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_' . $clang->getId(), 'text', true)); - $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/lang/de_de.lang b/lang/de_de.lang index 99a2681..069e14e 100644 --- a/lang/de_de.lang +++ b/lang/de_de.lang @@ -6,19 +6,24 @@ wildcard_docs = Hilfe # Konfigurationsformulare wildcard_config = Einstellungen -wildcard_config_starttag_label = Start-Marker -wildcard_config_starttag_notice = z.B. {{ -wildcard_config_endtag_label = End-Marker -wildcard_config_endtag_notice = z.B. }} +wildcard_config_opentag_label = Start-Marker +wildcard_config_opentag_notice = z.B. {{ +wildcard_config_closetag_label = End-Marker +wildcard_config_closetag_notice = z.B. }} # YForm Tabellen-Übersetzung wildcard_package = Package (AddOn) wildcard_wildcard = Platzhalter wildcard_text_de = DE +wildcard_text_de_DE = de_DE wildcard_text_en = EN +wildcard_text_en_GB = en_GB wildcard_text_fr = FR wildcard_text_es = ES wildcard_createdate = Erstellt am... wildcard_createuser = Erstellt von... wildcard_updatedate = Zuletzt geändert am... wildcard_updateuser = Zuletzt geändert von... + +wildcard_quicknavigation_search_button = Suchen +wildcard_quicknavigation_search_placeholder = Platzhalter oder Text eingeben diff --git a/lib/QuickNavigationButton.php b/lib/QuickNavigationButton.php new file mode 100644 index 0000000..725c3e3 --- /dev/null +++ b/lib/QuickNavigationButton.php @@ -0,0 +1,11 @@ +parse('wildcard/backend/WildcardQuicknavigationButton.php'); + } +} diff --git a/lib/Sync.php b/lib/Sync.php index bed7676..2c3c357 100644 --- a/lib/Sync.php +++ b/lib/Sync.php @@ -3,17 +3,59 @@ namespace Alexplusde\Wildcard; use rex_config; +use rex_path; class Sync { - /* - * WIP + /* Synchronisiere die Wildcard-Dateien mit der Datenbank. */ + private static function getWildcardFiles(string $packageName = null) :array + { + 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'); + } + public static function fileToDb() + { + // Hole alle Wildcard-Dateien. + $wildcardFiles = self::getWildcardFiles(); + + foreach ($wildcardFiles as $wildcardFile) { + // Lese die Datei ein. + $wildcardData = json_decode(\rex_file::get($wildcardFile), true); + $packageName = basename(dirname(dirname($wildcardFile))); + + // Trage alle Übersetzungen in die Datenbank ein. + if(!isset($wildcardData['wildcards'])) { + continue; + } + + foreach($wildcardData['wildcards'] as $wildcard => $data) { + + if(!$wildcard || !$data) { + continue; + } + + $wildcard_dataset = wildcard::findByWildcard($packageName, $wildcard); + if($wildcard_dataset && ($wildcard_dataset->getUpdatedate() <= $data['timestamp'])) { + // continue; + } + 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['de_DE'])) { + dd($data['de_DE']); + $wildcard_dataset->setValue('text_de_DE', $data['de_DE']); + } + $wildcard_dataset->setValue('updatedate', $data['timestamp']); + $wildcard_dataset->save(); - public static function sync() - { - // Prüfe, ob die Datei neuer ist als der Zeitstempel in der Datenbank. - if (date(rex_config::get('wildcard', 'syncdatestamp') >= date())) { } + } - */ + } } diff --git a/lib/rex_api_call_wildcard.php b/lib/rex_api_call_wildcard.php new file mode 100644 index 0000000..e1fa291 --- /dev/null +++ b/lib/rex_api_call_wildcard.php @@ -0,0 +1,22 @@ +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]; + } + /* Header setzen */ + header('Content-Type: application/json'); + echo json_encode($wildcards); + exit; + } +} diff --git a/lib/wildcard.php b/lib/wildcard.php index f7d7ff2..b1f6448 100644 --- a/lib/wildcard.php +++ b/lib/wildcard.php @@ -17,8 +17,17 @@ public static function getCurrentFieldName($field = 'text', $separator = '_') return $field . $separator . rex_clang::getCurrentId(); } + public static function findByWildcard(string $package = '', string $wildcard): ?self + { + return self::query() + ->where('wildcard', $wildcard) + ->where('package', $package) + ->findOne(); + } + public static function findWildcard(string $wildcard, mixed $clang_code = null) { + $clang_code ??= rex_clang::getCurrent()->getCode(); $wildcard = self::query() ->where('wildcard', $wildcard) diff --git a/package.yml b/package.yml index 38be7aa..3f869d6 100644 --- a/package.yml +++ b/package.yml @@ -1,5 +1,5 @@ package: wildcard -version: '0.1.0-dev-2024-05-12' +version: '0.1.0-2024-05-12' author: 'Alexander Walther' supportpage: https://github.com/alexplusde/wildcard @@ -38,8 +38,8 @@ page: default_config: syncdatestamp: '' - open_tag: '{{' - close_tag: '}}' + opentag: '{{' + closetag: '}}' installer_ignore: - .git diff --git a/pages/docs.php b/pages/docs.php new file mode 100644 index 0000000..3105372 --- /dev/null +++ b/pages/docs.php @@ -0,0 +1,4 @@ +getProperty('yform', []); $yform = $yform[rex_be_controller::getCurrentPage()] ?? []; From 21a382edf4a7b2bf71d4277b54c4ac2498935006 Mon Sep 17 00:00:00 2001 From: alxndr-w Date: Sat, 25 May 2024 18:49:11 +0000 Subject: [PATCH 2/2] Apply php-cs-fixer changes --- boot.php | 28 ++++++------- .../backend/WildcardQuicknavigationButton.php | 8 ++-- install.php | 39 +++++++++---------- lib/QuickNavigationButton.php | 12 ++++-- lib/Sync.php | 34 ++++++++-------- lib/rex_api_call_wildcard.php | 8 ++-- lib/wildcard.php | 11 +++--- pages/docs.php | 5 +-- 8 files changed, 74 insertions(+), 71 deletions(-) diff --git a/boot.php b/boot.php index 52e7a1c..8da96ce 100644 --- a/boot.php +++ b/boot.php @@ -2,12 +2,14 @@ namespace Alexplusde\Wildcard; +use FriendsOfRedaxo\QuickNavigation\Button\ButtonRegistry; use rex; use rex_addon; use rex_config; use rex_extension; +use rex_package; +use rex_view; use rex_yform_manager_dataset; -use FriendsOfRedaxo\QuickNavigation\Button\ButtonRegistry; if (rex_addon::get('yform')->isAvailable() && !rex::isSafeMode()) { rex_yform_manager_dataset::setModelClass( @@ -16,7 +18,7 @@ ); } -if(rex::isBackend() && rex::isDebugMode() && rex_addon::get('developer') && rex_addon::get('developer')->isAvailable()) { +if (rex::isBackend() && rex::isDebugMode() && rex_addon::get('developer') && rex_addon::get('developer')->isAvailable()) { FragmentScanner::scan('sh_neues'); Sync::fileToDb(); } @@ -34,26 +36,26 @@ /* 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( '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') . '
'; + }, ); } }); @@ -61,7 +63,7 @@ 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 */ 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/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 2c3c357..86e1226 100644 --- a/lib/Sync.php +++ b/lib/Sync.php @@ -2,19 +2,24 @@ namespace Alexplusde\Wildcard; -use rex_config; +use rex_file; use rex_path; +use function dirname; + +use const DIRECTORY_SEPARATOR; + 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,40 +27,37 @@ 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['de_DE'])) { + if (isset($data['de_DE'])) { dd($data['de_DE']); $wildcard_dataset->setValue('text_de_DE', $data['de_DE']); } $wildcard_dataset->setValue('updatedate', $data['timestamp']); $wildcard_dataset->save(); - } - } } } 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 b1f6448..8721c24 100644 --- a/lib/wildcard.php +++ b/lib/wildcard.php @@ -17,17 +17,16 @@ 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) { - $clang_code ??= rex_clang::getCurrent()->getCode(); $wildcard = self::query() ->where('wildcard', $wildcard) 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 @@ -