Skip to content

Commit

Permalink
Templates können via Key geladen werden (#2892)
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaddade authored and kodiakhq[bot] committed Jan 22, 2020
1 parent 88a61ce commit b278540
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 5 deletions.
2 changes: 2 additions & 0 deletions redaxo/src/addons/structure/plugins/content/install.php
Expand Up @@ -78,13 +78,15 @@

rex_sql_table::get(rex::getTable('template'))
->ensureColumn(new rex_sql_column('id', 'int(10) unsigned', false, null, 'AUTO_INCREMENT'))
->ensureColumn(new rex_sql_column('key', 'varchar(191)', true))
->ensureColumn(new rex_sql_column('name', 'varchar(255)', true))
->ensureColumn(new rex_sql_column('content', 'mediumtext', true))
->ensureColumn(new rex_sql_column('active', 'tinyint(1)', true))
->ensureGlobalColumns()
->ensureColumn(new rex_sql_column('attributes', 'text', true))
->ensureColumn(new rex_sql_column('revision', 'int(11)'))
->setPrimaryKey('id')
->ensureIndex(new rex_sql_index('key', ['key'], rex_sql_index::UNIQUE))
->ensure();

rex_sql_table::get(rex::getTable('action'))
Expand Down
4 changes: 4 additions & 0 deletions redaxo/src/addons/structure/plugins/content/lang/de_de.lang
Expand Up @@ -78,12 +78,16 @@ template_categories_custom = Nur in folgenden Kategorien verfügbar
edit_template = Template editieren
create_template = Neues Template erstellen
template_name = Name
template_key = Schlüssel
template_key_notice = Der Schlüssel sollte eindeutig vergeben oder nicht ausgefüllt werden.
template_key_exists = Der Schlüssel existiert bereits.
checkbox_template_active = Aktiv
checkbox_template_active_info = [Erscheint bei den Artikeln als Auswahl]
save_template_and_quit = Template speichern
save_template_and_continue = Template übernehmen
header_template = Template
header_template_description = Templatebezeichnung
header_template_key = Schlüssel
header_template_active = Aktiv
header_template_functions = Funktionen
delete_template = Template löschen
Expand Down
4 changes: 4 additions & 0 deletions redaxo/src/addons/structure/plugins/content/lang/en_gb.lang
Expand Up @@ -78,12 +78,16 @@ template_categories_custom = Only available in the following categories
edit_template = Edit template
create_template = Create template
template_name = Name
template_key = Key
template_key_notice = The key should be uniquely assigned or not filled in.
template_key_exists = The key already exists.
checkbox_template_active = Active
checkbox_template_active_info = [Selectable for articles]
save_template_and_quit = Save template
save_template_and_continue = Update template
header_template = Template
header_template_description = Template description
header_template_key = Key
header_template_active = Active
header_template_functions = Functions
delete_template = Delete template
Expand Down
12 changes: 12 additions & 0 deletions redaxo/src/addons/structure/plugins/content/lib/var_template.php
Expand Up @@ -10,6 +10,18 @@ class rex_var_template extends rex_var
protected function getOutput()
{
$template_id = $this->getParsedArg('id', 0, true);
$template_key = $this->getArg('key', null, true);

if (0 === $template_id && $template_key) {
$sql = rex_sql::factory()->setQuery(
'SELECT `id` FROM '.rex::getTable('template').' WHERE `key` = :key',
['key' => $template_key]
);

if (1 == $sql->getRows()) {
$template_id = $sql->getValue('id');
}
}

if ($template_id > 0) {
return self::class . '::getTemplateOutput(require ' . self::class . '::getTemplateStream(' . $template_id . ', $this))';
Expand Down
49 changes: 44 additions & 5 deletions redaxo/src/addons/structure/plugins/content/pages/templates.php
Expand Up @@ -74,6 +74,7 @@
$hole = rex_sql::factory();
$hole->setQuery('SELECT * FROM ' . rex::getTablePrefix() . 'template WHERE id = "' . $template_id . '"');
if (1 == $hole->getRows()) {
$templatekey = $hole->getValue('key');
$templatename = $hole->getValue('name');
$template = $hole->getValue('content');
$active = $hole->getValue('active');
Expand All @@ -82,6 +83,7 @@
$function = '';
}
} else {
$templatekey = null;
$templatename = '';
$template = '';
$active = '';
Expand All @@ -94,11 +96,18 @@
if ('ja' == $save && !$csrfToken->isValid()) {
echo rex_view::error(rex_i18n::msg('csrf_token_invalid'));
$save = 'nein';
} elseif ('ja' == $save) {
}

if ('ja' == $save) {
$active = rex_post('active', 'int');
$templatename = rex_post('templatename', 'string');
$template = rex_post('content', 'string');

$templatekey = trim(rex_post('templatekey', 'string'));
$templatekey = '' === $templatekey ? null : $templatekey;

$ctypes = rex_post('ctype', 'array');

$num_ctypes = count($ctypes);
if ('' == $ctypes[$num_ctypes]) {
unset($ctypes[$num_ctypes]);
Expand All @@ -125,16 +134,36 @@
}
}

$attributes['ctype'] = $ctypes;
$attributes['modules'] = $modules;
$attributes['categories'] = $categories;

if (null !== $templatekey) {
$templateKeySql = rex_sql::factory();
$templateKeySql->setTable(rex::getTable('template'));
if ('edit' == $function) {
$templateKeySql->setWhere('`key` = :templateKey AND id != :templateId', ['templateKey' => $templatekey, 'templateId' => $template_id]);
} else {
$templateKeySql->setWhere('`key` = :templateKey', ['templateKey' => $templatekey]);
}
$templateKeySql->select('id');

if ($templateKeySql->getRows() >= 1) {
$error = rex_i18n::msg('template_key_exists');
$save = 'nein';
}
}
}

if ('ja' == $save) {
$TPL = rex_sql::factory();
$TPL->setTable(rex::getTablePrefix() . 'template');
$TPL->setValue('key', $templatekey);
$TPL->setValue('name', $templatename);
$TPL->setValue('active', $active);
$TPL->setValue('content', $template);
$TPL->addGlobalCreateFields();

$attributes['ctype'] = $ctypes;
$attributes['modules'] = $modules;
$attributes['categories'] = $categories;
$TPL->setArrayValue('attributes', $attributes);

if ('add' == $function) {
Expand All @@ -146,6 +175,7 @@
$success = rex_i18n::msg('template_added');
$success = rex_extension::registerPoint(new rex_extension_point('TEMPLATE_ADDED', $success, [
'id' => $template_id,
'key' => $templatekey,
'name' => $templatename,
'content' => $template,
'active' => $active,
Expand All @@ -165,6 +195,7 @@
$success = rex_i18n::msg('template_updated');
$success = rex_extension::registerPoint(new rex_extension_point('TEMPLATE_UPDATED', $success, [
'id' => $template_id,
'key' => $templatekey,
'name' => $templatename,
'content' => $template,
'active' => $active,
Expand Down Expand Up @@ -349,6 +380,12 @@
$n['note'] = rex_i18n::msg('translatable');
$formElements[] = $n;

$n = [];
$n['label'] = '<label for="rex-id-templatekey">' . rex_i18n::msg('template_key') . '</label>';
$n['field'] = '<input class="form-control" id="rex-id-templatekey" type="text" name="templatekey" value="' . rex_escape($templatekey) . '" />';
$n['note'] = rex_i18n::msg('template_key_notice');
$formElements[] = $n;

$fragment = new rex_fragment();
$fragment->setVar('flush', true);
$fragment->setVar('elements', $formElements, false);
Expand Down Expand Up @@ -514,7 +551,7 @@
$message .= rex_view::error($error);
}

$list = rex_list::factory('SELECT id, name, active FROM ' . rex::getTablePrefix() . 'template ORDER BY name', 100);
$list = rex_list::factory('SELECT id, `key`, name, active FROM ' . rex::getTablePrefix() . 'template ORDER BY name', 100);
$list->addParam('start', rex_request('start', 'int'));
$list->addTableAttribute('class', 'table-striped table-hover');

Expand All @@ -526,6 +563,8 @@
$list->setColumnLabel('id', rex_i18n::msg('id'));
$list->setColumnLayout('id', ['<th class="rex-table-id">###VALUE###</th>', '<td class="rex-table-id" data-title="' . rex_i18n::msg('id') . '">###VALUE###</td>']);

$list->setColumnLabel('key', rex_i18n::msg('header_template_key'));

$list->setColumnLabel('name', rex_i18n::msg('header_template_description'));
$list->setColumnParams('name', ['function' => 'edit', 'template_id' => '###id###']);

Expand Down

0 comments on commit b278540

Please sign in to comment.