ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.CharacteristicDBRepo.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Style\Content;
22 
23 use ilDBInterface;
24 use ilObjStyleSheet;
25 
30 {
31  protected ilDBInterface $db;
33 
34  public function __construct(
35  ilDBInterface $db,
36  InternalDataService $factory
37  ) {
38  $this->db = $db;
39  $this->factory = $factory;
40  }
41 
42  public function addCharacteristic(
43  int $style_id,
44  string $type,
45  string $char,
46  bool $hidden = false,
47  int $order_nr = 0,
48  bool $outdated = false
49  ): void {
50  $db = $this->db;
51 
52  $db->insert("style_char", [
53  "style_id" => ["integer", $style_id],
54  "type" => ["text", $type],
55  "characteristic" => ["text", $char],
56  "hide" => ["integer", $hidden],
57  "order_nr" => ["integer", $order_nr],
58  "outdated" => ["integer", $outdated]
59  ]);
60  }
61 
62  public function exists(
63  int $style_id,
64  string $type,
65  string $char
66  ): bool {
67  $db = $this->db;
68 
69  $set = $db->queryF(
70  "SELECT * FROM style_char " .
71  " WHERE style_id = %s AND type = %s AND characteristic = %s",
72  ["integer", "text", "text"],
73  [$style_id, $type, $char]
74  );
75  if ($db->fetchAssoc($set)) {
76  return true;
77  }
78  return false;
79  }
80 
81  public function getByKey(
82  int $style_id,
83  string $type,
84  string $characteristic
85  ): ?Characteristic {
86  $db = $this->db;
87 
88  $set = $db->queryF(
89  "SELECT * FROM style_char " .
90  " WHERE style_id = %s AND type = %s AND characteristic = %s ",
91  ["integer", "text", "text"],
92  [$style_id, $type, $characteristic]
93  );
94  if ($rec = $db->fetchAssoc($set)) {
95  $set2 = $db->queryF(
96  "SELECT * FROM style_char_title " .
97  " WHERE style_id = %s AND type = %s AND characteristic = %s ",
98  ["integer", "text", "text"],
99  [$style_id, $type, $characteristic]
100  );
101  $titles = [];
102  while ($rec2 = $db->fetchAssoc($set2)) {
103  $titles[$rec2["lang"]] = $rec2["title"];
104  }
105  return $this->factory->characteristic(
106  $type,
107  $characteristic,
108  (bool) $rec["hide"],
109  $titles,
110  $style_id,
111  (int) $rec["order_nr"],
112  (bool) $rec["outdated"]
113  );
114  }
115  return null;
116  }
117 
118  public function getByType(
119  int $style_id,
120  string $type
121  ): array {
122  return $this->getByTypes(
123  $style_id,
124  [$type]
125  );
126  }
127 
128  public function getByTypes(
129  int $style_id,
130  array $types,
131  bool $include_hidden = true,
132  bool $include_outdated = true
133  ): array {
134  $db = $this->db;
135 
136  $set = $db->queryF(
137  "SELECT * FROM style_char " .
138  " WHERE style_id = %s AND " . $db->in("type", $types, false, "text") .
139  " ORDER BY order_nr, type, characteristic",
140  ["integer"],
141  [$style_id]
142  );
143  $chars = [];
144  while ($rec = $db->fetchAssoc($set)) {
145  if (($rec["hide"] && !$include_hidden) ||
146  ($rec["outdated"] && !$include_outdated)) {
147  continue;
148  }
149 
150  $set2 = $db->queryF(
151  "SELECT * FROM style_char_title " .
152  " WHERE style_id = %s AND type = %s AND characteristic = %s ",
153  ["integer", "text", "text"],
154  [$style_id, $rec["type"], $rec["characteristic"]]
155  );
156  $titles = [];
157  while ($rec2 = $db->fetchAssoc($set2)) {
158  $titles[$rec2["lang"]] = $rec2["title"];
159  }
160  $chars[] = $this->factory->characteristic(
161  $rec["type"],
162  $rec["characteristic"],
163  (bool) $rec["hide"],
164  $titles,
165  $style_id,
166  (int) $rec["order_nr"],
167  (bool) $rec["outdated"]
168  );
169  }
170  return $chars;
171  }
172 
177  public function getBySuperType(
178  int $style_id,
179  string $super_type
180  ): array {
182  $types = $stypes[$super_type];
183 
184  return $this->getByTypes(
185  $style_id,
186  $types
187  );
188  }
189 
190 
194  public function saveTitles(
195  int $style_id,
196  string $type,
197  string $characteristic,
198  array $titles
199  ): void {
200  $db = $this->db;
201 
202  $db->manipulateF(
203  "DELETE FROM style_char_title " .
204  " WHERE style_id = %s AND type = %s AND characteristic = %s ",
205  ["integer", "text", "text"],
206  [$style_id, $type, $characteristic]
207  );
208 
209  foreach ($titles as $l => $title) {
210  $this->addTitle(
211  $style_id,
212  $type,
213  $characteristic,
214  $l,
215  $title
216  );
217  }
218  }
219 
220  public function addTitle(
221  int $style_id,
222  string $type,
223  string $characteristic,
224  string $lang,
225  string $title
226  ): void {
227  $db = $this->db;
228 
229  $db->insert("style_char_title", [
230  "style_id" => ["integer", $style_id],
231  "type" => ["text", $type],
232  "characteristic" => ["text", $characteristic],
233  "lang" => ["text", $lang],
234  "title" => ["text", $title]
235  ]);
236  }
237 
238 
239  public function cloneAllFromStyle(
240  int $from_style_id,
241  int $to_style_id
242  ) : void {
243  $set = $this->db->queryF("SELECT * FROM style_char " .
244  " WHERE style_id = %s ",
245  ["integer"],
246  [$from_style_id]
247  );
248  while ($rec = $this->db->fetchAssoc($set)) {
249  $this->db->insert("style_char", [
250  "style_id" => ["integer", $to_style_id],
251  "type" => ["text", $rec["type"]],
252  "characteristic" => ["text", $rec["characteristic"]],
253  "hide" => ["integer", (int) ($rec["hide"] ?? 0)],
254  "outdated" => ["integer", (int) ($rec["outdated"] ?? 0)],
255  "order_nr" => ["integer", (int) ($rec["order_nr"] ?? 0)]
256  ]);
257  }
258  $set = $this->db->queryF("SELECT * FROM style_char_title " .
259  " WHERE style_id = %s ",
260  ["integer"],
261  [$from_style_id]
262  );
263  while ($rec = $this->db->fetchAssoc($set)) {
264  $this->db->insert("style_char_title", [
265  "style_id" => ["integer", $to_style_id],
266  "type" => ["text", $rec["type"]],
267  "characteristic" => ["text", $rec["characteristic"]],
268  "lang" => ["text", $rec["lang"]],
269  "title" => ["text", $rec["title"]]
270  ]);
271  }
272  }
273 
277  public function saveHidden(
278  int $style_id,
279  string $type,
280  string $characteristic,
281  bool $hide
282  ): void {
283  $db = $this->db;
284 
285  $db->update(
286  "style_char",
287  [
288  "hide" => ["integer", $hide]
289  ],
290  [ // where
291  "style_id" => ["integer", $style_id],
292  "type" => ["text", $type],
293  "characteristic" => ["text", $characteristic]
294  ]
295  );
296  }
297 
301  public function saveOutdated(
302  int $style_id,
303  string $type,
304  string $characteristic,
305  bool $outdated
306  ): void {
307  $db = $this->db;
308 
309  $db->update(
310  "style_char",
311  [
312  "outdated" => ["integer", $outdated]
313  ],
314  [ // where
315  "style_id" => ["integer", $style_id],
316  "type" => ["text", $type],
317  "characteristic" => ["text", $characteristic]
318  ]
319  );
320  }
321 
322  public function isOutdated(
323  int $style_id,
324  string $type,
325  string $characteristic
326  ): bool {
327  $db = $this->db;
328 
329  $set = $db->queryF("SELECT outdated FROM style_char " .
330  " WHERE style_id = %s AND type = %s AND characteristic = %s",
331  ["integer", "text", "text"],
332  [$style_id, $type, $characteristic]
333  );
334  if ($rec = $db->fetchAssoc($set)) {
335  return (bool) $rec["outdated"];
336  }
337  return false;
338  }
339 
340  public function saveOrderNr(
341  int $style_id,
342  string $type,
343  string $characteristic,
344  int $order_nr
345  ): void {
346  $db = $this->db;
347 
348  $db->update(
349  "style_char",
350  [
351  "order_nr" => ["integer", $order_nr]
352  ],
353  [ // where
354  "style_id" => ["integer", $style_id],
355  "type" => ["text", $type],
356  "characteristic" => ["text", $characteristic]
357  ]
358  );
359  }
360 
361  public function deleteCharacteristic(
362  int $style_id,
363  string $type,
364  string $tag,
365  string $class
366  ): void {
367  $db = $this->db;
368 
369  // delete characteristic record
370  $db->manipulateF(
371  "DELETE FROM style_char WHERE style_id = %s AND type = %s AND characteristic = %s",
372  array("integer", "text", "text"),
373  array($style_id, $type, $class)
374  );
375 
376  // delete parameter records
377  $db->manipulateF(
378  "DELETE FROM style_parameter WHERE style_id = %s AND tag = %s AND type = %s AND class = %s",
379  array("integer", "text", "text", "text"),
380  array($style_id, $tag, $type, $class)
381  );
382  }
383 
384  //
385  // Parameter
386  //
387 
388  public function replaceParameter(
389  int $style_id,
390  string $a_tag,
391  string $a_class,
392  string $a_par,
393  string $a_val,
394  string $a_type,
395  int $a_mq_id = 0,
396  bool $a_custom = false
397  ): void {
398  $db = $this->db;
399 
400  $set = $db->queryF(
401  "SELECT * FROM style_parameter " .
402  " WHERE style_id = %s AND tag = %s AND class = %s AND mq_id = %s " .
403  " AND custom = %s AND type = %s AND parameter = %s ",
404  ["integer", "text", "text", "integer", "integer", "text", "text"],
405  [$style_id, $a_tag, $a_class, $a_mq_id, $a_custom, $a_type, $a_par]
406  );
407 
408  if ($set->fetchRow()) {
409  $db->update(
410  "style_parameter",
411  [
412  "value" => ["text", $a_val]
413  ],
414  [ // where
415  "style_id" => ["integer", $style_id],
416  "tag" => ["text", $a_tag],
417  "class" => ["text", $a_class],
418  "mq_id" => ["integer", $a_mq_id],
419  "custom" => ["integer", $a_custom],
420  "type" => ["text", $a_type],
421  "parameter" => ["text", $a_par]
422  ]
423  );
424  } else {
425  $id = $db->nextId("style_parameter");
426  $db->insert("style_parameter", [
427  "id" => ["integer", $id],
428  "value" => ["text", $a_val],
429  "style_id" => ["integer", $style_id],
430  "tag" => ["text", $a_tag],
431  "class" => ["text", $a_class],
432  "type" => ["text", $a_type],
433  "parameter" => ["text", $a_par],
434  "mq_id" => ["integer", $a_mq_id],
435  "custom" => ["integer", $a_custom]
436  ]);
437  }
438  }
439 
440  public function deleteParameter(
441  int $style_id,
442  string $tag,
443  string $class,
444  string $par,
445  string $type,
446  int $mq_id = 0,
447  bool $custom = false
448  ): void {
449  $db = $this->db;
450 
451  $q = "DELETE FROM style_parameter WHERE " .
452  " style_id = " . $db->quote($style_id, "integer") . " AND " .
453  " tag = " . $db->quote($tag, "text") . " AND " .
454  " class = " . $db->quote($class, "text") . " AND " .
455  " mq_id = " . $db->quote($mq_id, "integer") . " AND " .
456  " custom = " . $db->quote($custom, "integer") . " AND " .
457  " " . $db->equals("type", $type, "text", true) . " AND " .
458  " parameter = " . $db->quote($par, "text");
459 
460  $db->manipulate($q);
461  }
462 
463  public function updateColorName(
464  int $style_id,
465  string $old_name,
466  string $new_name
467  ): void {
468  if ($old_name == $new_name) {
469  return;
470  }
471 
472  $db = $this->db;
473 
474  $color_attributes = [
475  "background-color",
476  "color",
477  "border-color",
478  "border-top-color",
479  "border-bottom-color",
480  "border-left-color",
481  "border-right-color",
482  ];
483 
484  $set = $db->queryF(
485  "SELECT * FROM style_parameter " .
486  " WHERE style_id = %s AND " . $db->in("parameter", $color_attributes, false, "text"),
487  ["integer"],
488  [$style_id]
489  );
490  while ($rec = $db->fetchAssoc($set)) {
491  if ($rec["value"] == "!" . $old_name ||
492  is_int(strpos($rec["value"], "!" . $old_name . "("))) {
493  // parameter is based on color -> rename it
494  $this->replaceParameter(
495  $style_id,
496  $rec["tag"],
497  $rec["class"],
498  $rec["parameter"],
499  str_replace($old_name, $new_name, $rec["value"]),
500  $rec["type"],
501  $rec["mq_id"],
502  $rec["custom"]
503  );
504  }
505  }
506  }
507 }
addCharacteristic(int $style_id, string $type, string $char, bool $hidden=false, int $order_nr=0, bool $outdated=false)
saveOrderNr(int $style_id, string $type, string $characteristic, int $order_nr)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
insert(string $table_name, array $values)
manipulateF(string $query, array $types, array $values)
__construct(ilDBInterface $db, InternalDataService $factory)
replaceParameter(int $style_id, string $a_tag, string $a_class, string $a_par, string $a_val, string $a_type, int $a_mq_id=0, bool $a_custom=false)
$type
equals(string $columns, $value, string $type, bool $emptyOrNull=false)
fetchAssoc(ilDBStatement $statement)
getByKey(int $style_id, string $type, string $characteristic)
saveOutdated(int $style_id, string $type, string $characteristic, bool $outdated)
Save characteristic outdated status.
update(string $table_name, array $values, array $where)
$where MUST contain existing columns only.
addTitle(int $style_id, string $type, string $characteristic, string $lang, string $title)
quote($value, string $type)
exists(int $style_id, string $type, string $char)
deleteParameter(int $style_id, string $tag, string $class, string $par, string $type, int $mq_id=0, bool $custom=false)
getByTypes(int $style_id, array $types, bool $include_hidden=true, bool $include_outdated=true)
saveTitles(int $style_id, string $type, string $characteristic, array $titles)
Save titles for characteristic.
saveHidden(int $style_id, string $type, string $characteristic, bool $hide)
Save characteristic hidden status.
nextId(string $table_name)
cloneAllFromStyle(int $from_style_id, int $to_style_id)
queryF(string $query, array $types, array $values)
Repository internal data service.
Characteristic (Class) of style.
in(string $field, array $values, bool $negate=false, string $type="")
$lang
Definition: xapiexit.php:26
isOutdated(int $style_id, string $type, string $characteristic)
getBySuperType(int $style_id, string $super_type)
Get characteristics by supertype.
deleteCharacteristic(int $style_id, string $type, string $tag, string $class)
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
manipulate(string $query)
Run a (write) Query on the database.
updateColorName(int $style_id, string $old_name, string $new_name)