ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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();
50 
51  protected $context; // [array]
52 
56  protected $log;
57 
65  public function __construct($a_file)
66  {
67  parent::__construct($a_file,true);
68  $this->log = ilLoggerFactory::getLogger('amet');
69  }
70 
78  public function setMode($a_mode)
79  {
80  $this->mode = $a_mode;
81  }
82 
89  public function getMode()
90  {
91  return $this->mode;
92  }
93 
94 
102  public function startParsing()
103  {
104  parent::startParsing();
105  if($this->is_error)
106  {
107  include_once('./Services/Xml/exceptions/class.ilSaxParserException.php');
108  throw new ilSaxParserException(implode('<br/>',$this->error_msg));
109  }
110  }
111 
118  public function setHandlers($a_xml_parser)
119  {
120  xml_set_object($a_xml_parser,$this);
121  xml_set_element_handler($a_xml_parser,'handlerBeginTag','handlerEndTag');
122  xml_set_character_data_handler($a_xml_parser,'handlerCharacterData');
123  }
124 
130  protected function handlerBeginTag($a_xml_parser,$a_name,$a_attribs)
131  {
132  switch($a_name)
133  {
134  case 'AdvancedMetaDataRecords':
135  $this->is_error = false;
136  $this->error_msg = array();
137  // Nothing to do
138  break;
139 
140  case 'Record':
141  $this->fields = array();
142  $this->current_field = null;
143  $this->current_record = null;
144  if(!strlen($a_attribs['id']) or !isset($a_attribs['active']))
145  {
146  $this->appendErrorMessage('Missing XML attribute for element "Record".');
147  }
148  if(!$this->initRecordObject($a_attribs['id']))
149  {
150  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
151  }
152  $this->getCurrentRecord()->setActive($a_attribs['active']);
153  $this->getCurrentRecord()->setImportId($a_attribs['id']);
154  $this->getCurrentRecord()->setAssignedObjectTypes(array());
155  break;
156 
157  case 'Title':
158  break;
159 
160  case 'Field':
161  if(!strlen($a_attribs['id']) or !isset($a_attribs['searchable']) or !isset($a_attribs['fieldType']))
162  {
163  $this->appendErrorMessage('Missing XML attribute for element "Field".');
164  }
165  if(!$this->initFieldObject($a_attribs['id'], $a_attribs['fieldType']))
166  {
167  $this->appendErrorMessage('Invalid attribute Id given for element "Record".');
168  }
169  $this->getCurrentField()->setImportId($a_attribs['id']);
170  $this->getCurrentField()->setSearchable($a_attribs['searchable'] == 'Yes' ? true : false);
171  break;
172 
173  case 'FieldTitle':
174  case 'FieldDescription':
175  case 'FieldPosition':
176  case 'FieldValue':
177  $this->field_value_id = $a_attribs['id'];
178  break;
179  }
180  }
181 
187  protected function handlerEndTag($a_xml_parser,$a_name)
188  {
189  switch($a_name)
190  {
191  case 'AdvancedMetaDataRecords':
192  break;
193 
194  case 'Record':
195  $this->storeRecords();
196  break;
197 
198  case 'Title':
199  $this->getCurrentRecord()->setTitle(trim($this->cdata));
200  break;
201 
202  case 'Description':
203  $this->getCurrentRecord()->setDescription(trim($this->cdata));
204  break;
205 
206  case 'ObjectType':
207  // #12980
208  $parts = explode(":", trim($this->cdata));
209  $this->getCurrentRecord()->appendAssignedObjectType($parts[0], $parts[1]);
210  break;
211 
212  case 'Field':
213  break;
214 
215 
216  case 'FieldTitle':
217  $this->getCurrentField()->setTitle(trim($this->cdata));
218  break;
219 
220  case 'FieldDescription':
221  $this->getCurrentField()->setDescription(trim($this->cdata));
222  break;
223 
224  case 'FieldPosition':
225  $this->getCurrentField()->setPosition((int) trim($this->cdata));
226  break;
227 
228  case 'FieldValue':
229  $this->getCurrentField()->importXMLProperty($this->field_value_id, trim($this->cdata));
230  break;
231  }
232  $this->cdata = '';
233  }
234 
241  protected function handlerCharacterData($a_xml_parser,$a_data)
242  {
243  if($a_data != "\n")
244  {
245  // Replace multiple tabs with one space
246  $a_data = preg_replace("/\t+/"," ",$a_data);
247 
248  $this->cdata .= $a_data;
249  }
250  }
251 
259  private function initRecordObject($a_id)
260  {
261  switch($this->getMode())
262  {
263  case self::MODE_INSERT:
264  case self::MODE_INSERT_VALIDATION:
265  $this->current_record = new ilAdvancedMDRecord(0);
266  return true;
267 
268  default:
269  $this->current_record = ilAdvancedMDRecord::_getInstanceByRecordId($this->extractRecordId($a_id));
270  return true;
271  break;
272  }
273  }
274 
282  private function initFieldObject($a_id, $a_type)
283  {
284  switch($this->getMode())
285  {
286  case self::MODE_INSERT:
287  case self::MODE_INSERT_VALIDATION:
289  $this->fields[] = $this->current_field;
290  return true;
291 
292  default:
293  // ??? nonsense
294  $this->current_field = ilAdvancedMDRecord::_getInstanceByFieldId($this->extractFieldId($a_id));
295  return true;
296  break;
297  }
298  }
299 
300 
301 
308  private function getCurrentRecord()
309  {
310  return $this->current_record;
311  }
312 
318  private function getCurrentField()
319  {
320  return $this->current_field;
321  }
322 
330  private function extractRecordId($a_id_string)
331  {
332  // first lookup import id
333  if($record_id = ilAdvancedMDRecord::_lookupRecordIdByImportId($a_id_string))
334  {
335  $this->record_exists = true;
336  return $record_id;
337  }
338  return 0;
339  }
340 
341 
342 
350  private function appendErrorMessage($a_msg)
351  {
352  $this->is_error = true;
353  $this->error_msg[] = $a_msg;
354  }
355 
363  private function storeRecords()
364  {
365  switch($this->getMode())
366  {
367  case self::MODE_INSERT_VALIDATION:
368  case self::MODE_UPDATE_VALIDATION:
369  return true;
370 
371  case self::MODE_INSERT:
372  // set local context
373  if(is_array($this->context))
374  {
375  $this->getCurrentRecord()->setParentObject($this->context["obj_id"]);
376  $this->getCurrentRecord()->setAssignedObjectTypes(array(
377  array(
378  "obj_type" => $this->context["obj_type"],
379  "sub_type" => $this->context["sub_type"],
380  "optional" => false
381  )));
382  }
383 
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->save();
394 
395  // see getRecordMap()
396  $this->log->debug("add to record map, rec id: ".$this->getCurrentRecord()->getRecordId().
397  ", import id: ".$field->getImportId().", field id:".$field->getFieldId());
398  $this->rec_map[$this->getCurrentRecord()->getRecordId()][$field->getImportId()] = $field->getFieldId();
399  break;
400  }
401  }
402  }
403 
404  public function setContext($a_obj_id, $a_obj_type, $a_sub_type = null)
405  {
406  if(!$a_sub_type)
407  {
408  $a_sub_type = "-";
409  }
410 
411  $this->context = array(
412  "obj_id" => $a_obj_id,
413  "obj_type" => $a_obj_type,
414  "sub_type" => $a_sub_type
415  );
416  }
417 
418  public function getRecordMap()
419  {
420  return $this->rec_map;
421  }
422 }
423 ?>
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.
startParsing()
stores xml data in array
handlerCharacterData($a_xml_parser, $a_data)
handler for character data
Base class for sax-based expat parsing extended classes need to overwrite the method setHandlers and ...
$errors fields
Definition: imgupload.php:52
$a_type
Definition: workflow.php:93
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.
Create styles array
The data for the language used.
initRecordObject($a_id)
Init record object.
setContext($a_obj_id, $a_obj_type, $a_sub_type=null)
getCurrentField()
get current field definition private
static getLogger($a_component_id)
Get component logger.
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.