ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.ilAdvancedMDFieldDefinitionSelectMulti.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2013 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once "Services/AdvancedMetaData/classes/Types/class.ilAdvancedMDFieldDefinitionSelect.php";
5 
15 {
16  const XML_SEPARATOR = "~|~";
17 
18  //
19  // generic types
20  //
21 
22  public function getType()
23  {
25  }
26 
27 
28  //
29  // ADT
30  //
31 
32  protected function initADTDefinition()
33  {
34  $def = ilADTFactory::getInstance()->getDefinitionInstanceByType("MultiEnum");
35  $def->setNumeric(false);
36 
37  $options = $this->getOptions();
38  $def->setOptions(array_combine($options, $options));
39 
40  // see ilAdvancedMDValues::getActiveRecord()
41  // using ilADTMultiEnumDBBridge::setFakeSingle()
42 
43  return $def;
44  }
45 
46 
47  //
48  // definition (NOT ADT-based)
49  //
50 
52  {
53  $old = $this->getOptions();
54  $new = $a_form->getInput("opts");
55 
56  $missing = array_diff($old, $new);
57  if(sizeof($missing))
58  {
59  $this->confirmed_objects = $this->buildConfirmedObjects($a_form);
60  if(!is_array($this->confirmed_objects))
61  {
62  $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($this->getADTDefinition(), false, false);
64 
65  foreach($missing as $missing_value)
66  {
67  $in_use = $this->findBySingleValue($search, $missing_value);
68  if(sizeof($in_use))
69  {
70  foreach($in_use as $item)
71  {
72  $this->confirm_objects[$missing_value][] = $item;
73  }
74  }
75  }
76  }
77  }
78 
79  $this->old_options = $old;
80  $this->setOptions($new);
81  }
82 
83  protected function findBySingleValue(ilADTEnumSearchBridgeMulti $a_search, $a_value)
84  {
85  $res = array();
86 
87  $a_search->getADT()->setSelections(array($a_value));
89 
91  "adv_md_values",
92  "Enum",
93  $this->getFieldId(),
94  $condition
95  );
96  if($in_use)
97  {
98  foreach($in_use as $item)
99  {
100  $res[] = array($item["obj_id"], $item["sub_type"], $item["sub_id"], $item["value"]);
101  }
102  }
103 
104  return $res;
105  }
106 
107 
108  //
109  // definition CRUD
110  //
111 
112  public function update()
113  {
114  if(sizeof($this->confirmed_objects))
115  {
116  // we need the "old" options for the search
117  $def = $this->getADTDefinition();
118  $def = clone($def);
119  $def->setOptions(array_combine($this->old_options, $this->old_options));
120  $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($def, false, false);
122 
123  foreach($this->confirmed_objects as $old_option => $item_ids)
124  {
125  // get complete old values
126  $old_values = array();
127  foreach($this->findBySingleValue($search, $old_option) as $item)
128  {
129  $old_values[$item[0]."_".$item[1]."_".$item[2]] = $item[3];
130  }
131 
132  foreach($item_ids as $item => $new_option)
133  {
134  $parts = explode("_", $item);
135  $obj_id = $parts[0];
136  $sub_type = $parts[1];
137  $sub_id = $parts[2];
138 
139  if(!$new_option)
140  {
141  // remove existing value
142  $primary = array(
143  "obj_id" => array("integer", $obj_id),
144  "sub_type" => array("text", $sub_type),
145  "sub_id" => array("integer", $sub_id),
146  "field_id" => array("integer", $this->getFieldId())
147  );
148  ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $primary, "MultiEnum");
149  }
150  else
151  {
152  // update existing value (with changed option)
153  if(isset($old_values[$item]))
154  {
155  // find changed option in old value
156  $old_value = explode(ilADTMultiEnumDBBridge::SEPARATOR, $old_values[$item]);
157  // remove separators
158  array_shift($old_value);
159  array_pop($old_value);
160  $old_idx = array_keys($old_value, $old_option);
161  if(sizeof($old_idx))
162  {
163  // switch option
164  $old_idx = array_pop($old_idx);
165  $old_value[$old_idx] = $new_option;
166  $new_value = array_unique($old_value);
167  // add separators
169  implode(ilADTMultiEnumDBBridge::SEPARATOR, $new_value).
171 
172  $primary = array(
173  "obj_id" => array("integer", $obj_id),
174  "sub_type" => array("text", $sub_type),
175  "sub_id" => array("integer", $sub_id),
176  "field_id" => array("integer", $this->getFieldId())
177  );
178  ilADTActiveRecordByType::writeByPrimary("adv_md_values", $primary, "MultiEnum", $new_value);
179  }
180  }
181  }
182 
183  if($sub_type == "wpg")
184  {
185  // #15763 - adapt advmd page lists
186  include_once "Modules/Wiki/classes/class.ilPCAMDPageList.php";
187  ilPCAMDPageList::migrateField($obj_id, $this->getFieldId(), $old_option, $new_option, true);
188  }
189  }
190  }
191 
192  $this->confirmed_objects = array();
193  }
194 
195  parent::update();
196  }
197 
198 
199  //
200  // import/export
201  //
202 
203  public function getValueForXML(ilADT $element)
204  {
205  return self::XML_SEPARATOR.
206  implode(self::XML_SEPARATOR, $element->getSelections()).
207  self::XML_SEPARATOR;
208  }
209 
210  public function importValueFromXML($a_cdata)
211  {
212  $this->getADT()->setSelections(explode(self::XML_SEPARATOR, $a_cdata));
213  }
214 }