ILIAS  Release_4_2_x_branch Revision 61807
 All Data Structures Namespaces Files Functions Variables Groups Pages
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;
28 
29  private $in_metadata = false;
30 
34  public function __construct($webr,$xml)
35  {
37  $this->setXMLContent($xml);
38  $this->setWebLink($webr);
39 
40  $this->setMDObject(new ilMD($this->getWebLink()->getId(),$this->getWebLink()->getId(),'webr'));
41  $this->setThrowException(true);
42  }
43 
49  public function setWebLink(ilObject $webl)
50  {
51  $this->webl = $webl;
52  }
53 
58  public function getWebLink()
59  {
60  return $this->webl;
61  }
62 
68  public function setMode($a_mode)
69  {
70  $this->mode = $a_mode;
71  }
72 
77  public function getMode()
78  {
79  return $this->mode;
80  }
81 
89  public function start()
90  {
91  return $this->startParsing();
92  }
93 
100  public function setHandlers($a_xml_parser)
101  {
102  xml_set_object($a_xml_parser,$this);
103  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
104  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
105  }
106 
114  public function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
115  {
116  global $ilErr;
117 
118  if($this->in_metadata)
119  {
120  parent::handlerBeginTag($a_xml_parser,$a_name,$a_attribs);
121  return;
122  }
123 
124  switch($a_name)
125  {
126  case "MetaData":
127  $this->in_metadata = true;
128 
129  // Delete old meta data
130  $md = new ilMD($this->getWebLink()->getId(),0,'webr');
131  $md->deleteAll();
132 
133  parent::handlerBeginTag($a_xml_parser,$a_name,$a_attribs);
134  break;
135 
136  case 'WebLink':
137 
138  $this->current_link_update = false;
139  $this->current_link_delete = false;
140  $this->current_parameters = array();
141 
142  if($this->getMode() == self::MODE_CREATE or (isset($a_attribs['action']) and $a_attribs['action'] == 'Create'))
143  {
144  // New weblink
145  $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
146  }
147  elseif($this->getMode() == self::MODE_UPDATE and $a_attribs['action'] == 'Delete')
148  {
149  $this->current_link_delete = true;
150  $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
151  $this->current_link->delete($a_attribs['id']);
152  break;
153  }
154  elseif($this->getMode() == self::MODE_UPDATE and ($a_attribs['action'] == 'Update' or !isset($a_attribs['action'])))
155  {
156  $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
157  $this->current_link->readItem($a_attribs['id']);
158  $this->current_link_update = true;
159 
160  // Delete all dynamic parameter
161  include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
162  foreach(ilParameterAppender::getParameterIds($this->getWebLink()->getId(), $a_attribs['id']) as $param_id)
163  {
164  $param = new ilParameterAppender($this->getWebLink()->getId());
165  $param->delete($param_id);
166  }
167  }
168  else
169  {
170  throw new ilWebLinkXmlParserException('Invalid action given for element "Weblink"');
171  }
172 
173  // Active
174  $this->current_link->setActiveStatus($a_attribs['active'] ? 1 : 0);
175 
176  // Valid
177  if(!isset($a_attribs['valid']))
178  {
179  $valid = 1;
180  }
181  else
182  {
183  $valid = $a_attribs['valid'] ? 1 : 0;
184  }
185  $this->current_link->setValidStatus($valid);
186 
187  // Disable check
188  $this->current_link->setDisableCheckStatus($a_attribs['disableValidation'] ? 1 : 0);
189  break;
190 
191 
192  case 'Sorting':
193 
194  include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
195  $sort = new ilContainerSortingSettings($this->getWebLink()->getId());
196  $sort->delete();
197 
198  switch($a_attribs['type'])
199  {
200  case 'Manual':
201  $sort->setSortMode(ilContainer::SORT_MANUAL);
202  break;
203 
204  case 'Title':
205  default:
206  $sort->setSortMode(ilContainer::SORT_TITLE);
207  }
208  $sort->save();
209  break;
210 
211  case 'WebLinks':
212  case 'Title':
213  case 'Description':
214  case 'Target':
215  // Nothing to do
216  break;
217 
218  case 'DynamicParameter':
219 
220  $param = new ilParameterAppender($this->getWebLink()->getId());
221  $param->setName($a_attribs['name']);
222 
223  switch($a_attribs['type'])
224  {
225  case 'userName':
226 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_LOGIN);
227  $param->setValue(LINKS_LOGIN);
228  break;
229 
230  case 'userId':
231 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_USER_ID);
232  $param->setValue(LINKS_USER_ID);
233  break;
234 
235  case 'matriculation':
236 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_MATRICULATION);
237  $param->setValue(LINKS_MATRICULATION);
238  break;
239 
240  default:
241  throw new ilWebLinkXmlParserException('Invalid attribute "type" given for element "Dynamic parameter". Aborting');
242  break;
243  }
244 
245  $this->current_parameters[] = $param;
246  break;
247  }
248  }
249 
258  public function handlerEndTag($a_xml_parser,$a_name)
259  {
260  if($this->in_metadata)
261  {
262  parent::handlerEndTag($a_xml_parser,$a_name);
263  }
264 
265  $GLOBALS['ilLog']->write(__METHOD__.': Called '.$a_name);
266 
267  switch($a_name)
268  {
269  case 'MetaData':
270  $this->in_metadata = false;
271  parent::handlerEndTag($a_xml_parser,$a_name);
272  break;
273 
274  case 'WebLinks':
275  $this->getWebLink()->MDUpdateListener('General');
276  $this->getWebLink()->update();
277  break;
278 
279  case 'WebLink':
280 
281  if($this->current_link_delete)
282  {
283  break;
284  }
285  if(!$this->current_link)
286  {
287  throw new ilSaxParserException('Invalid xml structure given. Missing start tag "WebLink"');
288  }
289  if(!$this->current_link->validate())
290  {
291  throw new ilWebLinkXmlParserException('Missing required elements "Title, Target"');
292  }
293 
294  if($this->current_link_update)
295  {
296  $this->current_link->update();
297  }
298  else
299  {
300  $this->current_link->add();
301  }
302 
303  // Save dynamic parameters
304  foreach($this->current_parameters as $param)
305  {
306  $param->add($this->current_link->getLinkId());
307  }
308 
309  unset($this->current_link);
310  break;
311 
312  case 'Title':
313  if($this->current_link)
314  {
315  $this->current_link->setTitle(trim($this->cdata));
316  }
317  break;
318 
319  case 'Description':
320  if($this->current_link)
321  {
322  $this->current_link->setDescription(trim($this->cdata));
323  }
324  break;
325 
326  case 'Target':
327  if($this->current_link)
328  {
329  $this->current_link->setTarget(trim($this->cdata));
330  }
331  break;
332  }
333 
334  // Reset cdata
335  $this->cdata = '';
336  }
337 
338 
339 
346  public function handlerCharacterData($a_xml_parser,$a_data)
347  {
348  if($this->in_metadata)
349  {
350  parent::handlerCharacterData($a_xml_parser,$a_data);
351  }
352 
353  if($a_data != "\n")
354  {
355  // Replace multiple tabs with one space
356  $a_data = preg_replace("/\t+/"," ",$a_data);
357  $this->cdata .= $a_data;
358  }
359  }
360 
361 
362 }
363 ?>