ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules 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  {
95  parent::startParsing();
96  if($this->is_error)
97  {
98  include_once('./Services/Xml/exceptions/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  // #12980
221  $parts = explode(":", trim($this->cdata));
222  $this->getCurrentRecord()->appendAssignedObjectType($parts[0], $parts[1]);
223  break;
224 
225  case 'Field':
226  break;
227 
228 
229  case 'FieldTitle':
230  $this->getCurrentField()->setTitle(trim($this->cdata));
231  break;
232 
233  case 'FieldDescription':
234  $this->getCurrentField()->setDescription(trim($this->cdata));
235  break;
236 
237  case 'FieldPosition':
238  $this->getCurrentField()->setPosition((int) trim($this->cdata));
239  break;
240 
241  case 'FieldValue':
242  $this->getCurrentField()->appendFieldValue(trim($this->cdata));
243  break;
244  }
245  $this->cdata = '';
246  }
247 
254  protected function handlerCharacterData($a_xml_parser,$a_data)
255  {
256  if($a_data != "\n")
257  {
258  // Replace multiple tabs with one space
259  $a_data = preg_replace("/\t+/"," ",$a_data);
260 
261  $this->cdata .= $a_data;
262  }
263  }
264 
272  private function initRecordObject($a_id)
273  {
274  switch($this->getMode())
275  {
276  case self::MODE_INSERT:
277  case self::MODE_INSERT_VALIDATION:
278  $this->current_record = new ilAdvancedMDRecord(0);
279  return true;
280 
281  default:
282  $this->current_record = ilAdvancedMDRecord::_getInstanceByRecordId($this->extractRecordId($a_id));
283  return true;
284  break;
285  }
286  }
287 
295  private function initFieldObject($a_id)
296  {
297  switch($this->getMode())
298  {
299  case self::MODE_INSERT:
300  case self::MODE_INSERT_VALIDATION:
301  $this->current_field = new ilAdvancedMDFieldDefinition(0);
302  $this->fields[] = $this->current_field;
303  return true;
304 
305  default:
306  $this->current_field = ilAdvancedMDRecord::_getInstanceByFieldId($this->extractFieldId($a_id));
307  return true;
308  break;
309  }
310  }
311 
312 
313 
320  private function getCurrentRecord()
321  {
322  return $this->current_record;
323  }
324 
330  private function getCurrentField()
331  {
332  return $this->current_field;
333  }
334 
342  private function extractRecordId($a_id_string)
343  {
344  // first lookup import id
345  if($record_id = ilAdvancedMDRecord::_lookupRecordIdByImportId($a_id_string))
346  {
347  $this->record_exists = true;
348  return $record_id;
349  }
350  return 0;
351  }
352 
353 
354 
362  private function appendErrorMessage($a_msg)
363  {
364  $this->is_error = true;
365  $this->error_msg[] = $a_msg;
366  }
367 
375  private function storeRecords()
376  {
377  switch($this->getMode())
378  {
379  case self::MODE_INSERT_VALIDATION:
380  case self::MODE_UPDATE_VALIDATION:
381  return true;
382 
383  case self::MODE_INSERT:
384  $this->getCurrentRecord()->save();
385  break;
386  }
387  foreach($this->fields as $field)
388  {
389  $field->setRecordId($this->getCurrentRecord()->getRecordId());
390  switch($this->getMode())
391  {
392  case self::MODE_INSERT:
393  $field->add();
394  break;
395  }
396 
397  }
398  }
399 
400 }
401 ?>
setHandlers($a_xml_parser)
set event handlers
SaxParserException thrown by ilSaxParser if property throwException is set.
handlerEndTag($a_xml_parser, $a_name)
Handler for end tags.
startParsing()
stores xml data in array
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
$errors fields
Definition: imgupload.php:47
static _getInstanceByRecordId($a_record_id)
Get instance by record id.
handlerBeginTag($a_xml_parser, $a_name, $a_attribs)
Handler for start tags.
extractRecordId($a_id_string)
Extract id.
initFieldObject($a_id)
Init field definition object.
initRecordObject($a_id)
Init record object.
getCurrentField()
get current field definition private
SAX based XML parser for record import files.
static _lookupRecordIdByImportId($a_ilias_id)
Lookup record Id by import id.