ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDclSelectionFieldModel.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
10 {
11  const SELECTION_TYPE_SINGLE = 'selection_type_single';
12  const SELECTION_TYPE_MULTI = 'selection_type_multi';
13  const SELECTION_TYPE_COMBOBOX = 'selection_type_combobox';
14  // those should be overwritten by subclasses
15  const PROP_SELECTION_TYPE = '';
17 
18 
22  public function getValidFieldProperties()
23  {
24  return array(static::PROP_SELECTION_OPTIONS, static::PROP_SELECTION_TYPE);
25  }
26 
27 
35  public function getRecordQueryFilterObject($filter_value = "", ilDclBaseFieldModel $sort_field = null)
36  {
37  global $DIC;
38  $ilDB = $DIC['ilDB'];
39 
40  $join_str
41  = " INNER 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 = "
42  . $ilDB->quote($this->getId(), 'integer') . ") ";
43 
44  if ($this->isMulti()) {
45  $join_str .= " INNER 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 AND (" .
46  "filter_stloc_{$this->getId()}.value = " . $ilDB->quote("[$filter_value]", 'text') . " OR " .
47  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%\"$filter_value\"%", 'text') . " OR " .
48  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%,$filter_value,%", 'text') . " OR " .
49  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%[$filter_value,%", 'text') . " OR " .
50  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%,$filter_value]%", 'text') .
51  ")) ";
52  } else {
53  $join_str .= " INNER 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 AND filter_stloc_{$this->getId()}.value = "
54  . $ilDB->quote($filter_value, 'integer') . ") ";
55  }
56 
57  $sql_obj = new ilDclRecordQueryObject();
58  $sql_obj->setJoinStatement($join_str);
59 
60  return $sql_obj;
61  }
62 
63 
64  public function isMulti()
65  {
66  return ($this->getProperty(static::PROP_SELECTION_TYPE) == self::SELECTION_TYPE_MULTI);
67  }
68 
69 
76  {
78 
79  $field_props = $this->getValidFieldProperties();
80  foreach ($field_props as $property) {
81  $value = $form->getInput($representation->getPropertyInputFieldId($property));
82 
83  // break down the multidimensional array from the multi input
84  // e.g.: { [0] => { [0] => 'x' }, [1] => { [1] => 'y' } } TO { [0] => 'x', [1] => 'y' }
85  if (is_array($value)) {
86  foreach ($value as $k => $v) {
87  if (is_array($v)) {
88  $value[$k] = array_shift($v);
89  }
90  }
91  }
92 
93  // save non empty values and set them to null, when they already exist. Do not override plugin-hook when already set.
94  if (!empty($value) || ($this->getPropertyInstance($property) != null && $property != self::PROP_PLUGIN_HOOK_NAME)) {
95  $this->setProperty($property, $value);
96  }
97  }
98  }
99 
100 
107  {
108  $values = array(
109  'table_id' => $this->getTableId(),
110  'field_id' => $this->getId(),
111  'title' => $this->getTitle(),
112  'datatype' => $this->getDatatypeId(),
113  'description' => $this->getDescription(),
114  'required' => $this->getRequired(),
115  'unique' => $this->isUnique(),
116  );
117 
118  $properties = $this->getValidFieldProperties();
119  foreach ($properties as $prop) {
120  if ($prop == static::PROP_SELECTION_OPTIONS) {
122  $prop_values = array();
123  foreach ($options as $option) {
124  // the 'selection_value' is for a correct input
125  $prop_values[$option->getOptId()] = array('selection_value' => $option->getValue());
126  }
127 
128  $values['prop_' . $prop] = $prop_values;
129  } else {
130  $values['prop_' . $prop] = $this->getProperty($prop);
131  }
132  }
133 
134  $form->setValuesByArray($values);
135 
136  return true;
137  }
138 
139 
144  public function setProperty($key, $value)
145  {
146  $is_update = $this->getProperty($key);
147  switch ($key) {
148  case static::PROP_SELECTION_OPTIONS:
149 
151  $sorting = 1;
152  foreach ($value as $id => $val) {
153  ilDclSelectionOption::storeOption($this->getId(), $id, $sorting, $val);
154  $sorting++;
155  }
156  // if the field is not being created reorder the options in the existing record fields
157  if ($is_update) {
158  $this->reorderExistingValues();
159  }
160  break;
161  case static::PROP_SELECTION_TYPE:
162  $will_be_multi = ($value == self::SELECTION_TYPE_MULTI);
163  // if the "Multi" property has changed, adjust the record field values
164  if ($is_update && ($this->isMulti() && !$will_be_multi || !$this->isMulti() && $will_be_multi)) {
165  $this->multiPropertyChanged($will_be_multi);
166  }
167  parent::setProperty($key, $value)->store();
168  break;
169  default:
170  parent::setProperty($key, $value)->store();
171  }
172  }
173 
174 
178  public function reorderExistingValues()
179  {
181  // loop each record(-field)
182  foreach (ilDclCache::getTableCache($this->getTableId())->getRecords() as $record) {
183  $record_field = $record->getRecordField($this->getId());
184  $record_field_value = $record_field->getValue();
185 
186  if (is_array($record_field_value) && count($record_field_value) > 1) {
187  $sorted_array = array();
188  // $options has the right order, so loop those
189  foreach ($options as $option) {
190  if (in_array($option->getOptId(), $record_field_value)) {
191  $sorted_array[] = $option->getOptId();
192  }
193  }
194  $record_field->setValue($sorted_array);
195  $record_field->doUpdate();
196  }
197  }
198  }
199 
200 
206  protected function multiPropertyChanged($is_multi_now)
207  {
208  foreach (ilDclCache::getTableCache($this->getTableId())->getRecords() as $record) {
209  $record_field = $record->getRecordField($this->getId());
210  $record_field_value = $record_field->getValue();
211 
212  if ($record_field_value && !is_array($record_field_value) && $is_multi_now) {
213  $record_field->setValue(array($record_field_value));
214  $record_field->doUpdate();
215  } else {
216  if (is_array($record_field_value) && !$is_multi_now) {
217  $record_field->setValue(array_shift($record_field_value));
218  $record_field->doUpdate();
219  }
220  }
221  }
222  }
223 
224 
230  public function getProperty($key)
231  {
232  switch ($key) {
233  case static::PROP_SELECTION_OPTIONS:
235  break;
236  default:
237  return parent::getProperty($key);
238  }
239  }
240 
241 
248  public function getRecordQuerySortObject($direction = "asc", $sort_by_status = false)
249  {
250  global $DIC;
251  $ilDB = $DIC['ilDB'];
252 
253  if ($this->isMulti()) {
254  return null;
255  }
256 
257  $sql_obj = new ilDclRecordQueryObject();
258 
259  $select_str = "sel_opts_{$this->getId()}.value AS field_{$this->getId()}";
260  $join_str
261  = "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 = "
262  . $ilDB->quote($this->getId(), 'integer') . ") ";
263  $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) ";
264  //if ($this->isMulti()) {
265  // $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->'$[0]' AND sel_opts_{$this->getId()}.field_id = " . $ilDB->quote($this->getId(), 'integer') . ") ";
266  //} else {
267  $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 = "
268  . $ilDB->quote($this->getId(), 'integer') . ") ";
269  //}
270 
271  $sql_obj->setSelectStatement($select_str);
272  $sql_obj->setJoinStatement($join_str);
273  $sql_obj->setOrderStatement("field_{$this->getId()} {$direction}");
274 
275  return $sql_obj;
276  }
277 
278 
279  public function cloneProperties(ilDclBaseFieldModel $originalField)
280  {
281  parent::cloneProperties($originalField);
283  foreach ($options as $opt) {
284  $new_opt = new ilDclSelectionOption();
285  $new_opt->cloneOption($opt);
286  $new_opt->setFieldId($this->getId());
287  $new_opt->store();
288  }
289  }
290 
291 
295  public function doDelete()
296  {
297  foreach (ilDclSelectionOption::getAllForField($this->getId()) as $option) {
298  $option->delete();
299  }
300  parent::doDelete();
301  }
302 
303 
308  {
309  $will_be_multi = ($form->getInput('prop_' . static::PROP_SELECTION_TYPE) == self::SELECTION_TYPE_MULTI);
310 
311  return $this->isMulti() && !$will_be_multi;
312  }
313 
314 
319  {
320  global $DIC;
321  $representation = ilDclFieldFactory::getFieldRepresentationInstance($this);
322  $prop_selection_options = $representation->getPropertyInputFieldId(static::PROP_SELECTION_OPTIONS);
323  $prop_selection_type = $representation->getPropertyInputFieldId(static::PROP_SELECTION_TYPE);
324 
325  $ilConfirmationGUI = parent::getConfirmationGUI($form);
326  $ilConfirmationGUI->setHeaderText($DIC->language()->txt('dcl_msg_mc_to_sc_confirmation'));
327  $ilConfirmationGUI->addHiddenItem($prop_selection_type, $form->getInput($prop_selection_type));
328  foreach ($form->getInput($prop_selection_options) as $key => $option) {
329  $ilConfirmationGUI->addHiddenItem($prop_selection_options . "[$key][selection_value]", $option['selection_value']);
330  }
331 
332  return $ilConfirmationGUI;
333  }
334 }
Class ilDclBaseFieldModel.
This class represents a property form user interface.
Class ilDclRecordQueryObject.
global $DIC
Definition: saml.php:7
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
Returns FieldRepresentation from BaseFieldModel.
getRecordQueryFilterObject($filter_value="", ilDclBaseFieldModel $sort_field=null)
Returns a query-object for building the record-loader-sql-query.
getPropertyInstance($key)
Return ActiveRecord of property.
cloneProperties(ilDclBaseFieldModel $originalField)
static getTableCache($table_id=0)
fillPropertiesForm(ilPropertyFormGUI &$form)
if(isset($_POST['submit'])) $form
Class ilDclSelectionFieldModel.
$values
getRequired()
Get Required Required.
multiPropertyChanged($is_multi_now)
changes the values of all record fields, since the property "multi" has changed
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
getDescription()
Get description.
storePropertiesFromForm(ilPropertyFormGUI $form)
called when saving the &#39;edit field&#39; form
isConfirmationRequired(ilPropertyFormGUI $form)
getRecordQuerySortObject($direction="asc", $sort_by_status=false)
global $ilDB
Class ilDclSelectionOption.
$key
Definition: croninfo.php:18
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
reorderExistingValues()
sorts record field values by the new order
getDatatypeId()
Get datatype_id.
getConfirmationGUI(ilPropertyFormGUI $form)