ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  $this->confirmed_objects = $this->buildConfirmedObjects($a_form);
59  if (!is_array($this->confirmed_objects)) {
60  $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($this->getADTDefinition(), false, false);
62 
63  foreach ($missing as $missing_value) {
64  $in_use = $this->findBySingleValue($search, $missing_value);
65  if (sizeof($in_use)) {
66  foreach ($in_use as $item) {
67  $this->confirm_objects[$missing_value][] = $item;
68  }
69  }
70  }
71  }
72  }
73 
74  $this->old_options = $old;
75  $this->setOptions($new);
76  }
77 
78  protected function findBySingleValue(ilADTEnumSearchBridgeMulti $a_search, $a_value)
79  {
80  $res = array();
81 
82  $a_search->getADT()->setSelections(array($a_value));
84 
86  "adv_md_values",
87  "Enum",
88  $this->getFieldId(),
89  $condition
90  );
91  if ($in_use) {
92  foreach ($in_use as $item) {
93  $res[] = array($item["obj_id"], $item["sub_type"], $item["sub_id"], $item["value"]);
94  }
95  }
96 
97  return $res;
98  }
99 
100 
101  //
102  // definition CRUD
103  //
104 
105  public function update()
106  {
107  if (is_array($this->confirmed_objects) && count($this->confirmed_objects) > 0) {
108  // we need the "old" options for the search
109  $def = $this->getADTDefinition();
110  $def = clone($def);
111  $def->setOptions(array_combine($this->old_options, $this->old_options));
112  $search = ilADTFactory::getInstance()->getSearchBridgeForDefinitionInstance($def, false, false);
114 
115  foreach ($this->confirmed_objects as $old_option => $item_ids) {
116  // get complete old values
117  $old_values = array();
118  foreach ($this->findBySingleValue($search, $old_option) as $item) {
119  $old_values[$item[0] . "_" . $item[1] . "_" . $item[2]] = $item[3];
120  }
121 
122  foreach ($item_ids as $item => $new_option) {
123  $parts = explode("_", $item);
124  $obj_id = $parts[0];
125  $sub_type = $parts[1];
126  $sub_id = $parts[2];
127 
128  // update existing value (with changed option)
129  if (isset($old_values[$item])) {
130  // find changed option in old value
131  $old_value = explode(ilADTMultiEnumDBBridge::SEPARATOR, $old_values[$item]);
132  // remove separators
133  array_shift($old_value);
134  array_pop($old_value);
135 
136  $old_idx = array_keys($old_value, $old_option);
137  if (sizeof($old_idx)) {
138  $old_idx = array_pop($old_idx);
139 
140  // switch option
141  if ($new_option) {
142  $old_value[$old_idx] = $new_option;
143  }
144  // #18885 - remove option
145  else {
146  unset($old_value[$old_idx]);
147  }
148  $new_value = array_unique($old_value);
149 
150  $primary = array(
151  "obj_id" => array("integer", $obj_id),
152  "sub_type" => array("text", $sub_type),
153  "sub_id" => array("integer", $sub_id),
154  "field_id" => array("integer", $this->getFieldId())
155  );
156 
157  // update value
158  if (sizeof($new_value)) {
159  // add separators
160  $new_value = ilADTMultiEnumDBBridge::SEPARATOR .
161  implode(ilADTMultiEnumDBBridge::SEPARATOR, $new_value) .
163 
164  ilADTActiveRecordByType::writeByPrimary("adv_md_values", $primary, "MultiEnum", $new_value);
165  }
166  // remove existing value - nothing left
167  else {
168  ilADTActiveRecordByType::deleteByPrimary("adv_md_values", $primary, "MultiEnum");
169  }
170  }
171  }
172 
173  if ($sub_type == "wpg") {
174  // #15763 - adapt advmd page lists
175  include_once "Modules/Wiki/classes/class.ilPCAMDPageList.php";
176  ilPCAMDPageList::migrateField($obj_id, $this->getFieldId(), $old_option, $new_option, true);
177  }
178  }
179  }
180 
181  $this->confirmed_objects = array();
182  }
183 
184  parent::update();
185  }
186 
187 
188  //
189  // import/export
190  //
191 
192  public function getValueForXML(ilADT $element)
193  {
194  return self::XML_SEPARATOR .
195  implode(self::XML_SEPARATOR, $element->getSelections()) .
196  self::XML_SEPARATOR;
197  }
198 
199  public function importValueFromXML($a_cdata)
200  {
201  $this->getADT()->setSelections(explode(self::XML_SEPARATOR, $a_cdata));
202  }
203 
204 
205  //
206  // presentation
207  //
208 
209  public function prepareElementForEditor(ilADTFormBridge $a_enum)
210  {
211  assert($a_enum instanceof ilADTMultiEnumFormBridge);
212 
213  $a_enum->setAutoSort(false);
214  }
215 }
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.
foreach($_POST as $key=> $value) $res
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.
update($pash, $contents, Config $config)
$def
Definition: croninfo.php:21
buildConfirmedObjects(ilPropertyFormGUI $a_form)
Process custom post values from definition form.