ILIAS  release_8 Revision v8.24
class.ilStyleDefinition.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
25{
29 public static string $current_style;
30
35 public static array $skins = [];
36
40 protected ilSkin $skin;
41
45 protected static array $cached_all_styles_information;
46
52
54
59 public function __construct(string $skin_id = '', ilSystemStyleConfig $system_style_config = null)
60 {
61 global $DIC;
62
63 if ($skin_id == '') {
64 $skin_id = self::getCurrentSkin();
65 }
66
69 } else {
70 $this->setSystemStylesConf($system_style_config);
71 }
72
73 $this->skin_factory = new ilSkinFactory($DIC->language(), $this->getSystemStylesConf());
74
75 if ($skin_id != $this->getSystemStylesConf()->getDefaultSkinId()) {
76 $this->setSkin($this->skin_factory->skinFromXML($this->getSystemStylesConf()->getCustomizingSkinPath() . $skin_id . '/template.xml'));
77 } else {
78 $this->setSkin($this->skin_factory->skinFromXML($this->getSystemStylesConf()->getDefaultTemplatePath()));
79 }
80 }
81
87 public static function getCurrentSkin(): ?string
88 {
89 global $DIC;
90
91 if (!$DIC || is_array($DIC)) {
92 return null;
93 }
94 if ($DIC->isDependencyAvailable('systemStyle') && is_object($DIC->systemStyle()->getSkin())) {
95 return $DIC->systemStyle()->getSkin()->getId();
96 } else {
97 $system_style_conf = new ilSystemStyleConfig();
98
99 if ($DIC->isDependencyAvailable('user') && is_object($DIC->user()) && property_exists(
100 $DIC->user(),
101 'skin'
102 )) {
103 $skin_id = $DIC->user()->skin;
104 if ($skin_id && !self::skinExists($skin_id)) {
105 if($DIC->isDependencyAvailable('systemStyle')) {
106 $messages = new ilSystemStyleMessageStack($DIC->ui()->mainTemplate());
107 $message_text = $DIC->language()->txt('set_skin_does_not_exist') . ' ' . $skin_id;
108 $messages->addMessage(new ilSystemStyleMessage($message_text, ilSystemStyleMessage::TYPE_ERROR));
109 $messages->sendMessages();
110 }
111 $skin_id = $system_style_conf->getDefaultSkinId();
112 }
113 return $skin_id;
114 } else {
115 return null;
116 }
117 }
118 }
119
123 public function getStyles(): array
124 {
125 return $this->getSkin()->getStyles();
126 }
127
128 public function getTemplateName(): string
129 {
130 return $this->getSkin()->getName();
131 }
132
136 public function getStyle(string $a_id): ilSkinStyle
137 {
138 return $this->getSkin()->getStyle($a_id);
139 }
140
144 public function getStyleName(string $a_id): string
145 {
146 return $this->getSkin()->getStyle($a_id)->getName();
147 }
148
152 public function getImageDirectory(string $style_id): string
153 {
154 if (!$style_id) {
156 }
157 try {
158 return $this->getSkin()->getStyle($style_id)->getImageDirectory();
159 } catch (ilSystemStyleException $e) {
161 }
162 }
163
167 public function getSoundDirectory(string $style_id): string
168 {
169 return $this->getSkin()->getStyle($style_id)->getSoundDirectory();
170 }
171
175 public static function getAllSkins(?ilSystemStyleConfig $system_style_config = null): array
176 {
177 if (!self::$skins) {
180 }
181
182 global $DIC;
183
185
189 $skins = [];
191
193 $cust_skins_directory = new RecursiveDirectoryIterator(
195 FilesystemIterator::SKIP_DOTS
196 );
197 foreach ($cust_skins_directory as $skin_folder) {
198 if ($skin_folder->isDir()) {
199 $template_path = $skin_folder->getRealPath() . '/template.xml';
200 if (file_exists($template_path)) {
201 $skin = $skin_factory->skinFromXML($template_path);
202 $skins[$skin->getId()] = $skin;
203 }
204 }
205 }
206 }
207
209 }
210
211 return self::$skins;
212 }
213
217 public static function skinExists(string $skin_id, ?ilSystemStyleConfig $system_style_config = null): bool
218 {
221 }
222
223 if ($skin_id == $system_style_config->getDefaultSkinId()) {
225 return true;
226 }
227 } elseif (is_file($system_style_config->getCustomizingSkinPath() . $skin_id . '/template.xml')) {
228 return true;
229 }
230 return false;
231 }
232
239 public static function getCurrentStyle(): ?string
240 {
241 global $DIC;
242
243 if (isset(self::$current_style)) {
245 }
246
247 if (!$DIC || is_array($DIC) || !$DIC->isDependencyAvailable('user')) {
248 return null;
249 }
250
251 if (array_key_exists('style', $DIC->user()->prefs) && $DIC->user()->prefs['style']) {
252 self::setCurrentStyle($DIC->user()->prefs['style']);
253 } else {
256 }
257
258 if ($DIC->isDependencyAvailable('systemStyle') && self::styleExistsForCurrentSkin(self::$current_style)) {
259 if ($DIC->systemStyle()->getSkin()->hasStyleSubstyles(self::$current_style)) {
260 // read assignments, if given
262 self::getCurrentSkin(),
263 self::$current_style
264 );
265 if (count($assignments) > 0) {
266 $ref_ass = [];
267 foreach ($assignments as $a) {
268 if ($DIC->systemStyle()->getSkin()->hasStyle($a['substyle'])) {
269 $ref_ass[$a['ref_id']] = $a['substyle'];
270 }
271 }
272
273 $ref_id = '';
274
275 if ($DIC->http()->wrapper()->query()->has('ref_id')) {
276 $ref_id = $DIC->http()->wrapper()->query()->retrieve(
277 'ref_id',
278 $DIC->refinery()->kindlyTo()->string()
279 );
280 } elseif ($DIC->http()->wrapper()->query()->has('target')) {
281 $target = $DIC->http()->wrapper()->query()->retrieve(
282 'target',
283 $DIC->refinery()->kindlyTo()->string()
284 );
285 $target_arr = explode('_', $target);
286 $ref_id = $target_arr[1] ?? '';
287 }
288
289 // check whether any ref id assigns a new style
290 if ($DIC->isDependencyAvailable('repositoryTree') && $ref_id && $DIC->repositoryTree()->isInTree((int) $ref_id)) {
291 $path = $DIC->repositoryTree()->getPathId((int) $ref_id);
292 for ($i = count($path) - 1; $i >= 0; $i--) {
293 if (isset($ref_ass[$path[$i]])) {
294 self::$current_style = $ref_ass[$path[$i]];
296 }
297 }
298 }
299 }
300 }
301 }
302
303 if ($DIC->isDependencyAvailable('systemStyle') && !self::styleExistsForCurrentSkin(self::$current_style)) {
304 $messages = new ilSystemStyleMessageStack($DIC->ui()->mainTemplate());
305 $message_text = $DIC->language()->txt('set_style_does_not_exist') . ' ' . self::$current_style;
306 $messages->addMessage(new ilSystemStyleMessage($message_text, ilSystemStyleMessage::TYPE_ERROR));
307 $messages->sendMessages();
311 }
312
314 }
315
321 public static function getAllSkinStyles(): ?array
322 {
323 global $DIC;
324
325 if (!self::getCachedAllStylesInformation()) {
326 $all_styles = [];
327
328 $skins = $DIC->systemStyle()->getSkins();
329
330 foreach ($skins as $skin) {
331 foreach ($skin->getStyles() as $style) {
332 $num_users = ilObjUser::_getNumberOfUsersForStyle($skin->getId(), $style->getId());
333
334 $parent_name = '';
335 if ($style->getSubstyleOf()) {
336 $parent_name = $skin->getStyle($style->getSubstyleOf())->getName();
337 }
338
340 if ($version == '$Id$') {
341 $version = '-';
342 }
343 // default selection list
344 $all_styles[$skin->getId() . ':' . $style->getId()] = [
345 'title' => $skin->getName() . ' / ' . $style->getName(),
346 'id' => $skin->getId() . ':' . $style->getId(),
347 'skin_id' => $skin->getId(),
348 'skin_name' => $skin->getName(),
349 'template_id' => $skin->getId(),
350 'template_name' => $skin->getName(),
351 'style_id' => $style->getId(),
352 'style_name' => $style->getName(),
353 'substyle_of' => $style->getSubstyleOf(),
354 'substyle_of_name' => $parent_name,
355 'users' => $num_users,
356 'version' => $version,
357 ];
358 }
359 }
361 }
362
364 }
365
369 public static function setCurrentSkin(string $a_skin): void
370 {
371 global $DIC;
372
373 if ($DIC->isDependencyAvailable('systemStyle') && $DIC->systemStyle()->getSkin()->getName() != $a_skin) {
374 $styleDefinition = new ilStyleDefinition($a_skin);
375 if (!self::styleExistsForCurrentSkin(self::$current_style)) {
376 $styleDefinition->setCurrentStyle($DIC->systemStyle()->getSkin()->getDefaultStyle()->getId());
377 }
378 }
379 }
380
384 public static function styleExists(string $style_id): bool
385 {
386 foreach (self::getSkins() as $skin) {
387 if ($skin->hasStyle($style_id)) {
388 return true;
389 }
390 }
391 return false;
392 }
393
397 public static function styleExistsForSkinId(string $skin_id, string $style_id): bool
398 {
399 global $DIC;
400
401 if (!self::skinExists($skin_id)) {
402 return false;
403 }
404 $factory = new ilSkinFactory($DIC->language());
405 $skin = $factory->skinStyleContainerFromId($skin_id, new ilSystemStyleMessageStack($DIC->ui()->mainTemplate()))->getSkin();
406 return $skin->hasStyle($style_id);
407 }
408
409 public static function styleExistsForCurrentSkin(string $style_id): bool
410 {
411 global $DIC;
412
413 return $DIC->systemStyle()->getSkin()->hasStyle($style_id);
414 }
415
416 public static function setCurrentStyle(string $a_style): void
417 {
418 self::$current_style = $a_style;
419 }
420
425 public static function getSkins(): array
426 {
427 return self::getAllSkins();
428 }
429
433 public static function setSkins(array $skins): void
434 {
435 self::$skins = $skins;
436 }
437
438 public function getSkin(): ilSkin
439 {
440 return $this->skin;
441 }
442
443 public function setSkin(ilSkin $skin): void
444 {
445 $this->skin = $skin;
446 }
447
448 protected static function getCachedAllStylesInformation(): ?array
449 {
450 if (!isset(self::$cached_all_styles_information)) {
451 return null;
452 }
454 }
455
457 {
458 self::$cached_all_styles_information = $cached_all_styles_information;
459 }
460
462 {
464 }
465
467 {
468 $this->system_style_config = $system_style_config;
469 }
470}
$version
Definition: plugin.php:24
static _getNumberOfUsersForStyle(string $a_skin, string $a_style)
Factory to create Skin classes holds an manages the basic data of a skin as provide by the template o...
ilSkin holds an manages the basic data of a skin as provide by the template of the skin.
hasStyle(string $id)
getStyle(string $id)
ilStyleDefinition acts as a wrapper of style related actions.
static setCachedAllStylesInformation(array $cached_all_styles_information)
static getCurrentSkin()
get the current skin use always this function instead of getting the account's skin the current skin ...
getSoundDirectory(string $style_id)
static string $current_style
currently selected style, used for caching
static skinExists(string $skin_id, ?ilSystemStyleConfig $system_style_config=null)
Check whether a skin exists.
ilSkin $skin
Sets the current skin.
static setCurrentSkin(string $a_skin)
static styleExistsForCurrentSkin(string $style_id)
ilSystemStyleConfig $system_style_config
Used to wire this component up with the correct paths into the customizing directory.
static getAllSkinStyles()
Get all skins/styles as array (convenient for tables) Attention: tempalte_name/template_id in this ar...
static setCurrentStyle(string $a_style)
static array $cached_all_styles_information
Used for caching.
static setSkins(array $skins)
setSystemStylesConf(ilSystemStyleConfig $system_style_config)
getImageDirectory(string $style_id)
static styleExists(string $style_id)
__construct(string $skin_id='', ilSystemStyleConfig $system_style_config=null)
ilStyleDefinition constructor.
static getCurrentStyle()
get the current style or sub style use always this function instead of getting the account's style th...
static styleExistsForSkinId(string $skin_id, string $style_id)
ilSystemStyleConfig wraps all 'constants' to ensure the testability of all classes using those 'const...
Class for advanced editing exception handling in ILIAS.
Used to stack messages to be shown to the user.
static getSystemStyleCategoryAssignments(string $a_skin_id, string $a_style_id)
Get all system sub styles category assignments.
global $DIC
Definition: feed.php:28
$target_arr
Definition: goto.php:50
$ref_id
Definition: ltiauth.php:67
$path
Definition: ltiservices.php:32
$factory
Definition: metadata.php:75
$i
Definition: metadata.php:41
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
$messages
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: xapiexit.php:22