ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
class.ilDclBaseFieldModel.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
16 {
17 
21  protected $id;
25  protected $table_id;
29  protected $title;
33  protected $description;
37  protected $datatypeId;
41  protected $required;
45  protected $order;
49  protected $unique;
53  protected $locked;
57  protected $property = array();
61  protected $exportable;
65  protected $datatype;
69  protected $storage_location_override = null;
73  const PROP_LENGTH = "lenght";
74  const PROP_REGEX = "regex";
75  const PROP_REFERENCE = "table_id";
76  const PROP_URL = "url";
77  const PROP_TEXTAREA = "text_area";
78  const PROP_REFERENCE_LINK = "reference_link";
79  const PROP_WIDTH = "width";
80  const PROP_HEIGHT = "height";
81  const PROP_LEARNING_PROGRESS = "learning_progress";
82  const PROP_ILIAS_REFERENCE_LINK = "ILIAS_reference_link";
83  const PROP_N_REFERENCE = "multiple_selection";
84  const PROP_FORMULA_EXPRESSION = "expression";
85  const PROP_DISPLAY_COPY_LINK_ACTION_MENU = "display_action_menu";
86  const PROP_LINK_DETAIL_PAGE_TEXT = "link_detail_page";
87  const PROP_SUPPORTED_FILE_TYPES = "supported_file_types";
88  const PROP_PLUGIN_HOOK_NAME = "plugin_hook_name";
89  const PROP_TEXT_SELECTION_OPTIONS = "text_selection_options";
90  const PROP_TEXT_SELECTION_TYPE = "text_selection_type";
91  const PROP_DATE_SELECTION_OPTIONS = "date_selection_options";
92  const PROP_DATE_SELECTION_TYPE = "date_selection_type";
93  // type of table il_dcl_view
94  const EDIT_VIEW = 2;
95  const EXPORTABLE_VIEW = 4;
96 
97 
101  public function __construct($a_id = 0)
102  {
103  if ($a_id != 0) {
104  $this->id = $a_id;
105  $this->doRead();
106  }
107  }
108 
109 
117  public static function _getTitleInvalidChars($a_as_regex = true)
118  {
119  if ($a_as_regex) {
120  return '/^[^<>\\\\":]*$/i';
121  } else {
122  return '\ < > " :';
123  }
124  }
125 
126 
133  public static function _getFieldIdByTitle($title, $table_id)
134  {
135  global $DIC;
136  $ilDB = $DIC['ilDB'];
137  $result = $ilDB->query(
138  'SELECT id FROM il_dcl_field WHERE title = ' . $ilDB->quote($title, 'text') . ' AND table_id = '
139  . $ilDB->quote($table_id, 'integer')
140  );
141  $id = 0;
142  while ($rec = $ilDB->fetchAssoc($result)) {
143  $id = $rec['id'];
144  }
145 
146  return $id;
147  }
148 
149 
155  public function setId($a_id)
156  {
157  $this->id = $a_id;
158  }
159 
160 
166  public function getId()
167  {
168  return $this->id;
169  }
170 
171 
177  public function setTableId($a_id)
178  {
179  $this->table_id = $a_id;
180  }
181 
182 
188  public function getTableId()
189  {
190  return $this->table_id;
191  }
192 
193 
199  public function setTitle($a_title)
200  {
201  //title cannot begin with _ as this is saved for other purposes. make __ instead.
202  if (substr($a_title, 0, 1) == "_" && substr($a_title, 0, 2) != "__") {
203  $a_title = "_" . $a_title;
204  }
205  $this->title = $a_title;
206  }
207 
208 
214  public function getTitle()
215  {
216  return $this->title;
217  }
218 
219 
225  public function setDescription($a_desc)
226  {
227  $this->description = $a_desc;
228  }
229 
230 
236  public function getDescription()
237  {
238  return $this->description;
239  }
240 
241 
247  public function setDatatypeId($a_id)
248  {
249  //unset the cached datatype.
250  $this->datatype = null;
251  $this->datatypeId = $a_id;
252  }
253 
254 
260  public function getDatatypeId()
261  {
262  if ($this->isStandardField()) {
264  }
265 
266  return $this->datatypeId;
267  }
268 
269 
275  public function setRequired($a_required)
276  {
277  $this->required = $a_required;
278  }
279 
280 
286  public function getRequired()
287  {
288  return $this->required;
289  }
290 
291 
295  public function isUnique()
296  {
297  return $this->unique;
298  }
299 
300 
304  public function setUnique($unique)
305  {
306  $this->unique = $unique ? 1 : 0;
307  }
308 
309 
313  public function getDatatype()
314  {
315  $this->loadDatatype();
316 
317  return $this->datatype;
318  }
319 
320 
324  public function getDatatypeTitle()
325  {
326  $this->loadDatatype();
327 
328  return $this->datatype->getTitle();
329  }
330 
331 
337  public function getStorageLocation()
338  {
339  if ($this->getStorageLocationOverride() !== null) {
340  return $this->getStorageLocationOverride();
341  }
342 
343  $this->loadDatatype();
344 
345  return $this->datatype->getStorageLocation();
346  }
347 
348 
352  protected function loadDatatype()
353  {
354  if ($this->datatype == null) {
355  $this->datatype = ilDclCache::getDatatype($this->datatypeId);
356  }
357  }
358 
359 
363  protected function loadTableFieldSetting()
364  {
365  $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
366  $this->exportable = $tablefield_setting->isExportable();
367  $this->order = $tablefield_setting->getFieldOrder();
368  }
369 
370 
374  public function getExportable()
375  {
376  if (!isset($this->exportable)) {
377  $this->loadExportability();
378  }
379 
380  return $this->exportable;
381  }
382 
383 
387  private function loadExportability()
388  {
389  if ($this->exportable == null) {
390  $this->loadTableFieldSetting();
391  }
392  }
393 
394 
398  public function toArray()
399  {
400  return (array) $this;
401  }
402 
403 
407  public function isStandardField()
408  {
409  return false;
410  }
411 
412 
416  public function doRead()
417  {
418  global $DIC;
419  $ilDB = $DIC['ilDB'];
420 
421  //THEN 1 ELSE 0 END AS has_options FROM il_dcl_field f WHERE id = ".$ilDB->quote($this->getId(),"integer");
422  $query = "SELECT * FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "integer");
423  $set = $ilDB->query($query);
424  $rec = $ilDB->fetchAssoc($set);
425 
426  $this->setTableId($rec["table_id"]);
427  $this->setTitle($rec["title"]);
428  $this->setDescription($rec["description"]);
429  $this->setDatatypeId($rec["datatype_id"]);
430  $this->setRequired($rec["required"]);
431  $this->setUnique($rec["is_unique"]);
432  $this->setLocked($rec["is_locked"]);
433  $this->loadProperties();
434  $this->loadTableFieldSetting();
435  }
436 
437 
443  public function buildFromDBRecord($rec)
444  {
445  $this->setId($rec["id"]);
446  $this->setTableId($rec["table_id"]);
447  $this->setTitle($rec["title"]);
448  $this->setDescription($rec["description"]);
449  $this->setDatatypeId($rec["datatype_id"]);
450  $this->setRequired($rec["required"]);
451  $this->setUnique($rec["is_unique"]);
452  $this->setLocked($rec["is_locked"]);
453  }
454 
455 
459  public function doCreate()
460  {
461  global $DIC;
462  $ilDB = $DIC['ilDB'];
463  $this->getLocked() == null ? $this->setLocked(false) : true;
464 
465  if (!ilDclTable::_tableExists($this->getTableId())) {
466  throw new ilException("The field does not have a related table!");
467  }
468 
469  $id = $ilDB->nextId("il_dcl_field");
470  $this->setId($id);
471  $query = "INSERT INTO il_dcl_field (" . "id" . ", table_id" . ", datatype_id" . ", title" . ", description" . ", required" . ", is_unique"
472  . ", is_locked" . " ) VALUES (" . $ilDB->quote($this->getId(), "integer") . "," . $ilDB->quote($this->getTableId(), "integer") . ","
473  . $ilDB->quote($this->getDatatypeId(), "integer") . "," . $ilDB->quote($this->getTitle(), "text") . ","
474  . $ilDB->quote($this->getDescription(), "text") . "," . $ilDB->quote($this->getRequired(), "integer") . ","
475  . $ilDB->quote($this->isUnique(), "integer") . "," . $ilDB->quote($this->getLocked() ? 1 : 0, "integer") . ")";
476  $ilDB->manipulate($query);
477 
478  $this->updateTableFieldSetting();
479 
480  $this->addToTableViews();
481  }
482 
483 
487  protected function addToTableViews()
488  {
489  foreach (ilDclTableView::getAllForTableId($this->table_id) as $tableview) {
490  $tableview->createFieldSetting($this->id);
491  }
492  }
493 
494 
498  public function doUpdate()
499  {
500  global $DIC;
501  $ilDB = $DIC['ilDB'];
502 
503  $ilDB->update(
504  "il_dcl_field",
505  array(
506  "table_id" => array(
507  "integer",
508  $this->getTableId(),
509  ),
510  "datatype_id" => array(
511  "text",
512  $this->getDatatypeId(),
513  ),
514  "title" => array(
515  "text",
516  $this->getTitle(),
517  ),
518  "description" => array(
519  "text",
520  $this->getDescription(),
521  ),
522  "required" => array(
523  "integer",
524  $this->getRequired(),
525  ),
526  "is_unique" => array(
527  "integer",
528  $this->isUnique(),
529  ),
530  "is_locked" => array(
531  "integer",
532  $this->getLocked() ? 1 : 0,
533  ),
534  ),
535  array(
536  "id" => array(
537  "integer",
538  $this->getId(),
539  ),
540  )
541  );
542  $this->updateTableFieldSetting();
543  $this->updateProperties();
544  }
545 
546 
550  public function updateProperties()
551  {
552  foreach ($this->property as $prop) {
553  $prop->store();
554  }
555  }
556 
557 
562  protected function updateTableFieldSetting()
563  {
564  $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
565  $tablefield_setting->setExportable($this->exportable);
566  $tablefield_setting->setFieldOrder($this->order);
567  $tablefield_setting->store();
568  }
569 
570 
574  public function doDelete()
575  {
576  global $DIC;
577  $ilDB = $DIC['ilDB'];
578 
579  // delete tablefield setting.
580  ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId())->delete();
581 
582  $query = "DELETE FROM il_dcl_field_prop WHERE field_id = " . $ilDB->quote($this->getId(), "text");
583  $ilDB->manipulate($query);
584 
585  $query = "DELETE FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "text");
586  $ilDB->manipulate($query);
587 
588  foreach ($this->getFieldSettings() as $field_setting) {
589  $field_setting->delete();
590  }
591  }
592 
593 
594  public function getFieldSettings()
595  {
596  return ilDclTableViewFieldSetting::where(array('field' => $this->getId()))->get();
597  }
598 
599 
603  public function getOrder()
604  {
605  if ($this->order == null) {
606  $this->loadTableFieldSetting();
607  }
608 
609  return !$this->order ? 0 : $this->order;
610  }
611 
612 
616  public function setOrder($order)
617  {
618  $this->order = $order;
619  }
620 
621 
627  protected function loadProperties()
628  {
629  $this->property = ilDclCache::getFieldProperties($this->getId());
630  }
631 
632 
640  public function hasProperty($key)
641  {
642  $this->loadProperties();
643 
644  return (isset($this->property[$key]) && $this->property[$key]->getValue() != null);
645  }
646 
647 
655  public function getProperty($key)
656  {
657  $instance = $this->getPropertyInstance($key);
658 
659  return ($instance !== null) ? $instance->getValue() : null;
660  }
661 
662 
670  public function getPropertyInstance($key)
671  {
672  $this->loadProperties();
673  if ($this->hasProperty($key)) {
674  $value = $this->property[$key];
675 
676  return $value;
677  }
678 
679  return null;
680  }
681 
682 
689  public function setProperty($key, $value)
690  {
691  $this->loadProperties();
692  if (isset($this->property[$key])) {
693  $this->property[$key]->setValue($value);
694  } else {
696  $property->setName($key);
697  $property->setFieldId($this->getId());
698  $property->setValue($value);
699 
700  $this->property[$key] = $property;
701  }
702 
703  return $this->property[$key];
704  }
705 
706 
712  public function getValidFieldProperties()
713  {
714  return array();
715  }
716 
717 
721  public function setLocked($locked)
722  {
723  $this->locked = $locked;
724  }
725 
726 
730  public function getLocked()
731  {
732  return $this->locked;
733  }
734 
735 
740  public function checkValidityFromForm(ilPropertyFormGUI &$form, $record_id = null)
741  {
742  $value = $form->getInput('field_' . $this->getId());
743  $this->checkValidity($value, $record_id);
744  }
745 
746 
756  public function checkValidity($value, $record_id = null)
757  {
758  //Don't check empty values
759  if ($value == null) {
760  return true;
761  }
762 
763  if ($this->isUnique()) {
765  foreach ($table->getRecords() as $record) {
766  if ($this->normalizeValue($record->getRecordFieldValue($this->getId())) == $this->normalizeValue($value) && ($record->getId() != $record_id || $record_id == 0)) {
768  }
769  }
770  }
771 
772  return true;
773  }
774 
775 
781  protected function normalizeValue($value)
782  {
783  if (is_string($value)) {
784  $value = trim(preg_replace("/\\s+/uism", " ", $value));
785  }
786 
787  return $value;
788  }
789 
790 
796  public function cloneStructure($original_id)
797  {
798  $original = ilDclCache::getFieldCache($original_id);
799  $this->setTitle($original->getTitle());
800  $this->setDatatypeId($original->getDatatypeId());
801  $this->setDescription($original->getDescription());
802  $this->setLocked($original->getLocked());
803  $this->setOrder($original->getOrder());
804  $this->setRequired($original->getRequired());
805  $this->setUnique($original->isUnique());
806  $this->setExportable($original->getExportable());
807  $this->doCreate();
808  $this->cloneProperties($original);
809 
810  // mandatory for all cloning functions
811  ilDclCache::setCloneOf($original_id, $this->getId(), ilDclCache::TYPE_FIELD);
812  }
813 
814 
818  public function afterClone($records)
819  {
820  foreach ($records as $rec) {
821  ilDclCache::getRecordFieldCache($rec, $this)->afterClone();
822  }
823  }
824 
825 
829  public function cloneProperties(ilDclBaseFieldModel $originalField)
830  {
831  $orgProps = $originalField->getValidFieldProperties();
832  if (count($orgProps) == 0) {
833  return;
834  }
835  foreach ($orgProps as $prop_name) {
836  $fieldprop_obj = new ilDclFieldProperty();
837  $fieldprop_obj->setFieldId($this->getId());
838  $fieldprop_obj->setName($prop_name);
839 
840  $value = $originalField->getProperty($prop_name);
841 
842  // If reference field, we must reset the referenced field, otherwise it will point to the old ID
844  $value = null;
845  }
846 
847  $fieldprop_obj->setValue($value);
848  $fieldprop_obj->create();
849  }
850  }
851 
852 
856  public function setExportable($exportable)
857  {
858  $this->exportable = $exportable;
859  }
860 
861 
865  public function allowFilterInListView()
866  {
867  return true;
868  }
869 
870 
879  public function getRecordQuerySortObject($direction = "asc", $sort_by_status = false)
880  {
881  global $DIC;
882  $ilDB = $DIC['ilDB'];
883 
884  $sql_obj = new ilDclRecordQueryObject();
885 
886  $select_str = "sort_stloc_{$this->getId()}.value AS field_{$this->getId()}";
887  $join_str
888  = "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 = "
889  . $ilDB->quote($this->getId(), 'integer') . ") ";
890  $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)";
891 
892  $sql_obj->setSelectStatement($select_str);
893  $sql_obj->setJoinStatement($join_str);
894  $sql_obj->setOrderStatement("field_{$this->getId()} {$direction}");
895 
896  return $sql_obj;
897  }
898 
899 
908  public function getRecordQueryFilterObject($filter_value = "", ilDclBaseFieldModel $sort_field = null)
909  {
910  return null;
911  }
912 
913 
919  public function getSortField()
920  {
921  return $this->getTitle();
922  }
923 
924 
930  public function hasNumericSorting()
931  {
933  return true;
934  }
935 
936  return false;
937  }
938 
939 
948  {
949  return true;
950  }
951 
952 
956  public function getStorageLocationOverride()
957  {
959  }
960 
961 
966  {
967  $this->storage_location_override = $storage_location_override;
968  }
969 
970 
976  public function fillHeaderExcel(ilExcel $worksheet, &$row, &$col)
977  {
978  $worksheet->setCell($row, $col, $this->getTitle());
979  $col++;
980  }
981 
982 
987  public function checkTitlesForImport(array &$titles, array &$import_fields)
988  {
989  foreach ($titles as $k => $title) {
990  if (!ilStr::isUtf8($title)) {
991  $title = utf8_encode($title);
992  }
993  if ($title == $this->getTitle()) {
994  $import_fields[$k] = $this;
995  }
996  }
997  }
998 
999 
1006  {
1007  $field_props = $this->getValidFieldProperties();
1008  foreach ($field_props as $property) {
1009  $representation = ilDclFieldFactory::getFieldRepresentationInstance($this);
1010  $value = $form->getInput($representation->getPropertyInputFieldId($property));
1011 
1012  // save non empty values and set them to null, when they already exist. Do not override plugin-hook when already set.
1013  if (!empty($value) || ($this->getPropertyInstance($property) != null && $property != self::PROP_PLUGIN_HOOK_NAME)) {
1014  $this->setProperty($property, $value)->store();
1015  }
1016  }
1017  }
1018 
1019 
1028  {
1029  $values = array(
1030  'table_id' => $this->getTableId(),
1031  'field_id' => $this->getId(),
1032  'title' => $this->getTitle(),
1033  'datatype' => $this->getDatatypeId(),
1034  'description' => $this->getDescription(),
1035  'required' => $this->getRequired(),
1036  'unique' => $this->isUnique(),
1037  );
1038 
1039  $properties = $this->getValidFieldProperties();
1040  foreach ($properties as $prop) {
1041  $values['prop_' . $prop] = $this->getProperty($prop);
1042  }
1043 
1044  $form->setValuesByArray($values);
1045 
1046  return true;
1047  }
1048 
1049 
1059  {
1060  return false;
1061  }
1062 
1063 
1072  {
1073  global $DIC;
1074  $ilConfirmationGUI = new ilConfirmationGUI();
1075  $ilConfirmationGUI->setFormAction($form->getFormAction());
1076  $ilConfirmationGUI->addHiddenItem('confirmed', 1);
1077  $ilConfirmationGUI->addHiddenItem('field_id', $form->getInput('field_id'));
1078  $ilConfirmationGUI->addHiddenItem('title', $form->getInput('title'));
1079  $ilConfirmationGUI->addHiddenItem('description', $form->getInput('description'));
1080  $ilConfirmationGUI->addHiddenItem('datatype', $form->getInput('datatype'));
1081  $ilConfirmationGUI->addHiddenItem('required', $form->getInput('required'));
1082  $ilConfirmationGUI->addHiddenItem('unique', $form->getInput('unique'));
1083  $ilConfirmationGUI->setConfirm($DIC->language()->txt('dcl_update_field'), 'update');
1084  $ilConfirmationGUI->setCancel($DIC->language()->txt('cancel'), 'edit');
1085 
1086  return $ilConfirmationGUI;
1087  }
1088 }
checkValidity($value, $record_id=null)
Check if input is valid.
Class ilDclBaseFieldModel.
static setCloneOf($old, $new, $type)
static getInstance($table_id, $field)
getFormAction()
Get FormAction.
setDescription($a_desc)
Set description.
checkTitlesForImport(array &$titles, array &$import_fields)
getConfirmationGUI(ilPropertyFormGUI $form)
called by ilDclFieldEditGUI if isConfirmationRequired returns true
$result
static _getTitleInvalidChars($a_as_regex=true)
All valid chars for filed titles.
This class represents a property form user interface.
static getFieldProperties($field_id)
Cache Field properties.
setDatatypeId($a_id)
Set datatype id.
Class ilDclRecordQueryObject.
global $DIC
Definition: saml.php:7
checkFieldCreationInput(ilPropertyFormGUI $form)
Checks input of specific fields befor saving.
static getFieldCache($field_id=0)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
Returns FieldRepresentation from BaseFieldModel.
isConfirmationRequired(ilPropertyFormGUI $form)
called by ilDclFieldEditGUI when updating field properties if you overwrite this method, remember to also overwrite getConfirmationGUI
Class ilDclFieldProperty.
doDelete()
Remove field and properties.
storePropertiesFromForm(ilPropertyFormGUI $form)
called when saving the &#39;edit field&#39; form
static getAllForTableId($table_id)
loadTableFieldSetting()
loadTableFieldSetting
Class ilDclBaseFieldModel.
updateProperties()
Update properties of this field in Database.
getSortField()
Returns the sort-field id.
getPropertyInstance($key)
Return ActiveRecord of property.
static _getFieldIdByTitle($title, $table_id)
$records
Definition: simple_test.php:22
static getTableCache($table_id=0)
fillPropertiesForm(ilPropertyFormGUI &$form)
called to fill the &#39;edit field&#39; form
getStorageLocation()
Get storage location for the model.
static where($where, $operator=null)
setProperty($key, $value)
Set a property for a field (does not save)
static _getDatatypeForId($id)
gives you the datatype id of a specified standard field.
addToTableViews()
create ilDclTableViewFieldSettings for this field in each tableview
setTableId($a_id)
Set table id.
loadExportability()
Load exportability.
loadProperties()
Get all properties of a field.
if(isset($_POST['submit'])) $form
setRequired($a_required)
Set Required.
static _tableExists($table_id)
hasProperty($key)
Checks if a certain property for a field is set.
setStorageLocationOverride($storage_location_override)
cloneProperties(ilDclBaseFieldModel $originalField)
getRecordQueryFilterObject($filter_value="", ilDclBaseFieldModel $sort_field=null)
Returns a query-object for building the record-loader-sql-query.
$values
buildFromDBRecord($rec)
Builds model from db record.
const PROP_LENGTH
General properties.
getRequired()
Get Required Required.
setCell($a_row, $a_col, $a_value, $a_datatype=null)
Set cell value.
$query
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
fillHeaderExcel(ilExcel $worksheet, &$row, &$col)
getDescription()
Get description.
loadDatatype()
Load datatype for model.
updateTableFieldSetting()
update exportable and fieldorder
$row
hasNumericSorting()
Set to true, when the sorting should be handled numerical.
static getRecordFieldCache($record, $field)
getProperty($key)
Returns a certain property of a field.
global $ilDB
getValidFieldProperties()
Returns all valid properties for a field-type.
if(empty($password)) $table
Definition: pwgen.php:24
setTitle($a_title)
Set title.
checkValidityFromForm(ilPropertyFormGUI &$form, $record_id=null)
$key
Definition: croninfo.php:18
static getDatatype($datatyp_id)
Get cached datatypes.
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
getRecordQuerySortObject($direction="asc", $sort_by_status=false)
Returns a query-object for building the record-loader-sql-query.
getDatatypeId()
Get datatype_id.
Confirmation screen class.
static isUtf8($a_str)
Check whether string is utf-8.