ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
class.ilContainerReferenceXmlParser.php
Go to the documentation of this file.
1<?php
2/*
3 +-----------------------------------------------------------------------------+
4 | ILIAS open source |
5 +-----------------------------------------------------------------------------+
6 | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7 | |
8 | This program is free software; you can redistribute it and/or |
9 | modify it under the terms of the GNU General Public License |
10 | as published by the Free Software Foundation; either version 2 |
11 | of the License, or (at your option) any later version. |
12 | |
13 | This program is distributed in the hope that it will be useful, |
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 | GNU General Public License for more details. |
17 | |
18 | You should have received a copy of the GNU General Public License |
19 | along with this program; if not, write to the Free Software |
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21 +-----------------------------------------------------------------------------+
22*/
23
24require_once("./Services/Xml/classes/class.ilSaxParser.php");
25require_once('./Services/User/classes/class.ilObjUser.php');
26include_once('./Services/Calendar/classes/class.ilDateTime.php');
27
28
39{
43 protected $error;
44
45 const MODE_CREATE = 1;
46 const MODE_UPDATE = 2;
47
48 private $ref = null;
49 private $parent_id = 0;
50
54 protected $logger;
55
59 protected $import_mapping;
60
69 public function __construct($a_xml, $a_parent_id = 0)
70 {
71 global $DIC;
72
73 $this->error = $DIC["ilErr"];
75
77 $this->setXMLContent($a_xml);
78
79 $this->logger = $DIC->logger()->exp();
80 }
81
85 public function setImportMapping(ilImportMapping $mapping)
86 {
87 $this->import_mapping = $mapping;
88 }
89
94 public function getParentId()
95 {
96 return $this->parent_id;
97 }
98
104 public function setHandlers($a_xml_parser)
105 {
106 xml_set_object($a_xml_parser, $this);
107 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
108 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
109 }
110
114 public function startParsing()
115 {
116 parent::startParsing();
117
118 if ($this->ref instanceof ilContainerReference) {
119 return $this->ref;
120 }
121 return 0;
122 }
123
124
128 public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
129 {
131
132 switch ($a_name) {
133 case "ContainerReference":
134 break;
135
136 case 'Title':
137 switch ($a_attribs['type']) {
140 break;
141
142 default:
144 break;
145 }
146 break;
147
148 case 'Target':
149 $target_id = $this->parseTargetId(isset($a_attribs['id']) ? (string) $a_attribs['id'] : '');
150 if ($target_id) {
151 $this->logger->debug('Using mapped target_id: ' . $target_id);
152 $this->getReference()->setTargetId($target_id);
153 } else {
154 $this->logger->info('No mapping found for: ' . $a_attribs['id']);
155 $this->getReference()->setTargetId(0);
156 }
157 break;
158 }
159 }
160
165 protected function parseTargetId(string $attribute_target) : int
166 {
167 if (!strlen($attribute_target)) {
168 $this->logger->debug('No target id provided');
169 return 0;
170 }
171 if (!$this->import_mapping instanceof ilImportMapping) {
172 return 0;
173 }
174 $obj_mapping_id = $this->import_mapping->getMapping('Services/Container', 'objs', $attribute_target);
175 if (!$obj_mapping_id) {
176 $this->logger->debug('Cannot find object mapping for target_id: ' . $attribute_target);
177 return 0;
178 }
179 return $obj_mapping_id;
180 }
181
182
188 public function handlerEndTag($a_xml_parser, $a_name)
189 {
190 switch ($a_name) {
191 case "ContainerReference":
192 $this->save();
193 break;
194
195 case 'Title':
196 if ($this->getReference()->getTitleType() == ilContainerReference::TITLE_TYPE_CUSTOM) {
197 $this->getReference()->setTitle(trim($this->cdata));
198 }
199 break;
200 }
201 $this->cdata = '';
202 }
203
204
208 public function handlerCharacterData($a_xml_parser, $a_data)
209 {
210 #$a_data = str_replace("<","&lt;",$a_data);
211 #$a_data = str_replace(">","&gt;",$a_data);
212
213 if (!empty($a_data)) {
214 $this->cdata .= $a_data;
215 }
216 }
217
222 protected function save()
223 {
227 include_once './Modules/Category/classes/class.ilCategoryXmlParser.php';
228 if ($this->mode == ilCategoryXmlParser::MODE_CREATE) {
229 $this->create();
230 $this->getReference()->create();
231 $this->getReference()->createReference();
232 $this->getReference()->putInTree($this->getParentId());
233 $this->getReference()->setPermissions($this->getParentId());
234 }
235 $this->getReference()->update();
236 return true;
237 }
238
239
240
241
246 public function setMode($mode)
247 {
248 $this->mode = $mode;
249 }
250
251
257 {
258 $this->ref = $ref;
259 }
260
265 public function getReference()
266 {
267 return $this->ref;
268 }
269}
An exception for terminatinating execution or to throw for unit testing.
error($a_errmsg)
set error message @access public
handlerEndTag($a_xml_parser, $a_name)
Handler end tag.
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
__construct($a_xml, $a_parent_id=0)
Constructor.
setHandlers($a_xml_parser)
set event handler should be overwritten by inherited class @access private
setReference(ilContainerReference $ref)
Set container reference.
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
setXMLContent($a_xml_content)
$target_id
Definition: goto.php:49
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
$ilErr
Definition: raiseError.php:18
$DIC
Definition: xapitoken.php:46