ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
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
4require_once('./Modules/DataCollection/classes/Fields/Base/class.ilDclFieldProperty.php');
5require_once('./Services/Exceptions/classes/class.ilException.php');
6require_once('./Modules/DataCollection/classes/Helpers/class.ilDclCache.php');
7require_once('./Modules/DataCollection/classes/Helpers/class.ilDclRecordQueryObject.php');
8require_once('./Modules/DataCollection/classes/Table/class.ilDclTableFieldSetting.php');
9
22
26 protected $id;
30 protected $table_id;
34 protected $title;
38 protected $description;
42 protected $datatypeId;
46 protected $required;
50 protected $order;
54 protected $unique;
58 protected $locked;
62 protected $property = array();
66 protected $exportable;
70 protected $datatype;
71
76
80 const PROP_LENGTH = "lenght";
81 const PROP_REGEX = "regex";
82 const PROP_REFERENCE = "table_id";
83 const PROP_URL = "url";
84 const PROP_TEXTAREA = "text_area";
85 const PROP_REFERENCE_LINK = "reference_link";
86 const PROP_WIDTH = "width";
87 const PROP_HEIGHT = "height";
88 const PROP_LEARNING_PROGRESS = "learning_progress";
89 const PROP_ILIAS_REFERENCE_LINK = "ILIAS_reference_link";
90 const PROP_N_REFERENCE = "multiple_selection";
91 const PROP_FORMULA_EXPRESSION = "expression";
92 const PROP_DISPLAY_COPY_LINK_ACTION_MENU = "display_action_menu";
93 const PROP_LINK_DETAIL_PAGE_TEXT = "link_detail_page";
94 const PROP_SUPPORTED_FILE_TYPES = "supported_file_types";
95 const PROP_PLUGIN_HOOK_NAME = "plugin_hook_name";
96
97
98 // type of table il_dcl_view
99 const EDIT_VIEW = 2;
101
105 public function __construct($a_id = 0) {
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 if ($a_as_regex) {
122 return '/^[^<>\\\\":]*$/i';
123 } else {
124 return '\ < > " :';
125 }
126 }
127
128
135 public static function _getFieldIdByTitle($title, $table_id) {
136 global $DIC;
137 $ilDB = $DIC['ilDB'];
138 $result = $ilDB->query('SELECT id FROM il_dcl_field WHERE title = ' . $ilDB->quote($title, 'text') . ' AND table_id = '
139 . $ilDB->quote($table_id, 'integer'));
140 $id = 0;
141 while ($rec = $ilDB->fetchAssoc($result)) {
142 $id = $rec['id'];
143 }
144
145 return $id;
146 }
147
148
154 public function setId($a_id) {
155 $this->id = $a_id;
156 }
157
158
164 public function getId() {
165 return $this->id;
166 }
167
168
174 public function setTableId($a_id) {
175 $this->table_id = $a_id;
176 }
177
178
184 public function getTableId() {
185 return $this->table_id;
186 }
187
188
194 public function setTitle($a_title) {
195 //title cannot begin with _ as this is saved for other purposes. make __ instead.
196 if (substr($a_title, 0, 1) == "_" && substr($a_title, 0, 2) != "__") {
197 $a_title = "_" . $a_title;
198 }
199 $this->title = $a_title;
200 }
201
202
208 public function getTitle() {
209 return $this->title;
210 }
211
212
218 public function setDescription($a_desc) {
219 $this->description = $a_desc;
220 }
221
222
228 public function getDescription() {
229 return $this->description;
230 }
231
232
238 public function setDatatypeId($a_id) {
239 //unset the cached datatype.
240 $this->datatype = NULL;
241 $this->datatypeId = $a_id;
242 }
243
244
250 public function getDatatypeId() {
251 if ($this->isStandardField()) {
253 }
254
255 return $this->datatypeId;
256 }
257
258
264 public function setRequired($a_required) {
265 $this->required = $a_required;
266 }
267
268
274 public function getRequired() {
275 return $this->required;
276 }
277
281 public function isUnique() {
282 return $this->unique;
283 }
284
285
289 public function setUnique($unique) {
290 $this->unique = $unique ? 1 : 0;
291 }
292
296 public function getDatatype() {
297 $this->loadDatatype();
298
299 return $this->datatype;
300 }
301
302
306 public function getDatatypeTitle() {
307 $this->loadDatatype();
308
309 return $this->datatype->getTitle();
310 }
311
312
317 public function getStorageLocation() {
318 if($this->getStorageLocationOverride() !== null) {
319 return $this->getStorageLocationOverride();
320 }
321
322 $this->loadDatatype();
323 return $this->datatype->getStorageLocation();
324 }
325
326
330 protected function loadDatatype() {
331 if ($this->datatype == NULL) {
332 $this->datatype = ilDclCache::getDatatype($this->datatypeId);
333 }
334 }
335
339 protected function loadTableFieldSetting() {
340 $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
341 $this->exportable = $tablefield_setting->isExportable();
342 $this->order = $tablefield_setting->getFieldOrder();
343 }
344
348 public function getExportable() {
349 if (! isset($this->exportable)) {
350 $this->loadExportability();
351 }
352
353 return $this->exportable;
354 }
355
359 private function loadExportability() {
360 if ($this->exportable == NULL) {
361 $this->loadTableFieldSetting();
362 }
363 }
364
365
369 public function toArray() {
370 return (array)$this;
371 }
372
373
377 public function isStandardField() {
378 return false;
379 }
380
381
385 public function doRead() {
386 global $DIC;
387 $ilDB = $DIC['ilDB'];
388
389 //THEN 1 ELSE 0 END AS has_options FROM il_dcl_field f WHERE id = ".$ilDB->quote($this->getId(),"integer");
390 $query = "SELECT * FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "integer");
391 $set = $ilDB->query($query);
392 $rec = $ilDB->fetchAssoc($set);
393
394 $this->setTableId($rec["table_id"]);
395 $this->setTitle($rec["title"]);
396 $this->setDescription($rec["description"]);
397 $this->setDatatypeId($rec["datatype_id"]);
398 $this->setRequired($rec["required"]);
399 $this->setUnique($rec["is_unique"]);
400 $this->setLocked($rec["is_locked"]);
401 $this->loadProperties();
402 $this->loadTableFieldSetting();
403 }
404
405
410 public function buildFromDBRecord($rec) {
411 $this->setId($rec["id"]);
412 $this->setTableId($rec["table_id"]);
413 $this->setTitle($rec["title"]);
414 $this->setDescription($rec["description"]);
415 $this->setDatatypeId($rec["datatype_id"]);
416 $this->setRequired($rec["required"]);
417 $this->setUnique($rec["is_unique"]);
418 $this->setLocked($rec["is_locked"]);
419 }
420
421
425 public function doCreate() {
426 global $DIC;
427 $ilDB = $DIC['ilDB'];
428 $this->getLocked() == NULL ? $this->setLocked(false) : true;
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" . ", required" . ", is_unique"
437 . ", is_locked" . " ) 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->getRequired(), "integer") . ","
440 . $ilDB->quote($this->isUnique(), "integer") . "," . $ilDB->quote($this->getLocked() ? 1 : 0, "integer") . ")";
441 $ilDB->manipulate($query);
442
444
445 $this->addToTableViews();
446 }
447
451 protected function addToTableViews()
452 {
453 foreach (ilDclTableView::getAllForTableId($this->table_id) as $tableview)
454 {
455 $tableview->createFieldSetting($this->id);
456 }
457 }
458
459
463 public function doUpdate() {
464 global $DIC;
465 $ilDB = $DIC['ilDB'];
466
467 $ilDB->update("il_dcl_field", array(
468 "table_id" => array(
469 "integer",
470 $this->getTableId()
471 ),
472 "datatype_id" => array(
473 "text",
474 $this->getDatatypeId()
475 ),
476 "title" => array(
477 "text",
478 $this->getTitle()
479 ),
480 "description" => array(
481 "text",
482 $this->getDescription()
483 ),
484 "required" => array(
485 "integer",
486 $this->getRequired()
487 ),
488 "is_unique" => array(
489 "integer",
490 $this->isUnique()
491 ),
492 "is_locked" => array(
493 "integer",
494 $this->getLocked() ? 1 : 0
495 ),
496 ), array(
497 "id" => array(
498 "integer",
499 $this->getId()
500 )
501 ));
503 $this->updateProperties();
504 }
505
506
510 public function updateProperties() {
511 foreach ($this->property as $prop) {
512 $prop->store();
513 }
514 }
515
516
521 protected function updateTableFieldSetting() {
522 $tablefield_setting = ilDclTableFieldSetting::getInstance($this->getTableId(), $this->getId());
523 $tablefield_setting->setExportable($this->exportable);
524 $tablefield_setting->setFieldOrder($this->order);
525 $tablefield_setting->store();
526 }
527
531 public function doDelete() {
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->getFieldSettings() as $field_setting)
545 {
546 $field_setting->delete();
547 }
548 }
549
550 public function getFieldSettings() {
551 return ilDclTableViewFieldSetting::where(array('field' => $this->getId()))->get();
552 }
553
557 public function getOrder() {
558 if ($this->order == null) {
559 $this->loadTableFieldSetting();
560 }
561
562 return ! $this->order ? 0 : $this->order;
563 }
564
565
569 public function setOrder($order) {
570 $this->order = $order;
571 }
572
578 protected function loadProperties() {
579 $this->property = ilDclCache::getFieldProperties($this->getId());
580 }
581
588 public function hasProperty($key) {
589 $this->loadProperties();
590 return (isset($this->property[$key]) && $this->property[$key]->getValue() != NULL);
591 }
592
593
600 public function getProperty($key) {
601 $instance = $this->getPropertyInstance($key);
602
603 return ($instance !== NULL)? $instance->getValue() : null;
604 }
605
606
614 public function getPropertyInstance($key) {
615 $this->loadProperties();
616 if($this->hasProperty($key)) {
617 $value = $this->property[$key];
618 return $value;
619 }
620 return null;
621 }
622
623
630 public function setProperty($key, $value) {
631 $this->loadProperties();
632 if(isset($this->property[$key])) {
633 $this->property[$key]->setValue($value);
634 } else {
636 $property->setName($key);
637 $property->setFieldId($this->getId());
638 $property->setValue($value);
639
640 $this->property[$key] = $property;
641 }
642
643 return $this->property[$key];
644 }
645
646
651 public function getValidFieldProperties() {
652 return array();
653 }
654
658 public function setLocked($locked) {
659 $this->locked = $locked;
660 }
661
662
666 public function getLocked() {
667 return $this->locked;
668 }
669
670
675 public function checkValidityFromForm(ilPropertyFormGUI &$form, $record_id = NULL) {
676 $value = $form->getInput('field_' . $this->getId());
677 $this->checkValidity($value, $record_id);
678 }
687 public function checkValidity($value, $record_id = NULL) {
688 //Don't check empty values
689 if ($value == NULL) {
690 return true;
691 }
692
693 if ($this->isUnique()) {
694 $table = ilDclCache::getTableCache($this->getTableId());
695 foreach ($table->getRecords() as $record) {
696 if ($this->normalizeValue($record->getRecordFieldValue($this->getId())) == $this->normalizeValue($value) &&
697 ($record->getId() != $record_id || $record_id == 0)) {
699 }
700 }
701 }
702
703 return true;
704 }
705
706
712 protected function normalizeValue($value) {
713 if (is_string($value)) {
714 $value = trim(preg_replace("/\\s+/uism", " ", $value));
715 }
716
717 return $value;
718 }
719
720
726 public function cloneStructure($original_id) {
727 $original = ilDclCache::getFieldCache($original_id);
728 $this->setTitle($original->getTitle());
729 $this->setDatatypeId($original->getDatatypeId());
730 $this->setDescription($original->getDescription());
731 $this->setLocked($original->getLocked());
732 $this->setOrder($original->getOrder());
733 $this->setRequired($original->getRequired());
734 $this->setUnique($original->isUnique());
735 $this->setExportable($original->getExportable());
736 $this->doCreate();
737 $this->cloneProperties($original);
738
739 // mandatory for all cloning functions
740 ilDclCache::setCloneOf($original_id, $this->getId(), ilDclCache::TYPE_FIELD);
741 }
742
743
747 public function afterClone($records) {
748 foreach ($records as $rec) {
749 ilDclCache::getRecordFieldCache($rec, $this)->afterClone();
750 }
751 }
752
756 public function cloneProperties(ilDclBaseFieldModel $originalField) {
757 $orgProps = $originalField->getValidFieldProperties();
758 if (count($orgProps) == 0) {
759 return;
760 }
761 foreach ($orgProps as $prop_name) {
762 $fieldprop_obj = new ilDclFieldProperty();
763 $fieldprop_obj->setFieldId($this->getId());
764 $fieldprop_obj->setName($prop_name);
765
766 $value = $originalField->getProperty($prop_name);
767
768 // If reference field, we must reset the referenced field, otherwise it will point to the old ID
770 $value = NULL;
771 }
772
773 $fieldprop_obj->setValue($value);
774 $fieldprop_obj->create();
775 }
776 }
777
778
782 public function setExportable($exportable) {
783 $this->exportable = $exportable;
784 }
785
789 public function allowFilterInListView() {
790 return true;
791 }
792
793
802 public function getRecordQuerySortObject($direction = "asc", $sort_by_status = false){
803 global $DIC;
804 $ilDB = $DIC['ilDB'];
805
806 $sql_obj = new ilDclRecordQueryObject();
807
808 $select_str = "sort_stloc_{$this->getId()}.value AS field_{$this->getId()}";
809 $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 = "
810 . $ilDB->quote($this->getId(), 'integer') . ") ";
811 $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)";
812
813 $sql_obj->setSelectStatement($select_str);
814 $sql_obj->setJoinStatement($join_str);
815 $sql_obj->setOrderStatement("field_{$this->getId()} ".$direction);
816
817 return $sql_obj;
818 }
819
820
828 public function getRecordQueryFilterObject($filter_value = "", ilDclBaseFieldModel $sort_field = null){
829 return null;
830 }
831
832
837 public function getSortField() {
838 return $this->getTitle();
839 }
840
841
846 public function hasNumericSorting() {
848 return true;
849 }
850 return false;
851 }
852
853
861 return true;
862 }
863
864
868 public function getStorageLocationOverride() {
870 }
871
872
877 $this->storage_location_override = $storage_location_override;
878 }
879
880
886 public function fillHeaderExcel(ilExcel $worksheet, &$row, &$col) {
887 $worksheet->setCell($row, $col, $this->getTitle());
888 $col++;
889 }
890
891
896 public function checkTitlesForImport(array &$titles, array &$import_fields) {
897 foreach ($titles as $k => $title) {
898 if (!ilStr::isUtf8($title)) {
899 $title = utf8_encode($title);
900 }
901 if ($title == $this->getTitle()) {
902 $import_fields[$k] = $this;
903 }
904 }
905 }
906}
$worksheet
$result
static where($where, $operator=null)
An exception for terminatinating execution or to throw for unit testing.
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.
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.
addToTableViews()
create ilDclTableViewFieldSettings for this field in each tableview
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.
checkValidity($value, $record_id=NULL)
Check if input is valid.
checkTitlesForImport(array &$titles, array &$import_fields)
const PROP_LENGTH
General properties.
getSortField()
Returns the sort-field id.
setStorageLocationOverride($storage_location_override)
checkValidityFromForm(ilPropertyFormGUI &$form, $record_id=NULL)
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)
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.
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.
$records
Definition: simple_test.php:22
global $ilDB
global $DIC