Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

+-Schaltfläche im Hauptmenü zum Hinzufügen neuer Einträge #10

Merged
merged 4 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 43 additions & 17 deletions boot.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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();
Expand All @@ -35,47 +40,68 @@

/* 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 '<div class="text-nowrap"><i class="rex-icon '.$packageIcon.'"></i>&nbsp;'.$a['list']->getValue('package').'</div>';
}
$packageIcon = rex_package::get($a['list']->getValue('package'))->getProperty('page')['icon'] ?? 'rex-icon-package';
return '<div class="text-nowrap"><i class="rex-icon ' . $packageIcon . '"></i>&nbsp;' . $a['list']->getValue('package') . '</div>';
},
);

$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 '<div class="text-nowrap" data-wildcard-copy="'.$value.'" role="button"> <i class="rex-icon fa-clone"></i> <code> '.$a['list']->getValue('wildcard') . '</code></div>';
}
static function ($a) {
$value = rex_config::get('wildcard', 'opentag') . $a['list']->getValue('wildcard') . rex_config::get('wildcard', 'closetag');
return '<div class="text-nowrap" data-wildcard-copy="' . $value . '" role="button"> <i class="rex-icon fa-clone"></i> <code> ' . $a['list']->getValue('wildcard') . '</code></div>';
},
);
}
});
}

/* 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() && rex_addon::get('wildcard') && rex_addon::get('wildcard')->isAvailable() && !rex::isSafeMode()) {
$addon = rex_addon::get('wildcard');
$page = $addon->getProperty('page');

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';

$href = rex_url::backendPage('wildcard/entry', $params);

$page['title'] .= ' <a class="label label-primary tex-primary" style="position: absolute; right: 18px; top: 10px; padding: 0.2em 0.6em 0.3em; border-radius: 3px; color: white; display: inline; width: auto;" href="' . $href . '">+</a>';
$addon->setProperty('page', $page);
}
}
8 changes: 4 additions & 4 deletions fragments/wildcard/backend/WildcardQuicknavigationButton.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,20 @@
</button>
<div style="max-width: calc(100vw - 200px); width: 700px" class="quicknavi quicknavi-items list-group dropdown-menu dropdown-menu-right" aria-labelledby="wildcardSearch">
<div style="padding: 10px;">


<div class="input-group">
<input type="search" class="form-control" name="q" placeholder="<?= \rex_i18n::msg('wildcard_quicknavigation_search_placeholder') ?>">
<span class="input-group-btn">
<button class="btn btn-primary" id="wildcardSearchButton"><span class="rex-icon fa-search"></span> <?= \rex_i18n::msg('wildcard_quicknavigation_search_button') ?></button>
</span>
</div>
</form>

<div id="wildcardSearchResults" style="padding-top: 10px;">
</div>
</div>


</div>
</div>
39 changes: 18 additions & 21 deletions install.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,37 @@
$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
$data = json_decode($tableset, true);

// 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;
}
Expand All @@ -49,15 +47,14 @@
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'));
$table = $table->ensurePrimaryIdColumn();
$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, ''));
Expand Down
49 changes: 18 additions & 31 deletions lib/FragmentScanner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
);
}

Expand All @@ -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
{
Expand All @@ -71,29 +65,24 @@ 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']);
}

/**
* 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' => [],
];
}
}
Expand All @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions lib/QuickNavigationButton.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
<?php


namespace Alexplusde\Wildcard;
class QuickNavigationButton implements \FriendsOfRedaxo\QuickNavigation\Button\ButtonInterface {
public function get(): string {

$fragment = new \rex_fragment();
use FriendsOfRedaxo\QuickNavigation\Button\ButtonInterface;
use rex_fragment;

class QuickNavigationButton implements ButtonInterface
{
public function get(): string
{
$fragment = new rex_fragment();
return $fragment->parse('wildcard/backend/WildcardQuicknavigationButton.php');
}
}
Loading