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

Services/AdvancedMetaData/classes/class.ilAdvancedMDSubstitution.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 
00032 class ilAdvancedMDSubstitution
00033 {
00034         private static $instances = null; 
00035 
00036         protected $db;
00037         
00038         protected $type;
00039         protected $substitutions;
00040         protected $bold = array();
00041         protected $newline = array();
00042         
00043         protected $enabled_desc = true;
00044         protected $enabled_field_names = true;
00045         protected $active = false;
00046         protected $date_fields = array();
00047         protected $active_fields = array();
00048         
00049         
00050         /*
00051          * Singleton class
00052          * Use _getInstance
00053          * @access private
00054          * @param
00055          */
00056         private function __construct($a_type)
00057         {
00058                 global $ilDB;
00059                 
00060                 $this->db = $ilDB;
00061                 $this->type = $a_type;
00062                 
00063                 $this->read();  
00064         }
00065         
00074         public static function _getInstanceByObjectType($a_type)
00075         {
00076                 if(isset(self::$instances[$a_type]))
00077                 {
00078                         return self::$instances[$a_type];
00079                 }
00080                 return self::$instances[$a_type] = new ilAdvancedMDSubstitution($a_type);
00081         }
00082         
00090         public function sortDefinitions($a_definitions)
00091         {
00092                 $sorted = array();
00093                 foreach($this->substitutions as $field_id)
00094                 {
00095                         $sorted[] = $field_id;
00096                         $key = array_search($field_id,$a_definitions);
00097                         unset($a_definitions[$key]);
00098                 }
00099                 return array_merge($sorted,$a_definitions);
00100         }
00101         
00108         public function isActive()
00109         {
00110                 return $this->active;
00111         }
00112         
00119         public function isDescriptionEnabled()
00120         {
00121                 return (bool) $this->enabled_desc;
00122         }
00123         
00131         public function enableDescription($a_status)
00132         {
00133                 $this->enabled_desc = $a_status;
00134         }
00135         
00142         public function enabledFieldNames()
00143         {
00144                 return (bool) $this->hide_field_names;
00145         }
00146         
00154         public function enableFieldNames($a_status)
00155         {
00156                 $this->hide_field_names = $a_status;
00157         }
00158         
00168         public function getParsedSubstitutions($a_ref_id,$a_obj_id)
00169         {
00170                 if(!count($this->getSubstitutions()))
00171                 {
00172                         return array();
00173                 }
00174                 
00175                 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
00176                 $values = ilAdvancedMDValues::_getValuesByObjId($a_obj_id);
00177                                 
00178                 $counter = 0;
00179                 foreach($this->getSubstitutions() as $field_id)
00180                 {
00181                         if(!isset($this->active_fields[$field_id]))
00182                         {
00183                                 continue;
00184                         }
00185                         if(!isset($values[$field_id]) or !$values[$field_id])
00186                         {
00187                                 if($this->hasNewline($field_id) and $counter)
00188                                 {
00189                                         $substituted[$counter-1]['newline'] = true;
00190                                 }
00191                                 continue;
00192                         }
00193                         
00194                         if(in_array($field_id,$this->date_fields))
00195                         {
00196                                 $value = ilFormat::formatUnixTime((int) $values[$field_id]);
00197                         }
00198                         else
00199                         {
00200                                 $value = $values[$field_id];
00201                         }
00202         
00203                         $substituted[$counter]['name'] = $this->active_fields[$field_id];
00204                         $substituted[$counter]['value'] = $value;
00205                         $substituted[$counter]['bold'] = $this->isBold($field_id);
00206                         if($this->hasNewline($field_id))
00207                         {
00208                                 $substituted[$counter]['newline'] = true;
00209                         }
00210                         else
00211                         {
00212                                 $substituted[$counter]['newline'] = false;
00213                         }
00214                         $counter++;
00215                 }
00216                 
00217                 return $substituted ? $substituted : array();
00218                 /*
00219                 $string = $this->getSubstitutionString();
00220                 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDValues.php');
00221                 foreach(ilAdvancedMDValues::_getValuesByObjId($a_obj_id) as $field_id => $value)
00222                 {
00223                         if(!in_array($field_id,$this->active_fields))
00224                         {
00225                                 continue;
00226                         }
00227                         
00228                         if(in_array($field_id,$this->date_fields) and $value)
00229                         {
00230                                 $value = ilFormat::formatUnixTime((int) $value);
00231                         }
00232                         
00233                         if($value)
00234                         {
00235                                 // Substitute variables
00236                                 $string = str_replace('[F_'.$field_id.']',$value,$string);
00237                                 // Delete block varaibles
00238                                 $string = preg_replace('/\[\/?IF_F_'.$field_id.'\]/U','',$string);
00239                         }
00240                 }
00241                 // Replace fixed variables
00242                 $string = str_replace('[OBJ_ID]',$a_obj_id,$string);
00243                 
00244                 // Delete all other blocks
00245                 $string = preg_replace('/\[IF_F_\d+\].*\[\/IF_F_\d+\]/U','',$string);
00246                 return $string;
00247                 */
00248         }
00249         
00257         public function resetSubstitutions()
00258         {
00259                 $this->substitutions = array();
00260                 $this->bold = array();
00261                 $this->newline = array();
00262         }
00263         
00271         public function appendSubstitution($a_field_id,$a_bold = false,$a_newline = false)
00272         {
00273                 $this->substitutions[] = $a_field_id;
00274                 if($a_bold)
00275                 {
00276                         $this->bold[] = $a_field_id;
00277                 }
00278                 if($a_newline)
00279                 {
00280                         $this->newline[] = $a_field_id;
00281                 } 
00282         }
00283         
00291         public function getSubstitutions()
00292         {
00293                 return $this->substitutions ? $this->substitutions : array();
00294         }
00295         
00303         public function isSubstituted($a_field_id)
00304         {
00305                 return in_array($a_field_id,$this->getSubstitutions());
00306         }
00307         
00315         public function isBold($a_field_id)
00316         {
00317                 #var_dump("<pre>",$this->bold,$a_field_id,"</pre>");
00318                 
00319                 return in_array($a_field_id,$this->bold);
00320         }
00321         
00329         public function hasNewline($a_field_id)
00330         {
00331                 return in_array($a_field_id,$this->newline);
00332         }
00339         public function update()
00340         {
00341                 $counter = 0;
00342                 $substitutions = array();
00343         
00344                 foreach($this->substitutions as $field_id)
00345                 {
00346                         $substitutions[$counter]['field_id'] = $field_id;
00347                         $substitutions[$counter]['bold'] = $this->isBold($field_id);
00348                         $substitutions[$counter]['newline'] = $this->hasNewline($field_id);
00349                         $counter++;
00350                 }
00351                 
00352                         
00353                 $query = "REPLACE INTO adv_md_substitutions ".
00354                         "SET obj_type = ".$this->db->quote($this->type).", ".
00355                         "substitution = ".$this->db->quote(serialize($substitutions)).", ".
00356                         "hide_description = ".$this->db->quote(!$this->isDescriptionEnabled()).', '.
00357                         "hide_field_names = ".$this->db->quote(!$this->enabledFieldNames());
00358                         
00359                         
00360                 $res = $this->db->query($query);
00361         }
00362         
00369         private function read()
00370         {
00371                 include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
00372                 $this->date_fields = ilAdvancedMDFieldDefinition::_lookupDateFields();
00373                 
00374                 // Check active status
00375                 $query = "SELECT active,field_id,amfd.title FROM adv_md_record AS amr ".
00376                         "JOIN adv_md_record_objs AS amro ON amr.record_id = amro.record_id ".
00377                         "JOIN adv_md_field_definition AS amfd ON amr.record_id = amfd.record_id ".
00378                         "WHERE active = 1 ".
00379                         "AND obj_type = ".$this->db->quote($this->type)." ";
00380                 $res = $this->db->query($query);
00381                 $this->active = $res->numRows() ? true : false;
00382                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00383                 {
00384                         $this->active_fields[$row->field_id] = $row->title;
00385                 }
00386                         
00387                 $query = "SELECT * FROM adv_md_substitutions ".
00388                         "WHERE obj_type = ".$this->db->quote($this->type)." ";
00389                 $res = $this->db->query($query);
00390                 $this->substitutions = array();
00391                 $this->bold = array();
00392                 $this->newline = array();
00393                 while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
00394                 {
00395                         $tmp_substitutions = unserialize($row->substitution);
00396                         if(is_array($tmp_substitutions))
00397                         {
00398                                 foreach($tmp_substitutions as $substitution)
00399                                 {
00400                                         if($substitution['field_id'])
00401                                         {
00402                                                 $this->substitutions[] = $substitution['field_id'];
00403                                         }
00404                                         if($substitution['bold'])
00405                                         {
00406                                                 $this->bold[] = $substitution['field_id'];
00407                                         }
00408                                         if($substitution['newline'])
00409                                         {
00410                                                 $this->newline[] = $substitution['field_id'];
00411                                         }
00412                                         
00413                                 }
00414                         }
00415                         $this->enabled_desc = !$row->hide_description;
00416                         $this->enabled_field_names = !$row->hide_field_names;
00417                 }
00418         }
00419 }
00420 ?>

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