ILIAS  eassessment Revision 61809
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDRecordParser.php
Go to the documentation of this file.
1 <?php
2 /*
3  +-----------------------------------------------------------------------------+
4  | ILIAS open source |
5  +-----------------------------------------------------------------------------+
6  | Copyright (c) 1998-2006 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 
34 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDRecord.php');
35 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
36 
38 {
39  const MODE_UPDATE = 1;
40  const MODE_INSERT = 2;
43 
44  private $mode;
45 
46  private $fields = array();
47 
48  private $is_error = false;
49  private $error_msg = array();
57  public function __construct($a_file)
58  {
59  parent::__construct($a_file,true);
60  }
61 
69  public function setMode($a_mode)
70  {
71  $this->mode = $a_mode;
72  }
73 
80  public function getMode()
81  {
82  return $this->mode;
83  }
84 
85 
93  public function startParsing()
94  {
96  if($this->is_error)
97  {
98  include_once('classes/class.ilSaxParserException.php');
99  throw new ilSaxParserException(implode('<br/>',$this->error_msg));
100  }
101  }
102 
109  public function setHandlers($a_xml_parser)
110  {
111  xml_set_object($a_xml_parser,$this);
112  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
113  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
114  }
115 
121  protected function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
122  {
123  switch($a_name)
124  {
125  case 'AdvancedMetaDataRecords':
126  $this->is_error = false;
127  $this->error_msg = array();
128  // Nothing to do
129  break;
130 
131  case 'Record':
132  $this->fields = array();
133  $this->current_field = null;
134  $this->current_record = null;
135  if(!strlen($a_attribs['id']) or !isset($a_attribs['active']))
136  {
137  $this->appendErrorMessage('Missing XML attribute for element "Record".');
138  }
139  if(!$this->initRecordObject($a_attribs['id']))
140  {
141  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
142  }
143  $this->getCurrentRecord()->setActive($a_attribs['active']);
144  $this->getCurrentRecord()->setImportId($a_attribs['id']);
145  $this->getCurrentRecord()->setAssignedObjectTypes(array());
146  break;
147 
148  case 'Title':
149  break;
150 
151  case 'Field':
152  if(!strlen($a_attribs['id']) or !isset($a_attribs['searchable']) or !isset($a_attribs['fieldType']))
153  {
154  $this->appendErrorMessage('Missing XML attribute for element "Field".');
155  }
156  if(!$this->initFieldObject($a_attribs['id']))
157  {
158  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
159  }
160  switch($a_attribs['fieldType'])
161  {
162  case 'Select':
164  break;
165 
166  case 'Date':
168  break;
169 
170  case 'DateTime':
172  break;
173 
174  case 'Text':
176  break;
177 
178  default:
179  $this->appendErrorMessage('Invalid attribute value given for element "Record::FieldType".');
180  break;
181 
182  }
183  $this->getCurrentField()->setImportId($a_attribs['id']);
184  $this->getCurrentField()->enableSearchable($a_attribs['searchable'] == 'Yes' ? true : false);
185  break;
186 
187  case 'FieldTitle':
188  case 'FieldDescription':
189  case 'FieldPosition':
190  case 'FieldValue':
191  break;
192  }
193  }
194 
200  protected function handlerEndTag($a_xml_parser,$a_name)
201  {
202  switch($a_name)
203  {
204  case 'AdvancedMetaDataRecords':
205  break;
206 
207  case 'Record':
208  $this->storeRecords();
209  break;
210 
211  case 'Title':
212  $this->getCurrentRecord()->setTitle(trim($this->cdata));
213  break;
214 
215  case 'Description':
216  $this->getCurrentRecord()->setDescription(trim($this->cdata));
217  break;
218 
219  case 'ObjectType':
220  $this->getCurrentRecord()->appendAssignedObjectType(trim($this->cdata));
221  break;
222 
223  case 'Field':
224  break;
225 
226 
227  case 'FieldTitle':
228  $this->getCurrentField()->setTitle(trim($this->cdata));
229  break;
230 
231  case 'FieldDescription':
232  $this->getCurrentField()->setDescription(trim($this->cdata));
233  break;
234 
235  case 'FieldPosition':
236  $this->getCurrentField()->setPosition((int) trim($this->cdata));
237  break;
238 
239  case 'FieldValue':
240  $this->getCurrentField()->appendFieldValue(trim($this->cdata));
241  break;
242  }
243  $this->cdata = '';
244  }
245 
252  protected function handlerCharacterData($a_xml_parser,$a_data)
253  {
254  if($a_data != "\n")
255  {
256  // Replace multiple tabs with one space
257  $a_data = preg_replace("/\t+/"," ",$a_data);
258 
259  $this->cdata .= $a_data;
260  }
261  }
262 
270  private function initRecordObject($a_id)
271  {
272  switch($this->getMode())
273  {
274  case self::MODE_INSERT:
275  case self::MODE_INSERT_VALIDATION:
276  $this->current_record = new ilAdvancedMDRecord(0);
277  return true;
278 
279  default:
280  $this->current_record = ilAdvancedMDRecord::_getInstanceByRecordId($this->extractRecordId($a_id));
281  return true;
282  break;
283  }
284  }
285 
293  private function initFieldObject($a_id)
294  {
295  switch($this->getMode())
296  {
297  case self::MODE_INSERT:
298  case self::MODE_INSERT_VALIDATION:
299  $this->current_field = new ilAdvancedMDFieldDefinition(0);
300  $this->fields[] = $this->current_field;
301  return true;
302 
303  default:
304  $this->current_field = ilAdvancedMDRecord::_getInstanceByFieldId($this->extractFieldId($a_id));
305  return true;
306  break;
307  }
308  }
309 
310 
311 
318  private function getCurrentRecord()
319  {
320  return $this->current_record;
321  }
322 
328  private function getCurrentField()
329  {
330  return $this->current_field;
331  }
332 
340  private function extractRecordId($a_id_string)
341  {
342  // first lookup import id
343  if($record_id = ilAdvancedMDRecord::_lookupRecordIdByImportId($a_id_string))
344  {
345  $this->record_exists = true;
346  return $record_id;
347  }
348  return 0;
349  }
350 
351 
352 
360  private function appendErrorMessage($a_msg)
361  {
362  $this->is_error = true;
363  $this->error_msg[] = $a_msg;
364  }
365 
373  private function storeRecords()
374  {
375  switch($this->getMode())
376  {
377  case self::MODE_INSERT_VALIDATION:
378  case self::MODE_UPDATE_VALIDATION:
379  return true;
380 
381  case self::MODE_INSERT:
382  $this->getCurrentRecord()->save();
383  break;
384  }
385  foreach($this->fields as $field)
386  {
387  $field->setRecordId($this->getCurrentRecord()->getRecordId());
388  switch($this->getMode())
389  {
390  case self::MODE_INSERT:
391  $field->add();
392  break;
393  }
394 
395  }
396  }
397 
398 }
399 ?>