ILIAS  release_8 Revision v8.24
class.ilSettingsTemplate.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
28{
29 protected ilDBInterface $db;
30 private int $id;
31 private string $type = '';
32 private string $title = '';
33 private string $description = '';
34 private array $setting = [];
36 private array $hidden_tab = [];
37 private bool $auto_generated = false;
39 private bool $available = true;
40
41 public function __construct(
42 int $a_id = 0,
44 ) {
45 global $DIC;
46
47 $this->db = $DIC->database();
48 if ($a_id > 0) {
49 if ($config) {
50 $this->setConfig($config);
51 }
52 $this->setId($a_id);
53 $this->read();
54 }
55 }
56
57 public function setId(int $a_val): void
58 {
59 $this->id = $a_val;
60 }
61
62 public function getId(): int
63 {
64 return $this->id;
65 }
66
67 public function setAutoGenerated(bool $a_status): void
68 {
69 $this->auto_generated = $a_status;
70 }
71
72 public function getAutoGenerated(): bool
73 {
75 }
76
77 public function setTitle(string $a_val): void
78 {
79 $this->title = $a_val;
80 }
81
82 public function getTitle(): string
83 {
84 return $this->title;
85 }
86
87 public function setType(string $a_val): void
88 {
89 $this->type = $a_val;
90 }
91
92 public function getType(): string
93 {
94 return $this->type;
95 }
96
97 public function setDescription(string $a_val): void
98 {
99 $this->description = $a_val;
100 }
101
102 public function getDescription(): string
103 {
104 return $this->description;
105 }
106
107 public function isAvailable(): bool
108 {
109 return $this->available;
110 }
111
116 public function setSetting(
117 string $a_setting,
118 $a_value,
119 bool $a_hide = false
120 ): void {
121 if ($this->getConfig()) {
122 $settings = $this->getConfig()->getSettings();
123
124 if (isset($settings[$a_setting]['type']) &&
125 $settings[$a_setting]['type'] === ilSettingsTemplateConfig::CHECKBOX) {
126 if (is_array($a_value)) {
127 $a_value = serialize($a_value);
128 } else {
129 $a_value = unserialize($a_value, ['allowed_classes' => false]);
130 }
131 }
132 }
133
134 $this->setting[$a_setting] = [
135 "value" => $a_value,
136 "hide" => $a_hide
137 ];
138 }
139
140 public function removeSetting(string $a_setting): void
141 {
142 unset($this->setting[$a_setting]);
143 }
144
145 public function removeAllSettings(): void
146 {
147 $this->setting = [];
148 }
149
150 public function getSettings(): array
151 {
152 return $this->setting;
153 }
154
155 public function addHiddenTab(string $a_tab_id): void
156 {
157 $this->hidden_tab[$a_tab_id] = $a_tab_id;
158 }
159
160 public function removeAllHiddenTabs(): void
161 {
162 $this->hidden_tab = [];
163 }
164
165 public function getHiddenTabs(): array
166 {
167 return $this->hidden_tab;
168 }
169
175 {
176 return $this->config;
177 }
178
183 {
184 $this->config = $config;
185 }
186
187 public function read(): void
188 {
189 $ilDB = $this->db;
190
191 // read template
192 $set = $ilDB->query(
193 "SELECT * FROM adm_settings_template WHERE " .
194 " id = " . $ilDB->quote($this->getId(), "integer")
195 );
196 if ($ilDB->numRows($set) === 0) {
197 $this->available = false;
198 return;
199 }
200
201 $rec = $ilDB->fetchAssoc($set);
202 $this->setTitle($rec["title"]);
203 $this->setType($rec["type"]);
204 $this->setDescription($rec["description"] ?? '');
205 // begin-patch lok
206 $this->setAutoGenerated((bool) $rec['auto_generated']);
207 // end-patch lok
208
209 // read template setttings
210 $set = $ilDB->query(
211 "SELECT * FROM adm_set_templ_value WHERE " .
212 " template_id = " . $ilDB->quote($this->getId(), "integer")
213 );
214 while ($rec = $ilDB->fetchAssoc($set)) {
215 $this->setSetting(
216 $rec["setting"],
217 $rec["value"],
218 (bool) $rec["hide"]
219 );
220 }
221
222 // read hidden tabs
223 $set = $ilDB->query(
224 "SELECT * FROM adm_set_templ_hide_tab WHERE " .
225 " template_id = " . $ilDB->quote($this->getId(), "integer")
226 );
227 while ($rec = $ilDB->fetchAssoc($set)) {
228 $this->addHiddenTab($rec["tab_id"]);
229 }
230 $this->available = true;
231 }
232
233 public function create(): void
234 {
235 $ilDB = $this->db;
236
237 $this->setId($ilDB->nextId("adm_settings_template"));
238
239 // write template
240 $ilDB->insert("adm_settings_template", [
241 "id" => ["integer", $this->getId()],
242 "title" => ["text", $this->getTitle()],
243 "type" => ["text", $this->getType()],
244 // begin-patch lok
245 "description" => ["clob", $this->getDescription()],
246 'auto_generated' => ['integer', $this->getAutoGenerated()]
247 // end-patch lok
248 ]);
249
250 // write settings
251 $this->insertSettings();
252
253 // write hidden tabs
254 $this->insertHiddenTabs();
255 }
256
257 public function update(): void
258 {
259 $ilDB = $this->db;
260
261 // update template
262 $ilDB->update("adm_settings_template", [
263 "title" => ["text", $this->getTitle()],
264 "type" => ["text", $this->getType()],
265 // begin-patch lok
266 "description" => ["clob", $this->getDescription()],
267 'auto_generated' => ['integer', $this->getAutoGenerated()]
268 ], [
269 "id" => ["integer", $this->getId()],
270 ]);
271
272 // delete settings and hidden tabs
273 $ilDB->manipulate(
274 "DELETE FROM adm_set_templ_value WHERE "
275 . " template_id = " . $ilDB->quote($this->getId(), "integer")
276 );
277 $ilDB->manipulate(
278 "DELETE FROM adm_set_templ_hide_tab WHERE "
279 . " template_id = " . $ilDB->quote($this->getId(), "integer")
280 );
281
282 // insert settings and hidden tabs
283 $this->insertSettings();
284 $this->insertHiddenTabs();
285 }
286
287 private function insertSettings(): void
288 {
289 $ilDB = $this->db;
290
291 foreach ($this->getSettings() as $s => $set) {
292 $ilDB->manipulate("INSERT INTO adm_set_templ_value " .
293 "(template_id, setting, value, hide) VALUES (" .
294 $ilDB->quote($this->getId(), "integer") . "," .
295 $ilDB->quote($s, "text") . "," .
296 $ilDB->quote($set["value"], "text") . "," .
297 $ilDB->quote($set["hide"], "integer") .
298 ")");
299 }
300 }
301
302 public function insertHiddenTabs(): void
303 {
304 $ilDB = $this->db;
305
306 foreach ($this->getHiddenTabs() as $tab_id) {
307 $ilDB->manipulate("INSERT INTO adm_set_templ_hide_tab " .
308 "(template_id, tab_id) VALUES (" .
309 $ilDB->quote($this->getId(), "integer") . "," .
310 $ilDB->quote($tab_id, "text") .
311 ")");
312 }
313 }
314
315 public function delete(): void
316 {
317 $ilDB = $this->db;
318
319 $ilDB->manipulate(
320 "DELETE FROM adm_settings_template WHERE "
321 . " id = " . $ilDB->quote($this->getId(), "integer")
322 );
323 $ilDB->manipulate(
324 "DELETE FROM adm_set_templ_value WHERE "
325 . " template_id = " . $ilDB->quote($this->getId(), "integer")
326 );
327 $ilDB->manipulate(
328 "DELETE FROM adm_set_templ_hide_tab WHERE "
329 . " template_id = " . $ilDB->quote($this->getId(), "integer")
330 );
331 }
332
336 public static function getAllSettingsTemplates(
337 string $a_type,
338 bool $a_include_auto_generated = false
339 ): array {
340 global $DIC;
341
342 $ilDB = $DIC->database();
343
344 if ($a_include_auto_generated) {
345 $set = $ilDB->query("SELECT * FROM adm_settings_template " .
346 " WHERE type = " . $ilDB->quote($a_type, "text") .
347 " ORDER BY title");
348 } else {
349 $set = $ilDB->query("SELECT * FROM adm_settings_template " .
350 " WHERE type = " . $ilDB->quote($a_type, "text") .
351 'AND auto_generated = ' . $ilDB->quote(0, 'integer') . ' ' .
352 " ORDER BY title");
353 }
354
355 $settings_template = [];
356 while ($rec = $ilDB->fetchAssoc($set)) {
357 $settings_template[] = $rec;
358 }
359 return $settings_template;
360 }
361
362 protected static function lookupProperty(
363 int $a_id,
364 string $a_prop
365 ): string {
366 global $DIC;
367
368 $ilDB = $DIC->database();
369
370 $set = $ilDB->query(
371 "SELECT $a_prop FROM adm_settings_template WHERE " .
372 " id = " . $ilDB->quote($a_id, "integer")
373 );
374 $rec = $ilDB->fetchAssoc($set);
375 return $rec[$a_prop];
376 }
377
378 public static function lookupTitle(int $a_id): string
379 {
380 return self::lookupProperty($a_id, 'title');
381 }
382
383 public static function lookupDescription(int $a_id): string
384 {
385 return self::lookupProperty($a_id, 'description');
386 }
387
388 public static function translate(string $a_title_desc): string
389 {
390 global $DIC;
391
392 if (str_starts_with($a_title_desc, 'il_')) {
393 return $DIC->language()->txt($a_title_desc);
394 }
395 return $a_title_desc;
396 }
397}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Settings template application class.
setSetting(string $a_setting, $a_value, bool $a_hide=false)
Set setting.
setAutoGenerated(bool $a_status)
getConfig()
Returns the template config associated with this template or NULL if none is given.
__construct(int $a_id=0, ?ilSettingsTemplateConfig $config=null)
static translate(string $a_title_desc)
static lookupDescription(int $a_id)
static lookupProperty(int $a_id, string $a_prop)
addHiddenTab(string $a_tab_id)
removeSetting(string $a_setting)
ilSettingsTemplateConfig $config
static getAllSettingsTemplates(string $a_type, bool $a_include_auto_generated=false)
Get all settings templates of type.
setConfig(ilSettingsTemplateConfig $config)
Sets the template config for this template.
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
Interface ilDBInterface.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
setSetting(string $name, string $value=null)
Set a setting value.
Definition: System.php:269
getSettings()
Get an array of all setting values.
Definition: System.php:287
array $settings
Setting values (LTI parameters, custom parameters and local parameters).
Definition: System.php:200