ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilCategoryXmlParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2001 ILIAS open source, University of Cologne |
7  | |
8  | This program is free software; you can redistribute it and/or |
9  | modify it under the terms of the GNU General Public License |
10  | as published by the Free Software Foundation; either version 2 |
11  | of the License, or (at your option) any later version. |
12  | |
13  | This program is distributed in the hope that it will be useful, |
14  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
15  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16  | GNU General Public License for more details. |
17  | |
18  | You should have received a copy of the GNU General Public License |
19  | along with this program; if not, write to the Free Software |
20  | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
21  +-----------------------------------------------------------------------------+
22 */
23 
24 require_once("./Services/Xml/classes/class.ilSaxParser.php");
25 require_once('./Services/User/classes/class.ilObjUser.php');
26 include_once('./Services/Calendar/classes/class.ilDateTime.php');
27 
28 
39 {
40  const MODE_CREATE = 1;
41  const MODE_UPDATE = 2;
42 
43  private $cat = null;
44  private $parent_id = 0;
45 
46  private $current_translation = array();
47 
48 
57  public function __construct($a_xml, $a_parent_id)
58  {
59  parent::__construct(null);
60 
62  $this->parent_id = $a_parent_id;
63  $this->setXMLContent($a_xml);
64  }
65 
70  public function getParentId()
71  {
72  return $this->parent_id;
73  }
74 
78  protected function getCurrentTranslation()
79  {
81  }
82 
83 
84 
90  public function setHandlers($a_xml_parser)
91  {
92  xml_set_object($a_xml_parser,$this);
93  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
94  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
95  }
96 
100  public function startParsing()
101  {
103 
104  if ($this->mode == ilCategoryXmlParser::MODE_CREATE)
105  {
106  return is_object($this->cat) ? $this->cat->getRefId() : false;
107  }
108  else
109  {
110  return is_object($this->cat) ? $this->cat->update() : false;
111  }
112  }
113 
114 
118  public function handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
119  {
120  global $ilErr;
121 
122  switch($a_name)
123  {
124  case "Category":
125  break;
126 
127  case 'Translations':
128  $this->getCategory()->removeTranslations();
129  break;
130 
131  case 'Translation':
132  $this->current_translation = array();
133  $this->current_translation['default'] = $a_attribs['default'] ? 1 : 0;
134  $this->current_translation['lang'] = $a_attribs['language'];
135  break;
136 
137  case 'Sorting':
138  include_once './Services/Container/classes/class.ilContainerSortingSettings.php';
139  $sort = new ilContainerSortingSettings($this->getCategory()->getId());
140 
141  switch($a_attribs['type'])
142  {
143  case 'Manual':
144  $sort->setSortMode(ilContainer::SORT_MANUAL);
145  break;
146 
147  default:
148  $sort->setSortMode(ilContainer::SORT_TITLE);
149  break;
150  }
151  $sort->update();
152  break;
153  }
154  }
155 
156 
162  public function handlerEndTag($a_xml_parser, $a_name)
163  {
164  switch($a_name)
165  {
166  case "Category":
167  $this->save();
168  break;
169 
170  case 'Title':
171  $this->current_translation['title'] = trim($this->cdata);
172 
173  if($this->current_translation['default'])
174  {
175  $this->getCategory()->setTitle(trim($this->cdata));
176  }
177 
178  break;
179 
180  case 'Description':
181  $this->current_translation['description'] = trim($this->cdata);
182 
183  if($this->current_translation['default'])
184  {
185  $this->getCategory()->setDescription(trim($this->cdata));
186  }
187 
188  break;
189 
190  case 'Translation':
191  // Add translation
192  $this->getCategory()->addTranslation(
193  (string) $this->current_translation['title'],
194  (string) $this->current_translation['description'],
195  (string) $this->current_translation['lang'],
196  (int) $this->current_translation['default']
197  );
198  break;
199  }
200  $this->cdata = '';
201  }
202 
203 
207  function handlerCharacterData($a_xml_parser, $a_data)
208  {
209  #$a_data = str_replace("<","&lt;",$a_data);
210  #$a_data = str_replace(">","&gt;",$a_data);
211 
212  if(!empty($a_data))
213  {
214  $this->cdata .= $a_data;
215  }
216  }
217 
222  protected function save()
223  {
224 
228  if ($this->mode == ilCategoryXmlParser::MODE_CREATE)
229  {
230  $this->create();
231  $this->getCategory()->create();
232  $this->getCategory()->createReference();
233  $this->getCategory()->putInTree($this->getParentId());
234  $this->getCategory()->setPermissions($this->getParentId());
235  }
236  $this->getCategory()->update();
237  return true;
238  }
239 
240 
241 
242 
247  public function setMode($mode)
248  {
249  $this->mode = $mode;
250  }
251 
256  public function setCategory($cat)
257  {
258  $this->cat = $cat;
259  }
260 
261  public function getCategory()
262  {
263  return $this->cat;
264  }
265 }
266 ?>