ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilSkinXML.php
Go to the documentation of this file.
1 <?php
2 include_once("Services/Style/System/classes/Exceptions/class.ilSystemStyleException.php");
3 include_once("Services/Style/System/classes/Utilities/class.ilSkinStyleXML.php");
4 
14 class ilSkinXML implements \Iterator, \Countable
15 {
16 
21  protected $id = "";
22 
23 
28  protected $name = "";
29 
35  protected $styles = array();
36 
42  protected $version = "0.1";
43 
44 
49  public function __construct($id, $name)
50  {
51  $this->setId($id);
52  $this->setName($name);
53  }
54 
60  public static function parseFromXML($path = "")
61  {
62  try {
63  $xml = new SimpleXMLElement(file_get_contents($path));
64  } catch (Exception $e) {
66  }
67 
68  $id = basename(dirname($path));
69  $skin = new self($id, (string) $xml->attributes()["name"]);
70  $skin->setVersion((string) $xml->attributes()["version"]);
71 
75  $last_style = null;
76 
77 
78  foreach ($xml->children() as $style_xml) {
80 
84  if ($style_xml->getName() == "substyle") {
85  if (!$last_style) {
87  }
88  $style->setSubstyleOf($last_style->getId());
89  } else {
90  $last_style = $style;
91  }
92  $skin->addStyle($style);
93  }
94  return $skin;
95  }
96 
102  public function asXML()
103  {
104  $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><template/>');
105  $xml->addAttribute("xmlns", "http://www.w3.org");
106  $xml->addAttribute("version", $this->getVersion());
107  $xml->addAttribute("name", $this->getName());
108 
109  $last_style = null;
110 
111  foreach ($this->getStyles() as $style) {
112  if (!$style->isSubstyle()) {
113  $this->addChildToXML($xml, $style);
114 
115  foreach ($this->getSubstylesOfStyle($style->getId()) as $substyle) {
116  $this->addChildToXML($xml, $substyle);
117  }
118  }
119  }
120 
121  $dom = new DOMDocument('1.0', 'utf-8');
122  $dom->formatOutput = true;
123  $dom->loadXML($xml->asXML());
124  return $dom->saveXML();
125  }
126 
134  {
135  $xml_style = null;
136  if ($style->isSubstyle()) {
137  $xml_style = $xml->addChild("substyle");
138  } else {
139  $xml_style = $xml->addChild("style");
140  }
141  $xml_style->addAttribute("id", $style->getId());
142  $xml_style->addAttribute("name", $style->getName());
143  $xml_style->addAttribute("image_directory", $style->getImageDirectory());
144  $xml_style->addAttribute("css_file", $style->getCssFile());
145  $xml_style->addAttribute("sound_directory", $style->getSoundDirectory());
146  $xml_style->addAttribute("font_directory", $style->getFontDirectory());
147  }
148 
152  public function writeToXMLFile($path)
153  {
154  file_put_contents($path, $this->asXML());
155  }
159  public function addStyle(ilSkinStyleXML $style)
160  {
161  $this->styles[] = $style;
162  }
163 
168  public function removeStyle($id)
169  {
170  foreach ($this->getStyles() as $index => $style) {
171  if ($style->getId() == $id) {
172  unset($this->styles[$index]);
173  return;
174  }
175  }
177  }
178 
184  public function getStyle($id)
185  {
186  foreach ($this->getStyles() as $style) {
187  if ($style->getId() == $id) {
188  return $style;
189  }
190  }
192  }
193 
198  public function hasStyle($id)
199  {
200  foreach ($this->getStyles() as $style) {
201  if ($style->getId() == $id) {
202  return true;
203  }
204  }
205  return false;
206  }
207 
211  public function getDefaultStyle()
212  {
213  return array_values($this->styles)[0];
214  }
215 
221  public function valid()
222  {
223  return current($this->styles) !== false;
224  }
225 
229  public function key()
230  {
231  return key($this->styles);
232  }
233 
237  public function current()
238  {
239  return current($this->styles);
240  }
241 
242  public function next()
243  {
244  next($this->styles);
245  }
246  public function rewind()
247  {
248  reset($this->styles);
249  }
250 
254  public function count()
255  {
256  return count($this->styles);
257  }
258 
262  public function getId()
263  {
264  return $this->id;
265  }
266 
271  public function setId($id)
272  {
273  if (strpos($id, ' ') !== false) {
275  }
276  $this->id = str_replace(" ", "_", $id);
277  }
278 
279 
283  public function getName()
284  {
285  return $this->name;
286  }
287 
291  public function setName($name)
292  {
293  $this->name = $name;
294  }
295 
299  public function getStyles()
300  {
301  return $this->styles;
302  }
303 
307  public function setStyles($styles)
308  {
309  $this->styles = $styles;
310  }
311 
315  public function getVersion()
316  {
317  return $this->version;
318  }
319 
323  public function setVersion($version)
324  {
325  if ($version != null && $version != '' && $this->isVersionChangeable()) {
326  $this->version = $version;
327  }
328  }
329 
333  public function getVersionStep($version)
334  {
335  if ($this->isVersionChangeable()) {
336  $v = explode('.', ($version == "" ? '0.1' : $version));
337  $v[count($v) - 1] = ($v[count($v) - 1] + 1);
338  $this->version = implode('.', $v);
339  }
340  return $this->version;
341  }
342 
343  public function isVersionChangeable()
344  {
345  return ($this->version != '$Id$');
346  }
347 
352  public function getSubstylesOfStyle($style_id)
353  {
354  $substyles = array();
355 
356  if ($this->getStyle($style_id)) {
357  foreach ($this->getStyles() as $style) {
358  if ($style->getId() != $style_id && $style->isSubstyle()) {
359  if ($style->getSubstyleOf() == $style_id) {
360  $substyles[$style->getId()] = $style;
361  }
362  }
363  }
364  }
365  return $substyles;
366  }
367 
373  public function hasStyleSubstyles($style_id)
374  {
375  if ($this->getStyle($style_id)) {
376  foreach ($this->getStyles() as $style) {
377  if ($style->getId() != $style_id && $style->isSubstyle()) {
378  if ($style->getSubstyleOf() == $style_id) {
379  return true;
380  }
381  }
382  }
383  }
384  return false;
385  }
386 
390  public function hasStyles()
391  {
392  return count($this->getStyles()) > 0;
393  }
394 }
$path
Definition: aliased.php:25
addStyle(ilSkinStyleXML $style)
$style
Definition: example_012.php:70
writeToXMLFile($path)
setStyles($styles)
__construct($id, $name)
ilSkinXML constructor.
setVersion($version)
static parseFromXMLElement(SimpleXMLElement $xml_element)
hasStyleSubstyles($style_id)
Returns wheter a given style has substyles.
getSubstylesOfStyle($style_id)
$index
Definition: metadata.php:60
ilSkinXml holds an manages the basic data of a skin as provide by the template of the skin...
isSubstyle()
Return wheter this style is a substyle of another.
addChildToXML(SimpleXMLElement $xml, ilSkinStyleXML $style)
Used to generate the xml for styles contained by the skin.
Class for advanced editing exception handling in ILIAS.
count()
Countable implementations.
valid()
Iterator implementations.
asXML()
Stores the skin and all it&#39;s styles as xml.
getVersionStep($version)