ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
4 include_once("Services/MetaData/classes/class.ilMDSaxParser.php");
5 include_once("Services/MetaData/classes/class.ilMD.php");
6 include_once('Services/Utilities/interfaces/interface.ilSaxSubsetParser.php');
7 
8 include_once './Modules/WebResource/classes/class.ilLinkResourceItems.php';
9 include_once './Modules/WebResource/classes/class.ilWebLinkXmlParserException.php';
10 include_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;
27  private $mode = self::MODE_UNDEFINED;
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  break;
194 
195 
196  case 'Sorting':
197 
198  include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
199  $sort = new ilContainerSortingSettings($this->getWebLink()->getId());
200  $sort->delete();
201 
202  switch($a_attribs['type'])
203  {
204  case 'Manual':
205  $sort->setSortMode(ilContainer::SORT_MANUAL);
206  break;
207 
208  case 'Title':
209  default:
210  $sort->setSortMode(ilContainer::SORT_TITLE);
211  }
212  $sort->save();
213  break;
214 
215  case 'WebLinks':
216  $this->sorting_positions = array();
217  case 'Title':
218  case 'Description':
219  case 'Target':
220  // Nothing to do
221  break;
222 
223  case 'DynamicParameter':
224 
225  $param = new ilParameterAppender($this->getWebLink()->getId());
226  $param->setName($a_attribs['name']);
227 
228  switch($a_attribs['type'])
229  {
230  case 'userName':
231 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_LOGIN);
232  $param->setValue(LINKS_LOGIN);
233  break;
234 
235  case 'userId':
236 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_USER_ID);
237  $param->setValue(LINKS_USER_ID);
238  break;
239 
240  case 'matriculation':
241 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_MATRICULATION);
242  $param->setValue(LINKS_MATRICULATION);
243  break;
244 
245  default:
246  throw new ilWebLinkXmlParserException('Invalid attribute "type" given for element "Dynamic parameter". Aborting');
247  break;
248  }
249 
250  $this->current_parameters[] = $param;
251  break;
252  }
253  }
254 
263  public function handlerEndTag($a_xml_parser,$a_name)
264  {
265  if($this->in_metadata)
266  {
267  parent::handlerEndTag($a_xml_parser,$a_name);
268  }
269 
270  $GLOBALS['ilLog']->write(__METHOD__.': Called '.$a_name);
271 
272  switch($a_name)
273  {
274  case 'MetaData':
275  $this->in_metadata = false;
276  parent::handlerEndTag($a_xml_parser,$a_name);
277  break;
278 
279  case 'WebLinks':
280  $this->getWebLink()->MDUpdateListener('General');
281  $this->getWebLink()->update();
282 
283  // save sorting
284  include_once './Services/Container/classes/class.ilContainerSorting.php';
285  $sorting = ilContainerSorting::_getInstance($this->getWebLink()->getId());
286  $sorting->savePost($this->sorting_positions);
287  ilLoggerFactory::getLogger('webr')->dump($this->sorting_positions);
288  break;
289 
290  case 'WebLink':
291 
292  if($this->current_link_delete)
293  {
294  break;
295  }
296  if(!$this->current_link)
297  {
298  throw new ilSaxParserException('Invalid xml structure given. Missing start tag "WebLink"');
299  }
300  if(!$this->current_link->validate())
301  {
302  throw new ilWebLinkXmlParserException('Missing required elements "Title, Target"');
303  }
304 
305  if($this->current_link_update)
306  {
307  $this->current_link->update();
308  }
309  else
310  {
311  $this->current_link->add();
312  }
313 
314  // Save dynamic parameters
315  foreach($this->current_parameters as $param)
316  {
317  $param->add($this->current_link->getLinkId());
318  }
319 
320 
321  // store positions
322  $this->sorting_positions[$this->current_link->getLinkId()] = (int) $this->current_sorting_position;
323 
324  unset($this->current_link);
325  break;
326 
327  case 'Title':
328  if($this->current_link)
329  {
330  $this->current_link->setTitle(trim($this->cdata));
331  }
332  break;
333 
334  case 'Description':
335  if($this->current_link)
336  {
337  $this->current_link->setDescription(trim($this->cdata));
338  }
339  break;
340 
341  case 'Target':
342  if($this->current_link)
343  {
344  $this->current_link->setTarget(trim($this->cdata));
345  }
346  break;
347  }
348 
349  // Reset cdata
350  $this->cdata = '';
351  }
352 
353 
354 
361  public function handlerCharacterData($a_xml_parser,$a_data)
362  {
363  if($this->in_metadata)
364  {
365  parent::handlerCharacterData($a_xml_parser,$a_data);
366  }
367 
368  if($a_data != "\n")
369  {
370  // Replace multiple tabs with one space
371  $a_data = preg_replace("/\t+/"," ",$a_data);
372  $this->cdata .= $a_data;
373  }
374  }
375 
376 
377 }
378 ?>
XML parser for weblink xml.
static getParameterIds($a_webr_id, $a_link_id)
Get Parameter ids of link.
startParsing()
stores xml data in array
setMode($a_mode)
Set parsing mode.
Class ilObject Basic functions for all objects.
$valid
const LINKS_MATRICULATION
setThrowException($throwException)
set error handling
SaxParserException thrown by ilSaxParser if property throwException is set.
const LINKS_LOGIN
getMode()
Return parsing mode.
setHandlers($a_xml_parser)
set event handlers
getWebLink()
Get weblink object.
__construct($webr, $xml)
Constructor.
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
handler for begin of element
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
Class ilObjLinkResourceGUI.
Class ilParameterAppender.
setWebLink(ilObject $webl)
set weblink
static getLogger($a_component_id)
Get component logger.
static _getInstance($a_obj_id)
get instance by obj_id
$GLOBALS['PHPCAS_CLIENT']
This global variable is used by the interface class phpCAS.
Definition: CAS.php:276
const LINKS_USER_ID
setXMLContent($a_xml_content)
handlerEndTag($a_xml_parser, $a_name)
handler for end of element