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