ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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  {
24  return self::TYPE_SELECT_MULTI;
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  // update existing value (with changed option)
140  if(isset($old_values[$item]))
141  {
142  // find changed option in old value
143  $old_value = explode(ilADTMultiEnumDBBridge::SEPARATOR, $old_values[$item]);
144  // remove separators
145  array_shift($old_value);
146  array_pop($old_value);
147 
148  $old_idx = array_keys($old_value, $old_option);
149  if(sizeof($old_idx))
150  {
151  $old_idx = array_pop($old_idx);
152 
153  // switch option
154  if($new_option)
155  {
156  $old_value[$old_idx] = $new_option;
157  }
158  // #18885 - remove option
159  else
160  {
161  unset($old_value[$old_idx]);
162  }
163  $new_value = array_unique($old_value);
164 
165  $primary = array(
166  "obj_id" => array("integer", $obj_id),
167  "sub_type" => array("text", $sub_type),
168  "sub_id" => array("integer", $sub_id),
169  "field_id" => array("integer", $this->getFieldId())
170  );
171 
172  // update value
173  if(sizeof($new_value))
174  {
175  // add separators
177  implode(ilADTMultiEnumDBBridge::SEPARATOR, $new_value).
179 
180  ilADTActiveRecordByType::writeByPrimary("adv_md_values", $primary, "MultiEnum", $new_value);
181  }
182  // remove existing value - nothing left
183  else
184  {
185  ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $primary, "MultiEnum");
186  }
187  }
188  }
189 
190  if($sub_type == "wpg")
191  {
192  // #15763 - adapt advmd page lists
193  include_once "Modules/Wiki/classes/class.ilPCAMDPageList.php";
194  ilPCAMDPageList::migrateField($obj_id, $this->getFieldId(), $old_option, $new_option, true);
195  }
196  }
197  }
198 
199  $this->confirmed_objects = array();
200  }
201 
202  parent::update();
203  }
204 
205 
206  //
207  // import/export
208  //
209 
210  public function getValueForXML(ilADT $element)
211  {
212  return self::XML_SEPARATOR.
213  implode(self::XML_SEPARATOR, $element->getSelections()).
214  self::XML_SEPARATOR;
215  }
216 
217  public function importValueFromXML($a_cdata)
218  {
219  $this->getADT()->setSelections(explode(self::XML_SEPARATOR, $a_cdata));
220  }
221 
222 
223  //
224  // presentation
225  //
226 
227  public function prepareElementForEditor(ilADTFormBridge $a_enum)
228  {
229  assert($a_enum instanceof ilADTMultiEnumFormBridge);
230 
231  $a_enum->setAutoSort(false);
232  }
233 }
findBySingleValue(ilADTEnumSearchBridgeMulti $a_search, $a_value)
This class represents a property form user interface.
getADTDefinition()
Get ADT definition instance.
static initActiveRecordByType()
Init active record by type.
ADT form bridge base class.
static writeByPrimary($a_table, array $a_primary, $a_type, $a_value)
Write directly.
static getInstance()
Get singleton.
ADT base class.
Definition: class.ilADT.php:11
static find($a_table, $a_type, $a_field_id, $a_condition, $a_additional_fields=null)
Find entries.
static deleteByPrimary($a_table, array $a_primary, $a_type=null)
Delete values by (partial) primary key.
static migrateField($a_obj_id, $a_field_id, $old_option, $new_option, $a_is_multi=false)
Migrate search/filter values on advmd change.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
$old
Create styles array
The data for the language used.
buildConfirmedObjects(ilPropertyFormGUI $a_form)
Process custom post values from definition form.