ILIAS  release_8 Revision v8.24
class.ilCategoryImportParser.php
Go to the documentation of this file.
1<?php
2
3declare(strict_types=1);
4
5/******************************************************************************
6 *
7 * This file is part of ILIAS, a powerful learning management system.
8 *
9 * ILIAS is licensed with the GPL-3.0, you should have received a copy
10 * of said license along with the source code.
11 *
12 * If this is not the case or you just want to try ILIAS, you'll find
13 * us at:
14 * https://www.ilias.de
15 * https://github.com/ILIAS-eLearning
16 *
17 *****************************************************************************/
24{
29 public array $parent; // current parent ref id
30 public int $parent_cnt;
31 public int $withrol;
32 protected ilLogger $cat_log;
33 protected ?ilObjCategory $category = null;
34 protected string $default_language = "";
35 protected string $cur_spec_lang = "";
36 protected string $cur_title = "";
37 protected string $cur_description = "";
38 protected string $cdata = "";
39
46 public function __construct(
47 string $a_xml_file,
48 int $a_parent,
49 int $withrol
50 ) {
52 global $DIC;
53
54 $this->rbacadmin = $DIC->rbac()->admin();
55 $this->rbacreview = $DIC->rbac()->review();
56 $this->rbacsystem = $DIC->rbac()->system();
57 $this->parent_cnt = 0;
58 $this->parent[$this->parent_cnt] = $a_parent;
59 $this->parent_cnt++;
60 $this->withrol = $withrol;
61
62 parent::__construct($a_xml_file);
63 }
64
65 public function setHandlers($a_xml_parser): void
66 {
67 xml_set_object($a_xml_parser, $this);
68 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
69 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
70 }
71
72 // generate a tag with given name and attributes
73 public function buildTag(
74 string $type,
75 string $name,
76 array $attr = null
77 ): string {
78 $tag = "<";
79
80 if ($type === "end") {
81 $tag .= "/";
82 }
83
84 $tag .= $name;
85
86 if (is_array($attr)) {
87 foreach ($attr as $k => $v) {
88 $tag .= " " . $k . "=\"$v\"";
89 }
90 }
91
92 $tag .= ">";
93
94 return $tag;
95 }
96
103 public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
104 {
105 $a_attribs = $this->trimAndStripAttribs($a_attribs);
106 switch ($a_name) {
107 case "Category":
108 $cur_parent = $this->parent[$this->parent_cnt - 1];
109 $this->category = new ilObjCategory();
110 $this->category->setImportId($a_attribs["Id"] . " (#" . $cur_parent . ")");
111 $this->default_language = $a_attribs["DefaultLanguage"];
112 $this->category->setTitle($a_attribs["Id"]);
113 $this->category->create();
114 $this->category->createReference();
115 $this->category->putInTree($cur_parent);
116 $this->parent[$this->parent_cnt++] = $this->category->getRefId();
117 break;
118
119 case "CategorySpec":
120 $this->cur_spec_lang = $a_attribs["Language"];
121 break;
122
123 }
124 }
125
131 public function handlerEndTag($a_xml_parser, string $a_name): void
132 {
133 $this->cdata = $this->trimAndStrip($this->cdata);
134 switch ($a_name) {
135 case "Category":
136 unset($this->category, $this->parent[$this->parent_cnt - 1]);
137 $this->parent_cnt--;
138 break;
139
140 case "CategorySpec":
141 $is_def = '0';
142 if ($this->cur_spec_lang === $this->default_language) {
143 $this->category->setTitle($this->cur_title);
144 $this->category->setDescription($this->cur_description);
145 $this->category->update();
146 $is_def = '1';
147 }
148 $this->category->addTranslation(
149 $this->cur_title,
150 $this->cur_description,
151 $this->cur_spec_lang,
152 $is_def
153 );
154 break;
155
156 case "Title":
157 $this->cur_title = $this->cdata;
158 break;
159
160 case "Description":
161 $this->cur_description = $this->cdata;
162 break;
163 }
164
165 $this->cdata = "";
166 }
167
173 public function handlerCharacterData($a_xml_parser, string $a_data): void
174 {
175 // i don't know why this is necessary, but
176 // the parser seems to convert "&gt;" to ">" and "&lt;" to "<"
177 // in character data, but we don't want that, because it's the
178 // way we mask user html in our content, so we convert back...
179 $a_data = str_replace(["<", ">"], ["&lt;", "&gt;"], $a_data);
180
181 // DELETE WHITESPACES AND NEWLINES OF CHARACTER DATA
182 $a_data = preg_replace("/\n/", "", $a_data);
183 $a_data = preg_replace("/\t+/", "", $a_data);
184 if (!empty($a_data)) {
185 $this->cdata .= $a_data;
186 }
187 }
188}
handlerEndTag($a_xml_parser, string $a_name)
handlerCharacterData($a_xml_parser, string $a_data)
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
buildTag(string $type, string $name, array $attr=null)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Component logger with individual log levels by component id.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class ilRbacAdmin Core functions for role based access control.
class ilRbacReview Contains Review functions of core Rbac.
class ilRbacSystem system function like checkAccess, addActiveRole ... Supporting system functions ar...
__construct(?string $path_to_file='', ?bool $throw_exception=false)
global $DIC
Definition: feed.php:28
if($format !==null) $name
Definition: metadata.php:247
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$type