ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilWebLinkXmlParser.php
Go to the documentation of this file.
1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4include_once("Services/MetaData/classes/class.ilMDSaxParser.php");
5include_once("Services/MetaData/classes/class.ilMD.php");
6include_once('Services/Utilities/interfaces/interface.ilSaxSubsetParser.php');
7
8include_once './Modules/WebResource/classes/class.ilLinkResourceItems.php';
9include_once './Modules/WebResource/classes/class.ilWebLinkXmlParserException.php';
10include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
11
21{
22 const MODE_UNDEFINED = 0;
23 const MODE_UPDATE = 1;
24 const MODE_CREATE = 2;
25
26 private $webl;
28
29 private $in_metadata = false;
30
31 private $sorting_positions = array();
33
37 public function __construct($webr, $xml)
38 {
39 parent::__construct();
40 $this->setXMLContent($xml);
41 $this->setWebLink($webr);
42
43 $this->setMDObject(new ilMD($this->getWebLink()->getId(), $this->getWebLink()->getId(), 'webr'));
44 $this->setThrowException(true);
45 }
46
52 public function setWebLink(ilObject $webl)
53 {
54 $this->webl = $webl;
55 }
56
61 public function getWebLink()
62 {
63 return $this->webl;
64 }
65
71 public function setMode($a_mode)
72 {
73 $this->mode = $a_mode;
74 }
75
80 public function getMode()
81 {
82 return $this->mode;
83 }
84
92 public function start()
93 {
94 return $this->startParsing();
95 }
96
103 public function setHandlers($a_xml_parser)
104 {
105 xml_set_object($a_xml_parser, $this);
106 xml_set_element_handler($a_xml_parser, 'handlerBeginTag', 'handlerEndTag');
107 xml_set_character_data_handler($a_xml_parser, 'handlerCharacterData');
108 }
109
117 public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
118 {
119 global $DIC;
120
121 $ilErr = $DIC['ilErr'];
122
123 if ($this->in_metadata) {
124 parent::handlerBeginTag($a_xml_parser, $a_name, $a_attribs);
125 return;
126 }
127
128 switch ($a_name) {
129 case "MetaData":
130 $this->in_metadata = true;
131
132 // Delete old meta data
133 $md = new ilMD($this->getWebLink()->getId(), 0, 'webr');
134 $md->deleteAll();
135
136 parent::handlerBeginTag($a_xml_parser, $a_name, $a_attribs);
137 break;
138
139 case 'WebLink':
140
141 $this->current_sorting_position = ($a_attribs['position'] ? $a_attribs['position'] : 0);
142 $this->current_link_update = false;
143 $this->current_link_delete = false;
144 $this->current_parameters = array();
145
146 if ($this->getMode() == self::MODE_CREATE or (isset($a_attribs['action']) and $a_attribs['action'] == 'Create')) {
147 // New weblink
148 $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
149 } elseif ($this->getMode() == self::MODE_UPDATE and $a_attribs['action'] == 'Delete') {
150 $this->current_link_delete = true;
151 $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
152 $this->current_link->delete($a_attribs['id']);
153 break;
154 } elseif ($this->getMode() == self::MODE_UPDATE and ($a_attribs['action'] == 'Update' or !isset($a_attribs['action']))) {
155 $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
156 $this->current_link->readItem($a_attribs['id']);
157 $this->current_link_update = true;
158
159 // Delete all dynamic parameter
160 include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
161 foreach (ilParameterAppender::getParameterIds($this->getWebLink()->getId(), $a_attribs['id']) as $param_id) {
162 $param = new ilParameterAppender($this->getWebLink()->getId());
163 $param->delete($param_id);
164 }
165 } else {
166 throw new ilWebLinkXmlParserException('Invalid action given for element "Weblink"');
167 }
168
169 // Active
170 $this->current_link->setActiveStatus($a_attribs['active'] ? 1 : 0);
171
172 // Valid
173 if (!isset($a_attribs['valid'])) {
174 $valid = 1;
175 } else {
176 $valid = $a_attribs['valid'] ? 1 : 0;
177 }
178 $this->current_link->setValidStatus($valid);
179
180 // Disable check
181 $this->current_link->setDisableCheckStatus($a_attribs['disableValidation'] ? 1 : 0);
182
183 // internal
184 if (isset($a_attribs['internal'])) {
185 $this->current_link->setInternal($a_attribs['internal']);
186 }
187 break;
188
189
190 case 'Sorting':
191
192 include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
193 $sort = new ilContainerSortingSettings($this->getWebLink()->getId());
194 $sort->delete();
195
196 switch ($a_attribs['type']) {
197 case 'Manual':
198 $sort->setSortMode(ilContainer::SORT_MANUAL);
199 break;
200
201 case 'Title':
202 default:
203 $sort->setSortMode(ilContainer::SORT_TITLE);
204 }
205 $sort->save();
206 break;
207
208 case 'WebLinks':
209 $this->sorting_positions = array();
210 // no break
211 case 'Title':
212 case 'Description':
213 case 'Target':
214 // Nothing to do
215 break;
216
217 case 'DynamicParameter':
218
219 $param = new ilParameterAppender($this->getWebLink()->getId());
220 $param->setName($a_attribs['name']);
221
222 switch ($a_attribs['type']) {
223 case 'userName':
224 $param->setValue(LINKS_LOGIN);
225 break;
226
227 case 'userId':
228 $param->setValue(LINKS_USER_ID);
229 break;
230
231 case 'matriculation':
232 $param->setValue(LINKS_MATRICULATION);
233 break;
234
235 default:
236 throw new ilWebLinkXmlParserException('Invalid attribute "type" given for element "Dynamic parameter". Aborting');
237 break;
238 }
239
240 $this->current_parameters[] = $param;
241 break;
242 }
243 }
244
253 public function handlerEndTag($a_xml_parser, $a_name)
254 {
255 if ($this->in_metadata) {
256 parent::handlerEndTag($a_xml_parser, $a_name);
257 }
258
259
260 switch ($a_name) {
261 case 'MetaData':
262 $this->in_metadata = false;
263 parent::handlerEndTag($a_xml_parser, $a_name);
264 break;
265
266 case 'WebLinks':
267 $this->getWebLink()->MDUpdateListener('General');
268 $this->getWebLink()->update();
269
270 // save sorting
271 include_once './Services/Container/classes/class.ilContainerSorting.php';
272 $sorting = ilContainerSorting::_getInstance($this->getWebLink()->getId());
273 $sorting->savePost($this->sorting_positions);
274 ilLoggerFactory::getLogger('webr')->dump($this->sorting_positions);
275 break;
276
277 case 'WebLink':
278
279 if ($this->current_link_delete) {
280 break;
281 }
282 if (!$this->current_link) {
283 throw new ilSaxParserException('Invalid xml structure given. Missing start tag "WebLink"');
284 }
285 if (!$this->current_link->validate()) {
286 throw new ilWebLinkXmlParserException('Missing required elements "Title, Target"');
287 }
288
289 if ($this->current_link_update) {
290 $this->current_link->update();
291 } else {
292 $this->current_link->add();
293 }
294
295 // Save dynamic parameters
296 foreach ($this->current_parameters as $param) {
297 $param->add($this->current_link->getLinkId());
298 }
299
300
301 // store positions
302 $this->sorting_positions[$this->current_link->getLinkId()] = (int) $this->current_sorting_position;
303
304 unset($this->current_link);
305 break;
306
307 case 'Title':
308 if ($this->current_link) {
309 $this->current_link->setTitle(trim($this->cdata));
310 }
311 break;
312
313 case 'Description':
314 if ($this->current_link) {
315 $this->current_link->setDescription(trim($this->cdata));
316 }
317 break;
318
319 case 'Target':
320 if ($this->current_link) {
321 $this->current_link->setTarget(trim($this->cdata));
322 }
323 break;
324 }
325
326 // Reset cdata
327 $this->cdata = '';
328 }
329
330
331
338 public function handlerCharacterData($a_xml_parser, $a_data)
339 {
340 if ($this->in_metadata) {
341 parent::handlerCharacterData($a_xml_parser, $a_data);
342 }
343
344 if ($a_data != "\n") {
345 // Replace multiple tabs with one space
346 $a_data = preg_replace("/\t+/", " ", $a_data);
347 $this->cdata .= $a_data;
348 }
349 }
350}
An exception for terminatinating execution or to throw for unit testing.
const LINKS_USER_ID
const LINKS_LOGIN
const LINKS_MATRICULATION
static _getInstance($a_obj_id)
get instance by obj_id
Class ilObjLinkResourceGUI.
static getLogger($a_component_id)
Get component logger.
Class ilObject Basic functions for all objects.
Class ilParameterAppender.
static getParameterIds($a_webr_id, $a_link_id)
Get Parameter ids of link.
SaxParserException thrown by ilSaxParser if property throwException is set.
setThrowException($throwException)
set error handling
setXMLContent($a_xml_content)
startParsing()
stores xml data in array
XML parser for weblink xml.
setMode($a_mode)
Set parsing mode.
getWebLink()
Get weblink object.
getMode()
Return parsing mode.
handlerEndTag($a_xml_parser, $a_name)
handler for end of element
setHandlers($a_xml_parser)
set event handlers
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
__construct($webr, $xml)
Constructor.
setWebLink(ilObject $webl)
set weblink
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
$valid
$ilErr
Definition: raiseError.php:18
global $DIC
Definition: saml.php:7