ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
class.ilCategoryXmlParser.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
27{
28 public const MODE_CREATE = 1;
29 public const MODE_UPDATE = 2;
30
31 private ?ilObjCategory $cat = null;
32 private int $parent_id = 0;
33 private array $current_translation = [];
35 protected ilLogger $cat_log;
36 protected int $mode;
37 protected string $cdata = "";
38
39 public function __construct(string $a_xml, int $a_parent_id)
40 {
42
43 $this->mode = self::MODE_CREATE;
44 $this->parent_id = $a_parent_id;
45 $this->setXMLContent($a_xml);
46
47 $this->cat_log = ilLoggerFactory::getLogger("cat");
48 }
49
50 public function getParentId(): int
51 {
52 return $this->parent_id;
53 }
54
58 protected function getCurrentTranslation(): array
59 {
61 }
62
63 public function setHandlers($a_xml_parser): void
64 {
65 xml_set_element_handler($a_xml_parser, $this->handlerBeginTag(...), $this->handlerEndTag(...));
66 xml_set_character_data_handler($a_xml_parser, $this->handlerCharacterData(...));
67 }
68
69 public function startParsing(): void
70 {
71 parent::startParsing();
72
73 if ($this->mode !== self::MODE_CREATE) {
74 $this->cat->update();
75 }
76 }
77
84 public function handlerBeginTag($a_xml_parser, string $a_name, array $a_attribs): void
85 {
86 $a_attribs = $this->trimAndStripAttribs($a_attribs);
87 switch ($a_name) {
88 case "Category":
89 break;
90
91 case 'Translations':
92 $this->getCategory()->removeTranslations();
93 break;
94
95 case 'Translation':
96 $this->current_translation = [];
97 $this->current_translation['default'] = $a_attribs['default'] ? 1 : 0;
98 $this->current_translation['lang'] = $a_attribs['language'];
99 break;
100
101 case 'Sorting':
102 case 'Sort':
104 break;
105
106 case 'ContainerSetting':
107 $this->current_container_setting = $a_attribs['id'];
108 break;
109 }
110 }
111
117 public function handlerEndTag($a_xml_parser, string $a_name): void
118 {
119 $this->cdata = $this->trimAndStrip($this->cdata);
120 switch ($a_name) {
121 case "Category":
122 $this->save();
123 break;
124
125 case 'Title':
126 $this->current_translation['title'] = trim($this->cdata);
127
128 if ($this->current_translation['default']) {
129 $this->getCategory()->setTitle(trim($this->cdata));
130 }
131
132 break;
133
134 case 'Description':
135 $this->current_translation['description'] = trim($this->cdata);
136
137 if ($this->current_translation['default']) {
138 $this->getCategory()->setDescription(trim($this->cdata));
139 }
140
141 break;
142
143 case 'Translation':
144 // Add translation
145 $this->getCategory()->addTranslation(
146 (string) $this->current_translation['title'],
147 (string) $this->current_translation['description'],
148 (string) $this->current_translation['lang'],
149 (string) ((int) $this->current_translation['default'])
150 );
151 break;
152
153 case 'ContainerSetting':
154 if ($this->current_container_setting) {
155 $this->cat_log->debug("Write container Setting, ID: " . $this->getCategory()->getId() . ", setting: " .
156 $this->current_container_setting . ", data: " . $this->cdata);
158 $this->getCategory()->getId(),
159 $this->current_container_setting,
160 $this->cdata
161 );
162 }
163 break;
164
165 case 'ContainerSettings':
166 $this->cat->readContainerSettings(); // read container settings to member vars (call getter/setter), see #0019870
167 break;
168 }
169 $this->cdata = '';
170 }
171
177 public function handlerCharacterData($a_xml_parser, string $a_data): void
178 {
179 if (!empty($a_data)) {
180 $this->cdata .= $a_data;
181 }
182 }
183
184 protected function save(): bool
185 {
189 if ($this->mode === self::MODE_CREATE) {
190 $this->getCategory()->create();
191 $this->getCategory()->createReference();
192 $this->getCategory()->putInTree($this->getParentId());
193 $this->getCategory()->setPermissions($this->getParentId());
194 }
195 $this->getCategory()->update();
196 return true;
197 }
198
199 public function setMode(int $mode): void
200 {
201 $this->mode = $mode;
202 }
203
204 public function setCategory(ilObjCategory $cat): void
205 {
206 $this->cat = $cat;
207 }
208
209 public function getCategory(): ?ilObjCategory
210 {
211 return $this->cat;
212 }
213}
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.
Class ilObjCategory.
setXMLContent(string $a_xml_content)
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc