ILIAS  release_7 Revision v7.30-3-g800a261c036
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 $order;
45 protected $unique;
49 protected $property = array();
53 protected $exportable;
57 protected $datatype;
65 const PROP_LENGTH = "lenght";
66 const PROP_REGEX = "regex";
67 const PROP_REFERENCE = "table_id";
68 const PROP_URL = "url";
69 const PROP_TEXTAREA = "text_area";
70 const PROP_REFERENCE_LINK = "reference_link";
71 const PROP_WIDTH = "width";
72 const PROP_HEIGHT = "height";
73 const PROP_LEARNING_PROGRESS = "learning_progress";
74 const PROP_ILIAS_REFERENCE_LINK = "ILIAS_reference_link";
75 const PROP_N_REFERENCE = "multiple_selection";
76 const PROP_FORMULA_EXPRESSION = "expression";
77 const PROP_DISPLAY_COPY_LINK_ACTION_MENU = "display_action_menu";
78 const PROP_LINK_DETAIL_PAGE_TEXT = "link_detail_page";
79 const PROP_SUPPORTED_FILE_TYPES = "supported_file_types";
80 const PROP_PLUGIN_HOOK_NAME = "plugin_hook_name";
81 const PROP_TEXT_SELECTION_OPTIONS = "text_selection_options";
82 const PROP_TEXT_SELECTION_TYPE = "text_selection_type";
83 const PROP_DATE_SELECTION_OPTIONS = "date_selection_options";
84 const PROP_DATE_SELECTION_TYPE = "date_selection_type";
85 // type of table il_dcl_view
86 const EDIT_VIEW = 2;
87 const EXPORTABLE_VIEW = 4;
88
89
93 public function __construct($a_id = 0)
94 {
95 if ($a_id != 0) {
96 $this->id = $a_id;
97 $this->doRead();
98 }
99 }
100
101
109 public static function _getTitleInvalidChars($a_as_regex = true)
110 {
111 if ($a_as_regex) {
112 return '/^[^<>\\\\":]*$/i';
113 } else {
114 return '\ < > " :';
115 }
116 }
117
118
125 public static function _getFieldIdByTitle($title, $table_id)
126 {
127 global $DIC;
128 $ilDB = $DIC['ilDB'];
129 $result = $ilDB->query(
130 'SELECT id FROM il_dcl_field WHERE title = ' . $ilDB->quote($title, 'text') . ' AND table_id = '
131 . $ilDB->quote($table_id, 'integer')
132 );
133 $id = 0;
134 while ($rec = $ilDB->fetchAssoc($result)) {
135 $id = $rec['id'];
136 }
137
138 return $id;
139 }
140
141
147 public function setId($a_id)
148 {
149 $this->id = $a_id;
150 }
151
152
158 public function getId()
159 {
160 return $this->id;
161 }
162
163
169 public function setTableId($a_id)
170 {
171 $this->table_id = $a_id;
172 }
173
174
180 public function getTableId()
181 {
182 return $this->table_id;
183 }
184
185
191 public function setTitle($a_title)
192 {
193 //title cannot begin with _ as this is saved for other purposes. make __ instead.
194 if (substr($a_title, 0, 1) == "_" && substr($a_title, 0, 2) != "__") {
195 $a_title = "_" . $a_title;
196 }
197 $this->title = $a_title;
198 }
199
200
206 public function getTitle()
207 {
208 return $this->title;
209 }
210
211
217 public function setDescription($a_desc)
218 {
219 $this->description = $a_desc;
220 }
221
222
228 public function getDescription()
229 {
230 return $this->description;
231 }
232
233
239 public function setDatatypeId($a_id)
240 {
241 //unset the cached datatype.
242 $this->datatype = null;
243 $this->datatypeId = $a_id;
244 }
245
246
252 public function getDatatypeId()
253 {
254 if ($this->isStandardField()) {
256 }
257
258 return $this->datatypeId;
259 }
260
261
265 public function isUnique()
266 {
267 return $this->unique;
268 }
269
270
274 public function setUnique($unique)
275 {
276 $this->unique = $unique ? 1 : 0;
277 }
278
279
283 public function getDatatype()
284 {
285 $this->loadDatatype();
286
287 return $this->datatype;
288 }
289
290
294 public function getDatatypeTitle()
295 {
296 $this->loadDatatype();
297
298 return $this->datatype->getTitle();
299 }
300
301
307 public function getStorageLocation()
308 {
309 if ($this->getStorageLocationOverride() !== null) {
310 return $this->getStorageLocationOverride();
311 }
312
313 $this->loadDatatype();
314
315 return $this->datatype->getStorageLocation();
316 }
317
318
322 protected function loadDatatype()
323 {
324 if ($this->datatype == null) {
325 $this->datatype = ilDclCache::getDatatype($this->datatypeId);
326 }
327 }
328
329
333 protected function loadTableFieldSetting()
334 {
335 $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
336 $this->exportable = $tablefield_setting->isExportable();
337 $this->order = $tablefield_setting->getFieldOrder();
338 }
339
340
344 public function getExportable()
345 {
346 if (!isset($this->exportable)) {
347 $this->loadExportability();
348 }
349
350 return $this->exportable;
351 }
352
353
357 private function loadExportability()
358 {
359 if ($this->exportable == null) {
360 $this->loadTableFieldSetting();
361 }
362 }
363
364
368 public function toArray()
369 {
370 return (array) $this;
371 }
372
373
377 public function isStandardField()
378 {
379 return false;
380 }
381
382
386 public function doRead()
387 {
388 global $DIC;
389 $ilDB = $DIC['ilDB'];
390
391 //THEN 1 ELSE 0 END AS has_options FROM il_dcl_field f WHERE id = ".$ilDB->quote($this->getId(),"integer");
392 $query = "SELECT * FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "integer");
393 $set = $ilDB->query($query);
394 $rec = $ilDB->fetchAssoc($set);
395
396 $this->setTableId($rec["table_id"]);
397 $this->setTitle($rec["title"]);
398 $this->setDescription($rec["description"]);
399 $this->setDatatypeId($rec["datatype_id"]);
400 $this->setUnique($rec["is_unique"]);
401 $this->loadProperties();
402 $this->loadTableFieldSetting();
403 }
404
405
411 public function buildFromDBRecord($rec)
412 {
413 $this->setId($rec["id"]);
414 $this->setTableId($rec["table_id"]);
415 $this->setTitle($rec["title"]);
416 $this->setDescription($rec["description"]);
417 $this->setDatatypeId($rec["datatype_id"]);
418 $this->setUnique($rec["is_unique"]);
419 }
420
421
425 public function doCreate()
426 {
427 global $DIC;
428 $ilDB = $DIC['ilDB'];
429
430 if (!ilDclTable::_tableExists($this->getTableId())) {
431 throw new ilException("The field does not have a related table!");
432 }
433
434 $id = $ilDB->nextId("il_dcl_field");
435 $this->setId($id);
436 $query = "INSERT INTO il_dcl_field (" . "id" . ", table_id" . ", datatype_id" . ", title" . ", description" . ", is_unique"
437 . " ) VALUES (" . $ilDB->quote($this->getId(), "integer") . "," . $ilDB->quote($this->getTableId(), "integer") . ","
438 . $ilDB->quote($this->getDatatypeId(), "integer") . "," . $ilDB->quote($this->getTitle(), "text") . ","
439 . $ilDB->quote($this->getDescription(), "text") . "," . $ilDB->quote($this->isUnique(), "integer") . ")";
440 $ilDB->manipulate($query);
441
443
444 $this->addToTableViews();
445 }
446
447
451 protected function addToTableViews()
452 {
453 foreach (ilDclTableView::getAllForTableId($this->table_id) as $tableview) {
454 $tableview->createFieldSetting($this->id);
455 }
456 }
457
458
462 public function doUpdate()
463 {
464 global $DIC;
465 $ilDB = $DIC['ilDB'];
466
467 $ilDB->update(
468 "il_dcl_field",
469 array(
470 "table_id" => array(
471 "integer",
472 $this->getTableId(),
473 ),
474 "datatype_id" => array(
475 "text",
476 $this->getDatatypeId(),
477 ),
478 "title" => array(
479 "text",
480 $this->getTitle(),
481 ),
482 "description" => array(
483 "text",
484 $this->getDescription(),
485 ),
486 "is_unique" => array(
487 "integer",
488 $this->isUnique(),
489 ),
490 ),
491 array(
492 "id" => array(
493 "integer",
494 $this->getId(),
495 ),
496 )
497 );
499 $this->updateProperties();
500 }
501
502
506 public function updateProperties()
507 {
508 foreach ($this->property as $prop) {
509 $prop->store();
510 }
511 }
512
513
518 protected function updateTableFieldSetting()
519 {
520 $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
521 $tablefield_setting->setExportable($this->exportable);
522 $tablefield_setting->setFieldOrder($this->order);
523 $tablefield_setting->store();
524 }
525
526
530 public function doDelete()
531 {
532 global $DIC;
533 $ilDB = $DIC['ilDB'];
534
535 // delete tablefield setting.
536 ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId())->delete();
537
538 $query = "DELETE FROM il_dcl_field_prop WHERE field_id = " . $ilDB->quote($this->getId(), "text");
539 $ilDB->manipulate($query);
540
541 $query = "DELETE FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "text");
542 $ilDB->manipulate($query);
543
544 foreach ($this->getViewSettings() as $field_setting) {
545 $field_setting->delete();
546 }
547 }
548
552 public function getViewSettings() : array
553 {
554 return ilDclTableViewFieldSetting::where(array('field' => $this->getId()))->get();
555 }
556
561 public function getViewSetting(int $tableview_id) : ilDclTableViewFieldSetting
562 {
563 return ilDclTableViewFieldSetting::where(array('field' => $this->getId(), 'tableview_id' => $tableview_id))->first();
564 }
565
566
570 public function getOrder()
571 {
572 if ($this->order == null) {
573 $this->loadTableFieldSetting();
574 }
575
576 return !$this->order ? 0 : $this->order;
577 }
578
579
583 public function setOrder($order)
584 {
585 $this->order = $order;
586 }
587
588
594 protected function loadProperties()
595 {
596 $this->property = ilDclCache::getFieldProperties($this->getId());
597 }
598
599
607 public function hasProperty($key)
608 {
609 $this->loadProperties();
610
611 return (isset($this->property[$key]) && $this->property[$key]->getValue() != null);
612 }
613
614
622 public function getProperty($key)
623 {
624 $instance = $this->getPropertyInstance($key);
625
626 return ($instance !== null) ? $instance->getValue() : null;
627 }
628
629
637 public function getPropertyInstance($key)
638 {
639 $this->loadProperties();
640 if ($this->hasProperty($key)) {
641 $value = $this->property[$key];
642
643 return $value;
644 }
645
646 return null;
647 }
648
649
656 public function setProperty($key, $value)
657 {
658 $this->loadProperties();
659 if (isset($this->property[$key])) {
660 $this->property[$key]->setValue($value);
661 } else {
663 $property->setName($key);
664 $property->setFieldId($this->getId());
665 $property->setValue($value);
666
667 $this->property[$key] = $property;
668 }
669
670 return $this->property[$key];
671 }
672
673
679 public function getValidFieldProperties()
680 {
681 return array();
682 }
683
684
689 public function checkValidityFromForm(ilPropertyFormGUI &$form, $record_id = null)
690 {
691 $value = $form->getInput('field_' . $this->getId());
692 $this->checkValidity($value, $record_id);
693 }
694
695
705 public function checkValidity($value, $record_id = null)
706 {
707 //Don't check empty values
708 if ($value == null) {
709 return true;
710 }
711
712 if ($this->isUnique()) {
713 $table = ilDclCache::getTableCache($this->getTableId());
714 foreach ($table->getRecords() as $record) {
715 if ($this->normalizeValue($record->getRecordFieldValue($this->getId())) == $this->normalizeValue($value) && ($record->getId() != $record_id || $record_id == 0)) {
717 }
718 }
719 }
720
721 return true;
722 }
723
724
730 protected function normalizeValue($value)
731 {
732 if (is_string($value)) {
733 $value = trim(preg_replace("/\\s+/uism", " ", $value));
734 }
735
736 return $value;
737 }
738
739
745 public function cloneStructure($original_id)
746 {
747 $original = ilDclCache::getFieldCache($original_id);
748 $this->setTitle($original->getTitle());
749 $this->setDatatypeId($original->getDatatypeId());
750 $this->setDescription($original->getDescription());
751 $this->setOrder($original->getOrder());
752 $this->setUnique($original->isUnique());
753 $this->setExportable($original->getExportable());
754 $this->doCreate();
755 $this->cloneProperties($original);
756
757 // mandatory for all cloning functions
758 ilDclCache::setCloneOf($original_id, $this->getId(), ilDclCache::TYPE_FIELD);
759 }
760
761
765 public function afterClone($records)
766 {
767 foreach ($records as $rec) {
768 ilDclCache::getRecordFieldCache($rec, $this)->afterClone();
769 }
770 }
771
772
776 public function cloneProperties(ilDclBaseFieldModel $originalField)
777 {
778 $orgProps = $originalField->getValidFieldProperties();
779 if (count($orgProps) == 0) {
780 return;
781 }
782 foreach ($orgProps as $prop_name) {
783 $fieldprop_obj = new ilDclFieldProperty();
784 $fieldprop_obj->setFieldId($this->getId());
785 $fieldprop_obj->setName($prop_name);
786
787 $value = $originalField->getProperty($prop_name);
788
789 // If reference field, we must reset the referenced field, otherwise it will point to the old ID
791 $value = null;
792 }
793
794 $fieldprop_obj->setValue($value);
795 $fieldprop_obj->create();
796 }
797 }
798
799
803 public function setExportable($exportable)
804 {
805 $this->exportable = $exportable;
806 }
807
808
812 public function allowFilterInListView()
813 {
814 return true;
815 }
816
817
826 public function getRecordQuerySortObject($direction = "asc", $sort_by_status = false)
827 {
828 global $DIC;
829 $ilDB = $DIC['ilDB'];
830
831 $sql_obj = new ilDclRecordQueryObject();
832
833 $select_str = "sort_stloc_{$this->getId()}.value AS field_{$this->getId()}";
834 $join_str
835 = "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 = "
836 . $ilDB->quote($this->getId(), 'integer') . ") ";
837 $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)";
838
839 $sql_obj->setSelectStatement($select_str);
840 $sql_obj->setJoinStatement($join_str);
841 $sql_obj->setOrderStatement("field_{$this->getId()} {$direction}");
842
843 return $sql_obj;
844 }
845
846
855 public function getRecordQueryFilterObject($filter_value = "", ilDclBaseFieldModel $sort_field = null)
856 {
857 return null;
858 }
859
860
866 public function getSortField()
867 {
868 return $this->getTitle();
869 }
870
871
877 public function hasNumericSorting()
878 {
880 return true;
881 }
882
883 return false;
884 }
885
886
895 {
896 return true;
897 }
898
899
904 {
906 }
907
908
913 {
914 $this->storage_location_override = $storage_location_override;
915 }
916
917
923 public function fillHeaderExcel(ilExcel $worksheet, &$row, &$col)
924 {
925 $worksheet->setCell($row, $col, $this->getTitle());
926 $col++;
927 }
928
929
934 public function checkTitlesForImport(array &$titles, array &$import_fields)
935 {
936 foreach ($titles as $k => $title) {
937 if (!ilStr::isUtf8($title)) {
938 $title = utf8_encode($title);
939 }
940 if ($title == $this->getTitle()) {
941 $import_fields[$k] = $this;
942 }
943 }
944 }
945
946
953 {
954 $field_props = $this->getValidFieldProperties();
955 foreach ($field_props as $property) {
957 $value = $form->getInput($representation->getPropertyInputFieldId($property));
958
959 // save non empty values and set them to null, when they already exist. Do not override plugin-hook when already set.
960 if (!empty($value) || ($this->getPropertyInstance($property) != null && $property != self::PROP_PLUGIN_HOOK_NAME)) {
961 $this->setProperty($property, $value)->store();
962 }
963 }
964 }
965
966
974 public function fillPropertiesForm(ilPropertyFormGUI &$form)
975 {
976 $values = array(
977 'table_id' => $this->getTableId(),
978 'field_id' => $this->getId(),
979 'title' => $this->getTitle(),
980 'datatype' => $this->getDatatypeId(),
981 'description' => $this->getDescription(),
982 'unique' => $this->isUnique(),
983 );
984
985 $properties = $this->getValidFieldProperties();
986 foreach ($properties as $prop) {
987 $values['prop_' . $prop] = $this->getProperty($prop);
988 }
989
990 $form->setValuesByArray($values);
991
992 return true;
993 }
994
995
1005 {
1006 return false;
1007 }
1008
1009
1018 {
1019 global $DIC;
1020 $ilConfirmationGUI = new ilConfirmationGUI();
1021 $ilConfirmationGUI->setFormAction($form->getFormAction());
1022 $ilConfirmationGUI->addHiddenItem('confirmed', 1);
1023 $ilConfirmationGUI->addHiddenItem('field_id', $form->getInput('field_id'));
1024 $ilConfirmationGUI->addHiddenItem('title', $form->getInput('title'));
1025 $ilConfirmationGUI->addHiddenItem('description', $form->getInput('description'));
1026 $ilConfirmationGUI->addHiddenItem('datatype', $form->getInput('datatype'));
1027 $ilConfirmationGUI->addHiddenItem('unique', $form->getInput('unique'));
1028 $ilConfirmationGUI->setConfirm($DIC->language()->txt('dcl_update_field'), 'update');
1029 $ilConfirmationGUI->setCancel($DIC->language()->txt('cancel'), 'edit');
1030
1031 return $ilConfirmationGUI;
1032 }
1033}
$result
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
Confirmation screen class.
Class ilDclBaseFieldModel.
getViewSetting(int $tableview_id)
doDelete()
Remove field and properties.
fillHeaderExcel(ilExcel $worksheet, &$row, &$col)
updateTableFieldSetting()
update exportable and fieldorder
setDatatypeId($a_id)
Set datatype id.
updateProperties()
Update properties of this field in Database.
getPropertyInstance($key)
Return ActiveRecord of property.
getRecordQuerySortObject($direction="asc", $sort_by_status=false)
Returns a query-object for building the record-loader-sql-query.
checkValidityFromForm(ilPropertyFormGUI &$form, $record_id=null)
fillPropertiesForm(ilPropertyFormGUI &$form)
called to fill the 'edit field' form
loadTableFieldSetting()
loadTableFieldSetting
setProperty($key, $value)
Set a property for a field (does not save)
setDescription($a_desc)
Set description.
static _getTitleInvalidChars($a_as_regex=true)
All valid chars for filed titles.
loadDatatype()
Load datatype for model.
getConfirmationGUI(ilPropertyFormGUI $form)
called by ilDclFieldEditGUI if isConfirmationRequired returns true
addToTableViews()
create ilDclTableViewFieldSettings for this field in each tableview
storePropertiesFromForm(ilPropertyFormGUI $form)
called when saving the 'edit field' form
isConfirmationRequired(ilPropertyFormGUI $form)
called by ilDclFieldEditGUI when updating field properties if you overwrite this method,...
getRecordQueryFilterObject($filter_value="", ilDclBaseFieldModel $sort_field=null)
Returns a query-object for building the record-loader-sql-query.
getValidFieldProperties()
Returns all valid properties for a field-type.
static _getFieldIdByTitle($title, $table_id)
hasProperty($key)
Checks if a certain property for a field is set.
cloneProperties(ilDclBaseFieldModel $originalField)
getProperty($key)
Returns a certain property of a field.
loadProperties()
Get all properties of a field.
getStorageLocation()
Get storage location for the model.
loadExportability()
Load exportability.
buildFromDBRecord($rec)
Builds model from db record.
hasNumericSorting()
Set to true, when the sorting should be handled numerical.
checkTitlesForImport(array &$titles, array &$import_fields)
const PROP_LENGTH
General properties.
getSortField()
Returns the sort-field id.
checkValidity($value, $record_id=null)
Check if input is valid.
setStorageLocationOverride($storage_location_override)
setTableId($a_id)
Set table id.
checkFieldCreationInput(ilPropertyFormGUI $form)
Checks input of specific fields befor saving.
static getDatatype($datatyp_id)
Get cached datatypes.
static getRecordFieldCache($record, $field)
static getFieldProperties($field_id)
Cache Field properties.
static getTableCache($table_id=0)
static setCloneOf($old, $new, $type)
static getFieldCache($field_id=0)
static getFieldRepresentationInstance(ilDclBaseFieldModel $field)
Returns FieldRepresentation from BaseFieldModel.
Class ilDclFieldProperty.
Class ilDclBaseFieldModel.
Class ilDclRecordQueryObject.
static _getDatatypeForId($id)
gives you the datatype id of a specified standard field.
static getInstance($table_id, $field)
Class ilDclTableViewFieldSetting.
static getAllForTableId($table_id)
static _tableExists($table_id)
setCell($a_row, $a_col, $a_value, $a_datatype=null)
Set cell value.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
getFormAction()
Get FormAction.
This class represents a property form user interface.
setValuesByArray($a_values, $a_restrict_to_value_keys=false)
Set form values from an array.
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
static isUtf8($a_str)
Check whether string is utf-8.
global $DIC
Definition: goto.php:24
$query
global $ilDB