ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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'], $a_attribs['fieldType']))
157  {
158  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
159  }
160  $this->getCurrentField()->setImportId($a_attribs['id']);
161  $this->getCurrentField()->setSearchable($a_attribs['searchable'] == 'Yes' ? true : false);
162  break;
163 
164  case 'FieldTitle':
165  case 'FieldDescription':
166  case 'FieldPosition':
167  case 'FieldValue':
168  $this->field_value_id = $a_attribs['id'];
169  break;
170  }
171  }
172 
178  protected function handlerEndTag($a_xml_parser,$a_name)
179  {
180  switch($a_name)
181  {
182  case 'AdvancedMetaDataRecords':
183  break;
184 
185  case 'Record':
186  $this->storeRecords();
187  break;
188 
189  case 'Title':
190  $this->getCurrentRecord()->setTitle(trim($this->cdata));
191  break;
192 
193  case 'Description':
194  $this->getCurrentRecord()->setDescription(trim($this->cdata));
195  break;
196 
197  case 'ObjectType':
198  // #12980
199  $parts = explode(":", trim($this->cdata));
200  $this->getCurrentRecord()->appendAssignedObjectType($parts[0], $parts[1]);
201  break;
202 
203  case 'Field':
204  break;
205 
206 
207  case 'FieldTitle':
208  $this->getCurrentField()->setTitle(trim($this->cdata));
209  break;
210 
211  case 'FieldDescription':
212  $this->getCurrentField()->setDescription(trim($this->cdata));
213  break;
214 
215  case 'FieldPosition':
216  $this->getCurrentField()->setPosition((int) trim($this->cdata));
217  break;
218 
219  case 'FieldValue':
220  $this->getCurrentField()->importXMLProperty($this->field_value_id, trim($this->cdata));
221  break;
222  }
223  $this->cdata = '';
224  }
225 
232  protected function handlerCharacterData($a_xml_parser,$a_data)
233  {
234  if($a_data != "\n")
235  {
236  // Replace multiple tabs with one space
237  $a_data = preg_replace("/\t+/"," ",$a_data);
238 
239  $this->cdata .= $a_data;
240  }
241  }
242 
250  private function initRecordObject($a_id)
251  {
252  switch($this->getMode())
253  {
254  case self::MODE_INSERT:
255  case self::MODE_INSERT_VALIDATION:
256  $this->current_record = new ilAdvancedMDRecord(0);
257  return true;
258 
259  default:
260  $this->current_record = ilAdvancedMDRecord::_getInstanceByRecordId($this->extractRecordId($a_id));
261  return true;
262  break;
263  }
264  }
265 
273  private function initFieldObject($a_id, $a_type)
274  {
275  switch($this->getMode())
276  {
277  case self::MODE_INSERT:
278  case self::MODE_INSERT_VALIDATION:
279  $this->current_field = ilAdvancedMDFieldDefinition::getInstanceByTypeString($a_type);
280  $this->fields[] = $this->current_field;
281  return true;
282 
283  default:
284  // ??? nonsense
285  $this->current_field = ilAdvancedMDRecord::_getInstanceByFieldId($this->extractFieldId($a_id));
286  return true;
287  break;
288  }
289  }
290 
291 
292 
299  private function getCurrentRecord()
300  {
301  return $this->current_record;
302  }
303 
309  private function getCurrentField()
310  {
311  return $this->current_field;
312  }
313 
321  private function extractRecordId($a_id_string)
322  {
323  // first lookup import id
324  if($record_id = ilAdvancedMDRecord::_lookupRecordIdByImportId($a_id_string))
325  {
326  $this->record_exists = true;
327  return $record_id;
328  }
329  return 0;
330  }
331 
332 
333 
341  private function appendErrorMessage($a_msg)
342  {
343  $this->is_error = true;
344  $this->error_msg[] = $a_msg;
345  }
346 
354  private function storeRecords()
355  {
356  switch($this->getMode())
357  {
358  case self::MODE_INSERT_VALIDATION:
359  case self::MODE_UPDATE_VALIDATION:
360  return true;
361 
362  case self::MODE_INSERT:
363  $this->getCurrentRecord()->save();
364  break;
365  }
366  foreach($this->fields as $field)
367  {
368  $field->setRecordId($this->getCurrentRecord()->getRecordId());
369  switch($this->getMode())
370  {
371  case self::MODE_INSERT:
372  $field->save();
373  break;
374  }
375 
376  }
377  }
378 
379 }
380 ?>
setHandlers($a_xml_parser)
set event handlers
initFieldObject($a_id, $a_type)
Init field definition object.
SaxParserException thrown by ilSaxParser if property throwException is set.
handlerEndTag($a_xml_parser, $a_name)
Handler for end tags.
$errors fields
Definition: imgupload.php:48
startParsing()
stores xml data in array
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
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.
initRecordObject($a_id)
Init record object.
getCurrentField()
get current field definition private
SAX based XML parser for record import files.
static getInstanceByTypeString($a_type)
Get instance by type string (used by import)
static _lookupRecordIdByImportId($a_ilias_id)
Lookup record Id by import id.