ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilSkin.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27class ilSkin implements Iterator, Countable
28{
32 protected string $id = '';
33
34
38 protected string $name = '';
39
45 protected array $styles = [];
46
50 protected string $version = '0.1';
51
52 public function __construct(string $id, string $name)
53 {
54 $this->setId($id);
55 $this->setName($name);
56 }
57
61 public function asXML(): string
62 {
63 $xml = new SimpleXMLElement("<?xml version='1.0' encoding='UTF-8'?><template/>");
64 $xml->addAttribute('xmlns', 'http://www.w3.org');
65 $xml->addAttribute('version', $this->getVersion());
66 $xml->addAttribute('name', $this->getName());
67
68 foreach ($this->getStyles() as $style) {
69 if (!$style->isSubstyle()) {
70 $this->addChildToXML($xml, $style);
71
72 foreach ($this->getSubstylesOfStyle($style->getId()) as $substyle) {
73 $this->addChildToXML($xml, $substyle);
74 }
75 }
76 }
77
78 $dom = new DOMDocument('1.0', 'utf-8');
79 $dom->formatOutput = true;
80 $dom->loadXML($xml->asXML());
81 return $dom->saveXML();
82 }
83
87 protected function addChildToXML(SimpleXMLElement $xml, ilSkinStyle $style): void
88 {
89 if ($style->isSubstyle()) {
90 $xml_style = $xml->addChild('substyle');
91 } else {
92 $xml_style = $xml->addChild('style');
93 }
94 $xml_style->addAttribute('id', $style->getId());
95 $xml_style->addAttribute('name', $style->getName());
96 $xml_style->addAttribute('image_directory', $style->getImageDirectory());
97 $xml_style->addAttribute('css_file', $style->getCssFile());
98 $xml_style->addAttribute('sound_directory', $style->getSoundDirectory());
99 $xml_style->addAttribute('font_directory', $style->getFontDirectory());
100 }
101
102 public function writeToXMLFile(string $path): void
103 {
104 file_put_contents($path, $this->asXML());
105 }
106
107 public function addStyle(ilSkinStyle $style): void
108 {
109 $this->styles[] = $style;
110 }
111
115 public function removeStyle(string $id): void
116 {
117 foreach ($this->getStyles() as $index => $style) {
118 if ($style->getId() == $id) {
119 unset($this->styles[$index]);
120 return;
121 }
122 }
124 }
125
129 public function getStyle(string $id): ilSkinStyle
130 {
131 foreach ($this->getStyles() as $style) {
132 if ($style->getId() == $id) {
133 return $style;
134 }
135 }
137 }
138
139 public function hasStyle(string $id): bool
140 {
141 foreach ($this->getStyles() as $style) {
142 if ($style->getId() == $id) {
143 return true;
144 }
145 }
146 return false;
147 }
148
149 public function getDefaultStyle(): ilSkinStyle
150 {
151 return array_values($this->styles)[0];
152 }
153
154 public function valid(): bool
155 {
156 return current($this->styles) !== false;
157 }
158
159 public function key(): int
160 {
161 return key($this->styles);
162 }
163
164 public function current(): ilSkinStyle
165 {
166 return current($this->styles);
167 }
168
169 public function next(): void
170 {
171 next($this->styles);
172 }
173
174 public function rewind(): void
175 {
176 reset($this->styles);
177 }
178
179 public function count(): int
180 {
181 return count($this->styles);
182 }
183
184 public function getId(): string
185 {
186 return $this->id;
187 }
188
192 public function setId(string $id): void
193 {
194 if (strpos($id, ' ') !== false) {
196 }
197 $this->id = str_replace(' ', '_', $id);
198 }
199
200 public function getName(): string
201 {
202 return $this->name;
203 }
204
205 public function setName(string $name): void
206 {
207 $this->name = $name;
208 }
209
213 public function getStyles(): array
214 {
215 return $this->styles;
216 }
217
221 public function setStyles(array $styles): void
222 {
223 $this->styles = $styles;
224 }
225
226 public function getVersion(): string
227 {
228 return $this->version;
229 }
230
231 public function setVersion(string $version): void
232 {
233 if ($version != '' && $this->isVersionChangeable()) {
234 $this->version = $version;
235 }
236 }
237
238 public function getVersionStep(string $version): string
239 {
240 if ($this->isVersionChangeable()) {
241 $v = explode('.', ($version == '' ? '0.1' : $version));
242 $count = count($v) ;
243 $v[$count - 1] = ((int) $v[$count - 1] + 1); //ToDo PHP8 Review: You are adding an int to a string in strict_types.
244 $this->version = implode('.', $v);
245 }
246 return $this->version;
247 }
248
249 public function isVersionChangeable(): bool
250 {
251 return ($this->version != '$Id$');
252 }
253
254 public function updateParentStyleOfSubstyles(string $old_parent_style_id, string $new_parent_style_id): void
255 {
256 if ($this->hasStyleSubstyles($old_parent_style_id)) {
257 foreach ($this->getSubstylesOfStyle($old_parent_style_id) as $substyle) {
258 $substyle->setSubstyleOf($new_parent_style_id);
259 }
260 }
261 }
262
266 public function getSubstylesOfStyle(string $style_id): array
267 {
268 $substyles = [];
269
270 foreach ($this->getStyles() as $style) {
271 if ($style->getId() != $style_id && $style->isSubstyle()) {
272 if ($style->getSubstyleOf() == $style_id) {
273 $substyles[$style->getId()] = $style;
274 }
275 }
276 }
277
278 return $substyles;
279 }
280
284 public function hasStyleSubstyles(string $style_id): bool
285 {
286 foreach ($this->getStyles() as $style) {
287 if ($style->getId() != $style_id && $style->isSubstyle()) {
288 if ($style->getSubstyleOf() == $style_id) {
289 return true;
290 }
291 }
292 }
293 return false;
294 }
295
296 public function hasStyles(): bool
297 {
298 return count($this->getStyles()) > 0;
299 }
300}
isSubstyle()
Return wheter this style is a substyle of another.
ilSkin holds an manages the basic data of a skin as provide by the template of the skin.
removeStyle(string $id)
string $name
Name of the skin, as provided in the template.
hasStyle(string $id)
__construct(string $id, string $name)
setVersion(string $version)
updateParentStyleOfSubstyles(string $old_parent_style_id, string $new_parent_style_id)
addChildToXML(SimpleXMLElement $xml, ilSkinStyle $style)
Used to generate the xml for styles contained by the skin.
addStyle(ilSkinStyle $style)
setId(string $id)
asXML()
Stores the skin and all it's styles as xml.
getVersionStep(string $version)
array $styles
string $id
ID of the skin, equals the name of the folder this skin is stored in.
setStyles(array $styles)
writeToXMLFile(string $path)
isVersionChangeable()
hasStyleSubstyles(string $style_id)
Returns wheter a given style has substyles.
string $version
Version of skin, as provided by the template.
getSubstylesOfStyle(string $style_id)
getDefaultStyle()
setName(string $name)
getStyle(string $id)
Class for advanced editing exception handling in ILIAS.
$path
Definition: ltiservices.php:30