/**
  * save Field
  *
  * @param string $a_mode values: create | update
  */
 public function save($a_mode = "create")
 {
     global $ilCtrl, $lng;
     //check access
     if (!$this->table->hasPermissionToFields($this->parent_obj->ref_id)) {
         $this->accessDenied();
         return;
     }
     $this->initForm($a_mode == "update" ? "edit" : "create");
     if ($this->form->checkInput()) {
         $title = $this->form->getInput("title");
         if ($a_mode != "create" && $title != $this->field_obj->getTitle()) {
             ilUtil::sendInfo($lng->txt("dcl_field_title_change_warning"), true);
         }
         $this->field_obj->setTitle($title);
         $this->field_obj->setDescription($this->form->getInput("description"));
         $this->field_obj->setDatatypeId($this->form->getInput("datatype"));
         $this->field_obj->setRequired($this->form->getInput("required"));
         $this->field_obj->setUnique($this->form->getInput("unique"));
         if ($a_mode == "update") {
             $this->field_obj->doUpdate();
         } else {
             $this->field_obj->setVisible(true);
             $this->field_obj->setOrder($this->table->getNewOrder());
             $this->field_obj->doCreate();
         }
         // Get possible properties and save them
         include_once "./Modules/DataCollection/classes/class.ilDataCollectionFieldProp.php";
         foreach (ilDataCollectionDatatype::getProperties($this->field_obj->getDatatypeId()) as $property) {
             $fieldprop_obj = new ilDataCollectionFieldProp();
             $fieldprop_obj->setDatatypePropertyId($property['id']);
             $fieldprop_obj->setFieldId($this->field_obj->getId());
             $fieldprop_obj->setValue($this->form->getInput("prop_" . $property['id']));
             if ($a_mode == "update") {
                 $fieldprop_obj->doUpdate();
             } else {
                 $fieldprop_obj->doCreate();
             }
         }
         $ilCtrl->setParameter($this, "field_id", $this->field_obj->getId());
         if ($a_mode == "update") {
             ilUtil::sendSuccess($lng->txt("dcl_msg_field_modified"), true);
         } else {
             $this->table->addField($this->field_obj);
             $this->table->buildOrderFields();
             ilUtil::sendSuccess($lng->txt("msg_field_created"), false);
         }
         $ilCtrl->redirectByClass(strtolower("ilDataCollectionFieldListGUI"), "listFields");
     } else {
         global $tpl;
         $this->form->setValuesByPost();
         $tpl->setContent($this->form->getHTML());
     }
 }
 /**
  * init Form
  *
  * @param string $a_mode values: create | edit
  */
 public function initForm()
 {
     global $lng, $ilCtrl;
     //table_id
     $hidden_prop = new ilHiddenInputGUI("table_id");
     $hidden_prop->setValue($this->table_id);
     $this->form->addItem($hidden_prop);
     $ilCtrl->setParameter($this, "record_id", $this->record_id);
     $this->form->setFormAction($ilCtrl->getFormAction($this));
     $allFields = $this->table->getRecordFields();
     foreach ($allFields as $field) {
         $item = ilDataCollectionDatatype::getInputField($field);
         if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
             $fieldref = $field->getFieldRef();
             $reffield = ilDataCollectionCache::getFieldCache($fieldref);
             $options = array();
             if (!$field->isNRef()) {
                 $options[""] = '--';
             }
             $reftable = ilDataCollectionCache::getTableCache($reffield->getTableId());
             foreach ($reftable->getRecords() as $record) {
                 // If the referenced field is MOB or FILE, we display the filename in the dropdown
                 if ($reffield->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE) {
                     $file_obj = new ilObjFile($record->getRecordFieldValue($fieldref), false);
                     $options[$record->getId()] = $file_obj->getFileName();
                 } else {
                     if ($reffield->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB) {
                         $media_obj = new ilObjMediaObject($record->getRecordFieldValue($fieldref), false);
                         $options[$record->getId()] = $media_obj->getTitle();
                     } else {
                         $options[$record->getId()] = $record->getRecordFieldValue($fieldref);
                     }
                 }
             }
             $item->setOptions($options);
         }
         if ($this->record_id) {
             $record = ilDataCollectionCache::getRecordCache($this->record_id);
         }
         $item->setRequired($field->getRequired());
         //WORKAROUND. If field is from type file: if it's required but already has a value it is no longer required as the old value is taken as default without the form knowing about it.
         if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE || $field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB && ($this->record_id && $record->getId() != 0 && ($record->getRecordFieldValue($field->getId()) != "-" || $record->getRecordFieldValue($field->getId()) != ""))) {
             $item->setRequired(false);
         }
         if (!ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id) && $field->getLocked()) {
             $item->setDisabled(true);
         }
         $this->form->addItem($item);
     }
     // Add possibility to change the owner in edit mode
     if ($this->record_id) {
         $ownerField = $this->table->getField('owner');
         $inputfield = ilDataCollectionDatatype::getInputField($ownerField);
         $this->form->addItem($inputfield);
     }
     // save and cancel commands
     if (isset($this->record_id)) {
         $this->form->setTitle($lng->txt("dcl_update_record"));
         $this->form->addCommandButton("save", $lng->txt("dcl_update_record"));
         $this->form->addCommandButton("cancelUpdate", $lng->txt("cancel"));
     } else {
         $this->form->setTitle($lng->txt("dcl_add_new_record"));
         $this->form->addCommandButton("save", $lng->txt("save"));
         $this->form->addCommandButton("cancelSave", $lng->txt("cancel"));
     }
     $ilCtrl->setParameter($this, "table_id", $this->table_id);
     $ilCtrl->setParameter($this, "record_id", $this->record_id);
 }
 public function checkValidity($value, $record_id = null)
 {
     //Don't check empty values
     if ($value == NULL) {
         return true;
     }
     if (!ilDataCollectionDatatype::checkValidity($this->getDatatypeId(), $value)) {
         throw new ilDataCollectionInputException(ilDataCollectionInputException::TYPE_EXCEPTION);
     }
     $properties = $this->getPropertyvalues();
     $length = ilDataCollectionField::PROPERTYID_LENGTH;
     $regex_id = ilDataCollectionField::PROPERTYID_REGEX;
     $url = ilDataCollectionField::PROPERTYID_URL;
     if ($this->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_TEXT) {
         $regex = $properties[$regex_id];
         if (substr($regex, 0, 1) != "/") {
             $regex = "/" . $regex;
         }
         if (substr($regex, -1) != "/") {
             $regex .= "/";
         }
         if ($properties[$length] < strlen($value) && is_numeric($properties[$length])) {
             throw new ilDataCollectionInputException(ilDataCollectionInputException::LENGTH_EXCEPTION);
         }
         if (!($properties[$regex_id] == NULL || @preg_match($regex, $value))) {
             throw new ilDataCollectionInputException(ilDataCollectionInputException::REGEX_EXCEPTION);
         }
         //email or url
         if ($properties[$url] && !(preg_match('~(^(news|(ht|f)tp(s?)\\://){1}\\S+)~i', $value) || 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))) {
             throw new ilDataCollectionInputException(ilDataCollectionInputException::NOT_URL);
         }
     }
     if ($this->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB and $value['name']) {
         $arrMimeType = explode('/', ilObjMediaObject::getMimeType($value['name']));
         if ($arrMimeType[0] != "image") {
             throw new ilDataCollectionInputException(ilDataCollectionInputException::NOT_IMAGE);
         }
     }
     if ($this->isUnique() && $record_id === null) {
         $table = ilDataCollectionCache::getTableCache($this->getTableId());
         foreach ($table->getRecords() as $record) {
             if ($record->getRecordFieldValue($this->getId()) == $value && ($record->getId() != $record_id || $record_id == 0)) {
                 throw new ilDataCollectionInputException(ilDataCollectionInputException::UNIQUE_EXCEPTION);
             }
             //for text it has to be case insensitive.
             if ($this->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_TEXT) {
                 if (strtolower($record->getRecordFieldValue($this->getId())) == strtolower($value) && ($record->getId() != $record_id || $record_id == 0)) {
                     throw new ilDataCollectionInputException(ilDataCollectionInputException::UNIQUE_EXCEPTION);
                 }
             }
             if ($this->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_DATETIME) {
                 $datestring = $value["date"] . " " . $value["time"];
                 //["y"]."-".$value["date"]['m']."-".$value["date"]['d']." 00:00:00";
                 if ($record->getRecordFieldValue($this->getId()) == $datestring && ($record->getId() != $record_id || $record_id == 0)) {
                     throw new ilDataCollectionInputException(ilDataCollectionInputException::UNIQUE_EXCEPTION);
                 }
             }
         }
     }
     return true;
 }
 /**
  * init Form
  */
 public function initForm()
 {
     $this->form = new ilPropertyFormGUI();
     $prefix = $this->ctrl->isAsynch() ? 'dclajax' : 'dcl';
     // Used by datacolleciton.js to select input elements
     $this->form->setId($prefix . $this->table_id . $this->record_id);
     $hidden_prop = new ilHiddenInputGUI("table_id");
     $hidden_prop->setValue($this->table_id);
     $this->form->addItem($hidden_prop);
     if ($this->record_id) {
         $hidden_prop = new ilHiddenInputGUI("record_id");
         $hidden_prop->setValue($this->record_id);
         $this->form->addItem($hidden_prop);
     }
     $this->ctrl->setParameter($this, "record_id", $this->record_id);
     $this->form->setFormAction($this->ctrl->getFormAction($this));
     $allFields = $this->table->getRecordFields();
     $inline_css = '';
     foreach ($allFields as $field) {
         $item = ilDataCollectionDatatype::getInputField($field);
         if ($item === NULL) {
             continue;
             // Fields calculating values at runtime, e.g. ilDataCollectionFormulaField do not have input
         }
         if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
             $fieldref = $field->getFieldRef();
             $reffield = ilDataCollectionCache::getFieldCache($fieldref);
             $options = array();
             if (!$field->isNRef()) {
                 $options[""] = $this->lng->txt('dcl_please_select');
             }
             $reftable = ilDataCollectionCache::getTableCache($reffield->getTableId());
             foreach ($reftable->getRecords() as $record) {
                 // If the referenced field is MOB or FILE, we display the filename in the dropdown
                 switch ($reffield->getDatatypeId()) {
                     case ilDataCollectionDatatype::INPUTFORMAT_FILE:
                         $file_obj = new ilObjFile($record->getRecordFieldValue($fieldref), false);
                         $options[$record->getId()] = $file_obj->getFileName();
                         break;
                     case ilDataCollectionDatatype::INPUTFORMAT_MOB:
                         $media_obj = new ilObjMediaObject($record->getRecordFieldValue($fieldref), false);
                         $options[$record->getId()] = $media_obj->getTitle();
                         break;
                     case ilDataCollectionDatatype::INPUTFORMAT_DATETIME:
                         $options[$record->getId()] = $record->getRecordFieldSingleHTML($fieldref);
                         break;
                     default:
                         $options[$record->getId()] = $record->getRecordFieldValue($fieldref);
                         break;
                 }
             }
             asort($options);
             $item->setOptions($options);
             if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
                 // FSX use this to apply to MultiSelectInputGUI
                 //					if (!$field->isNRef()) { // addCustomAttribute only defined for single selects
                 if ($reftable->hasPermissionToAddRecord($_GET['ref_id'])) {
                     $item->addCustomAttribute('data-ref="1"');
                     $item->addCustomAttribute('data-ref-table-id="' . $reftable->getId() . '"');
                     $item->addCustomAttribute('data-ref-field-id="' . $reffield->getId() . '"');
                 }
                 //					}
             }
             if ($item instanceof ilMultiSelectInputGUI) {
                 $item->setWidth(400);
                 $item->setHeight(100);
                 $inline_css .= 'div#' . $item->getFieldId() . '{resize:both;} ';
             }
         }
         if ($this->record_id) {
             $record = ilDataCollectionCache::getRecordCache($this->record_id);
         }
         $item->setRequired($field->getRequired());
         //WORKAROUND. If field is from type file: if it's required but already has a value it is no longer required as the old value is taken as default without the form knowing about it.
         if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE || $field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB) {
             if ($this->record_id and $record->getId()) {
                 $field_value = $record->getRecordFieldValue($field->getId());
                 if ($field_value) {
                     $item->setRequired(false);
                 }
             }
             // If this is an ajax request to return the form, input files are currently not supported
             if ($this->ctrl->isAsynch()) {
                 $item->setDisabled(true);
             }
         }
         if (!ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id) && $field->getLocked()) {
             $item->setDisabled(true);
         }
         $this->form->addItem($item);
     }
     $this->tpl->addInlineCss($inline_css);
     // Add possibility to change the owner in edit mode
     if ($this->record_id) {
         $ownerField = $this->table->getField('owner');
         $inputfield = ilDataCollectionDatatype::getInputField($ownerField);
         $this->form->addItem($inputfield);
     }
     // save and cancel commands
     if ($this->record_id) {
         $this->form->setTitle($this->lng->txt("dcl_update_record"));
         $this->form->addCommandButton("save", $this->lng->txt("dcl_update_record"));
         if (!$this->ctrl->isAsynch()) {
             $this->form->addCommandButton("cancelUpdate", $this->lng->txt("cancel"));
         }
     } else {
         $this->form->setTitle($this->lng->txt("dcl_add_new_record"));
         $this->form->addCommandButton("save", $this->lng->txt("save"));
         if (!$this->ctrl->isAsynch()) {
             $this->form->addCommandButton("cancelSave", $this->lng->txt("cancel"));
         }
     }
     $this->ctrl->setParameter($this, "table_id", $this->table_id);
     $this->ctrl->setParameter($this, "record_id", $this->record_id);
 }
 public function initFilter()
 {
     foreach ($this->table->getFilterableFields() as $field) {
         $input = ilDataCollectionDatatype::addFilterInputFieldToTable($field, $this);
         $input->readFromSession();
         $value = $input->getValue();
         if (is_array($value)) {
             if ($value['from'] || $value['to']) {
                 $this->filter["filter_" . $field->getId()] = $value;
             }
         } else {
             if ($value != '') {
                 $this->filter["filter_" . $field->getId()] = $value;
             }
         }
     }
 }
 public function passThroughFilter(array $filter)
 {
     $pass = true;
     $this->loadTable();
     foreach ($this->table->getFields() as $field) {
         if (!ilDataCollectionDatatype::passThroughFilter($this, $field, $filter["filter_" . $field->getId()])) {
             $pass = false;
         }
     }
     return $pass;
 }
 /**
  * @param array $filter
  * @return bool
  */
 public function passThroughFilter(array $filter)
 {
     $this->loadTable();
     // If one field returns false, the whole record does not pass the filter #performance-improvements
     foreach ($this->table->getFilterableFields() as $field) {
         if (!isset($filter["filter_" . $field->getId()]) || !$filter["filter_" . $field->getId()]) {
             continue;
         }
         if (!ilDataCollectionDatatype::passThroughFilter($this, $field, $filter["filter_" . $field->getId()])) {
             return false;
         }
     }
     return true;
 }
 public function initFilter()
 {
     foreach ($this->table->getFilterableFields() as $field) {
         $input = ilDataCollectionDatatype::addFilterInputFieldToTable($field, $this);
         $input->readFromSession();
         $this->filter["filter_" . $field->getId()] = $input->getValue();
     }
 }