ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
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
34 public static ?array $skins = null;
35
39 protected ilSkin $skin;
40
44 protected static array $cached_all_styles_information;
45
51
53
58 public function __construct(string $skin_id = '', ?ilSystemStyleConfig $system_style_config = null)
59 {
60 global $DIC;
61
62 if ($skin_id == '') {
63 $skin_id = self::getCurrentSkin();
64 }
65
68 } else {
69 $this->setSystemStylesConf($system_style_config);
70 }
71
72 $this->skin_factory = new ilSkinFactory($DIC->language(), $this->getSystemStylesConf());
73
74 if ($skin_id != $this->getSystemStylesConf()->getDefaultSkinId()) {
75 $this->setSkin($this->skin_factory->skinFromXML($this->getSystemStylesConf()->getCustomizingSkinPath() . $skin_id . '/template.xml'));
76 } else {
77 $this->setSkin($this->skin_factory->skinFromXML($this->getSystemStylesConf()->getDefaultTemplatePath()));
78 }
79 }
80
86 public static function getCurrentSkin(): ?string
87 {
88 global $DIC;
89
90 if (!$DIC || is_array($DIC)) {
91 return null;
92 }
93 if ($DIC->isDependencyAvailable('systemStyle') && is_object($DIC->systemStyle()->getSkin())) {
94 return $DIC->systemStyle()->getSkin()->getId();
95 } else {
96 $system_style_conf = new ilSystemStyleConfig();
97
98 if ($DIC->isDependencyAvailable('user') && is_object($DIC->user())) {
99 $skin_id = $DIC->user()->getSkin();
100 if ($skin_id && !self::skinExists($skin_id)) {
101 if ($DIC->isDependencyAvailable('systemStyle')) {
102 $messages = new ilSystemStyleMessageStack($DIC->ui()->mainTemplate());
103 $message_text = $DIC->language()->txt('set_skin_does_not_exist') . ' ' . $skin_id;
104 $messages->addMessage(new ilSystemStyleMessage($message_text, ilSystemStyleMessage::TYPE_ERROR));
105 $messages->sendMessages();
106 }
107 $skin_id = $system_style_conf->getDefaultSkinId();
108 }
109 return $skin_id === '' ? $system_style_conf->getDefaultSkinId() : $skin_id;
110 } else {
111 return null;
112 }
113 }
114 }
115
119 public function getStyles(): array
120 {
121 return $this->getSkin()->getStyles();
122 }
123
124 public function getTemplateName(): string
125 {
126 return $this->getSkin()->getName();
127 }
128
132 public function getStyle(string $a_id): ilSkinStyle
133 {
134 return $this->getSkin()->getStyle($a_id);
135 }
136
140 public function getStyleName(string $a_id): string
141 {
142 return $this->getSkin()->getStyle($a_id)->getName();
143 }
144
148 public function getImageDirectory(string $style_id): string
149 {
150 if (!$style_id) {
152 }
153 try {
154 return $this->getSkin()->getStyle($style_id)->getImageDirectory();
155 } catch (ilSystemStyleException $e) {
157 }
158 }
159
163 public function getSoundDirectory(string $style_id): string
164 {
165 return $this->getSkin()->getStyle($style_id)->getSoundDirectory();
166 }
167
171 public static function getAllSkins(?ilSystemStyleConfig $system_style_config = null): array
172 {
173 if (!self::$skins) {
176 }
177
178 global $DIC;
179
181
185 $skins = [];
187
189 $cust_skins_directory = new RecursiveDirectoryIterator(
191 FilesystemIterator::SKIP_DOTS
192 );
193 foreach ($cust_skins_directory as $skin_folder) {
194 if ($skin_folder->isDir()) {
195 $template_path = $skin_folder->getRealPath() . '/template.xml';
196 if (file_exists($template_path)) {
197 $skin = $skin_factory->skinFromXML($template_path);
198 $skins[$skin->getId()] = $skin;
199 }
200 }
201 }
202 }
203
205 }
206
207 return self::$skins;
208 }
209
213 public static function skinExists(string $skin_id, ?ilSystemStyleConfig $system_style_config = null): bool
214 {
217 }
218
219 if ($skin_id == $system_style_config->getDefaultSkinId()) {
221 return true;
222 }
223 } elseif (is_file($system_style_config->getCustomizingSkinPath() . $skin_id . '/template.xml')) {
224 return true;
225 }
226 return false;
227 }
228
235 public static function getCurrentStyle(): ?string
236 {
237 global $DIC;
238
239 if (isset(self::$current_style)) {
241 }
242
243 if (!$DIC || is_array($DIC) || !$DIC->isDependencyAvailable('user')) {
244 return null;
245 }
246
247 if ($DIC->user()->getPref('style')) {
248 self::setCurrentStyle($DIC->user()->getPref('style'));
249 } else {
252 }
253
254 if ($DIC->isDependencyAvailable('systemStyle') && self::styleExistsForCurrentSkin(self::$current_style)) {
255 if ($DIC->systemStyle()->getSkin()->hasStyleSubstyles(self::$current_style)) {
256 // read assignments, if given
258 self::getCurrentSkin(),
259 self::$current_style
260 );
261 if (count($assignments) > 0) {
262 $ref_ass = [];
263 foreach ($assignments as $a) {
264 if ($DIC->systemStyle()->getSkin()->hasStyle($a['substyle'])) {
265 $ref_ass[$a['ref_id']] = $a['substyle'];
266 }
267 }
268
269 $ref_id = '';
270
271 if ($DIC->http()->wrapper()->query()->has('ref_id')) {
272 $ref_id = $DIC->http()->wrapper()->query()->retrieve(
273 'ref_id',
274 $DIC->refinery()->kindlyTo()->string()
275 );
276 } elseif ($DIC->http()->wrapper()->query()->has('target')) {
277 $target = $DIC->http()->wrapper()->query()->retrieve(
278 'target',
279 $DIC->refinery()->kindlyTo()->string()
280 );
281 $target_arr = explode('_', $target);
282 $ref_id = $target_arr[1] ?? '';
283 }
284
285 // check whether any ref id assigns a new style
286 if ($DIC->isDependencyAvailable('repositoryTree') && $ref_id && $DIC->repositoryTree()->isInTree((int) $ref_id)) {
287 $path = $DIC->repositoryTree()->getPathId((int) $ref_id);
288 for ($i = count($path) - 1; $i >= 0; $i--) {
289 if (isset($ref_ass[$path[$i]])) {
290 self::$current_style = $ref_ass[$path[$i]];
292 }
293 }
294 }
295 }
296 }
297 }
298
299 if ($DIC->isDependencyAvailable('systemStyle') && !self::styleExistsForCurrentSkin(self::$current_style)) {
300 $messages = new ilSystemStyleMessageStack($DIC->ui()->mainTemplate());
301 $message_text = $DIC->language()->txt('set_style_does_not_exist') . ' ' . self::$current_style;
302 $messages->addMessage(new ilSystemStyleMessage($message_text, ilSystemStyleMessage::TYPE_ERROR));
303 $messages->sendMessages();
307 }
308
310 }
311
317 public static function getAllSkinStyles(): ?array
318 {
319 global $DIC;
320
321 if (!self::getCachedAllStylesInformation()) {
322 $all_styles = [];
323
324 $skins = $DIC->systemStyle()->getSkins();
325
326 foreach ($skins as $skin) {
327 foreach ($skin->getStyles() as $style) {
328 $num_users = ilObjUser::_getNumberOfUsersForStyle($skin->getId(), $style->getId());
329
330 $parent_name = '';
331 if ($style->getSubstyleOf()) {
332 $parent_name = $skin->getStyle($style->getSubstyleOf())->getName();
333 }
334
336 if ($version == '$Id$') {
337 $version = '-';
338 }
339 // default selection list
340 $all_styles[$skin->getId() . ':' . $style->getId()] = [
341 'title' => $skin->getName() . ' / ' . $style->getName(),
342 'id' => $skin->getId() . ':' . $style->getId(),
343 'skin_id' => $skin->getId(),
344 'skin_name' => $skin->getName(),
345 'template_id' => $skin->getId(),
346 'template_name' => $skin->getName(),
347 'style_id' => $style->getId(),
348 'style_name' => $style->getName(),
349 'substyle_of' => $style->getSubstyleOf(),
350 'substyle_of_name' => $parent_name,
351 'users' => $num_users,
352 'version' => $version,
353 ];
354 }
355 }
357 }
358
360 }
361
365 public static function setCurrentSkin(string $a_skin): void
366 {
367 global $DIC;
368
369 if ($DIC->isDependencyAvailable('systemStyle') && $DIC->systemStyle()->getSkin()->getName() != $a_skin) {
370 $styleDefinition = new ilStyleDefinition($a_skin);
371 if (!self::styleExistsForCurrentSkin(self::$current_style)) {
372 $styleDefinition->setCurrentStyle($DIC->systemStyle()->getSkin()->getDefaultStyle()->getId());
373 }
374 }
375 }
376
380 public static function styleExists(string $style_id): bool
381 {
382 foreach (self::getSkins() as $skin) {
383 if ($skin->hasStyle($style_id)) {
384 return true;
385 }
386 }
387 return false;
388 }
389
393 public static function styleExistsForSkinId(string $skin_id, string $style_id): bool
394 {
395 global $DIC;
396
397 if (!self::skinExists($skin_id)) {
398 return false;
399 }
400 $factory = new ilSkinFactory($DIC->language());
401 $skin = $factory->skinStyleContainerFromId($skin_id, new ilSystemStyleMessageStack($DIC->ui()->mainTemplate()))->getSkin();
402 return $skin->hasStyle($style_id);
403 }
404
405 public static function styleExistsForCurrentSkin(string $style_id): bool
406 {
407 global $DIC;
408
409 return $DIC->systemStyle()->getSkin()->hasStyle($style_id);
410 }
411
412 public static function setCurrentStyle(string $a_style): void
413 {
414 self::$current_style = $a_style;
415 }
416
421 public static function getSkins(): array
422 {
423 return self::getAllSkins();
424 }
425
429 public static function setSkins(array $skins): void
430 {
431 self::$skins = $skins;
432 }
433
434 public function getSkin(): ilSkin
435 {
436 return $this->skin;
437 }
438
439 public function setSkin(ilSkin $skin): void
440 {
441 $this->skin = $skin;
442 }
443
444 protected static function getCachedAllStylesInformation(): ?array
445 {
446 if (!isset(self::$cached_all_styles_information)) {
447 return null;
448 }
450 }
451
453 {
454 self::$cached_all_styles_information = $cached_all_styles_information;
455 }
456
458 {
460 }
461
463 {
464 $this->system_style_config = $system_style_config;
465 }
466}
$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 array $skins
Skins available, used for caching.
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.
$ref_id
Definition: ltiauth.php:66
$path
Definition: ltiservices.php:30
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
global $DIC
Definition: shib_login.php:26
$messages
Definition: xapiexit.php:21