ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilSkinXML.php
Go to the documentation of this file.
1<?php
2include_once("Services/Style/System/classes/Exceptions/class.ilSystemStyleException.php");
3include_once("Services/Style/System/classes/Utilities/class.ilSkinStyleXML.php");
4
14class ilSkinXML implements \Iterator, \Countable{
15
20 protected $id = "";
21
22
27 protected $name = "";
28
34 protected $styles = array();
35
41 protected $version = "0.1";
42
43
48 public function __construct($id, $name)
49 {
50 $this->setId($id);
51 $this->setName($name);
52 }
53
59 public static function parseFromXML($path = ""){
60 try{
61 $xml = new SimpleXMLElement(file_get_contents($path));
62 }catch(Exception $e){
64 }
65
66 $id = basename (dirname($path));
67 $skin = new self($id,(string)$xml->attributes()["name"]);
68 $skin->setVersion((string)$xml->attributes()["version"]);
69
73 $last_style = null;
74
75
76 foreach($xml->children() as $style_xml){
77
79
83 if($style_xml->getName() == "substyle") {
84 if(!$last_style){
86 }
87 $style->setSubstyleOf($last_style->getId());
88 }else{
89 $last_style = $style;
90 }
91 $skin->addStyle($style);
92
93 }
94 return $skin;
95 }
96
102 public function asXML(){
103 $xml = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><template/>');
104 $xml->addAttribute("xmlns","http://www.w3.org");
105 $xml->addAttribute("version",$this->getVersion());
106 $xml->addAttribute("name",$this->getName());
107
108 $last_style = null;
109
110 foreach($this->getStyles() as $style){
111 if(!$style->isSubstyle()){
112 $this->addChildToXML($xml, $style);
113
114 foreach($this->getSubstylesOfStyle($style->getId()) as $substyle){
115 $this->addChildToXML($xml, $substyle);
116 }
117 }
118 }
119
120 $dom = new DOMDocument('1.0', 'utf-8');
121 $dom->formatOutput = true;
122 $dom->loadXML($xml->asXML());
123 return $dom->saveXML();
124 }
125
132 protected function addChildToXML(SimpleXMLElement $xml,ilSkinStyleXML $style){
133 $xml_style = null;
134 if($style->isSubstyle()){
135 $xml_style = $xml->addChild("substyle");
136 }
137 else{
138 $xml_style = $xml->addChild("style");
139 }
140 $xml_style->addAttribute("id", $style->getId());
141 $xml_style->addAttribute("name", $style->getName());
142 $xml_style->addAttribute("image_directory", $style->getImageDirectory());
143 $xml_style->addAttribute("css_file", $style->getCssFile());
144 $xml_style->addAttribute("sound_directory", $style->getSoundDirectory());
145 $xml_style->addAttribute("font_directory", $style->getFontDirectory());
146 }
147
151 public function writeToXMLFile($path){
152 file_put_contents($path, $this->asXML());
153 }
158 $this->styles[] = $style;
159 }
160
165 public function removeStyle($id){
166 foreach($this->getStyles() as $index => $style){
167 if($style->getId() == $id){
168 unset($this->styles[$index]);
169 return;
170 }
171 }
173 }
174
180 public function getStyle($id){
181 foreach($this->getStyles() as $style){
182 if($style->getId() == $id){
183 return $style;
184 }
185 }
187 }
188
193 public function hasStyle($id){
194 foreach($this->getStyles() as $style){
195 if($style->getId() == $id){
196 return true;
197 }
198 }
199 return false;
200 }
201
205 public function getDefaultStyle(){
206 return array_values($this->styles)[0];
207 }
208
214 public function valid() {
215 return current($this->styles) !== false;
216 }
217
221 public function key() {
222 return key($this->styles);
223 }
224
228 public function current() {
229 return current($this->styles);
230 }
231
232 public function next() {
233 next($this->styles);
234 }
235 public function rewind() {
236 reset($this->styles);
237 }
238
242 public function count(){
243 return count($this->styles);
244 }
245
249 public function getId()
250 {
251 return $this->id;
252 }
253
258 public function setId($id)
259 {
260 if (strpos($id, ' ') !== false) {
262 }
263 $this->id = str_replace(" ","_",$id);
264 }
265
266
270 public function getName()
271 {
272 return $this->name;
273 }
274
278 public function setName($name)
279 {
280 $this->name = $name;
281 }
282
286 public function getStyles()
287 {
288 return $this->styles;
289 }
290
294 public function setStyles($styles)
295 {
296 $this->styles = $styles;
297 }
298
302 public function getVersion()
303 {
304 return $this->version;
305 }
306
310 public function setVersion($version)
311 {
312 if($version != null && $version != '' && $this->isVersionChangeable()) {
313 $this->version = $version;
314 }
315 }
316
320 public function getVersionStep($version)
321 {
322 if($this->isVersionChangeable()) {
323 $v = explode('.', ($version == "" ? '0.1' : $version));
324 $v[count($v) - 1] = ($v[count($v) - 1] + 1);
325 $this->version = implode('.', $v);
326 }
327 return $this->version;
328 }
329
330 public function isVersionChangeable()
331 {
332 return ($this->version != '$Id$');
333 }
334
339 public function getSubstylesOfStyle($style_id){
340 $substyles = array();
341
342 if($this->getStyle($style_id)){
343 foreach($this->getStyles() as $style){
344 if($style->getId() != $style_id && $style->isSubstyle()){
345 if($style->getSubstyleOf() == $style_id){
346 $substyles[$style->getId()] = $style;
347 }
348 }
349 }
350 }
351 return $substyles;
352 }
353
359 public function hasStyleSubstyles($style_id){
360 if($this->getStyle($style_id)){
361 foreach($this->getStyles() as $style){
362 if($style->getId() != $style_id && $style->isSubstyle()){
363 if($style->getSubstyleOf() == $style_id){
364 return true;
365 }
366 }
367 }
368 }
369 return false;
370
371 }
372
376 public function hasStyles(){
377 return count($this->getStyles()) > 0;
378 }
379}
$path
Definition: aliased.php:25
An exception for terminatinating execution or to throw for unit testing.
static parseFromXMLElement(SimpleXMLElement $xml_element)
ilSkinXml holds an manages the basic data of a skin as provide by the template of the skin.
getVersionStep($version)
setStyles($styles)
asXML()
Stores the skin and all it's styles as xml.
valid()
Iterator implementations.
hasStyleSubstyles($style_id)
Returns wheter a given style has substyles.
getSubstylesOfStyle($style_id)
writeToXMLFile($path)
addChildToXML(SimpleXMLElement $xml, ilSkinStyleXML $style)
Used to generate the xml for styles contained by the skin.
setVersion($version)
__construct($id, $name)
ilSkinXML constructor.
addStyle(ilSkinStyleXML $style)
count()
Countable implementations.
Class for advanced editing exception handling in ILIAS.
$style
Definition: example_012.php:70