ILIAS  trunk Revision v11.0_alpha-1723-g8e69f309bab
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilStyleImportParser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
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_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
75  xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
76  }
77 
81  public function startParsing(): void
82  {
83  $this->styles = array();
84  parent::startParsing();
85  $this->style_obj->setStyle($this->styles);
86  $this->style_obj->setCharacteristics($this->chars);
87  }
88 
89  public function handlerBeginTag(
90  $a_xml_parser,
91  string $a_name,
92  array $a_attribs
93  ): void {
94  $a_attribs = $this->trimAndStripAttribs($a_attribs);
95  switch ($a_name) {
96  case "Style":
97  $this->current_tag = $a_attribs["Tag"];
98  $this->current_class = $a_attribs["Class"];
99  $this->current_type = $a_attribs["Type"];
100  if ($this->current_class == "PageTitle" && $this->current_type == "page_title" && $this->current_tag == "div") {
101  $this->current_tag = "h1";
102  }
103  if ($this->current_class == "Headline1" && $this->current_tag == "div") {
104  $this->current_tag = "h1";
105  $this->current_type = "heading1";
106  }
107  if ($this->current_class == "Headline2" && $this->current_tag == "div") {
108  $this->current_tag = "h2";
109  $this->current_type = "heading2";
110  }
111  if ($this->current_class == "Headline3" && $this->current_tag == "div") {
112  $this->current_tag = "h3";
113  $this->current_type = "heading3";
114  }
115  $this->current_tags = array();
116  $this->chars[] = array("type" => $this->current_type,
117  "class" => $this->current_class);
118  break;
119 
120  case "StyleParameter":
121  $this->current_tags[] = array(
122  "tag" => $this->current_tag,
123  "class" => $this->current_class,
124  "parameter" => $a_attribs["Name"],
125  "type" => $this->current_type,
126  "value" => $a_attribs["Value"],
127  "custom" => $a_attribs["Custom"] ?? null);
128  break;
129 
130  case "StyleColor":
131  $this->color_manager->addColor($a_attribs["Name"], $a_attribs["Code"]);
132  break;
133 
134  case "StyleTemplate":
135  $this->cur_template = array("type" => $a_attribs["Type"],
136  "name" => $a_attribs["Name"]);
137  $this->cur_template_classes = array();
138  break;
139 
140  case "StyleTemplateClass":
141  $this->cur_template_classes[$a_attribs["ClassType"]] =
142  $a_attribs["Class"];
143  break;
144  }
145  $this->cdata = "";
146  }
147 
148  public function handlerEndTag(
149  $a_xml_parser,
150  string $a_name
151  ): void {
152  $this->cdata = $this->trimAndStrip($this->cdata);
153  switch ($a_name) {
154  case "Title":
155  $this->style_obj->setTitle($this->cdata);
156  break;
157 
158  case "Description":
159  $this->style_obj->setDescription($this->cdata);
160  break;
161 
162  case "Style":
163  $this->styles[] = $this->current_tags;
164  break;
165 
166  case "StyleTemplate":
167  $this->style_obj->addTemplate(
168  $this->cur_template["type"],
169  $this->cur_template["name"],
170  $this->cur_template_classes
171  );
172  break;
173  }
174  }
175 
176  public function handlerCharacterData(
177  $a_xml_parser,
178  string $a_data
179  ): void {
180  // i don't know why this is necessary, but
181  // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
182  // in character data, but we don't want that, because it's the
183  // way we mask user html in our content, so we convert back...
184  $a_data = str_replace("<", "&lt;", $a_data);
185  $a_data = str_replace(">", "&gt;", $a_data);
186 
187  // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
188  $a_data = preg_replace("/\n/", "", $a_data);
189  $a_data = preg_replace("/\t+/", "", $a_data);
190  if (!empty($a_data)) {
191  $this->cdata .= $a_data;
192  }
193  }
194 
195  protected function trimAndStripAttribs(array $attribs): array
196  {
197  $ret = [];
198  foreach ($attribs as $k => $v) {
199  $ret[$k] = $this->trimAndStrip((string) $v);
200  }
201  return $ret;
202  }
203 
204  protected function trimAndStrip(string $input): string
205  {
206  return ilUtil::stripSlashes(trim($input));
207  }
208 }
handlerEndTag( $a_xml_parser, string $a_name)
static stripSlashes(string $a_str, bool $a_strip_html=true, string $a_allow="")
startParsing()
start the parser
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class private
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
global $DIC
Definition: shib_login.php:22
Content ColorManager $color_manager
__construct(Container $dic, ilPlugin $plugin)
handlerBeginTag( $a_xml_parser, string $a_name, array $a_attribs)
$service
Definition: ltiservices.php:40
__construct(string $a_xml_file, ilObjStyleSheet $a_style_obj)
handlerCharacterData( $a_xml_parser, string $a_data)