ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.CharacteristicManager.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
21 namespace ILIAS\Style\Content;
22 
24 use ilObjUser;
25 use ilObjStyleSheet;
26 
32 {
35  protected ilObjUser $user;
38  protected int $style_id;
39 
40  public function __construct(
41  int $style_id,
42  Access\StyleAccessManager $access_manager,
43  CharacteristicDBRepo $char_repo,
44  CharacteristicCopyPasteSessionRepo $char_copy_paste_repo,
45  ColorDBRepo $color_repo,
46  ilObjUser $user
47  ) {
48  $this->user = $user;
49  $this->repo = $char_repo;
50  $this->color_repo = $color_repo;
51  $this->access_manager = $access_manager;
52  $this->session = $char_copy_paste_repo;
53  $this->style_id = $style_id;
54  }
55 
56  public function addCharacteristic(
57  string $type,
58  string $char,
59  bool $hidden = false
60  ): void {
61  $this->repo->addCharacteristic(
62  $this->style_id,
63  $type,
64  $char,
65  $hidden
66  );
67 
68  ilObjStyleSheet::_writeUpToDate($this->style_id, false);
69  }
70 
74  public function exists(
75  string $type,
76  string $char
77  ): bool {
78  return $this->repo->exists(
79  $this->style_id,
80  $type,
81  $char
82  );
83  }
84 
88  public function getByKey(
89  string $type,
90  string $characteristic
91  ): ?Characteristic {
92  return $this->repo->getByKey(
93  $this->style_id,
94  $type,
95  $characteristic
96  );
97  }
98 
102  public function getByType(
103  string $type
104  ): array {
105  return $this->repo->getByType(
106  $this->style_id,
107  $type
108  );
109  }
110 
114  public function getByTypes(
115  array $types,
116  bool $include_hidden = true,
117  bool $include_outdated = true
118  ): array {
119  return $this->repo->getByTypes(
120  $this->style_id,
121  $types,
122  $include_hidden,
123  $include_outdated
124  );
125  }
126 
130  public function getBySuperType(
131  string $supertype
132  ): array {
133  return $this->repo->getBySuperType(
134  $this->style_id,
135  $supertype
136  );
137  }
138 
142  public function getPresentationTitle(
143  string $type,
144  string $characteristic,
145  bool $fallback_to_characteristic = true
146  ): string {
147  $char = $this->repo->getByKey(
148  $this->style_id,
149  $type,
150  $characteristic
151  );
152 
153  $titles = $char ? $char->getTitles() : [];
154 
155  $lang = $this->user->getLanguage();
156 
157  if (($titles[$lang] ?? "") != "") {
158  return $titles[$lang];
159  }
160  if ($fallback_to_characteristic) {
161  return $characteristic;
162  }
163  return "";
164  }
165 
170  public function saveTitles(
171  string $type,
172  string $characteristic,
173  array $titles
174  ): void {
175  if (!$this->access_manager->checkWrite()) {
176  throw new ContentStyleNoPermissionException("No write permission for style.");
177  }
178  $this->repo->saveTitles(
179  $this->style_id,
180  $type,
181  $characteristic,
182  $titles
183  );
184  }
185 
189  public function saveHidden(
190  string $type,
191  string $characteristic,
192  bool $hide
193  ): void {
194  if (!$this->access_manager->checkWrite()) {
195  throw new ContentStyleNoPermissionException("No write permission for style.");
196  }
197  $this->repo->saveHidden(
198  $this->style_id,
199  $type,
200  $characteristic,
201  $hide
202  );
203  }
204 
208  public function saveOutdated(
209  string $type,
210  string $characteristic,
211  bool $outdated
212  ): void {
213  if (!$this->access_manager->checkWrite()) {
214  throw new ContentStyleNoPermissionException("No write permission for style.");
215  }
216  $this->repo->saveOutdated(
217  $this->style_id,
218  $type,
219  $characteristic,
220  $outdated
221  );
222  }
223 
224  public function isOutdated(
225  string $type,
226  string $characteristic
227  ): bool {
228  return $this->repo->isOutdated(
229  $this->style_id,
230  $type,
231  $characteristic
232  );
233  }
234 
239  public function saveOrderNrs(
240  string $type,
241  array $order_nrs
242  ): void {
243  if (!$this->access_manager->checkWrite()) {
244  throw new ContentStyleNoPermissionException("No write permission for style.");
245  }
246 
247  asort($order_nrs, SORT_NUMERIC);
248 
249  foreach ($order_nrs as $char => $nr) {
250  $this->repo->saveOrderNr(
251  $this->style_id,
252  $type,
253  $char,
254  (int) $nr
255  );
256  }
257  }
258 
259 
264  public function deleteCharacteristic(
265  string $type,
266  string $class
267  ): void {
268  if (!$this->access_manager->checkWrite()) {
269  throw new ContentStyleNoPermissionException("No write permission for style.");
270  }
271  $tag = ilObjStyleSheet::_determineTag($type);
272 
273  // check, if characteristic is not a core style
274  $core_styles = ilObjStyleSheet::_getCoreStyles();
275  if (empty($core_styles[$type . "." . $tag . "." . $class])) {
276  $this->repo->deleteCharacteristic(
277  $this->style_id,
278  $type,
279  $tag,
280  $class
281  );
282  }
283 
284  ilObjStyleSheet::_writeUpToDate($this->style_id, false);
285  }
286 
287  public function setCopyCharacteristics(
288  string $style_type,
289  array $characteristics
290  ): void {
291  $this->session->set($this->style_id, $style_type, $characteristics);
292  }
293 
297  public function hasCopiedCharacteristics(string $style_type): bool
298  {
299  return $this->session->hasEntries($style_type);
300  }
301 
302  public function clearCopyCharacteristics(): void
303  {
304  $this->session->clear();
305  }
306 
308  {
309  $data = $this->session->getData();
310  return (int) $data->style_id;
311  }
312 
313  public function getCopyCharacteristicStyleType(): string
314  {
315  $data = $this->session->getData();
316  return $data->style_type;
317  }
318 
319  public function getCopyCharacteristics(): array
320  {
321  $data = $this->session->getData();
322  return $data->characteristics;
323  }
324 
330  int $source_style_id,
331  string $source_style_type,
332  string $source_char,
333  string $new_char,
334  array $new_titles
335  ): void {
336  if (!$this->access_manager->checkWrite()) {
337  throw new ContentStyleNoPermissionException("No write permission for style.");
338  }
339 
340  if ($this->exists($source_style_type, $new_char)) {
341  $target_char = $this->getByKey($source_style_type, $new_char);
342  if (count($new_titles) == 0) {
343  $new_titles = $target_char->getTitles();
344  }
345  $this->deleteCharacteristic($source_style_type, $new_char);
346  }
347 
348  $this->addCharacteristic($source_style_type, $new_char);
349  $this->saveTitles($source_style_type, $new_char, $new_titles);
350 
351  $from_style = new ilObjStyleSheet($source_style_id);
352 
353  // todo fix using mq_id
354  $pars = $from_style->getParametersOfClass($source_style_type, $source_char);
355 
356  $colors = array();
357  foreach ($pars as $p => $v) {
358  if (substr($v, 0, 1) == "!") {
359  $colors[] = substr($v, 1);
360  }
361  $this->replaceParameter(
362  ilObjStyleSheet::_determineTag($source_style_type),
363  $new_char,
364  $p,
365  $v,
366  $source_style_type
367  );
368  }
369 
370  // copy colors
371  foreach ($colors as $c) {
372  if (!$this->color_repo->colorExists($this->style_id, $c)) {
373  $this->color_repo->addColor(
374  $this->style_id,
375  $c,
376  $from_style->getColorCodeForName($c)
377  );
378  }
379  }
380  }
381 
382  public function replaceParameter(
383  string $a_tag,
384  string $a_class,
385  string $a_par,
386  string $a_val,
387  string $a_type,
388  int $a_mq_id = 0,
389  bool $a_custom = false
390  ): void {
391  if ($a_val != "") {
392  $this->repo->replaceParameter(
393  $this->style_id,
394  $a_tag,
395  $a_class,
396  $a_par,
397  $a_val,
398  $a_type,
399  $a_mq_id,
400  $a_custom
401  );
402  } else {
403  $this->deleteParameter(
404  $a_tag,
405  $a_class,
406  $a_par,
407  $a_type,
408  $a_mq_id,
409  $a_custom
410  );
411  }
412  }
413 
414  public function deleteParameter(
415  string $a_tag,
416  string $a_class,
417  string $a_par,
418  string $a_type,
419  int $a_mq_id = 0,
420  bool $a_custom = false
421  ): void {
422  $this->repo->deleteParameter(
423  $this->style_id,
424  $a_tag,
425  $a_class,
426  $a_par,
427  $a_type,
428  $a_mq_id,
429  $a_custom
430  );
431  }
432 }
deleteCharacteristic(string $type, string $class)
Delete Characteristic.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
isOutdated(string $type, string $characteristic)
$c
Definition: cli.php:38
$type
hasCopiedCharacteristics(string $style_type)
Is in copy process?
exists(string $type, string $char)
Check if characteristic exists.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
__construct(int $style_id, Access\StyleAccessManager $access_manager, CharacteristicDBRepo $char_repo, CharacteristicCopyPasteSessionRepo $char_copy_paste_repo, ColorDBRepo $color_repo, ilObjUser $user)
getByType(string $type)
Get characteristics by type.
replaceParameter(string $a_tag, string $a_class, string $a_par, string $a_val, string $a_type, int $a_mq_id=0, bool $a_custom=false)
addCharacteristic(string $type, string $char, bool $hidden=false)
static _writeUpToDate(int $a_id, bool $a_up_to_date)
getBySuperType(string $supertype)
Get characteristics by supertype.
deleteParameter(string $a_tag, string $a_class, string $a_par, string $a_type, int $a_mq_id=0, bool $a_custom=false)
getByKey(string $type, string $characteristic)
Get characteristic by key.
Main business logic for characteristics.
saveOutdated(string $type, string $characteristic, bool $outdated)
Save characteristic outdated status.
Characteristic (Class) of style.
$lang
Definition: xapiexit.php:26
getByTypes(array $types, bool $include_hidden=true, bool $include_outdated=true)
Get characteristics by type.
setCopyCharacteristics(string $style_type, array $characteristics)
saveHidden(string $type, string $characteristic, bool $hide)
Save characteristic hidden status.
saveOrderNrs(string $type, array $order_nrs)
Save characteristics order.
Manages access to content style editing.
static _determineTag(string $a_type)
copyCharacteristicFromSource(int $source_style_id, string $source_style_type, string $source_char, string $new_char, array $new_titles)
Copy characteristic.
getPresentationTitle(string $type, string $characteristic, bool $fallback_to_characteristic=true)
Get characteristic by key.
saveTitles(string $type, string $characteristic, array $titles)
Save titles for characteristic.