ILIAS  trunk Revision v11.0_alpha-1753-gb21ca8c4367
All Data Structures Namespaces Files Functions Variables Enumerations Enumerator Modules Pages
class.ilCategoryImportParser.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
27 {
32  public array $parent; // current parent ref id
33  public int $parent_cnt;
34  public int $withrol;
35  protected ilLogger $cat_log;
37  protected string $default_language = "";
38  protected string $cur_spec_lang = "";
39  protected string $cur_title = "";
40  protected string $cur_description = "";
41  protected string $cdata = "";
42 
49  public function __construct(
50  string $a_xml_file,
51  int $a_parent,
52  int $withrol
53  ) {
55  global $DIC;
56 
57  $this->rbacadmin = $DIC->rbac()->admin();
58  $this->rbacreview = $DIC->rbac()->review();
59  $this->rbacsystem = $DIC->rbac()->system();
60  $this->parent_cnt = 0;
61  $this->parent[$this->parent_cnt] = $a_parent;
62  $this->parent_cnt++;
63  $this->withrol = $withrol;
64 
65  parent::__construct($a_xml_file);
66  }
67 
68  public function setHandlers($a_xml_parser): void
69  {
70  xml_set_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
71  xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
72  }
73 
74  // generate a tag with given name and attributes
75  public function buildTag(
76  string $type,
77  string $name,
78  ?array $attr = null
79  ): string {
80  $tag = "<";
81 
82  if ($type === "end") {
83  $tag .= "/";
84  }
85 
86  $tag .= $name;
87 
88  if (is_array($attr)) {
89  foreach ($attr as $k => $v) {
90  $tag .= " " . $k . "=\"$v\"";
91  }
92  }
93 
94  $tag .= ">";
95 
96  return $tag;
97  }
98 
105  public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
106  {
107  $a_attribs = $this->trimAndStripAttribs($a_attribs);
108  switch ($a_name) {
109  case "Category":
110  $cur_parent = $this->parent[$this->parent_cnt - 1];
111  $this->category = new ilObjCategory();
112  $this->category->setImportId($a_attribs["Id"] . " (#" . $cur_parent . ")");
113  $this->default_language = $a_attribs["DefaultLanguage"];
114  $this->category->setTitle($a_attribs["Id"]);
115  $this->category->create();
116  $this->category->createReference();
117  $this->category->putInTree($cur_parent);
118  $this->parent[$this->parent_cnt++] = $this->category->getRefId();
119  break;
120 
121  case "CategorySpec":
122  $this->cur_spec_lang = $a_attribs["Language"];
123  break;
124  }
125  }
126 
132  public function handlerEndTag($a_xml_parser, string $a_name): void
133  {
134  $this->cdata = $this->trimAndStrip($this->cdata);
135  switch ($a_name) {
136  case "Category":
137  unset($this->category, $this->parent[$this->parent_cnt - 1]);
138  $this->parent_cnt--;
139  break;
140 
141  case "CategorySpec":
142  $is_def = '0';
143  if ($this->cur_spec_lang === $this->default_language) {
144  $this->category->setTitle($this->cur_title);
145  $this->category->setDescription($this->cur_description);
146  $this->category->update();
147  $is_def = '1';
148  }
149  $this->category->addTranslation(
150  $this->cur_title,
151  $this->cur_description,
152  $this->cur_spec_lang,
153  $is_def
154  );
155  break;
156 
157  case "Title":
158  $this->cur_title = $this->cdata;
159  break;
160 
161  case "Description":
162  $this->cur_description = $this->cdata;
163  break;
164  }
165 
166  $this->cdata = "";
167  }
168 
174  public function handlerCharacterData($a_xml_parser, string $a_data): void
175  {
176  // i don't know why this is necessary, but
177  // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
178  // in character data, but we don't want that, because it's the
179  // way we mask user html in our content, so we convert back...
180  $a_data = str_replace(["<", ">"], ["&lt;", "&gt;"], $a_data);
181 
182  // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
183  $a_data = preg_replace("/\n/", "", $a_data);
184  $a_data = preg_replace("/\t+/", "", $a_data);
185  if (!empty($a_data)) {
186  $this->cdata .= $a_data;
187  }
188  }
189 }
handlerCharacterData($a_xml_parser, string $a_data)
buildTag(string $type, string $name, ?array $attr=null)
while($session_entry=$r->fetchRow(ilDBConstants::FETCHMODE_ASSOC)) return null
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)
global $DIC
Definition: shib_login.php:22
Class ilObjCategory.
__construct(Container $dic, ilPlugin $plugin)
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
Class ilRbacAdmin Core functions for role based access control.
__construct(?string $path_to_file='', ?bool $throw_exception=false)