ILIAS  release_8 Revision v8.24
class.ilCategoryXmlParser.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{
25 public const MODE_CREATE = 1;
26 public const MODE_UPDATE = 2;
27
28 private ?ilObjCategory $cat = null;
29 private int $parent_id = 0;
30 private array $current_translation = [];
32 protected ilLogger $cat_log;
33 protected int $mode;
34 protected string $cdata = "";
35
36 public function __construct(string $a_xml, int $a_parent_id)
37 {
39
40 $this->mode = self::MODE_CREATE;
41 $this->parent_id = $a_parent_id;
42 $this->setXMLContent($a_xml);
43
44 $this->cat_log = ilLoggerFactory::getLogger("cat");
45 }
46
47 public function getParentId(): int
48 {
49 return $this->parent_id;
50 }
51
55 protected function getCurrentTranslation(): array
56 {
58 }
59
60 public function setHandlers($a_xml_parser): void
61 {
62 xml_set_object($a_xml_parser, $this);
63 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
64 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
65 }
66
67 public function startParsing(): void
68 {
69 parent::startParsing();
70
71 if ($this->mode !== self::MODE_CREATE) {
72 $this->cat->update();
73 }
74 }
75
82 public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
83 {
84 $a_attribs = $this->trimAndStripAttribs($a_attribs);
85 switch ($a_name) {
86 case "Category":
87 break;
88
89 case 'Translations':
90 $this->getCategory()->removeTranslations();
91 break;
92
93 case 'Translation':
94 $this->current_translation = [];
95 $this->current_translation['default'] = $a_attribs['default'] ? 1 : 0;
96 $this->current_translation['lang'] = $a_attribs['language'];
97 break;
98
99 case 'Sorting':
100 case 'Sort':
102 break;
103
104 case 'ContainerSetting':
105 $this->current_container_setting = $a_attribs['id'];
106 break;
107 }
108 }
109
115 public function handlerEndTag($a_xml_parser, string $a_name): void
116 {
117 $this->cdata = $this->trimAndStrip($this->cdata);
118 switch ($a_name) {
119 case "Category":
120 $this->save();
121 break;
122
123 case 'Title':
124 $this->current_translation['title'] = trim($this->cdata);
125
126 if ($this->current_translation['default']) {
127 $this->getCategory()->setTitle(trim($this->cdata));
128 }
129
130 break;
131
132 case 'Description':
133 $this->current_translation['description'] = trim($this->cdata);
134
135 if ($this->current_translation['default']) {
136 $this->getCategory()->setDescription(trim($this->cdata));
137 }
138
139 break;
140
141 case 'Translation':
142 // Add translation
143 $this->getCategory()->addTranslation(
144 (string) $this->current_translation['title'],
145 (string) $this->current_translation['description'],
146 (string) $this->current_translation['lang'],
147 (string) ((int) $this->current_translation['default'])
148 );
149 break;
150
151 case 'ContainerSetting':
152 if ($this->current_container_setting) {
153 $this->cat_log->debug("Write container Setting, ID: " . $this->getCategory()->getId() . ", setting: " .
154 $this->current_container_setting . ", data: " . $this->cdata);
156 $this->getCategory()->getId(),
157 $this->current_container_setting,
158 $this->cdata
159 );
160 }
161 break;
162
163 case 'ContainerSettings':
164 $this->cat->readContainerSettings(); // read container settings to member vars (call getter/setter), see #0019870
165 break;
166 }
167 $this->cdata = '';
168 }
169
175 public function handlerCharacterData($a_xml_parser, string $a_data): void
176 {
177 if (!empty($a_data)) {
178 $this->cdata .= $a_data;
179 }
180 }
181
182 protected function save(): bool
183 {
184
188 if ($this->mode === self::MODE_CREATE) {
189 $this->getCategory()->create();
190 $this->getCategory()->createReference();
191 $this->getCategory()->putInTree($this->getParentId());
192 $this->getCategory()->setPermissions($this->getParentId());
193 }
194 $this->getCategory()->update();
195 return true;
196 }
197
198 public function setMode(int $mode): void
199 {
200 $this->mode = $mode;
201 }
202
203 public function setCategory(ilObjCategory $cat): void
204 {
205 $this->cat = $cat;
206 }
207
208 public function getCategory(): ?ilObjCategory
209 {
210 return $this->cat;
211 }
212}
setCategory(ilObjCategory $cat)
__construct(string $a_xml, int $a_parent_id)
handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs)
handlerEndTag($a_xml_parser, string $a_name)
startParsing()
stores xml data in array
handlerCharacterData($a_xml_parser, string $a_data)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static _importContainerSortingSettings(array $attibs, int $obj_id)
sorting import for all container objects
static _writeContainerSetting(int $a_id, string $a_keyword, string $a_value)
static getLogger(string $a_component_id)
Get component logger.
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...
setXMLContent(string $a_xml_content)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc