ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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 $ilErr;
120
121 if($this->in_metadata)
122 {
123 parent::handlerBeginTag($a_xml_parser,$a_name,$a_attribs);
124 return;
125 }
126
127 switch($a_name)
128 {
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 {
148 // New weblink
149 $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
150 }
151 elseif($this->getMode() == self::MODE_UPDATE and $a_attribs['action'] == 'Delete')
152 {
153 $this->current_link_delete = true;
154 $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
155 $this->current_link->delete($a_attribs['id']);
156 break;
157 }
158 elseif($this->getMode() == self::MODE_UPDATE and ($a_attribs['action'] == 'Update' or !isset($a_attribs['action'])))
159 {
160 $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
161 $this->current_link->readItem($a_attribs['id']);
162 $this->current_link_update = true;
163
164 // Delete all dynamic parameter
165 include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
166 foreach(ilParameterAppender::getParameterIds($this->getWebLink()->getId(), $a_attribs['id']) as $param_id)
167 {
168 $param = new ilParameterAppender($this->getWebLink()->getId());
169 $param->delete($param_id);
170 }
171 }
172 else
173 {
174 throw new ilWebLinkXmlParserException('Invalid action given for element "Weblink"');
175 }
176
177 // Active
178 $this->current_link->setActiveStatus($a_attribs['active'] ? 1 : 0);
179
180 // Valid
181 if(!isset($a_attribs['valid']))
182 {
183 $valid = 1;
184 }
185 else
186 {
187 $valid = $a_attribs['valid'] ? 1 : 0;
188 }
189 $this->current_link->setValidStatus($valid);
190
191 // Disable check
192 $this->current_link->setDisableCheckStatus($a_attribs['disableValidation'] ? 1 : 0);
193
194 // internal
195 if(isset($a_attribs['internal']))
196 {
197 $this->current_link->setInternal($a_attribs['internal']);
198 }
199 break;
200
201
202 case 'Sorting':
203
204 include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
205 $sort = new ilContainerSortingSettings($this->getWebLink()->getId());
206 $sort->delete();
207
208 switch($a_attribs['type'])
209 {
210 case 'Manual':
211 $sort->setSortMode(ilContainer::SORT_MANUAL);
212 break;
213
214 case 'Title':
215 default:
216 $sort->setSortMode(ilContainer::SORT_TITLE);
217 }
218 $sort->save();
219 break;
220
221 case 'WebLinks':
222 $this->sorting_positions = array();
223 case 'Title':
224 case 'Description':
225 case 'Target':
226 // Nothing to do
227 break;
228
229 case 'DynamicParameter':
230
231 $param = new ilParameterAppender($this->getWebLink()->getId());
232 $param->setName($a_attribs['name']);
233
234 switch($a_attribs['type'])
235 {
236 case 'userName':
237# $GLOBALS['ilLog']->write("VALUE: ".LINKS_LOGIN);
238 $param->setValue(LINKS_LOGIN);
239 break;
240
241 case 'userId':
242# $GLOBALS['ilLog']->write("VALUE: ".LINKS_USER_ID);
243 $param->setValue(LINKS_USER_ID);
244 break;
245
246 case 'matriculation':
247# $GLOBALS['ilLog']->write("VALUE: ".LINKS_MATRICULATION);
248 $param->setValue(LINKS_MATRICULATION);
249 break;
250
251 default:
252 throw new ilWebLinkXmlParserException('Invalid attribute "type" given for element "Dynamic parameter". Aborting');
253 break;
254 }
255
256 $this->current_parameters[] = $param;
257 break;
258 }
259 }
260
269 public function handlerEndTag($a_xml_parser,$a_name)
270 {
271 if($this->in_metadata)
272 {
273 parent::handlerEndTag($a_xml_parser,$a_name);
274 }
275
276 $GLOBALS['ilLog']->write(__METHOD__.': Called '.$a_name);
277
278 switch($a_name)
279 {
280 case 'MetaData':
281 $this->in_metadata = false;
282 parent::handlerEndTag($a_xml_parser,$a_name);
283 break;
284
285 case 'WebLinks':
286 $this->getWebLink()->MDUpdateListener('General');
287 $this->getWebLink()->update();
288
289 // save sorting
290 include_once './Services/Container/classes/class.ilContainerSorting.php';
291 $sorting = ilContainerSorting::_getInstance($this->getWebLink()->getId());
292 $sorting->savePost($this->sorting_positions);
293 ilLoggerFactory::getLogger('webr')->dump($this->sorting_positions);
294 break;
295
296 case 'WebLink':
297
298 if($this->current_link_delete)
299 {
300 break;
301 }
302 if(!$this->current_link)
303 {
304 throw new ilSaxParserException('Invalid xml structure given. Missing start tag "WebLink"');
305 }
306 if(!$this->current_link->validate())
307 {
308 throw new ilWebLinkXmlParserException('Missing required elements "Title, Target"');
309 }
310
311 if($this->current_link_update)
312 {
313 $this->current_link->update();
314 }
315 else
316 {
317 $this->current_link->add();
318 }
319
320 // Save dynamic parameters
321 foreach($this->current_parameters as $param)
322 {
323 $param->add($this->current_link->getLinkId());
324 }
325
326
327 // store positions
328 $this->sorting_positions[$this->current_link->getLinkId()] = (int) $this->current_sorting_position;
329
330 unset($this->current_link);
331 break;
332
333 case 'Title':
334 if($this->current_link)
335 {
336 $this->current_link->setTitle(trim($this->cdata));
337 }
338 break;
339
340 case 'Description':
341 if($this->current_link)
342 {
343 $this->current_link->setDescription(trim($this->cdata));
344 }
345 break;
346
347 case 'Target':
348 if($this->current_link)
349 {
350 $this->current_link->setTarget(trim($this->cdata));
351 }
352 break;
353 }
354
355 // Reset cdata
356 $this->cdata = '';
357 }
358
359
360
367 public function handlerCharacterData($a_xml_parser,$a_data)
368 {
369 if($this->in_metadata)
370 {
371 parent::handlerCharacterData($a_xml_parser,$a_data);
372 }
373
374 if($a_data != "\n")
375 {
376 // Replace multiple tabs with one space
377 $a_data = preg_replace("/\t+/"," ",$a_data);
378 $this->cdata .= $a_data;
379 }
380 }
381
382
383}
384?>
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
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
global $ilErr
Definition: raiseError.php:16