ILIAS  release_8 Revision v8.24
class.ilStyleImportParser.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
22
29{
30 protected string $cdata = "";
31 protected array $cur_template_classes;
32 protected array $cur_template;
33 protected array $current_tags = [];
34 protected string $current_type = "";
35 protected string $current_class = "";
36 protected string $current_tag = "";
37 protected array $styles;
39 protected ilTree $tree;
41 protected array $chars = [];
42
43 public function __construct(
44 string $a_xml_file,
45 ilObjStyleSheet $a_style_obj
46 ) {
47 global $DIC;
48
49 $this->lng = $DIC->language();
50 $this->tree = $DIC->repositoryTree();
51
52 $service = $DIC->contentStyle()->internal();
53 $access_manager = $service->domain()->access(0, $DIC->user()->getId());
54 $access_manager->enableWrite(true);
55
56 $this->color_manager = $service->domain()->color(
57 $a_style_obj->getId(),
58 $access_manager
59 );
60
61 $this->style_obj = $a_style_obj;
62
63 parent::__construct($a_xml_file);
64 }
65
66
72 public function setHandlers($a_xml_parser): void
73 {
74 xml_set_object($a_xml_parser, $this);
75 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
76 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
77 }
78
82 public function startParsing(): void
83 {
84 $this->styles = array();
85 parent::startParsing();
86 $this->style_obj->setStyle($this->styles);
87 $this->style_obj->setCharacteristics($this->chars);
88 }
89
90 public function handlerBeginTag(
91 $a_xml_parser,
92 string $a_name,
93 array $a_attribs
94 ): void {
95 $a_attribs = $this->trimAndStripAttribs($a_attribs);
96 switch ($a_name) {
97 case "Style":
98 $this->current_tag = $a_attribs["Tag"];
99 $this->current_class = $a_attribs["Class"];
100 $this->current_type = $a_attribs["Type"];
101 if ($this->current_class == "PageTitle" && $this->current_type == "page_title" && $this->current_tag == "div") {
102 $this->current_tag = "h1";
103 }
104 if ($this->current_class == "Headline1" && $this->current_tag == "div") {
105 $this->current_tag = "h1";
106 $this->current_type = "heading1";
107 }
108 if ($this->current_class == "Headline2" && $this->current_tag == "div") {
109 $this->current_tag = "h2";
110 $this->current_type = "heading2";
111 }
112 if ($this->current_class == "Headline3" && $this->current_tag == "div") {
113 $this->current_tag = "h3";
114 $this->current_type = "heading3";
115 }
116 $this->current_tags = array();
117 $this->chars[] = array("type" => $this->current_type,
118 "class" => $this->current_class);
119 break;
120
121 case "StyleParameter":
122 $this->current_tags[] = array(
123 "tag" => $this->current_tag,
124 "class" => $this->current_class,
125 "parameter" => $a_attribs["Name"],
126 "type" => $this->current_type,
127 "value" => $a_attribs["Value"],
128 "custom" => $a_attribs["Custom"] ?? null);
129 break;
130
131 case "StyleColor":
132 $this->color_manager->addColor($a_attribs["Name"], $a_attribs["Code"]);
133 break;
134
135 case "StyleTemplate":
136 $this->cur_template = array("type" => $a_attribs["Type"],
137 "name" => $a_attribs["Name"]);
138 $this->cur_template_classes = array();
139 break;
140
141 case "StyleTemplateClass":
142 $this->cur_template_classes[$a_attribs["ClassType"]] =
143 $a_attribs["Class"];
144 break;
145
146 }
147 $this->cdata = "";
148 }
149
150 public function handlerEndTag(
151 $a_xml_parser,
152 string $a_name
153 ): void {
154 $this->cdata = $this->trimAndStrip($this->cdata);
155 switch ($a_name) {
156 case "Title":
157 $this->style_obj->setTitle($this->cdata);
158 break;
159
160 case "Description":
161 $this->style_obj->setDescription($this->cdata);
162 break;
163
164 case "Style":
165 $this->styles[] = $this->current_tags;
166 break;
167
168 case "StyleTemplate":
169 $this->style_obj->addTemplate(
170 $this->cur_template["type"],
171 $this->cur_template["name"],
172 $this->cur_template_classes
173 );
174 break;
175
176 }
177 }
178
179 public function handlerCharacterData(
180 $a_xml_parser,
181 string $a_data
182 ): void {
183 // i don't know why this is necessary, but
184 // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
185 // in character data, but we don't want that, because it's the
186 // way we mask user html in our content, so we convert back...
187 $a_data = str_replace("<", "&lt;", $a_data);
188 $a_data = str_replace(">", "&gt;", $a_data);
189
190 // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
191 $a_data = preg_replace("/\n/", "", $a_data);
192 $a_data = preg_replace("/\t+/", "", $a_data);
193 if (!empty($a_data)) {
194 $this->cdata .= $a_data;
195 }
196 }
197
198 protected function trimAndStripAttribs(array $attribs): array
199 {
200 $ret = [];
201 foreach ($attribs as $k => $v) {
202 $ret[$k] = $this->trimAndStrip((string) $v);
203 }
204 return $ret;
205 }
206
207 protected function trimAndStrip(string $input): string
208 {
209 return ilUtil::stripSlashes(trim($input));
210 }
211}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handlerEndTag( $a_xml_parser, string $a_name)
__construct(string $a_xml_file, ilObjStyleSheet $a_style_obj)
handlerCharacterData( $a_xml_parser, string $a_data)
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class @access private
handlerBeginTag( $a_xml_parser, string $a_name, array $a_attribs)
Content ColorManager $color_manager
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
global $DIC
Definition: feed.php:28
$service
Definition: ltiservices.php:43
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...