ILIAS  Release_4_1_x_branch Revision 61804
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDFieldDefinition.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 
32 {
33  const TYPE_SELECT = 1;
34  const TYPE_TEXT = 2;
35  const TYPE_DATE = 3;
36  const TYPE_DATETIME = 4;
37 
38  private static $instances = array();
39 
40  protected $db = null;
41 
42  protected $record_id;
43  protected $field_id;
44  protected $import_id;
45  protected $position;
46  protected $field_type;
47  protected $field_values = array();
48  protected $title;
49  protected $description;
50  protected $searchable;
51  protected $required = false;
52 
53 
59  public function __construct($a_field_id = 0)
60  {
61  global $ilDB;
62 
63  $this->db = $ilDB;
64 
65  $this->field_id = $a_field_id;
66  $this->read();
67  }
68 
77  public static function _lookupImportId($a_field_id)
78  {
79  global $ilDB;
80 
81  $query = "SELECT import_id FROM adv_mdf_definition ".
82  "WHERE field_id = ".$ilDB->quote($a_field_id,'integer')." ";
83  $res = $ilDB->query($query);
84  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
85  return $row['import_id'] ? $row['import_id'] : '';
86  }
87 
96  public static function _lookupFieldId($a_import_id)
97  {
98  global $ilDB;
99 
100  $query = "SELECT field_id FROM adv_mdf_definition ".
101  "WHERE import_id = ".$ilDB->quote($a_import_id,'text')." ";
102  $res = $ilDB->query($query);
103  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
104  return $row['field_id'] ? $row['field_id'] : 0;
105  }
106 
115  public static function _lookupFieldType($a_field_id)
116  {
117  global $ilDB;
118 
119  $query = "SELECT field_type FROM adv_mdf_definition ".
120  "WHERE field_id = ".$ilDB->quote($a_field_id ,'integer')." ";
121  $res = $ilDB->query($query);
122  $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
123  return $row['field_type'] ? $row['field_type'] : 0;
124  }
125 
126 
135  public static function _lookupDateTimeFields()
136  {
137  global $ilDB;
138 
139  $query = "SELECT field_id FROM adv_mdf_definition ".
140  "WHERE field_type = ".$ilDB->quote(self::TYPE_DATETIME ,'integer')." ";
141  $res = $ilDB->query($query);
142 
143  $date_fields = array();
144  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
145  {
146  $date_fields[] = $row->field_id;
147  }
148  return $date_fields;
149  }
150 
158  public static function _lookupDateFields()
159  {
160  global $ilDB;
161 
162  $query = "SELECT field_id FROM adv_mdf_definition ".
163  "WHERE field_type = ".$ilDB->quote(self::TYPE_DATE ,'integer')." ";
164  $res = $ilDB->query($query);
165 
166  $date_fields = array();
167  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
168  {
169  $date_fields[] = $row->field_id;
170  }
171  return $date_fields;
172  }
173 
182  public static function _getInstanceByFieldId($a_field_id)
183  {
184  if(isset(self::$instances[$a_field_id]))
185  {
186  return self::$instances[$a_field_id];
187  }
188  return self::$instances[$a_field_id] = new ilAdvancedMDFieldDefinition($a_field_id);
189  }
190 
191 
201  public static function _getDefinitionsByRecordId($a_record_id)
202  {
203  global $ilDB;
204 
205  $query = "SELECT field_id FROM adv_mdf_definition ".
206  "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer')." ".
207  "ORDER BY position ";
208  $res = $ilDB->query($query);
209  $defs = array();
210  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
211  {
212  $defs[] = self::_getInstanceByFieldId($row->field_id);
213  }
214  return $defs ? $defs : array();
215  }
216 
225  public static function _getActiveDefinitionsByObjType($a_type)
226  {
227  global $ilDB;
228 
229  $query = "SELECT field_id FROM adv_md_record_objs aro ".
230  "JOIN adv_md_record amr ON aro.record_id = amr.record_id ".
231  "JOIN adv_mdf_definition amf ON aro.record_id = amf.record_id ".
232  "WHERE active = 1 ".
233  "AND obj_type = ".$ilDB->quote($a_type,'text')." ".
234  "ORDER BY aro.record_id,position ";
235  $res = $ilDB->query($query);
236  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
237  {
238  $field_ids[] = $row->field_id;
239  }
240  return $field_ids ? $field_ids : array();
241  }
242 
249  public static function _getSearchableDefinitionIds()
250  {
251  global $ilDB;
252 
253  $query = "SELECT field_id FROM adv_md_record amr ".
254  "JOIN adv_mdf_definition amfd ON amr.record_id = amfd.record_id ".
255  "WHERE active = 1 ".
256  "AND searchable = 1";
257  $res = $ilDB->query($query);
258  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
259  {
260  $field_ids[] = $row->field_id;
261  }
262  return $field_ids ? $field_ids : array();
263  }
264 
265 
274  public static function _deleteByRecordId($a_record_id)
275  {
276  global $ilDB;
277 
278  $query = "SELECT field_id FROM adv_mdf_definition ".
279  "WHERE record_id = ".$ilDB->quote($a_record_id ,'integer');
280  $res = $ilDB->query($query);
281  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
282  {
283  // Delete values
284  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
286  }
287 
288  // Delete definitions
289  $query = "DELETE FROM adv_mdf_definition ".
290  "WHERE record_id = ".$ilDB->quote($a_record_id,'integer')." ";
291  $res = $ilDB->manipulate($query);
292  }
293 
301  public function isDeleted()
302  {
303  return $this->record_id ? false : true;
304  }
305 
306  // Setter, Getter...
314  public function setRecordId($a_id)
315  {
316  $this->record_id = $a_id;
317  }
318 
324  public function getRecordId()
325  {
326  return $this->record_id;
327  }
328 
336  public function getFieldId()
337  {
338  return $this->field_id;
339  }
340 
348  public function setImportId($a_id_string)
349  {
350  $this->import_id = $a_id_string;
351  }
352 
359  public function getImportId()
360  {
361  return $this->import_id;
362  }
363 
371  public function setPosition($a_pos)
372  {
373  $this->position = $a_pos;
374  }
375 
383  public function getPosition()
384  {
385  return $this->position;
386  }
387 
395  public function setFieldType($a_type_id)
396  {
397  $this->field_type = $a_type_id;
398  }
399 
406  public function getFieldType()
407  {
408  return $this->field_type;
409  }
410 
418  public function setFieldValues($a_values)
419  {
420  $this->field_values = $a_values;
421  }
422 
430  public function appendFieldValue($a_value)
431  {
432  if(strlen(trim($a_value)))
433  {
434  $this->field_values[] = trim($a_value);
435  }
436  }
437 
444  public function getFieldValues()
445  {
446  return $this->field_values;
447  }
448 
455  public function getFieldValuesForSelect()
456  {
457  global $lng;
458 
459  $values = array(0 => $lng->txt('select_one'));
460  foreach($this->field_values as $value)
461  {
462  $values[$value] = $value;
463  }
464  return $values;
465  }
466 
473  public function getFieldValuesForSearch()
474  {
475  global $lng;
476 
477  $values = array(0 => $lng->txt('search_any'));
478  foreach($this->field_values as $value)
479  {
480  $values[$value] = $value;
481  }
482  return $values;
483  }
484 
492  public function setTitle($a_title)
493  {
494  $this->title = $a_title;
495  }
496 
502  public function getTitle()
503  {
504  return $this->title;
505  }
506 
514  public function setDescription($a_desc)
515  {
516  $this->description = $a_desc;
517  }
518 
524  public function getDescription()
525  {
526  return $this->description;
527  }
528 
536  public function enableSearchable($a_status)
537  {
538  $this->searchable = (bool) $a_status;
539  }
540 
547  public function isSearchable()
548  {
549  return (bool) $this->searchable;
550  }
551 
558  public function isRequired()
559  {
560  return $this->required;
561  }
562 
568  public function delete()
569  {
570  global $ilDB;
571 
572  $query = "DELETE FROM adv_mdf_definition ".
573  "WHERE field_id = ".$this->db->quote($this->getFieldId() ,'integer')." ";
574  $res = $ilDB->manipulate($query);
575 
576  // Also delete all values
577  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
579  return true;
580  }
581 
587  public function add()
588  {
589  global $ilDB;
590 
591  sort($values = $this->getFieldValues(),SORT_STRING);
592 
593  $position = $this->getLastPosition();
594  $next_id = $ilDB->nextId('adv_mdf_definition');
595 
596  $query = "INSERT INTO adv_mdf_definition (field_id,record_id,import_id,position,field_type, ".
597  "field_values,title,description,searchable,required) ".
598  "VALUES( ".
599  $ilDB->quote($next_id,'integer').",".
600  $this->db->quote($this->getRecordId(),'integer').", ".
601  $this->db->quote($this->getImportId(),'text').", ".
602  $this->db->quote($position + 1 ,'integer').", ".
603  $this->db->quote($this->getFieldType() ,'integer').", ".
604  $ilDB->quote(serialize($values),'text').", ".
605  $this->db->quote($this->getTitle() ,'text').", ".
606  $this->db->quote($this->getDescription() ,'text').", ".
607  $ilDB->quote($this->isSearchable(),'integer').", ".
608  $ilDB->quote($this->isRequired(),'integer')." ".
609  ")";
610  $res = $ilDB->manipulate($query);
611  $this->field_id = $next_id;
612 
613  if(!strlen($this->getImportId()))
614  {
615  $query = "UPDATE adv_mdf_definition ".
616  "SET import_id = ".$this->db->quote($this->generateImportId(),'text')." ".
617  "WHERE field_id = ".$this->db->quote($this->field_id,'integer')." ";
618  $res = $ilDB->manipulate($query);
619  }
620  return true;
621  }
622 
629  public function validate()
630  {
631  global $ilErr,$lng;
632 
633  if(!strlen($this->getTitle()) or !$this->getFieldType())
634  {
635  $ilErr->setMessage('fill_out_all_required_fields');
636  return false;
637  }
638  return true;
639  }
640 
647  public function update()
648  {
649  global $ilDB;
650 
651  $query = "UPDATE adv_mdf_definition ".
652  "SET record_id = ".$this->db->quote($this->getRecordId() ,'integer').", ".
653  "import_id = ".$this->db->quote($this->getImportId() ,'text').", ".
654  "position = ".$this->db->quote($this->getPosition() ,'integer').", ".
655  "field_type = ".$this->db->quote($this->getFieldType() ,'integer').", ".
656  "field_values = ".$ilDB->quote(serialize($this->getFieldValues()),'text').", ".
657  "title = ".$this->db->quote($this->getTitle() ,'text').", ".
658  "description = ".$this->db->quote($this->getDescription() ,'text').", ".
659  "searchable = ".$ilDB->quote($this->isSearchable() ,'integer').", ".
660  "required = ".$ilDB->quote($this->isRequired(),'integer')." ".
661  "WHERE field_id = ".$this->db->quote($this->getFieldId() ,'integer')." ";
662  $res = $ilDB->manipulate($query);
663  return true;
664  }
665 
675  public function toXML(ilXmlWriter $writer)
676  {
677  switch($this->getFieldType())
678  {
679  case self::TYPE_TEXT:
680  $type = 'Text';
681  break;
682 
683  case self::TYPE_SELECT:
684  $type = 'Select';
685  break;
686 
687  case self::TYPE_DATE:
688  $type = 'Date';
689  break;
690 
691  case self::TYPE_DATETIME:
692  $type = 'DateTime';
693  break;
694  }
695 
696 
697  $writer->xmlStartTag('Field',array(
698  'id' => $this->generateImportId(),
699  'searchable' => ($this->isSearchable() ? 'Yes' : 'No'),
700  'fieldType' => $type));
701 
702  $writer->xmlElement('FieldTitle',null,$this->getTitle());
703  $writer->xmlElement('FieldDescription',null,$this->getDescription());
704  $writer->xmlElement('FieldPosition',null,$this->getPosition());
705 
706  foreach($this->getFieldValues() as $value)
707  {
708  if(strlen($value))
709  {
710  $writer->xmlElement('FieldValue',null,$value);
711  }
712  }
713 
714  $writer->xmlEndTag('Field');
715  }
716 
717 
724  private function read()
725  {
726  global $ilDB;
727 
728  if(!$this->field_id)
729  {
730  return false;
731  }
732  $query = "SELECT * FROM adv_mdf_definition ".
733  "WHERE field_id = ".$this->db->quote($this->getFieldId() ,'integer')." ";
734  $res = $this->db->query($query);
735  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
736  {
737  $this->record_id = $row->record_id;
738  $this->import_id = $row->import_id;
739  $this->position = $row->position;
740  $this->field_type = $row->field_type;
741  $this->field_values = unserialize(stripslashes($row->field_values));
742  $this->title = $row->title;
743  $this->description = $row->description;
744  $this->searchable = $row->searchable;
745  $this->required = $row->required;
746  }
747  }
748 
755  private function getLastPosition()
756  {
757  $query = "SELECT max(position) pos FROM adv_mdf_definition ".
758  "WHERE record_id = ".$this->db->quote($this->getRecordId() ,'integer')." ";
759  $res = $this->db->query($query);
760  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
761  {
762  return $row->pos;
763  }
764  return 0;
765  }
766 
773  protected function generateImportId()
774  {
775  return 'il_'.IL_INST_ID.'_adv_md_field_'.$this->getFieldId();
776  }
777 
778 }
779 ?>