ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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 
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  parent::handlerBeginTag($a_xml_parser, $a_name, $a_attribs);
123  return;
124  }
125 
126  switch ($a_name) {
127  case "MetaData":
128  $this->in_metadata = true;
129 
130  // Delete old meta data
131  $md = new ilMD($this->getWebLink()->getId(), 0, 'webr');
132  $md->deleteAll();
133 
134  parent::handlerBeginTag($a_xml_parser, $a_name, $a_attribs);
135  break;
136 
137  case 'WebLink':
138 
139  $this->current_sorting_position = ($a_attribs['position'] ? $a_attribs['position'] : 0);
140  $this->current_link_update = false;
141  $this->current_link_delete = false;
142  $this->current_parameters = array();
143 
144  if ($this->getMode() == self::MODE_CREATE or (isset($a_attribs['action']) and $a_attribs['action'] == 'Create')) {
145  // New weblink
146  $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
147  } elseif ($this->getMode() == self::MODE_UPDATE and $a_attribs['action'] == 'Delete') {
148  $this->current_link_delete = true;
149  $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
150  $this->current_link->delete($a_attribs['id']);
151  break;
152  } elseif ($this->getMode() == self::MODE_UPDATE and ($a_attribs['action'] == 'Update' or !isset($a_attribs['action']))) {
153  $this->current_link = new ilLinkResourceItems($this->getWebLink()->getId());
154  $this->current_link->readItem($a_attribs['id']);
155  $this->current_link_update = true;
156 
157  // Delete all dynamic parameter
158  include_once './Modules/WebResource/classes/class.ilParameterAppender.php';
159  foreach (ilParameterAppender::getParameterIds($this->getWebLink()->getId(), $a_attribs['id']) as $param_id) {
160  $param = new ilParameterAppender($this->getWebLink()->getId());
161  $param->delete($param_id);
162  }
163  } else {
164  throw new ilWebLinkXmlParserException('Invalid action given for element "Weblink"');
165  }
166 
167  // Active
168  $this->current_link->setActiveStatus($a_attribs['active'] ? 1 : 0);
169 
170  // Valid
171  if (!isset($a_attribs['valid'])) {
172  $valid = 1;
173  } else {
174  $valid = $a_attribs['valid'] ? 1 : 0;
175  }
176  $this->current_link->setValidStatus($valid);
177 
178  // Disable check
179  $this->current_link->setDisableCheckStatus($a_attribs['disableValidation'] ? 1 : 0);
180 
181  // internal
182  if (isset($a_attribs['internal'])) {
183  $this->current_link->setInternal($a_attribs['internal']);
184  }
185  break;
186 
187 
188  case 'Sorting':
189 
190  include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
191  $sort = new ilContainerSortingSettings($this->getWebLink()->getId());
192  $sort->delete();
193 
194  switch ($a_attribs['type']) {
195  case 'Manual':
196  $sort->setSortMode(ilContainer::SORT_MANUAL);
197  break;
198 
199  case 'Title':
200  default:
201  $sort->setSortMode(ilContainer::SORT_TITLE);
202  }
203  $sort->save();
204  break;
205 
206  case 'WebLinks':
207  $this->sorting_positions = array();
208  // no break
209  case 'Title':
210  case 'Description':
211  case 'Target':
212  // Nothing to do
213  break;
214 
215  case 'DynamicParameter':
216 
217  $param = new ilParameterAppender($this->getWebLink()->getId());
218  $param->setName($a_attribs['name']);
219 
220  switch ($a_attribs['type']) {
221  case 'userName':
222 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_LOGIN);
223  $param->setValue(LINKS_LOGIN);
224  break;
225 
226  case 'userId':
227 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_USER_ID);
228  $param->setValue(LINKS_USER_ID);
229  break;
230 
231  case 'matriculation':
232 # $GLOBALS['ilLog']->write("VALUE: ".LINKS_MATRICULATION);
233  $param->setValue(LINKS_MATRICULATION);
234  break;
235 
236  default:
237  throw new ilWebLinkXmlParserException('Invalid attribute "type" given for element "Dynamic parameter". Aborting');
238  break;
239  }
240 
241  $this->current_parameters[] = $param;
242  break;
243  }
244  }
245 
254  public function handlerEndTag($a_xml_parser, $a_name)
255  {
256  if ($this->in_metadata) {
257  parent::handlerEndTag($a_xml_parser, $a_name);
258  }
259 
260  $GLOBALS['ilLog']->write(__METHOD__ . ': Called ' . $a_name);
261 
262  switch ($a_name) {
263  case 'MetaData':
264  $this->in_metadata = false;
265  parent::handlerEndTag($a_xml_parser, $a_name);
266  break;
267 
268  case 'WebLinks':
269  $this->getWebLink()->MDUpdateListener('General');
270  $this->getWebLink()->update();
271 
272  // save sorting
273  include_once './Services/Container/classes/class.ilContainerSorting.php';
274  $sorting = ilContainerSorting::_getInstance($this->getWebLink()->getId());
275  $sorting->savePost($this->sorting_positions);
276  ilLoggerFactory::getLogger('webr')->dump($this->sorting_positions);
277  break;
278 
279  case 'WebLink':
280 
281  if ($this->current_link_delete) {
282  break;
283  }
284  if (!$this->current_link) {
285  throw new ilSaxParserException('Invalid xml structure given. Missing start tag "WebLink"');
286  }
287  if (!$this->current_link->validate()) {
288  throw new ilWebLinkXmlParserException('Missing required elements "Title, Target"');
289  }
290 
291  if ($this->current_link_update) {
292  $this->current_link->update();
293  } else {
294  $this->current_link->add();
295  }
296 
297  // Save dynamic parameters
298  foreach ($this->current_parameters as $param) {
299  $param->add($this->current_link->getLinkId());
300  }
301 
302 
303  // store positions
304  $this->sorting_positions[$this->current_link->getLinkId()] = (int) $this->current_sorting_position;
305 
306  unset($this->current_link);
307  break;
308 
309  case 'Title':
310  if ($this->current_link) {
311  $this->current_link->setTitle(trim($this->cdata));
312  }
313  break;
314 
315  case 'Description':
316  if ($this->current_link) {
317  $this->current_link->setDescription(trim($this->cdata));
318  }
319  break;
320 
321  case 'Target':
322  if ($this->current_link) {
323  $this->current_link->setTarget(trim($this->cdata));
324  }
325  break;
326  }
327 
328  // Reset cdata
329  $this->cdata = '';
330  }
331 
332 
333 
340  public function handlerCharacterData($a_xml_parser, $a_data)
341  {
342  if ($this->in_metadata) {
343  parent::handlerCharacterData($a_xml_parser, $a_data);
344  }
345 
346  if ($a_data != "\n") {
347  // Replace multiple tabs with one space
348  $a_data = preg_replace("/\t+/", " ", $a_data);
349  $this->cdata .= $a_data;
350  }
351  }
352 }
XML parser for weblink xml.
global $ilErr
Definition: raiseError.php:16
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.
$valid
$GLOBALS['loaded']
Global hash that tracks already loaded includes.
const LINKS_MATRICULATION
setThrowException($throwException)
set error handling
SaxParserException thrown by ilSaxParser if property throwException is set.
const LINKS_LOGIN
getMode()
Return parsing mode.
$xml
Definition: metadata.php:240
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
Create styles array
The data for the language used.
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
const LINKS_USER_ID
setXMLContent($a_xml_content)
handlerEndTag($a_xml_parser, $a_name)
handler for end of element