ILIAS  release_8 Revision v8.24
class.ilContainerReferenceXmlParser.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 ?ilContainerReference $ref = null;
29 private int $parent_id = 0;
30 protected ilLogger $logger;
32 protected string $cdata = "";
33 protected int $mode = 0;
34
35 public function __construct(
36 string $a_xml,
37 int $a_parent_id = 0
38 ) {
39 global $DIC;
40
42
43 $this->mode = self::MODE_CREATE;
44 $this->setXMLContent($a_xml);
45
46 $this->logger = $DIC->logger()->exp();
47 }
48
49 public function setImportMapping(ilImportMapping $mapping): void
50 {
51 $this->import_mapping = $mapping;
52 }
53
54 public function getParentId(): int
55 {
56 return $this->parent_id;
57 }
58
59 public function setHandlers($a_xml_parser): void
60 {
61 xml_set_object($a_xml_parser, $this);
62 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
63 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
64 }
65
72 public function handlerBeginTag(
73 $a_xml_parser,
74 string $a_name,
75 array $a_attribs
76 ): void {
77 $a_attribs = $this->trimAndStripAttribs($a_attribs);
78 switch ($a_name) {
79 case "ContainerReference":
80 break;
81
82 case 'Title':
83 switch ($a_attribs['type']) {
84
86 default:
88 break;
89 }
90 break;
91
92 case 'Target':
93 $target_id = $this->parseTargetId($a_attribs['id'] ?? '');
94 if ($target_id) {
95 $this->logger->debug('Using mapped target_id: ' . $target_id);
96 $this->getReference()->setTargetId($target_id);
97 } else {
98 $this->logger->info('No mapping found for: ' . $a_attribs['id']);
99 $this->getReference()->setTargetId(0);
100 }
101 break;
102 }
103 }
104
105 protected function parseTargetId(string $attribute_target): int
106 {
107 if ($attribute_target === '') {
108 $this->logger->debug('No target id provided');
109 return 0;
110 }
111 if (!$this->import_mapping instanceof ilImportMapping) {
112 return 0;
113 }
114 $obj_mapping_id = $this->import_mapping->getMapping('Services/Container', 'objs', $attribute_target);
115 if (!$obj_mapping_id) {
116 $this->logger->debug('Cannot find object mapping for target_id: ' . $attribute_target);
117 return 0;
118 }
119
120 return (int) $obj_mapping_id;
121 }
122
128 public function handlerEndTag(
129 $a_xml_parser,
130 string $a_name
131 ): void {
132 $this->cdata = $this->trimAndStrip($this->cdata);
133 switch ($a_name) {
134 case "ContainerReference":
135 $this->save();
136 break;
137
138 case 'Title':
139 if ($this->getReference()->getTitleType() === ilContainerReference::TITLE_TYPE_CUSTOM) {
140 $this->getReference()->setTitle(trim($this->cdata));
141 }
142 break;
143 }
144 $this->cdata = '';
145 }
146
152 public function handlerCharacterData(
153 $a_xml_parser,
154 string $a_data
155 ): void {
156 if (!empty($a_data)) {
157 $this->cdata .= $a_data;
158 }
159 }
160
161 protected function create(): void
162 {
163 }
164
165 protected function save(): void
166 {
167 if ($this->mode === ilCategoryXmlParser::MODE_CREATE) {
168 $this->create();
169 $this->getReference()->create();
170 $this->getReference()->createReference();
171 $this->getReference()->putInTree($this->getParentId());
172 $this->getReference()->setPermissions($this->getParentId());
173 }
174 $this->getReference()->update();
175 }
176
177 public function setMode(int $mode): void
178 {
179 $this->mode = $mode;
180 }
181
182
183 public function setReference(ilContainerReference $ref): void
184 {
185 $this->ref = $ref;
186 }
187
189 {
190 return $this->ref;
191 }
192}
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
handlerCharacterData( $a_xml_parser, string $a_data)
handlerBeginTag( $a_xml_parser, string $a_name, array $a_attribs)
__construct(string $a_xml, int $a_parent_id=0)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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.
setXMLContent(string $a_xml_content)
if(!file_exists(getcwd() . '/ilias.ini.php'))
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Definition: confirmReg.php:20
global $DIC
Definition: feed.php:28
$target_id
Definition: goto.php:52
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc