ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
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;
66
71
75 const PROP_LENGTH = "lenght";
76 const PROP_REGEX = "regex";
77 const PROP_REFERENCE = "table_id";
78 const PROP_URL = "url";
79 const PROP_TEXTAREA = "text_area";
80 const PROP_REFERENCE_LINK = "reference_link";
81 const PROP_WIDTH = "width";
82 const PROP_HEIGHT = "height";
83 const PROP_LEARNING_PROGRESS = "learning_progress";
84 const PROP_ILIAS_REFERENCE_LINK = "ILIAS_reference_link";
85 const PROP_N_REFERENCE = "multiple_selection";
86 const PROP_FORMULA_EXPRESSION = "expression";
87 const PROP_DISPLAY_COPY_LINK_ACTION_MENU = "display_action_menu";
88 const PROP_LINK_DETAIL_PAGE_TEXT = "link_detail_page";
89 const PROP_SUPPORTED_FILE_TYPES = "supported_file_types";
90 const PROP_PLUGIN_HOOK_NAME = "plugin_hook_name";
91 const PROP_TEXT_SELECTION_OPTIONS = "text_selection_options";
92 const PROP_TEXT_SELECTION_TYPE = "text_selection_type";
93 const PROP_DATE_SELECTION_OPTIONS = "date_selection_options";
94 const PROP_DATE_SELECTION_TYPE = "date_selection_type";
95
96
97 // type of table il_dcl_view
98 const EDIT_VIEW = 2;
99 const EXPORTABLE_VIEW = 4;
100
104 public function __construct($a_id = 0)
105 {
106 if ($a_id != 0) {
107 $this->id = $a_id;
108 $this->doRead();
109 }
110 }
111
112
120 public static function _getTitleInvalidChars($a_as_regex = true)
121 {
122 if ($a_as_regex) {
123 return '/^[^<>\\\\":]*$/i';
124 } else {
125 return '\ < > " :';
126 }
127 }
128
129
136 public static function _getFieldIdByTitle($title, $table_id)
137 {
138 global $DIC;
139 $ilDB = $DIC['ilDB'];
140 $result = $ilDB->query('SELECT id FROM il_dcl_field WHERE title = ' . $ilDB->quote($title, 'text') . ' AND table_id = '
141 . $ilDB->quote($table_id, 'integer'));
142 $id = 0;
143 while ($rec = $ilDB->fetchAssoc($result)) {
144 $id = $rec['id'];
145 }
146
147 return $id;
148 }
149
150
156 public function setId($a_id)
157 {
158 $this->id = $a_id;
159 }
160
161
167 public function getId()
168 {
169 return $this->id;
170 }
171
172
178 public function setTableId($a_id)
179 {
180 $this->table_id = $a_id;
181 }
182
183
189 public function getTableId()
190 {
191 return $this->table_id;
192 }
193
194
200 public function setTitle($a_title)
201 {
202 //title cannot begin with _ as this is saved for other purposes. make __ instead.
203 if (substr($a_title, 0, 1) == "_" && substr($a_title, 0, 2) != "__") {
204 $a_title = "_" . $a_title;
205 }
206 $this->title = $a_title;
207 }
208
209
215 public function getTitle()
216 {
217 return $this->title;
218 }
219
220
226 public function setDescription($a_desc)
227 {
228 $this->description = $a_desc;
229 }
230
231
237 public function getDescription()
238 {
239 return $this->description;
240 }
241
242
248 public function setDatatypeId($a_id)
249 {
250 //unset the cached datatype.
251 $this->datatype = null;
252 $this->datatypeId = $a_id;
253 }
254
255
261 public function getDatatypeId()
262 {
263 if ($this->isStandardField()) {
265 }
266
267 return $this->datatypeId;
268 }
269
270
276 public function setRequired($a_required)
277 {
278 $this->required = $a_required;
279 }
280
281
287 public function getRequired()
288 {
289 return $this->required;
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
312 public function getDatatype()
313 {
314 $this->loadDatatype();
315
316 return $this->datatype;
317 }
318
319
323 public function getDatatypeTitle()
324 {
325 $this->loadDatatype();
326
327 return $this->datatype->getTitle();
328 }
329
330
335 public function getStorageLocation()
336 {
337 if ($this->getStorageLocationOverride() !== null) {
338 return $this->getStorageLocationOverride();
339 }
340
341 $this->loadDatatype();
342 return $this->datatype->getStorageLocation();
343 }
344
345
349 protected function loadDatatype()
350 {
351 if ($this->datatype == null) {
352 $this->datatype = ilDclCache::getDatatype($this->datatypeId);
353 }
354 }
355
359 protected function loadTableFieldSetting()
360 {
361 $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
362 $this->exportable = $tablefield_setting->isExportable();
363 $this->order = $tablefield_setting->getFieldOrder();
364 }
365
369 public function getExportable()
370 {
371 if (!isset($this->exportable)) {
372 $this->loadExportability();
373 }
374
375 return $this->exportable;
376 }
377
381 private function loadExportability()
382 {
383 if ($this->exportable == null) {
384 $this->loadTableFieldSetting();
385 }
386 }
387
388
392 public function toArray()
393 {
394 return (array) $this;
395 }
396
397
401 public function isStandardField()
402 {
403 return false;
404 }
405
406
410 public function doRead()
411 {
412 global $DIC;
413 $ilDB = $DIC['ilDB'];
414
415 //THEN 1 ELSE 0 END AS has_options FROM il_dcl_field f WHERE id = ".$ilDB->quote($this->getId(),"integer");
416 $query = "SELECT * FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "integer");
417 $set = $ilDB->query($query);
418 $rec = $ilDB->fetchAssoc($set);
419
420 $this->setTableId($rec["table_id"]);
421 $this->setTitle($rec["title"]);
422 $this->setDescription($rec["description"]);
423 $this->setDatatypeId($rec["datatype_id"]);
424 $this->setRequired($rec["required"]);
425 $this->setUnique($rec["is_unique"]);
426 $this->setLocked($rec["is_locked"]);
427 $this->loadProperties();
428 $this->loadTableFieldSetting();
429 }
430
431
436 public function buildFromDBRecord($rec)
437 {
438 $this->setId($rec["id"]);
439 $this->setTableId($rec["table_id"]);
440 $this->setTitle($rec["title"]);
441 $this->setDescription($rec["description"]);
442 $this->setDatatypeId($rec["datatype_id"]);
443 $this->setRequired($rec["required"]);
444 $this->setUnique($rec["is_unique"]);
445 $this->setLocked($rec["is_locked"]);
446 }
447
448
452 public function doCreate()
453 {
454 global $DIC;
455 $ilDB = $DIC['ilDB'];
456 $this->getLocked() == null ? $this->setLocked(false) : true;
457
458 if (!ilDclTable::_tableExists($this->getTableId())) {
459 throw new ilException("The field does not have a related table!");
460 }
461
462 $id = $ilDB->nextId("il_dcl_field");
463 $this->setId($id);
464 $query = "INSERT INTO il_dcl_field (" . "id" . ", table_id" . ", datatype_id" . ", title" . ", description" . ", required" . ", is_unique"
465 . ", is_locked" . " ) VALUES (" . $ilDB->quote($this->getId(), "integer") . "," . $ilDB->quote($this->getTableId(), "integer") . ","
466 . $ilDB->quote($this->getDatatypeId(), "integer") . "," . $ilDB->quote($this->getTitle(), "text") . ","
467 . $ilDB->quote($this->getDescription(), "text") . "," . $ilDB->quote($this->getRequired(), "integer") . ","
468 . $ilDB->quote($this->isUnique(), "integer") . "," . $ilDB->quote($this->getLocked() ? 1 : 0, "integer") . ")";
469 $ilDB->manipulate($query);
470
472
473 $this->addToTableViews();
474 }
475
479 protected function addToTableViews()
480 {
481 foreach (ilDclTableView::getAllForTableId($this->table_id) as $tableview) {
482 $tableview->createFieldSetting($this->id);
483 }
484 }
485
486
490 public function doUpdate()
491 {
492 global $DIC;
493 $ilDB = $DIC['ilDB'];
494
495 $ilDB->update("il_dcl_field", array(
496 "table_id" => array(
497 "integer",
498 $this->getTableId()
499 ),
500 "datatype_id" => array(
501 "text",
502 $this->getDatatypeId()
503 ),
504 "title" => array(
505 "text",
506 $this->getTitle()
507 ),
508 "description" => array(
509 "text",
510 $this->getDescription()
511 ),
512 "required" => array(
513 "integer",
514 $this->getRequired()
515 ),
516 "is_unique" => array(
517 "integer",
518 $this->isUnique()
519 ),
520 "is_locked" => array(
521 "integer",
522 $this->getLocked() ? 1 : 0
523 ),
524 ), array(
525 "id" => array(
526 "integer",
527 $this->getId()
528 )
529 ));
531 $this->updateProperties();
532 }
533
534
538 public function updateProperties()
539 {
540 foreach ($this->property as $prop) {
541 $prop->store();
542 }
543 }
544
545
550 protected function updateTableFieldSetting()
551 {
552 $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
553 $tablefield_setting->setExportable($this->exportable);
554 $tablefield_setting->setFieldOrder($this->order);
555 $tablefield_setting->store();
556 }
557
561 public function doDelete()
562 {
563 global $DIC;
564 $ilDB = $DIC['ilDB'];
565
566 // delete tablefield setting.
567 ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId())->delete();
568
569 $query = "DELETE FROM il_dcl_field_prop WHERE field_id = " . $ilDB->quote($this->getId(), "text");
570 $ilDB->manipulate($query);
571
572 $query = "DELETE FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "text");
573 $ilDB->manipulate($query);
574
575 foreach ($this->getFieldSettings() as $field_setting) {
576 $field_setting->delete();
577 }
578 }
579
580 public function getFieldSettings()
581 {
582 return ilDclTableViewFieldSetting::where(array('field' => $this->getId()))->get();
583 }
584
588 public function getOrder()
589 {
590 if ($this->order == null) {
591 $this->loadTableFieldSetting();
592 }
593
594 return !$this->order ? 0 : $this->order;
595 }
596
597
601 public function setOrder($order)
602 {
603 $this->order = $order;
604 }
605
611 protected function loadProperties()
612 {
613 $this->property = ilDclCache::getFieldProperties($this->getId());
614 }
615
622 public function hasProperty($key)
623 {
624 $this->loadProperties();
625 return (isset($this->property[$key]) && $this->property[$key]->getValue() != null);
626 }
627
628
635 public function getProperty($key)
636 {
637 $instance = $this->getPropertyInstance($key);
638
639 return ($instance !== null)? $instance->getValue() : null;
640 }
641
642
650 public function getPropertyInstance($key)
651 {
652 $this->loadProperties();
653 if ($this->hasProperty($key)) {
654 $value = $this->property[$key];
655 return $value;
656 }
657 return null;
658 }
659
660
667 public function setProperty($key, $value)
668 {
669 $this->loadProperties();
670 if (isset($this->property[$key])) {
671 $this->property[$key]->setValue($value);
672 } else {
674 $property->setName($key);
675 $property->setFieldId($this->getId());
676 $property->setValue($value);
677
678 $this->property[$key] = $property;
679 }
680
681 return $this->property[$key];
682 }
683
684
689 public function getValidFieldProperties()
690 {
691 return array();
692 }
693
697 public function setLocked($locked)
698 {
699 $this->locked = $locked;
700 }
701
702
706 public function getLocked()
707 {
708 return $this->locked;
709 }
710
711
716 public function checkValidityFromForm(ilPropertyFormGUI &$form, $record_id = null)
717 {
718 $value = $form->getInput('field_' . $this->getId());
719 $this->checkValidity($value, $record_id);
720 }
729 public function checkValidity($value, $record_id = null)
730 {
731 //Don't check empty values
732 if ($value == null) {
733 return true;
734 }
735
736 if ($this->isUnique()) {
738 foreach ($table->getRecords() as $record) {
739 if ($this->normalizeValue($record->getRecordFieldValue($this->getId())) == $this->normalizeValue($value) &&
740 ($record->getId() != $record_id || $record_id == 0)) {
742 }
743 }
744 }
745
746 return true;
747 }
748
749
755 protected function normalizeValue($value)
756 {
757 if (is_string($value)) {
758 $value = trim(preg_replace("/\\s+/uism", " ", $value));
759 }
760
761 return $value;
762 }
763
764
770 public function cloneStructure($original_id)
771 {
772 $original = ilDclCache::getFieldCache($original_id);
773 $this->setTitle($original->getTitle());
774 $this->setDatatypeId($original->getDatatypeId());
775 $this->setDescription($original->getDescription());
776 $this->setLocked($original->getLocked());
777 $this->setOrder($original->getOrder());
778 $this->setRequired($original->getRequired());
779 $this->setUnique($original->isUnique());
780 $this->setExportable($original->getExportable());
781 $this->doCreate();
782 $this->cloneProperties($original);
783
784 // mandatory for all cloning functions
785 ilDclCache::setCloneOf($original_id, $this->getId(), ilDclCache::TYPE_FIELD);
786 }
787
788
792 public function afterClone($records)
793 {
794 foreach ($records as $rec) {
795 ilDclCache::getRecordFieldCache($rec, $this)->afterClone();
796 }
797 }
798
802 public function cloneProperties(ilDclBaseFieldModel $originalField)
803 {
804 $orgProps = $originalField->getValidFieldProperties();
805 if (count($orgProps) == 0) {
806 return;
807 }
808 foreach ($orgProps as $prop_name) {
809 $fieldprop_obj = new ilDclFieldProperty();
810 $fieldprop_obj->setFieldId($this->getId());
811 $fieldprop_obj->setName($prop_name);
812
813 $value = $originalField->getProperty($prop_name);
814
815 // If reference field, we must reset the referenced field, otherwise it will point to the old ID
817 $value = null;
818 }
819
820 $fieldprop_obj->setValue($value);
821 $fieldprop_obj->create();
822 }
823 }
824
825
829 public function setExportable($exportable)
830 {
831 $this->exportable = $exportable;
832 }
833
837 public function allowFilterInListView()
838 {
839 return true;
840 }
841
842
851 public function getRecordQuerySortObject($direction = "asc", $sort_by_status = false)
852 {
853 global $DIC;
854 $ilDB = $DIC['ilDB'];
855
856 $sql_obj = new ilDclRecordQueryObject();
857
858 $select_str = "sort_stloc_{$this->getId()}.value AS field_{$this->getId()}";
859 $join_str = "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 = "
860 . $ilDB->quote($this->getId(), 'integer') . ") ";
861 $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)";
862
863 $sql_obj->setSelectStatement($select_str);
864 $sql_obj->setJoinStatement($join_str);
865 $sql_obj->setOrderStatement("field_{$this->getId()} {$direction}");
866
867 return $sql_obj;
868 }
869
870
878 public function getRecordQueryFilterObject($filter_value = "", ilDclBaseFieldModel $sort_field = null)
879 {
880 return null;
881 }
882
883
888 public function getSortField()
889 {
890 return $this->getTitle();
891 }
892
893
898 public function hasNumericSorting()
899 {
901 return true;
902 }
903 return false;
904 }
905
906
914 {
915 return true;
916 }
917
918
923 {
925 }
926
927
932 {
933 $this->storage_location_override = $storage_location_override;
934 }
935
936
942 public function fillHeaderExcel(ilExcel $worksheet, &$row, &$col)
943 {
944 $worksheet->setCell($row, $col, $this->getTitle());
945 $col++;
946 }
947
948
953 public function checkTitlesForImport(array &$titles, array &$import_fields)
954 {
955 foreach ($titles as $k => $title) {
956 if (!ilStr::isUtf8($title)) {
957 $title = utf8_encode($title);
958 }
959 if ($title == $this->getTitle()) {
960 $import_fields[$k] = $this;
961 }
962 }
963 }
964
965
972 {
973 $field_props = $this->getValidFieldProperties();
974 foreach ($field_props as $property) {
976 $value = $form->getInput($representation->getPropertyInputFieldId($property));
977
978 // save non empty values and set them to null, when they already exist. Do not override plugin-hook when already set.
979 if (!empty($value) || ($this->getPropertyInstance($property) != null && $property != self::PROP_PLUGIN_HOOK_NAME)) {
980 $this->setProperty($property, $value)->store();
981 }
982 }
983 }
984
985
994 {
995 $values = array(
996 'table_id' => $this->getTableId(),
997 'field_id' => $this->getId(),
998 'title' => $this->getTitle(),
999 'datatype' => $this->getDatatypeId(),
1000 'description' => $this->getDescription(),
1001 'required' => $this->getRequired(),
1002 'unique' => $this->isUnique(),
1003 );
1004
1005 $properties = $this->getValidFieldProperties();
1006 foreach ($properties as $prop) {
1007 $values['prop_' . $prop] = $this->getProperty($prop);
1008 }
1009
1010 $form->setValuesByArray($values);
1011
1012 return true;
1013 }
1014
1015
1025 {
1026 return false;
1027 }
1028
1029
1038 {
1039 global $DIC;
1040 $ilConfirmationGUI = new ilConfirmationGUI();
1041 $ilConfirmationGUI->setFormAction($form->getFormAction());
1042 $ilConfirmationGUI->addHiddenItem('confirmed', 1);
1043 $ilConfirmationGUI->addHiddenItem('field_id', $form->getInput('field_id'));
1044 $ilConfirmationGUI->addHiddenItem('title', $form->getInput('title'));
1045 $ilConfirmationGUI->addHiddenItem('description', $form->getInput('description'));
1046 $ilConfirmationGUI->addHiddenItem('datatype', $form->getInput('datatype'));
1047 $ilConfirmationGUI->addHiddenItem('required', $form->getInput('required'));
1048 $ilConfirmationGUI->addHiddenItem('unique', $form->getInput('unique'));
1049 $ilConfirmationGUI->setConfirm($DIC->language()->txt('dcl_update_field'), 'update');
1050 $ilConfirmationGUI->setCancel($DIC->language()->txt('cancel'), 'edit');
1051 return $ilConfirmationGUI;
1052 }
1053}
$worksheet
$result
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
Confirmation screen class.
Class ilDclBaseFieldModel.
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.
setRequired($a_required)
Set Required.
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)
getRequired()
Get Required Required.
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)
static getAllForTableId($table_id)
static _tableExists($table_id)
Base class for ILIAS Exception handling.
This class represents a property form user interface.
static isUtf8($a_str)
Check whether string is utf-8.
$key
Definition: croninfo.php:18
$query
if(empty($password)) $table
Definition: pwgen.php:24
if(isset($_POST['submit'])) $form
global $DIC
Definition: saml.php:7
$records
Definition: simple_test.php:22
global $ilDB