• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Data Structures
  • Files
  • File List
  • Globals

Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php

Go to the documentation of this file.
00001 <?php
00002 /*
00003         +-----------------------------------------------------------------------------+
00004         | ILIAS open source                                                           |
00005         +-----------------------------------------------------------------------------+
00006         | Copyright (c) 1998-2006 ILIAS open source, University of Cologne            |
00007         |                                                                             |
00008         | This program is free software; you can redistribute it and/or               |
00009         | modify it under the terms of the GNU General Public License                 |
00010         | as published by the Free Software Foundation; either version 2              |
00011         | of the License, or (at your option) any later version.                      |
00012         |                                                                             |
00013         | This program is distributed in the hope that it will be useful,             |
00014         | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
00015         | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
00016         | GNU General Public License for more details.                                |
00017         |                                                                             |
00018         | You should have received a copy of the GNU General Public License           |
00019         | along with this program; if not, write to the Free Software                 |
00020         | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. |
00021         +-----------------------------------------------------------------------------+
00022 */
00023 
00031 class ilAdvancedMDFieldDefinition
00032 {
00033         const TYPE_SELECT = 1;
00034         const TYPE_TEXT = 2;
00035         const TYPE_DATE = 3;
00036 
00037         private static $instances = array();
00038         
00039         protected $db = null;
00040         
00041         protected $record_id;
00042         protected $field_id;
00043         protected $import_id;
00044         protected $position;
00045         protected $field_type;
00046         protected $field_values = array();
00047         protected $title;
00048         protected $description;
00049         protected $searchable;
00050         protected $required = false;
00051         
00052         
00058         public function __construct($a_field_id = 0)
00059         {
00060                 global $ilDB;
00061                 
00062                 $this->db = $ilDB;
00063                 
00064                 $this->field_id = $a_field_id;
00065                 $this->read();
00066         }
00067         
00076         public static function _lookupImportId($a_field_id)
00077         {
00078                 global $ilDB;
00079                 
00080                 $query = "SELECT import_id FROM adv_md_field_definition ".
00081                         "WHERE field_id = ".$ilDB->quote($a_field_id)." ";
00082                 $res = $ilDB->query($query);
00083                 $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
00084                 return $row['import_id'] ? $row['import_id'] : '';
00085         }
00086         
00095         public static function _lookupFieldId($a_import_id)
00096         {
00097                 global $ilDB;
00098                 
00099                 $query = "SELECT field_id FROM adv_md_field_definition ".
00100                         "WHERE import_id = ".$ilDB->quote($a_import_id)." ";
00101                 $res = $ilDB->query($query);
00102                 $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
00103                 return $row['field_id'] ? $row['field_id'] : 0;
00104         }
00105         
00113         public static function _lookupDateFields()
00114         {
00115                 global $ilDB;
00116                 
00117                 $query = "SELECT field_id FROM adv_md_field_definition ".
00118                         "WHERE field_type = ".self::TYPE_DATE." ";
00119                 $res = $ilDB->query($query);
00120                 
00121                 $date_fields = array();
00122                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00123                 {
00124                         $date_fields[] = $row->field_id; 
00125                 }
00126                 return $date_fields;
00127         }
00128         
00137         public static function _getInstanceByFieldId($a_field_id)
00138         {
00139                 if(isset(self::$instances[$a_field_id]))
00140                 {
00141                         return self::$instances[$a_field_id];
00142                 }
00143                 return self::$instances[$a_field_id] = new ilAdvancedMDFieldDefinition($a_field_id);
00144         }
00145         
00146 
00156         public static function _getDefinitionsByRecordId($a_record_id)
00157         {
00158                 global $ilDB;
00159                 
00160                 $query = "SELECT field_id FROM adv_md_field_definition ".
00161                         "WHERE record_id = ".$ilDB->quote($a_record_id)." ".
00162                         "ORDER BY position ";
00163                 $res = $ilDB->query($query);
00164                 $defs = array();
00165                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00166                 {
00167                         $defs[] = self::_getInstanceByFieldId($row->field_id);
00168                 }
00169                 return $defs ? $defs : array(); 
00170         }
00171         
00180         public static function _getActiveDefinitionsByObjType($a_type)
00181         {
00182                 global $ilDB;
00183                 
00184                 $query = "SELECT field_id FROM adv_md_record_objs AS aro ".
00185                         "JOIN adv_md_record AS amr ON aro.record_id = amr.record_id ".
00186                         "JOIN adv_md_field_definition AS amf ON aro.record_id = amf.record_id ".
00187                         "WHERE active = 1 ".
00188                         "AND obj_type = ".$ilDB->quote($a_type)." ".
00189                         "ORDER BY aro.record_id,position ";
00190                 $res = $ilDB->query($query);
00191                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00192                 {
00193                         $field_ids[] = $row->field_id;
00194                 }
00195                 return $field_ids ? $field_ids : array();
00196         }
00197         
00204         public static function _getSearchableDefinitionIds()
00205         {
00206                 global $ilDB;
00207                 
00208                 $query = "SELECT field_id FROM adv_md_record AS amr ".
00209                         "JOIN adv_md_field_definition AS amfd USING (record_id) ".
00210                         "WHERE active = 1 ".
00211                         "AND searchable = 1";
00212                 $res = $ilDB->query($query);
00213                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00214                 {
00215                         $field_ids[] = $row->field_id;
00216                 }
00217                 return $field_ids ? $field_ids : array();
00218         }
00219         
00220         
00229         public static function _deleteByRecordId($a_record_id)
00230         {
00231                 global $ilDB;
00232                 
00233                 $query = "SELECT field_id FROM adv_md_field_definition ".
00234                         "WHERE record_id = ".$ilDB->quote($a_record_id);
00235                 $res = $ilDB->query($query);    
00236                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00237                 {
00238                         // Delete values
00239                         include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
00240                         ilAdvancedMDValues::_deleteByFieldId($row->field_id);
00241                 }               
00242 
00243                 // Delete definitions
00244                 $query = "DELETE FROM adv_md_field_definition ".
00245                         "WHERE record_id  = ".$ilDB->quote($a_record_id)." ";
00246                 $res = $ilDB->query($query);    
00247         }
00248 
00249         // Setter, Getter...
00257         public function setRecordId($a_id)
00258         {
00259                 $this->record_id = $a_id;
00260         }
00261         
00267         public function getRecordId()
00268         {
00269                 return $this->record_id;
00270         }
00271         
00279         public function getFieldId()
00280         {
00281                 return $this->field_id;
00282         }
00283         
00291         public function setImportId($a_id_string)
00292         {
00293                 $this->import_id = $a_id_string;
00294         }
00295         
00302         public function getImportId()
00303         {
00304                 return $this->import_id;
00305         }
00306         
00314         public function setPosition($a_pos)
00315         {
00316                 $this->position = $a_pos;
00317         }
00318         
00326         public function getPosition()
00327         {
00328                 return $this->position;
00329         }
00330         
00338         public function setFieldType($a_type_id)
00339         {
00340                 $this->field_type = $a_type_id;
00341         }
00342         
00349         public function getFieldType()
00350         {
00351                 return $this->field_type;
00352         }
00353         
00361         public function setFieldValues($a_values)
00362         {
00363                 $this->field_values = $a_values;
00364         }
00365         
00373         public function appendFieldValue($a_value)
00374         {
00375                 if(strlen(trim($a_value)))
00376                 {
00377                         $this->field_values[] = trim($a_value);
00378                 }
00379         }
00380         
00387         public function getFieldValues()
00388         {
00389                 return $this->field_values;
00390         }
00391         
00398         public function getFieldValuesForSelect()
00399         {
00400                 global $lng;
00401                 
00402                 $values = array(0 => $lng->txt('select_one'));
00403                 foreach($this->field_values as $value)
00404                 {
00405                         $values[$value] = $value;
00406                 }
00407                 return $values;
00408         }
00409         
00417         public function setTitle($a_title)
00418         {
00419                 $this->title = $a_title;
00420         }
00421         
00427         public function getTitle()
00428         {
00429                 return $this->title;
00430         }
00431         
00439         public function setDescription($a_desc)
00440         {
00441                 $this->description = $a_desc;
00442         }
00443         
00449         public function getDescription()
00450         {
00451                 return $this->description;
00452         }
00453         
00461         public function enableSearchable($a_status)
00462         {
00463                 $this->searchable = (bool) $a_status;
00464         }
00465         
00472         public function isSearchable()
00473         {
00474                 return (bool) $this->searchable;
00475         }
00476         
00483         public function isRequired()
00484         {
00485                 return $this->required;
00486         }
00487         
00493         public function delete()
00494         {
00495                 $query = "DELETE FROM adv_md_field_definition ".
00496                         "WHERE field_id = ".$this->db->quote($this->getFieldId())." ";
00497                 $res = $this->db->query($query);
00498                 
00499                 // Also delete all values
00500                 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
00501                 ilAdvancedMDValues::_deleteByFieldId($this->getFieldId());
00502                 return true;    
00503         }
00504         
00510         public function add()
00511         {
00512                 sort($values = $this->getFieldValues(),SORT_STRING);
00513                 
00514                 $position = $this->getLastPosition();
00515                 
00516                 $query = "INSERT INTO adv_md_field_definition ".
00517                         "SET record_id = ".$this->db->quote($this->getRecordId()).", ".
00518                         "import_id = ".$this->db->quote($this->getImportId()).", ".
00519                         "position = ".$this->db->quote($position + 1).", ".
00520                         "field_type = ".$this->db->quote($this->getFieldType()).", ".
00521                         "field_values = '".addslashes(serialize($values))."', ".
00522                         "title = ".$this->db->quote($this->getTitle()).", ".
00523                         "description = ".$this->db->quote($this->getDescription()).", ".
00524                         "searchable = ".(int) $this->isSearchable().", ".
00525                         "required = ".(int) $this->isRequired();
00526                 $this->db->query($query);
00527                 $this->field_id = $this->db->getLastInsertId();
00528                 
00529                 if(!strlen($this->getImportId()))
00530                 {
00531                         $query = "UPDATE adv_md_field_definition ".
00532                                 "SET import_id = ".$this->db->quote($this->generateImportId())." ".
00533                                 "WHERE field_id = ".$this->db->quote($this->field_id)." ";
00534                         $this->db->query($query);
00535                 }
00536                 return true;
00537         }
00538         
00545         public function validate()
00546         {
00547                 global $ilErr,$lng;
00548                 
00549                 if(!strlen($this->getTitle()) or !$this->getFieldType())
00550                 {
00551                         $ilErr->setMessage('fill_out_all_required_fields');
00552                         return false;
00553                 }
00554                 return true;
00555         }
00556         
00563         public function update()
00564         {
00565                 $query = "UPDATE adv_md_field_definition ".
00566                         "SET record_id = ".$this->db->quote($this->getRecordId()).", ".
00567                         "import_id = ".$this->db->quote($this->getImportId()).", ".
00568                         "position = ".$this->db->quote($this->getPosition()).", ".
00569                         "field_type = ".$this->db->quote($this->getFieldType()).", ".
00570                         "field_values = '".addslashes(serialize($this->getFieldValues()))."', ".
00571                         "title = ".$this->db->quote($this->getTitle()).", ".
00572                         "description = ".$this->db->quote($this->getDescription()).", ".
00573                         "searchable = ".(int) $this->isSearchable().", ".
00574                         "required = ".(int) $this->isRequired()." ".
00575                         "WHERE field_id = ".$this->db->quote($this->getFieldId())." ";
00576                                                 
00577                 $this->db->query($query);
00578                 return true;
00579         }
00580         
00590          public function toXML(ilXmlWriter $writer)
00591          {
00592                 switch($this->getFieldType())
00593                 {
00594                         case self::TYPE_TEXT:
00595                                 $type = 'Text';
00596                                 break;
00597                                 
00598                         case self::TYPE_SELECT:
00599                                 $type = 'Select';
00600                                 break;
00601                                 
00602                         case self::TYPE_DATE:
00603                                 $type = 'Date';
00604                                 break;
00605                 }
00606                 
00607                 
00608                 $writer->xmlStartTag('Field',array(
00609                         'id' => $this->generateImportId(),
00610                         'searchable' => ($this->isSearchable() ? 'Yes' : 'No'),
00611                         'fieldType'      => $type));
00612                 
00613                 $writer->xmlElement('FieldTitle',null,$this->getTitle());
00614                 $writer->xmlElement('FieldDescription',null,$this->getDescription());
00615                 $writer->xmlElement('FieldPosition',null,$this->getPosition());
00616                 
00617                 foreach($this->getFieldValues() as $value)
00618                 {
00619                         if(strlen($value))
00620                         {
00621                                 $writer->xmlElement('FieldValue',null,$value);
00622                         }
00623                 }
00624                 
00625                 $writer->xmlEndTag('Field');
00626          }
00627         
00628         
00635         private function read()
00636         {
00637                 if(!$this->field_id)
00638                 {
00639                         return false;
00640                 }
00641                 $query = "SELECT * FROM adv_md_field_definition ".
00642                         "WHERE field_id = ".$this->db->quote($this->getFieldId())." ";
00643                 $res = $this->db->query($query);
00644                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00645                 {
00646                         $this->record_id = $row->record_id;
00647                         $this->import_id = $row->import_id;
00648                         $this->position = $row->position;
00649                         $this->field_type = $row->field_type;
00650                         $this->field_values = unserialize(stripslashes($row->field_values));
00651                         $this->title = $row->title;
00652                         $this->description = $row->description;
00653                         $this->searchable = $row->searchable;
00654                         $this->required = $row->required;
00655                 }
00656         }
00657         
00664         private function getLastPosition()
00665         {
00666                 $query = "SELECT max(position) as pos FROM adv_md_field_definition ".
00667                         "WHERE record_id = ".$this->db->quote($this->getRecordId())." ";
00668                 $res = $this->db->query($query);
00669                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00670                 {
00671                         return $row->pos;
00672                 }
00673                 return 0;
00674         }
00675         
00682         protected function generateImportId()
00683         {
00684                 return 'il_'.IL_INST_ID.'_adv_md_field_'.$this->getFieldId();
00685         }
00686         
00687 }
00688 ?>

Generated on Fri Dec 13 2013 17:56:55 for ILIAS Release_3_9_x_branch .rev 46835 by  doxygen 1.7.1