ILIAS  release_6 Revision v6.24-5-g0c8bfefb3b8
All Data Structures Namespaces Files Functions Variables Modules Pages
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  = " 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 = "
42  . $ilDB->quote($this->getId(), 'integer') . ") ";
43 
44  $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";
45 
46  $where_str = " AND ";
47  if ($filter_value == 'none') {
48  $where_str .= "("
49  . "filter_stloc_{$this->getId()}.value IS NULL "
50  . " OR filter_stloc_{$this->getId()}.value = " . $ilDB->quote("", 'text')
51  . " OR filter_stloc_{$this->getId()}.value = " . $ilDB->quote("[]", 'text')
52  . ") ";
53  } else {
54  if ($this->isMulti()) {
55  $where_str .= " (" .
56  "filter_stloc_{$this->getId()}.value = " . $ilDB->quote("[$filter_value]", 'text') . " OR " .
57  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%\"$filter_value\"%", 'text') . " OR " .
58  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%,$filter_value,%", 'text') . " OR " .
59  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%[$filter_value,%", 'text') . " OR " .
60  "filter_stloc_{$this->getId()}.value LIKE " . $ilDB->quote("%,$filter_value]%", 'text') .
61  ") ";;
62  } else {
63  $where_str .= "filter_stloc_{$this->getId()}.value = "
64  . $ilDB->quote($filter_value, 'integer');
65  }
66  }
67 
68  $join_str .= ") ";
69 
70  $sql_obj = new ilDclRecordQueryObject();
71  $sql_obj->setJoinStatement($join_str);
72  $sql_obj->setWhereStatement($where_str);
73 
74  return $sql_obj;
75  }
76 
77 
78  public function isMulti()
79  {
80  return ($this->getProperty(static::PROP_SELECTION_TYPE) == self::SELECTION_TYPE_MULTI);
81  }
82 
83 
92  {
94 
95  $field_props = $this->getValidFieldProperties();
96  foreach ($field_props as $property) {
97  $value = $form->getInput($representation->getPropertyInputFieldId($property));
98 
99  // break down the multidimensional array from the multi input
100  // e.g.: { [0] => { [0] => 'x' }, [1] => { [1] => 'y' } } TO { [0] => 'x', [1] => 'y' }
101  if (is_array($value)) {
102  foreach ($value as $k => $v) {
103  if (is_array($v)) {
104  $value[$k] = array_shift($v);
105  }
106  }
107  }
108 
109  // save non empty values and set them to null, when they already exist. Do not override plugin-hook when already set.
110  if (!empty($value) || ($this->getPropertyInstance($property) != null && $property != self::PROP_PLUGIN_HOOK_NAME)) {
111  $this->setProperty($property, $value);
112  }
113  }
114  }
115 
116 
122  public function fillPropertiesForm(ilPropertyFormGUI &$form)
123  {
124  $values = array(
125  'table_id' => $this->getTableId(),
126  'field_id' => $this->getId(),
127  'title' => $this->getTitle(),
128  'datatype' => $this->getDatatypeId(),
129  'description' => $this->getDescription(),
130  'required' => $this->getRequired(),
131  'unique' => $this->isUnique(),
132  );
133 
134  $properties = $this->getValidFieldProperties();
135  foreach ($properties as $prop) {
136  if ($prop == static::PROP_SELECTION_OPTIONS) {
137  $options = ilDclSelectionOption::getAllForField($this->getId());
138  $prop_values = array();
139  foreach ($options as $option) {
140  // the 'selection_value' is for a correct input
141  $prop_values[$option->getOptId()] = array('selection_value' => $option->getValue());
142  }
143 
144  $values['prop_' . $prop] = $prop_values;
145  } else {
146  $values['prop_' . $prop] = $this->getProperty($prop);
147  }
148  }
149 
150  $form->setValuesByArray($values);
151 
152  return true;
153  }
154 
155 
160  public function setProperty($key, $value)
161  {
162  $is_update = $this->getProperty($key);
163  switch ($key) {
164  case static::PROP_SELECTION_OPTIONS:
165 
167  $sorting = 1;
168  foreach ($value as $id => $val) {
169  ilDclSelectionOption::storeOption($this->getId(), $id, $sorting, $val);
170  $sorting++;
171  }
172  // if the field is not being created reorder the options in the existing record fields
173  if ($is_update) {
174  $this->reorderExistingValues();
175  }
176  break;
177  case static::PROP_SELECTION_TYPE:
178  $will_be_multi = ($value == self::SELECTION_TYPE_MULTI);
179  // if the "Multi" property has changed, adjust the record field values
180  if ($is_update && ($this->isMulti() && !$will_be_multi || !$this->isMulti() && $will_be_multi)) {
181  $this->multiPropertyChanged($will_be_multi);
182  }
183  parent::setProperty($key, $value)->store();
184  break;
185  default:
186  parent::setProperty($key, $value)->store();
187  }
188  }
189 
190 
194  public function reorderExistingValues()
195  {
196  $options = ilDclSelectionOption::getAllForField($this->getId());
197  // loop each record(-field)
198  foreach (ilDclCache::getTableCache($this->getTableId())->getRecords() as $record) {
199  $record_field = $record->getRecordField($this->getId());
200  $record_field_value = $record_field->getValue();
201 
202  if (is_array($record_field_value) && count($record_field_value) > 1) {
203  $sorted_array = array();
204  // $options has the right order, so loop those
205  foreach ($options as $option) {
206  if (in_array($option->getOptId(), $record_field_value)) {
207  $sorted_array[] = $option->getOptId();
208  }
209  }
210  $record_field->setValue($sorted_array);
211  $record_field->doUpdate();
212  }
213  }
214  }
215 
216 
222  protected function multiPropertyChanged($is_multi_now)
223  {
224  foreach (ilDclCache::getTableCache($this->getTableId())->getRecords() as $record) {
225  $record_field = $record->getRecordField($this->getId());
226  $record_field_value = $record_field->getValue();
227 
228  if ($record_field_value && !is_array($record_field_value) && $is_multi_now) {
229  $record_field->setValue(array($record_field_value));
230  $record_field->doUpdate();
231  } else {
232  if (is_array($record_field_value) && !$is_multi_now) {
233  $record_field->setValue(array_shift($record_field_value));
234  $record_field->doUpdate();
235  }
236  }
237  }
238  }
239 
240 
246  public function getProperty($key)
247  {
248  switch ($key) {
249  case static::PROP_SELECTION_OPTIONS:
251  break;
252  default:
253  return parent::getProperty($key);
254  }
255  }
256 
257 
264  public function getRecordQuerySortObject($direction = "asc", $sort_by_status = false)
265  {
266  global $DIC;
267  $ilDB = $DIC['ilDB'];
268 
269  if ($this->isMulti()) {
270  return null;
271  }
272 
273  $sql_obj = new ilDclRecordQueryObject();
274 
275  $select_str = "sel_opts_{$this->getId()}.value AS field_{$this->getId()}";
276  $join_str
277  = "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 = "
278  . $ilDB->quote($this->getId(), 'integer') . ") ";
279  $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) ";
280  //if ($this->isMulti()) {
281  // $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') . ") ";
282  //} else {
283  $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 = "
284  . $ilDB->quote($this->getId(), 'integer') . ") ";
285  //}
286 
287  $sql_obj->setSelectStatement($select_str);
288  $sql_obj->setJoinStatement($join_str);
289  $sql_obj->setOrderStatement("field_{$this->getId()} {$direction}");
290 
291  return $sql_obj;
292  }
293 
294 
295  public function cloneProperties(ilDclBaseFieldModel $originalField)
296  {
297  parent::cloneProperties($originalField);
298  $options = ilDclSelectionOption::getAllForField($originalField->getId());
299  foreach ($options as $opt) {
300  $new_opt = new ilDclSelectionOption();
301  $new_opt->cloneOption($opt);
302  $new_opt->setFieldId($this->getId());
303  $new_opt->store();
304  }
305  }
306 
307 
311  public function doDelete()
312  {
313  foreach (ilDclSelectionOption::getAllForField($this->getId()) as $option) {
314  $option->delete();
315  }
316  parent::doDelete();
317  }
318 
319 
324  {
325  $will_be_multi = ($form->getInput('prop_' . static::PROP_SELECTION_TYPE) == self::SELECTION_TYPE_MULTI);
326 
327  return $this->isMulti() && !$will_be_multi;
328  }
329 
330 
334  public function getConfirmationGUI(ilPropertyFormGUI $form)
335  {
336  global $DIC;
337  $representation = ilDclFieldFactory::getFieldRepresentationInstance($this);
338  $prop_selection_options = $representation->getPropertyInputFieldId(static::PROP_SELECTION_OPTIONS);
339  $prop_selection_type = $representation->getPropertyInputFieldId(static::PROP_SELECTION_TYPE);
340 
341  $ilConfirmationGUI = parent::getConfirmationGUI($form);
342  $ilConfirmationGUI->setHeaderText($DIC->language()->txt('dcl_msg_mc_to_sc_confirmation'));
343  $ilConfirmationGUI->addHiddenItem($prop_selection_type, $form->getInput($prop_selection_type));
344  foreach ($form->getInput($prop_selection_options) as $key => $option) {
345  $ilConfirmationGUI->addHiddenItem($prop_selection_options . "[$key][selection_value]", $option['selection_value']);
346  }
347 
348  return $ilConfirmationGUI;
349  }
350 }
Class ilDclBaseFieldModel.
This class represents a property form user interface.
Class ilDclRecordQueryObject.
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)
Class ilDclSelectionFieldModel.
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
$DIC
Definition: xapitoken.php:46
Class ilDclSelectionOption.
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)