ILIAS  release_10 Revision v10.1-43-ga1241a92c2f
class.ilContainerReferenceXmlParser.php
Go to the documentation of this file.
1 <?php
2 
3 declare(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 
41  parent::__construct(null);
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']) {
85  default:
87  break;
88  }
89  break;
90 
91  case 'Target':
92  $target_id = $this->parseTargetId($a_attribs['id'] ?? '');
93  if ($target_id) {
94  $this->logger->debug('Using mapped target_id: ' . $target_id);
95  $this->getReference()->setTargetId($target_id);
96  } else {
97  $this->logger->info('No mapping found for: ' . $a_attribs['id']);
98  $this->getReference()->setTargetId(0);
99  }
100  break;
101  }
102  }
103 
104  protected function parseTargetId(string $attribute_target): int
105  {
106  if ($attribute_target === '') {
107  $this->logger->debug('No target id provided');
108  return 0;
109  }
110  if (!$this->import_mapping instanceof ilImportMapping) {
111  return 0;
112  }
113  $obj_mapping_id = $this->import_mapping->getMapping('components/ILIAS/Container', 'objs', $attribute_target);
114  if (!$obj_mapping_id) {
115  $this->logger->debug('Cannot find object mapping for target_id: ' . $attribute_target);
116  return 0;
117  }
118 
119  return (int) $obj_mapping_id;
120  }
121 
127  public function handlerEndTag(
128  $a_xml_parser,
129  string $a_name
130  ): void {
131  $this->cdata = $this->trimAndStrip($this->cdata);
132  switch ($a_name) {
133  case "ContainerReference":
134  $this->save();
135  break;
136 
137  case 'Title':
138  if ($this->getReference()->getTitleType() === ilContainerReference::TITLE_TYPE_CUSTOM) {
139  $this->getReference()->setTitle(trim($this->cdata));
140  }
141  break;
142  }
143  $this->cdata = '';
144  }
145 
151  public function handlerCharacterData(
152  $a_xml_parser,
153  string $a_data
154  ): void {
155  if (!empty($a_data)) {
156  $this->cdata .= $a_data;
157  }
158  }
159 
160  protected function create(): void
161  {
162  }
163 
164  protected function save(): void
165  {
166  if ($this->mode === ilCategoryXmlParser::MODE_CREATE) {
167  $this->create();
168  $this->getReference()->create();
169  $this->getReference()->createReference();
170  $this->getReference()->putInTree($this->getParentId());
171  $this->getReference()->setPermissions($this->getParentId());
172  }
173  $this->getReference()->update();
174  }
175 
176  public function setMode(int $mode): void
177  {
178  $this->mode = $mode;
179  }
180 
181 
182  public function setReference(ilContainerReference $ref): void
183  {
184  $this->ref = $ref;
185  }
186 
187  public function getReference(): ?ilContainerReference
188  {
189  return $this->ref;
190  }
191 }
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...
handlerBeginTag( $a_xml_parser, string $a_name, array $a_attribs)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
global $DIC
Definition: shib_login.php:25
__construct(string $a_xml, int $a_parent_id=0)
__construct(Container $dic, ilPlugin $plugin)
handlerCharacterData( $a_xml_parser, string $a_data)
setXMLContent(string $a_xml_content)