ILIAS  release_8 Revision v8.24
class.ilDclSelectionFieldModel.php
Go to the documentation of this file.
1<?php
2
20{
21 public const SELECTION_TYPE_SINGLE = 'selection_type_single';
22 public const SELECTION_TYPE_MULTI = 'selection_type_multi';
23 public const SELECTION_TYPE_COMBOBOX = 'selection_type_combobox';
24 // those should be overwritten by subclasses
25 public const PROP_SELECTION_TYPE = '';
26 public const PROP_SELECTION_OPTIONS = '';
27
28 public function getValidFieldProperties(): array
29 {
30 return [static::PROP_SELECTION_OPTIONS, static::PROP_SELECTION_TYPE];
31 }
32
38 $filter_value = "",
39 ?ilDclBaseFieldModel $sort_field = null
41 global $DIC;
42 $ilDB = $DIC['ilDB'];
43
44 $join_str
45 = " LEFT JOIN il_dcl_record_field AS filter_record_field_{$this->getId()} ON (filter_record_field_{$this->getId()}.record_id = record.id AND filter_record_field_{$this->getId()}.field_id = "
46 . $ilDB->quote($this->getId(), 'integer') . ") ";
47
48 $join_str .= " LEFT JOIN il_dcl_stloc{$this->getStorageLocation()}_value AS filter_stloc_{$this->getId()} ON (filter_stloc_{$this->getId()}.record_field_id = filter_record_field_{$this->getId()}.id";
49
50 $where_str = " AND ";
51 if ($filter_value == 'none') {
52 $where_str .= "("
53 . "filter_stloc_{$this->getId()}.value IS NULL "
54 . " OR filter_stloc_{$this->getId()}.value = " . $ilDB->quote("", 'text')
55 . " OR filter_stloc_{$this->getId()}.value = " . $ilDB->quote("[]", 'text')
56 . ") ";
57 } else {
58 if ($this->isMulti()) {
59 $where_str .= " (" .
60 "filter_stloc_{$this->getId()}.value = " . $ilDB->quote("[$filter_value]", 'text') . " OR " .
61 "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%\"$filter_value\"%", 'text') . " OR " .
62 "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%,$filter_value,%", 'text') . " OR " .
63 "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%[$filter_value,%", 'text') . " OR " .
64 "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%,$filter_value]%", 'text') .
65 ") ";
66 } else {
67 $where_str .= "filter_stloc_{$this->getId()}.value = "
68 . $ilDB->quote($filter_value, 'integer');
69 }
70 }
71
72 $join_str .= ") ";
73
74 $sql_obj = new ilDclRecordQueryObject();
75 $sql_obj->setJoinStatement($join_str);
76 $sql_obj->setWhereStatement($where_str);
77
78 return $sql_obj;
79 }
80
81 public function isMulti(): bool
82 {
83 return ($this->getProperty(static::PROP_SELECTION_TYPE) == self::SELECTION_TYPE_MULTI);
84 }
85
86 public function checkFieldCreationInput(ilPropertyFormGUI $form): bool
87 {
88 $options_post_var = "prop_" . static::PROP_SELECTION_OPTIONS;
89 foreach ($form->getInput($options_post_var) as $value) {
90 if ($value["selection_value"] == "") {
91 $inputObj = $form->getItemByPostVar($options_post_var);
92 $inputObj->setAlert($this->lng->txt("msg_input_is_required"));
93 return false;
94 }
95 }
96
97 return parent::checkFieldCreationInput($form);
98 }
99
104 public function storePropertiesFromForm(ilPropertyFormGUI $form): void
105 {
107
108 $field_props = $this->getValidFieldProperties();
109 foreach ($field_props as $property) {
110 $value = $form->getInput($representation->getPropertyInputFieldId($property));
111
112 // break down the multidimensional array from the multi input
113 // e.g.: { [0] => { [0] => 'x' }, [1] => { [1] => 'y' } } TO { [0] => 'x', [1] => 'y' }
114 if (is_array($value)) {
115 foreach ($value as $k => $v) {
116 if (is_array($v)) {
117 $value[$k] = array_shift($v);
118 }
119 }
120 }
121
122 // save non empty values and set them to null, when they already exist. Do not override plugin-hook when already set.
123 if (!empty($value) || ($this->getPropertyInstance($property) != null && $property != self::PROP_PLUGIN_HOOK_NAME)) {
124 $this->setProperty($property, $value);
125 }
126 }
127 }
128
133 public function fillPropertiesForm(ilPropertyFormGUI &$form): bool
134 {
135 $values = [
136 'table_id' => $this->getTableId(),
137 'field_id' => $this->getId(),
138 'title' => $this->getTitle(),
139 'datatype' => $this->getDatatypeId(),
140 'description' => $this->getDescription(),
141 'unique' => $this->isUnique(),
142 ];
143
144 $properties = $this->getValidFieldProperties();
145 foreach ($properties as $prop) {
146 if ($prop == static::PROP_SELECTION_OPTIONS) {
147 $options = ilDclSelectionOption::getAllForField($this->getId());
148 $prop_values = [];
149 foreach ($options as $option) {
150 // the 'selection_value' is for a correct input
151 $prop_values[$option->getOptId()] = ['selection_value' => $option->getValue()];
152 }
153
154 $values['prop_' . $prop] = $prop_values;
155 } else {
156 $values['prop_' . $prop] = $this->getProperty($prop);
157 }
158 }
159
160 $form->setValuesByArray($values);
161
162 return true;
163 }
164
168 public function setProperty(string $key, $value): ?ilDclFieldProperty
169 {
170 $is_update = $this->getProperty($key);
171 switch ($key) {
172 case static::PROP_SELECTION_OPTIONS:
173
175 $sorting = 1;
176 foreach ($value as $id => $val) {
177 ilDclSelectionOption::storeOption($this->getId(), $id, $sorting, $val);
178 $sorting++;
179 }
180 // if the field is not being created reorder the options in the existing record fields
181 if ($is_update) {
182 $this->reorderExistingValues();
183 }
184 break;
185 case static::PROP_SELECTION_TYPE:
186 $will_be_multi = ($value == self::SELECTION_TYPE_MULTI);
187 // if the "Multi" property has changed, adjust the record field values
188 if ($is_update && ($this->isMulti() && !$will_be_multi || !$this->isMulti() && $will_be_multi)) {
189 $this->multiPropertyChanged($will_be_multi);
190 }
191 parent::setProperty($key, $value)->store();
192 break;
193 default:
194 parent::setProperty($key, $value)->store();
195 }
196
197 return null;
198 }
199
203 public function reorderExistingValues(): void
204 {
205 $options = ilDclSelectionOption::getAllForField($this->getId());
206 // loop each record(-field)
207 foreach (ilDclCache::getTableCache($this->getTableId())->getRecords() as $record) {
208 $record_field = $record->getRecordField($this->getId());
209 $record_field_value = $record_field->getValue();
210
211 if (is_array($record_field_value) && count($record_field_value) > 1) {
212 $sorted_array = [];
213 // $options has the right order, so loop those
214 foreach ($options as $option) {
215 if (in_array($option->getOptId(), $record_field_value)) {
216 $sorted_array[] = $option->getOptId();
217 }
218 }
219 $record_field->setValue($sorted_array);
220 $record_field->doUpdate();
221 }
222 }
223 }
224
228 protected function multiPropertyChanged(bool $is_multi_now): void
229 {
230 foreach (ilDclCache::getTableCache($this->getTableId())->getRecords() as $record) {
231 $record_field = $record->getRecordField($this->getId());
232 $record_field_value = $record_field->getValue();
233
234 if ($record_field_value && !is_array($record_field_value) && $is_multi_now) {
235 $record_field->setValue([$record_field_value]);
236 $record_field->doUpdate();
237 } else {
238 if (is_array($record_field_value) && !$is_multi_now) {
239 $record_field->setValue(array_shift($record_field_value));
240 $record_field->doUpdate();
241 }
242 }
243 }
244 }
245
250 public function getProperty(string $key)
251 {
252 switch ($key) {
253 case static::PROP_SELECTION_OPTIONS:
255 default:
256 return parent::getProperty($key);
257 }
258 }
259
261 string $direction = "asc",
262 bool $sort_by_status = false
264 global $DIC;
265 $ilDB = $DIC['ilDB'];
266
267 if ($this->isMulti()) {
268 return null;
269 }
270
271 $sql_obj = new ilDclRecordQueryObject();
272
273 $select_str = "sel_opts_{$this->getId()}.value AS field_{$this->getId()}";
274 $join_str
275 = "LEFT JOIN il_dcl_record_field AS sort_record_field_{$this->getId()} ON (sort_record_field_{$this->getId()}.record_id = record.id AND sort_record_field_{$this->getId()}.field_id = "
276 . $ilDB->quote($this->getId(), 'integer') . ") ";
277 $join_str .= "LEFT JOIN il_dcl_stloc{$this->getStorageLocation()}_value AS sort_stloc_{$this->getId()} ON (sort_stloc_{$this->getId()}.record_field_id = sort_record_field_{$this->getId()}.id) ";
278 $join_str .= "LEFT JOIN il_dcl_sel_opts as sel_opts_{$this->getId()} ON (sel_opts_{$this->getId()}.opt_id = sort_stloc_{$this->getId()}.value AND sel_opts_{$this->getId()}.field_id = "
279 . $ilDB->quote($this->getId(), 'integer') . ") ";
280
281 $sql_obj->setSelectStatement($select_str);
282 $sql_obj->setJoinStatement($join_str);
283 $sql_obj->setOrderStatement("field_{$this->getId()} {$direction} , ID ASC");
284
285 return $sql_obj;
286 }
287
288 public function cloneProperties(ilDclBaseFieldModel $originalField): void
289 {
290 parent::cloneProperties($originalField);
291 $options = ilDclSelectionOption::getAllForField($originalField->getId());
292 foreach ($options as $opt) {
293 $new_opt = new ilDclSelectionOption();
294 $new_opt->cloneOption($opt);
295 $new_opt->setFieldId($this->getId());
296 $new_opt->store();
297 }
298 }
299
300 public function doDelete(): void
301 {
302 foreach (ilDclSelectionOption::getAllForField($this->getId()) as $option) {
303 $option->delete();
304 }
305 parent::doDelete();
306 }
307
308 public function isConfirmationRequired(ilPropertyFormGUI $form): bool
309 {
310 $will_be_multi = ($form->getInput('prop_' . static::PROP_SELECTION_TYPE) == self::SELECTION_TYPE_MULTI);
311
312 return $this->isMulti() && !$will_be_multi;
313 }
314
316 {
317 global $DIC;
319 $prop_selection_options = $representation->getPropertyInputFieldId(static::PROP_SELECTION_OPTIONS);
320 $prop_selection_type = $representation->getPropertyInputFieldId(static::PROP_SELECTION_TYPE);
321
322 $ilConfirmationGUI = parent::getConfirmationGUI($form);
323 $ilConfirmationGUI->setHeaderText($DIC->language()->txt('dcl_msg_mc_to_sc_confirmation'));
324 $ilConfirmationGUI->addHiddenItem($prop_selection_type, $form->getInput($prop_selection_type));
325 foreach ($form->getInput($prop_selection_options) as $key => $option) {
326 $ilConfirmationGUI->addHiddenItem(
327 $prop_selection_options . "[$key][selection_value]",
328 $option['selection_value']
329 );
330 }
331
332 return $ilConfirmationGUI;
333 }
334}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getTableCache(int $table_id=null)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getRecordQuerySortObject(string $direction="asc", bool $sort_by_status=false)
Returns a query-object for building the record-loader-sql-query.
fillPropertiesForm(ilPropertyFormGUI &$form)
doDelete()
Remove field and properties.
cloneProperties(ilDclBaseFieldModel $originalField)
reorderExistingValues()
sorts record field values by the new order
checkFieldCreationInput(ilPropertyFormGUI $form)
Checks input of specific fields befor saving.
getConfirmationGUI(ilPropertyFormGUI $form)
called by ilDclFieldEditGUI if isConfirmationRequired returns true
multiPropertyChanged(bool $is_multi_now)
changes the values of all record fields, since the property "multi" has changed
storePropertiesFromForm(ilPropertyFormGUI $form)
called when saving the 'edit field' form
getValidFieldProperties()
Returns all valid properties for a field-type.
getRecordQueryFilterObject( $filter_value="", ?ilDclBaseFieldModel $sort_field=null)
Returns a query-object for building the record-loader-sql-query.
isConfirmationRequired(ilPropertyFormGUI $form)
called by ilDclFieldEditGUI when updating field properties if you overwrite this method,...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getAllForField(int $field_id)
static flushOptions(int $field_id)
This class represents a property form user interface.
setValuesByArray(array $a_values, bool $a_restrict_to_value_keys=false)
getInput(string $a_post_var, bool $ensureValidation=true)
Returns the input of an item, if item provides getInput method and as fallback the value of the HTTP-...
getItemByPostVar(string $a_post_var)
global $DIC
Definition: feed.php:28
string $key
Consumer key/client ID value.
Definition: System.php:193