ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilDataCollectionField.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once('./Services/Exceptions/classes/class.ilException.php');
5 require_once('Services/Utilities/classes/class.ilStr.php');
6 require_once('class.ilDataCollectionCache.php');
7 require_once('class.ilDataCollectionFieldProp.php');
8 
21 
25  protected $id;
29  protected $table_id;
33  protected $title;
37  protected $description;
41  protected $datatypeId;
45  protected $required;
49  protected $order;
53  protected $unique;
57  protected $visible;
61  protected $editable;
65  protected $filterable;
69  protected $locked;
73  protected $property = array();
77  protected $exportable;
81  protected $datatype;
82  const PROPERTYID_LENGTH = 1;
83  const PROPERTYID_REGEX = 2;
88  const PROPERTYID_URL = 4;
91  const PROPERTYID_WIDTH = 7;
92  const PROPERTYID_HEIGHT = 8;
101  // type of table il_dcl_view
102  const VIEW_VIEW = 1;
103  const EDIT_VIEW = 2;
104  const FILTER_VIEW = 3;
105  const EXPORTABLE_VIEW = 4;
106 
107 
111  public function __construct($a_id = 0) {
112  if ($a_id != 0) {
113  $this->id = $a_id;
114  $this->doRead();
115  }
116  }
117 
118 
126  public static function _getTitleValidChars($a_as_regex = true) {
127  if ($a_as_regex) {
128  return '/^[a-zA-Z\d \/\-.,äöüÄÖÜàéèÀÉÈç¢]*$/i';
129  } else {
130  return 'A-Z a-z 0-9 /-.,';
131  }
132  }
133 
134 
141  public static function _getFieldIdByTitle($title, $table_id) {
142  global $ilDB;
143  $result = $ilDB->query('SELECT id FROM il_dcl_field WHERE title = ' . $ilDB->quote($title, 'text') . ' AND table_id = '
144  . $ilDB->quote($table_id, 'integer'));
145  $id = 0;
146  while ($rec = $ilDB->fetchAssoc($result)) {
147  $id = $rec['id'];
148  }
149 
150  return $id;
151  }
152 
153 
159  public function setId($a_id) {
160  $this->id = $a_id;
161  }
162 
163 
169  public function getId() {
170  return $this->id;
171  }
172 
173 
179  public function setTableId($a_id) {
180  $this->table_id = $a_id;
181  }
182 
183 
189  public function getTableId() {
190  return $this->table_id;
191  }
192 
193 
199  public function setTitle($a_title) {
200  //title cannot begin with _ as this is saved for other purposes. make __ instead.
201  if (substr($a_title, 0, 1) == "_" && substr($a_title, 0, 2) != "__") {
202  $a_title = "_" . $a_title;
203  }
204  $this->title = $a_title;
205  }
206 
207 
213  public function getTitle() {
214  return $this->title;
215  }
216 
217 
223  public function setDescription($a_desc) {
224  $this->description = $a_desc;
225  }
226 
227 
233  public function getDescription() {
234  return $this->description;
235  }
236 
237 
243  public function setDatatypeId($a_id) {
244  //unset the cached datatype.
245  $this->datatype = NULL;
246  $this->datatypeId = $a_id;
247  }
248 
249 
255  public function getDatatypeId() {
256  if ($this->isStandardField()) {
258  }
259 
260  return $this->datatypeId;
261  }
262 
263 
269  public function setRequired($a_required) {
270  $this->required = $a_required;
271  }
272 
273 
279  public function getRequired() {
280  return $this->required;
281  }
282 
283 
290  public function setPropertyvalue($a_value, $a_id) {
291  $this->property[$a_id] = $a_value;
292  }
293 
294 
298  public function isUnique() {
299  return $this->unique;
300  }
301 
302 
306  public function setUnique($unique) {
307  $this->unique = $unique ? 1 : 0;
308  }
309 
310 
318  public function getPropertyvalues() {
319  if ($this->property == NULL) {
320  $this->loadProperties();
321  }
322 
323  return $this->property;
324  }
325 
326 
332  public function setVisible($visible) {
333  if ($visible == true && $this->order === NULL) {
334  $this->setOrder(0);
335  }
336 
337  $this->visible = $visible;
338  }
339 
340 
346  public function setFilterable($filterable) {
347  if ($filterable == true && $this->order === NULL) {
348  $this->setOrder(0);
349  }
350 
351  $this->filterable = $filterable;
352  }
353 
354 
355  /*
356  * getDatatype
357  */
358  public function getDatatype() {
359  $this->loadDatatype();
360 
361  return $this->datatype;
362  }
363 
364 
365  /*
366  * getLength
367  */
368  public function getLength() {
369  $props = $this->getPropertyvalues();
370  $l = self::PROPERTYID_LENGTH;
371 
372  return $props[$l];
373  }
374 
375 
379  public function getTextArea() {
380  $props = $this->getProperties();
381  $t = self::PROPERTYID_TEXTAREA;
382 
383  return $props[$t];
384  }
385 
386 
390  public function getLearningProgress() {
391  $props = $this->getPropertyvalues();
392  $p = self::PROPERTYID_LEARNING_PROGRESS;
393 
394  return $props[$p];
395  }
396 
397 
398  /*
399  * getDatatypeTitle
400  */
401  public function getDatatypeTitle() {
402  $this->loadDatatype();
403 
404  return $this->datatype->getTitle();
405  }
406 
407 
408  /*
409  * getStorageLocation
410  */
411  public function getStorageLocation() {
412  $this->loadDatatype();
413 
414  return $this->datatype->getStorageLocation();
415  }
416 
417 
418  protected function loadDatatype() {
419  if ($this->datatype == NULL) {
420  $this->datatype = new ilDataCollectionDatatype($this->datatypeId);
421  }
422  }
423 
424 
428  public function isVisible() {
429  if (! isset($this->visible)) {
430  $this->loadVisibility();
431  }
432 
433  return $this->visible;
434  }
435 
436 
437  protected function loadVisibility() {
438  if ($this->visible == NULL) {
439  $this->loadViewDefinition(self::VIEW_VIEW);
440  }
441  }
442 
443 
447  public function isFilterable() {
448  if (! isset($this->filterable)) {
449  $this->loadFilterability();
450  }
451 
452  return $this->filterable;
453  }
454 
455 
456  protected function loadFilterability() {
457  if ($this->filterable == NULL) {
458  $this->loadViewDefinition(self::FILTER_VIEW);
459  }
460  }
461 
462 
468  protected function loadViewDefinition($view) {
469  global $ilDB;
470  $query = " SELECT view.table_id, def.field_order, def.is_set FROM il_dcl_viewdefinition def
471  INNER JOIN il_dcl_view view ON view.id = def.view_id AND view.type = " . $ilDB->quote($view, "integer") . "
472  WHERE def.field LIKE '" . $this->id . "' AND view.table_id = " . $ilDB->quote($this->table_id, "integer");
473  $set = $ilDB->query($query);
474  $rec = $ilDB->fetchAssoc($set);
475  $prop = $rec['is_set'];
476 
477  switch ($view) {
478  case self::VIEW_VIEW:
479  $this->visible = $prop;
480  break;
481  case self::EDIT_VIEW:
482  $this->editable = $prop;
483  break;
484  case self::FILTER_VIEW:
485  $this->filterable = $prop;
486  break;
487  case self::EXPORTABLE_VIEW:
488  $this->exportable = $prop;
489  break;
490  }
491 
492  if (! $this->order) {
493  $this->order = $rec['field_order'];
494  }
495  }
496 
497 
503  public function isEditable() {
504  if (! isset($this->editable)) {
505  $this->loadEditability();
506  }
507 
508  return $this->editable;
509  }
510 
511 
512  /*
513  * editable
514  */
515  public function setEditable($editable) {
516  $this->editable = $editable;
517  }
518 
519 
520  public function getExportable() {
521  if (! isset($this->exportable)) {
522  $this->loadExportability();
523  }
524 
525  return $this->exportable;
526  }
527 
528 
529  /*
530  * loadEditability
531  */
532  private function loadEditability() {
533  if ($this->editable == NULL) {
534  $this->loadViewDefinition(self::EDIT_VIEW);
535  }
536  }
537 
538 
542  private function loadExportability() {
543  if ($this->exportable == NULL) {
544  $this->loadViewDefinition(self::EXPORTABLE_VIEW);
545  }
546  }
547 
548 
549  /*
550  * toArray
551  */
552  public function toArray() {
553  return (array)$this;
554  }
555 
556 
557  /*
558  * isStandardField
559  */
560  public function isStandardField() {
561  return false;
562  }
563 
564 
568  public function doRead() {
569  global $ilDB;
570 
571  //THEN 1 ELSE 0 END AS has_options FROM il_dcl_field f WHERE id = ".$ilDB->quote($this->getId(),"integer");
572  $query = "SELECT * FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "integer");
573  $set = $ilDB->query($query);
574  $rec = $ilDB->fetchAssoc($set);
575 
576  $this->setTableId($rec["table_id"]);
577  $this->setTitle($rec["title"]);
578  $this->setDescription($rec["description"]);
579  $this->setDatatypeId($rec["datatype_id"]);
580  $this->setRequired($rec["required"]);
581  $this->setUnique($rec["is_unique"]);
582  $this->setLocked($rec["is_locked"]);
583  $this->loadProperties();
584  }
585 
586 
587  /*
588  * buildFromDBRecord
589  */
590  public function buildFromDBRecord($rec) {
591  $this->setId($rec["id"]);
592  $this->setTableId($rec["table_id"]);
593  $this->setTitle($rec["title"]);
594  $this->setDescription($rec["description"]);
595  $this->setDatatypeId($rec["datatype_id"]);
596  $this->setRequired($rec["required"]);
597  $this->setUnique($rec["is_unique"]);
598  $this->setLocked($rec["is_locked"]);
599  }
600 
601 
605  public function doCreate() {
606  global $ilDB;
607  $this->getLocked() == NULL ? $this->setLocked(false) : true;
608 
610  throw new ilException("The field does not have a related table!");
611  }
612 
613  $id = $ilDB->nextId("il_dcl_field");
614  $this->setId($id);
615  $query = "INSERT INTO il_dcl_field (" . "id" . ", table_id" . ", datatype_id" . ", title" . ", description" . ", required" . ", is_unique"
616  . ", is_locked" . " ) VALUES (" . $ilDB->quote($this->getId(), "integer") . "," . $ilDB->quote($this->getTableId(), "integer") . ","
617  . $ilDB->quote($this->getDatatypeId(), "integer") . "," . $ilDB->quote($this->getTitle(), "text") . ","
618  . $ilDB->quote($this->getDescription(), "text") . "," . $ilDB->quote($this->getRequired(), "integer") . ","
619  . $ilDB->quote($this->isUnique(), "integer") . "," . $ilDB->quote($this->getLocked() ? 1 : 0, "integer") . ")";
620  $ilDB->manipulate($query);
621 
622  $this->updateVisibility();
623  $this->updateFilterability();
624  $this->updateEditability();
625  $this->updateExportability();
626  }
627 
628 
632  public function doUpdate() {
633  global $ilDB;
634 
635  $ilDB->update("il_dcl_field", array(
636  "table_id" => array(
637  "integer",
638  $this->getTableId()
639  ),
640  "datatype_id" => array(
641  "text",
642  $this->getDatatypeId()
643  ),
644  "title" => array(
645  "text",
646  $this->getTitle()
647  ),
648  "description" => array(
649  "text",
650  $this->getDescription()
651  ),
652  "required" => array(
653  "integer",
654  $this->getRequired()
655  ),
656  "is_unique" => array(
657  "integer",
658  $this->isUnique()
659  ),
660  "is_locked" => array(
661  "integer",
662  $this->getLocked() ? 1 : 0
663  ),
664  ), array(
665  "id" => array(
666  "integer",
667  $this->getId()
668  )
669  ));
670  $this->updateVisibility();
671  $this->updateFilterability();
672  $this->updateEditability();
673  $this->updateExportability();
674  $this->updateProperties();
675  }
676 
677 
681  protected function updateProperties() {
682  global $ilDB;
683  foreach ($this->property as $key => $value) {
684  $ilDB->update('il_dcl_field_prop', array(
685  'value' => array(
686  'integer',
687  $value
688  ),
689  ), array(
690  'field_id' => array(
691  'integer',
692  $this->getId()
693  ),
694  'datatype_prop_id' => array(
695  'integer',
696  $key
697  ),
698  ));
699  }
700  }
701 
702 
706  public function getFilterable() {
707  return $this->isFilterable();
708  }
709 
710 
711  /*
712  * updateVisibility
713  */
714  protected function updateVisibility() {
715  $this->updateViewDefinition(self::VIEW_VIEW);
716  }
717 
718 
719  /*
720  * updateFilterability
721  */
722  protected function updateFilterability() {
723  $this->updateViewDefinition(self::FILTER_VIEW);
724  }
725 
726 
727  protected function updateEditability() {
728  $this->updateViewDefinition(self::EDIT_VIEW);
729  }
730 
731 
732  protected function updateExportability() {
733  $this->updateViewDefinition(self::EXPORTABLE_VIEW);
734  }
735 
736 
742  private function updateViewDefinition($view) {
743  global $ilDB;
744 
745  switch ($view) {
746  case self::EDIT_VIEW:
747  $set = $this->isEditable();
748  break;
749  case self::VIEW_VIEW:
750  $set = $this->isVisible();
751  if ($set && $this->order === NULL) {
752  $this->order = 0;
753  }
754  break;
755  case self::FILTER_VIEW:
756  $set = $this->isFilterable();
757  if ($set && $this->order === NULL) {
758  $this->order = 0;
759  }
760  break;
761  case self::EXPORTABLE_VIEW:
762  $set = $this->getExportable();
763  if ($set && $this->order === NULL) {
764  $this->order = 0;
765  }
766  break;
767  }
768 
769  if (! $set) {
770  $set = 0;
771  } else {
772  $set = 1;
773  }
774 
775  if (! isset($this->order)) {
776  $this->order = 0;
777  }
778 
779  $query = "DELETE def FROM il_dcl_viewdefinition def INNER JOIN il_dcl_view ON il_dcl_view.type = " . $ilDB->quote($view, "integer")
780  . " AND il_dcl_view.table_id = " . $ilDB->quote($this->getTableId(), "integer") . " WHERE def.view_id = il_dcl_view.id AND def.field = "
781  . $ilDB->quote($this->getId(), "text");
782 
783  $ilDB->manipulate($query);
784 
785  $query = "INSERT INTO il_dcl_viewdefinition (view_id, field, field_order, is_set) SELECT id, " . $ilDB->quote($this->getId(), "text") . ", "
786  . $ilDB->quote($this->getOrder(), "integer") . ", " . $ilDB->quote($set, "integer") . " FROM il_dcl_view WHERE il_dcl_view.type = "
787  . $ilDB->quote($view, "integer") . " AND il_dcl_view.table_id = " . $ilDB->quote($this->getTableId(), "integer");
788 
789  $ilDB->manipulate($query);
790  }
791 
792 
793  /*
794  * deleteViewDefinition
795  */
796  private function deleteViewDefinition($view) {
797  global $ilDB;
798 
799  $query = "DELETE def FROM il_dcl_viewdefinition def INNER JOIN il_dcl_view ON il_dcl_view.type = " . $ilDB->quote($view, "integer")
800  . " AND il_dcl_view.table_id = " . $ilDB->quote($this->getTableId(), "integer") . " WHERE def.view_id = il_dcl_view.id AND def.field = "
801  . $ilDB->quote($this->getId(), "text");
802 
803  $ilDB->manipulate($query);
804  }
805 
806 
807  /*
808  * doDelete
809  */
810  public function doDelete() {
811  global $ilDB;
812 
813  // delete viewdefinitions.
814  $this->deleteViewDefinition(self::VIEW_VIEW);
815  $this->deleteViewDefinition(self::FILTER_VIEW);
816  $this->deleteViewDefinition(self::EDIT_VIEW);
817  $this->deleteViewDefinition(self::EXPORTABLE_VIEW);
818 
819  $query = "DELETE FROM il_dcl_field_prop WHERE field_id = " . $ilDB->quote($this->getId(), "text");
820  $ilDB->manipulate($query);
821 
822  $query = "DELETE FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "text");
823  $ilDB->manipulate($query);
824  }
825 
826 
827  /*
828  * getOrder
829  */
830  public function getOrder() {
831  if (! isset($this->order)) {
832  $this->loadVisibility();
833  }
834 
835  return ! $this->order ? 0 : $this->order;
836  }
837 
838 
839  /*
840  * setOrder
841  */
842  public function setOrder($order) {
843  $this->order = $order;
844  }
845 
846 
847  /*
848  * getFieldRef
849  */
850  public function getFieldRef() {
851  $props = $this->getPropertyvalues();
852  $id = self::PROPERTYID_REFERENCE;
853 
854  return $props[$id];
855  }
856 
857 
858  /*
859  * getFieldReflist
860  */
861  public function getFieldReflist() {
862  $props = $this->getPropertyvalues();
863  $id = self::PROPERTYID_N_REFERENCE;
864 
865  return $props[$id];
866  }
867 
868 
872  public function isNRef() {
873  $props = $this->getPropertyvalues();
874  $id = self::PROPERTYID_N_REFERENCE;
875 
876  return $props[$id];
877  }
878 
879 
885  private function loadProperties() {
886  global $ilDB;
887 
888  $query = "SELECT datatype_prop_id,
889  title,
890  value
891  FROM il_dcl_field_prop fp
892  LEFT JOIN il_dcl_datatype_prop AS p ON p.id = fp.datatype_prop_id
893  WHERE fp.field_id = " . $ilDB->quote($this->getId(), "integer");
894 
895  $set = $ilDB->query($query);
896 
897  while ($rec = $ilDB->fetchAssoc($set)) {
898  $this->setPropertyvalue($rec['value'], $rec['datatype_prop_id']);
899  }
900  }
901 
902 
908  public function getProperties() {
909  if ($this->property == NULL) {
910  $this->loadProperties();
911  }
912 
913  return $this->property;
914  }
915 
916 
917  public function setProperties($data) {
918  $this->property = $data;
919  }
920 
921 
925  public function setLocked($locked) {
926  $this->locked = $locked;
927  }
928 
929 
933  public function getLocked() {
934  return $this->locked;
935  }
936 
937 
938  /*
939  * checkValidity
940  */
941  public function checkValidity($value, $record_id = NULL) {
942  //Don't check empty values
943 
944  if ($value == NULL) {
945  return true;
946  }
947 
948  if (! ilDataCollectionDatatype::checkValidity($this->getDatatypeId(), $value)) {
950  }
951 
952  $properties = $this->getPropertyvalues();
956 
958  $regex = $properties[$regex_id];
959  if (substr($regex, 0, 1) != "/") {
960  $regex = "/" . $regex;
961  }
962  if (substr($regex, - 1) != "/") {
963  $regex .= "/";
964  }
965 
966  if ($properties[$length] < ilStr::strLen($value) AND is_numeric($properties[$length])) {
968  }
969 
970  if(
971  $properties[$regex_id] != null &&
972  !preg_match($regex, $value)
973  )
974  {
976  }
977  //email or url
978  if ($properties[$url]) {
979  if ($json = json_decode($value)) {
980  $value = $json->link;
981  }
982  if (substr($value, 0, 3) === 'www') {
983  $value = 'http://' . $value;
984  }
985  if (! filter_var($value, FILTER_VALIDATE_URL) && ! filter_var($value, FILTER_VALIDATE_EMAIL)) {
987  }
988  }
989  }
990 
991  if ($this->isUnique()) {
993  foreach ($table->getRecords() as $record) {
994  if ($this->normalizeValue($record->getRecordFieldValue($this->getId())) == $this->normalizeValue($value)
995  && ($record->getId() != $record_id || $record_id == 0)
996  ) {
998  }
999 
1000  //for text it has to be case insensitive.
1002  if (strtolower($this->normalizeValue($record->getRecordFieldValue($this->getId()))) == strtolower($this->normalizeValue($value))
1003  && ($record->getId() != $record_id
1004  || $record_id == 0)
1005  ) {
1007  }
1008  }
1009 
1011  $datestring = $value["date"] . " " . $value["time"];//["y"]."-".$value["date"]['m']."-".$value["date"]['d']." 00:00:00";
1012 
1013  if ($record->getRecordFieldValue($this->getId()) == $datestring && ($record->getId() != $record_id || $record_id == 0)) {
1015  }
1016  }
1017  }
1018  }
1019 
1020  return true;
1021  }
1022 
1023 
1029  protected function normalizeValue($value) {
1030  if (is_string($value)) {
1031  $value = trim(preg_replace("/\\s+/uism", " ", $value));
1032  }
1033 
1034  return $value;
1035  }
1036 
1037 
1043  public function cloneStructure($original_id) {
1044  $original = ilDataCollectionCache::getFieldCache($original_id);
1045  $this->setTitle($original->getTitle());
1046  $this->setDatatypeId($original->getDatatypeId());
1047  $this->setDescription($original->getDescription());
1048  $this->setEditable($original->isEditable());
1049  $this->setLocked($original->getLocked());
1050  $this->setFilterable($original->isFilterable());
1051  $this->setVisible($original->isVisible());
1052  $this->setOrder($original->getOrder());
1053  $this->setRequired($original->getRequired());
1054  $this->setUnique($original->isUnique());
1055  $this->setExportable($original->getExportable());
1056  $this->doCreate();
1057  $this->cloneProperties($original);
1058  }
1059 
1060 
1064  public function cloneProperties(ilDataCollectionField $originalField) {
1065  $orgProps = $originalField->getProperties();
1066  if ($orgProps == NULL) {
1067  return;
1068  }
1069  foreach ($orgProps as $id => $value) {
1070  $fieldprop_obj = new ilDataCollectionFieldProp();
1071  $fieldprop_obj->setDatatypePropertyId($id);
1072  $fieldprop_obj->setFieldId($this->getId());
1073  // If reference field, we must reset the referenced field, otherwise it will point to the old ID
1076  ) {
1077  $value = NULL;
1078  }
1079  $fieldprop_obj->setValue($value);
1080  $fieldprop_obj->doCreate();
1081  }
1082  }
1083 
1084 
1088  public function setExportable($exportable) {
1089  $this->exportable = $exportable;
1090  }
1091 }
1092 
1093 ?>
Class ilDataCollectionDatatype.
Base class for ILIAS Exception handling.
static strLen($a_string)
Definition: class.ilStr.php:79
checkValidity($value, $record_id=NULL)
setRequired($a_required)
Set Required.
$result
Class ilDataCollectionFieldProp.
static checkValidity($type_id, $value)
now only distinguishes between number and text values
$url
Definition: shib_logout.php:72
static _getTitleValidChars($a_as_regex=true)
All valid chars for filed titles.
getProperties()
Get all properties of a field.
updateProperties()
Update properties of this field in Database.
setDatatypeId($a_id)
Set datatype id.
getRequired()
Get Required Required.
updateViewDefinition($view)
updateViewDefinition
static _getDatatypeForId($id)
gives you the datatype id of a specified standard field.
const PROPERTYID_URL
LINK OR EMAIL!
$data
static _getFieldIdByTitle($title, $table_id)
loadProperties()
Get all properties of a field.
setPropertyvalue($a_value, $a_id)
Set Property Value.
loadViewDefinition($view)
loadViewDefinition
setFilterable($filterable)
setFilterable
Class ilDataCollectionField.
setDescription($a_desc)
Set description.
global $l
Definition: afr.php:30
cloneProperties(ilDataCollectionField $originalField)
global $ilDB
getPropertyvalues()
Get Property Values.