ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDValue.php
Go to the documentation of this file.
1 <?php
2 
3 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
4 
15 {
16  private static $instances = array();
17 
18  protected $db;
19 
20  private $obj_id;
21  private $field_id;
22  private $value;
23  private $disabled = false;
24 
33  public function __construct($a_field_id,$a_obj_id = 0, $a_sub_type = "", $a_sub_id = 0)
34  {
35  global $ilDB;
36 
37  $this->db = $ilDB;
38 
39  $this->obj_id = $a_obj_id;
40  $this->setSubType($a_sub_type);
41  $this->setSubId($a_sub_id);
42  $this->field_id = $a_field_id;
43 
44  $this->read();
45  }
46 
55  public static function _getInstance($a_obj_id,$a_field_id, $a_sub_type = "", $a_sub_id = 0)
56  {
57  if ($a_sub_type == "")
58  {
59  $a_sub_type = "-";
60  }
61  if(isset(self::$instances[$a_obj_id][$a_field_id][$a_sub_type][$a_sub_id]))
62  {
63  return self::$instances[$a_obj_id][$a_field_id][$a_sub_type][$a_sub_id];
64  }
65  return self::$instances[$a_obj_id][$a_field_id][$a_sub_type][$a_sub_id] = new ilAdvancedMDValue($a_field_id,$a_obj_id, $a_sub_type, $a_sub_id);
66  }
67 
74  public function __toString()
75  {
76  return $this->value;
77  }
78 
86  public function setObjId($a_obj_id)
87  {
88  $this->obj_id = $a_obj_id;
89  }
90 
96  function setSubType($a_val)
97  {
98  if ($a_val == "")
99  {
100  $a_val = "-";
101  }
102  $this->sub_type = $a_val;
103  }
104 
110  function getSubType()
111  {
112  return $this->sub_type;
113  }
114 
120  function setSubId($a_val)
121  {
122  $this->sub_id = (int) $a_val;
123  }
124 
130  function getSubId()
131  {
132  return $this->sub_id;
133  }
134 
142  public function appendXML($xml_writer)
143  {
144  include_once('Services/AdvancedMetaData/classes/class.ilAdvancedMDFieldDefinition.php');
145 
146  $xml_writer->xmlElement('Value',
147  array('id' => ilAdvancedMDFieldDefinition::_lookupImportId($this->field_id)),
148  $this->getValue());
149  }
150 
151 
159  public function setValue($a_value)
160  {
161  $this->value = $a_value;
162  }
163 
169  public function getValue()
170  {
171  return $this->value;
172  }
173 
182  public function isDisabled()
183  {
184  return (bool) $this->disabled;
185  }
186 
194  public function toggleDisabledStatus($a_status)
195  {
196  $this->disabled = (bool) $a_status;
197  }
198 
205  public function delete()
206  {
207  global $ilDB;
208 
209  $query = "DELETE FROM adv_md_values ".
210  "WHERE obj_id = ".$this->db->quote($this->obj_id ,'integer')." ".
211  "AND sub_type = ".$this->db->quote($this->getSubType() ,'text')." ".
212  "AND sub_id = ".$this->db->quote($this->getSubId() ,'integer')." ".
213  "AND field_id = ".$this->db->quote($this->field_id ,'integer');
214  $res = $ilDB->manipulate($query);
215  }
216 
223  public function save()
224  {
225  global $ilDB;
226 
227  $this->delete();
228 
229  $query = "INSERT INTO adv_md_values (obj_id,field_id,sub_type, sub_id, value,disabled) ".
230  "VALUES( ".
231  $this->db->quote($this->obj_id ,'integer').", ".
232  $this->db->quote($this->field_id ,'integer').", ".
233  $this->db->quote($this->getSubType() ,'text').", ".
234  $this->db->quote($this->getSubid() ,'integer').", ".
235  $this->db->quote($this->getValue() ,'text').", ".
236  $ilDB->quote($this->isDisabled(),'integer')." ".
237  ")";
238  $res = $ilDB->manipulate($query);
239  }
240 
246  private function read()
247  {
248  global $ilDB;
249 
250  if(!$this->obj_id or !$this->field_id)
251  {
252  return;
253  }
254 
255  $query = "SELECT * FROM adv_md_values ".
256  "WHERE obj_id = ".$this->db->quote($this->obj_id ,'integer')." ".
257  "AND sub_type = ".$this->db->quote($this->getSubType() ,'text')." ".
258  "AND sub_id = ".$this->db->quote($this->getSubId() ,'integer')." ".
259  "AND field_id = ".$this->db->quote($this->field_id ,'integer')." ";
260  $res = $this->db->query($query);
261  while($row = $res->fetchRow(DB_FETCHMODE_OBJECT))
262  {
263  $this->setValue($row->value);
264  $this->toggleDisabledStatus((bool) $row->disabled);
265  }
266  return true;
267  }
268 }
269 ?>