ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
295  /*
296  * isUnique
297  */
298  public function isUnique() {
299  return $this->unique;
300  }
301 
302 
303  /*
304  * setUnique
305  */
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( "integer", $this->getTableId() ),
637  "datatype_id" => array( "text", $this->getDatatypeId() ),
638  "title" => array( "text", $this->getTitle() ),
639  "description" => array( "text", $this->getDescription() ),
640  "required" => array( "integer", $this->getRequired() ),
641  "is_unique" => array( "integer", $this->isUnique() ),
642  "is_locked" => array( "integer", $this->getLocked() ? 1 : 0 ),
643  ), array(
644  "id" => array( "integer", $this->getId() )
645  ));
646  $this->updateVisibility();
647  $this->updateFilterability();
648  $this->updateEditability();
649  $this->updateExportability();
650  $this->updateProperties();
651  }
652 
653 
657  protected function updateProperties() {
658  global $ilDB;
659  foreach ($this->property as $key => $value) {
660  $ilDB->update('il_dcl_field_prop', array(
661  'value' => array( 'integer', $value ),
662  ), array(
663  'field_id' => array( 'integer', $this->getId() ),
664  'datatype_prop_id' => array( 'integer', $key ),
665  ));
666  }
667  }
668 
669 
673  public function getFilterable() {
674  return $this->isFilterable();
675  }
676 
677 
678  /*
679  * updateVisibility
680  */
681  protected function updateVisibility() {
682  $this->updateViewDefinition(self::VIEW_VIEW);
683  }
684 
685 
686  /*
687  * updateFilterability
688  */
689  protected function updateFilterability() {
690  $this->updateViewDefinition(self::FILTER_VIEW);
691  }
692 
693 
694  protected function updateEditability() {
695  $this->updateViewDefinition(self::EDIT_VIEW);
696  }
697 
698 
699  protected function updateExportability() {
700  $this->updateViewDefinition(self::EXPORTABLE_VIEW);
701  }
702 
703 
709  private function updateViewDefinition($view) {
710  global $ilDB;
711 
712  switch ($view) {
713  case self::EDIT_VIEW:
714  $set = $this->isEditable();
715  break;
716  case self::VIEW_VIEW:
717  $set = $this->isVisible();
718  if ($set && $this->order === NULL) {
719  $this->order = 0;
720  }
721  break;
722  case self::FILTER_VIEW:
723  $set = $this->isFilterable();
724  if ($set && $this->order === NULL) {
725  $this->order = 0;
726  }
727  break;
728  case self::EXPORTABLE_VIEW:
729  $set = $this->getExportable();
730  if ($set && $this->order === NULL) {
731  $this->order = 0;
732  }
733  break;
734  }
735 
736  if (!$set) {
737  $set = 0;
738  } else {
739  $set = 1;
740  }
741 
742  if (!isset($this->order)) {
743  $this->order = 0;
744  }
745 
746  $query = "DELETE def FROM il_dcl_viewdefinition def INNER JOIN il_dcl_view ON il_dcl_view.type = " . $ilDB->quote($view, "integer")
747  . " AND il_dcl_view.table_id = " . $ilDB->quote($this->getTableId(), "integer") . " WHERE def.view_id = il_dcl_view.id AND def.field = "
748  . $ilDB->quote($this->getId(), "text");
749 
750  $ilDB->manipulate($query);
751 
752  $query = "INSERT INTO il_dcl_viewdefinition (view_id, field, field_order, is_set) SELECT id, " . $ilDB->quote($this->getId(), "text") . ", "
753  . $ilDB->quote($this->getOrder(), "integer") . ", " . $ilDB->quote($set, "integer") . " FROM il_dcl_view WHERE il_dcl_view.type = "
754  . $ilDB->quote($view, "integer") . " AND il_dcl_view.table_id = " . $ilDB->quote($this->getTableId(), "integer");
755 
756  $ilDB->manipulate($query);
757  }
758 
759 
760  /*
761  * deleteViewDefinition
762  */
763  private function deleteViewDefinition($view) {
764  global $ilDB;
765 
766  $query = "DELETE def FROM il_dcl_viewdefinition def INNER JOIN il_dcl_view ON il_dcl_view.type = " . $ilDB->quote($view, "integer")
767  . " AND il_dcl_view.table_id = " . $ilDB->quote($this->getTableId(), "integer") . " WHERE def.view_id = il_dcl_view.id AND def.field = "
768  . $ilDB->quote($this->getId(), "text");
769 
770  $ilDB->manipulate($query);
771  }
772 
773 
774  /*
775  * doDelete
776  */
777  public function doDelete() {
778  global $ilDB;
779 
780  // delete viewdefinitions.
781  $this->deleteViewDefinition(self::VIEW_VIEW);
782  $this->deleteViewDefinition(self::FILTER_VIEW);
783  $this->deleteViewDefinition(self::EDIT_VIEW);
784  $this->deleteViewDefinition(self::EXPORTABLE_VIEW);
785 
786  $query = "DELETE FROM il_dcl_field_prop WHERE field_id = " . $ilDB->quote($this->getId(), "text");
787  $ilDB->manipulate($query);
788 
789  $query = "DELETE FROM il_dcl_field WHERE id = " . $ilDB->quote($this->getId(), "text");
790  $ilDB->manipulate($query);
791  }
792 
793 
794  /*
795  * getOrder
796  */
797  public function getOrder() {
798  if (!isset($this->order)) {
799  $this->loadVisibility();
800  }
801 
802  return !$this->order ? 0 : $this->order;
803  }
804 
805 
806  /*
807  * setOrder
808  */
809  public function setOrder($order) {
810  $this->order = $order;
811  }
812 
813 
814  /*
815  * getFieldRef
816  */
817  public function getFieldRef() {
818  $props = $this->getPropertyvalues();
819  $id = self::PROPERTYID_REFERENCE;
820 
821  return $props[$id];
822  }
823 
824 
825  /*
826  * getFieldReflist
827  */
828  public function getFieldReflist() {
829  $props = $this->getPropertyvalues();
830  $id = self::PROPERTYID_N_REFERENCE;
831 
832  return $props[$id];
833  }
834 
835 
839  public function isNRef() {
840  $props = $this->getPropertyvalues();
841  $id = self::PROPERTYID_N_REFERENCE;
842 
843  return $props[$id];
844  }
845 
846 
852  private function loadProperties() {
853  global $ilDB;
854 
855  $query = "SELECT datatype_prop_id,
856  title,
857  value
858  FROM il_dcl_field_prop fp
859  LEFT JOIN il_dcl_datatype_prop AS p ON p.id = fp.datatype_prop_id
860  WHERE fp.field_id = " . $ilDB->quote($this->getId(), "integer");
861 
862  $set = $ilDB->query($query);
863 
864  while ($rec = $ilDB->fetchAssoc($set)) {
865  $this->setPropertyvalue($rec['value'], $rec['datatype_prop_id']);
866  }
867  }
868 
869 
875  public function getProperties() {
876  if ($this->property == NULL) {
877  $this->loadProperties();
878  }
879 
880  return $this->property;
881  }
882 
883 
884  public function setProperties($data) {
885  $this->property = $data;
886  }
887 
888 
892  public function setLocked($locked) {
893  $this->locked = $locked;
894  }
895 
896 
900  public function getLocked() {
901  return $this->locked;
902  }
903 
904 
905  /*
906  * checkValidity
907  */
908  public function checkValidity($value, $record_id = NULL) {
909  //Don't check empty values
910  if ($value == NULL) {
911  return true;
912  }
913 
916  }
917 
918  $properties = $this->getPropertyvalues();
922 
924  $regex = $properties[$regex_id];
925  if (substr($regex, 0, 1) != "/") {
926  $regex = "/" . $regex;
927  }
928  if (substr($regex, - 1) != "/") {
929  $regex .= "/";
930  }
931 
932  if ($properties[$length] < ilStr::strLen($value) AND is_numeric($properties[$length])) {
934  }
935  if (!($properties[$regex_id] == NULL OR @preg_match($regex, $value))) {
937  }
938  //email or url
939  if ($properties[$url]
940  && !(preg_match('~(^(www|news|(ht|f)tp(s?)\://){1}\S+)~i', $value)
941  || preg_match("/^[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i", $value))
942  ) {
944  }
945  }
946 
947  if ($this->isUnique() && $record_id === NULL) {
949 
950  foreach ($table->getRecords() as $record) {
951  if ($record->getRecordFieldValue($this->getId()) == $value && ($record->getId() != $record_id || $record_id == 0)) {
953  }
954 
955  //for text it has to be case insensitive.
957  if (strtolower($record->getRecordFieldValue($this->getId())) == strtolower($value)
958  && ($record->getId() != $record_id
959  || $record_id == 0)
960  ) {
962  }
963  }
964 
966  $datestring = $value["date"] . " " . $value["time"];//["y"]."-".$value["date"]['m']."-".$value["date"]['d']." 00:00:00";
967 
968  if ($record->getRecordFieldValue($this->getId()) == $datestring && ($record->getId() != $record_id || $record_id == 0)) {
970  }
971  }
972  }
973  }
974 
975  return true;
976  }
977 
978 
984  public function cloneStructure($original_id) {
985  $original = ilDataCollectionCache::getFieldCache($original_id);
986  $this->setTitle($original->getTitle());
987  $this->setDatatypeId($original->getDatatypeId());
988  $this->setDescription($original->getDescription());
989  $this->setEditable($original->isEditable());
990  $this->setLocked($original->getLocked());
991  $this->setFilterable($original->isFilterable());
992  $this->setVisible($original->isVisible());
993  $this->setOrder($original->getOrder());
994  $this->setRequired($original->getRequired());
995  $this->setUnique($original->isUnique());
996  $this->setExportable($original->getExportable());
997  $this->doCreate();
998  $this->cloneProperties($original);
999  }
1000 
1001 
1005  public function cloneProperties(ilDataCollectionField $originalField) {
1006  $orgProps = $originalField->getProperties();
1007  if ($orgProps == NULL) {
1008  return;
1009  }
1010  foreach ($orgProps as $id => $value) {
1011  $fieldprop_obj = new ilDataCollectionFieldProp();
1012  $fieldprop_obj->setDatatypePropertyId($id);
1013  $fieldprop_obj->setFieldId($this->getId());
1014  // If reference field, we must reset the referenced field, otherwise it will point to the old ID
1017  ) {
1018  $value = NULL;
1019  }
1020  $fieldprop_obj->setValue($value);
1021  $fieldprop_obj->doCreate();
1022  }
1023  }
1024 
1025 
1029  public function setExportable($exportable) {
1030  $this->exportable = $exportable;
1031  }
1032 }
1033 
1034 ?>
Class ilDataCollectionDatatype.
Base class for ILIAS Exception handling.
static strLen($a_string)
Definition: class.ilStr.php:77
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
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!
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.
cloneProperties(ilDataCollectionField $originalField)
global $ilDB
getPropertyvalues()
Get Property Values.