/
select_template.php
102 lines (88 loc) · 2.77 KB
/
select_template.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
/**
* @package redaxo\structure\content
*/
class rex_template_select extends rex_select
{
/** @var bool */
private $loaded = false;
/** @var int|null */
private $categoryId;
/** @var string[]|null */
private $templates;
/** @var int */
private $clangId;
/**
* @param int|null $categoryId
* @param int|null $clangId
*/
public function __construct($categoryId = null, $clangId = null)
{
$this->categoryId = $categoryId;
$this->clangId = null === $clangId ? rex_clang::getCurrentId() : (int) $clangId;
parent::__construct();
}
/**
* @return string
*/
public function get()
{
if (!$this->loaded) {
$templates = $this->getTemplates();
if (count($templates) > 0) {
foreach ($templates as $templateId => $templateName) {
$this->addOption($templateName, $templateId);
}
} else {
$this->addOption(rex_i18n::msg('option_no_template'), '0');
}
$this->loaded = true;
}
return parent::get();
}
/**
* @throws rex_sql_exception
* @return void
*/
public function setSelectedFromStartArticle()
{
$selected = null;
// Inherit template_id from start article
if ($this->categoryId > 0) {
$sql = rex_sql::factory();
$sql->setQuery('SELECT template_id FROM ' . rex::getTable('article') . ' WHERE id = ? AND clang_id = ? AND startarticle = 1', [
$this->categoryId,
$this->clangId,
]);
if (1 == $sql->getRows()) {
$selected = $sql->getValue('template_id');
}
}
$templates = $this->getTemplates();
if (!$selected || !isset($templates[$selected])) {
$selected = rex_template::getDefaultId();
}
if ($selected && isset($templates[$selected])) {
parent::setSelected($selected);
}
}
/**
* @return string[]
*/
public function getTemplates()
{
if (null === $this->templates) {
$this->templates = [];
if (null !== $this->categoryId) {
$templates = rex_template::getTemplatesForCategory($this->categoryId);
} else {
$templates = rex_sql::factory()->getArray('SELECT id, name FROM ' . rex::getTable('template') . ' WHERE active = 1 ORDER BY name');
$templates = array_column($templates, 'name', 'id');
}
foreach ($templates as $templateId => $templateName) {
$this->templates[$templateId] = rex_i18n::translate($templateName, false);
}
}
return $this->templates;
}
}