/**
  * Save record
  */
 public function save()
 {
     $this->initForm();
     if ($this->form->checkInput()) {
         $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
         $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
         $record_obj->setTableId($this->table_id);
         $record_obj->setLastUpdate($date_obj->get(IL_CAL_DATETIME));
         $record_obj->setLastEditBy($this->user->getId());
         $create_mode = false;
         if (ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id)) {
             $all_fields = $this->table->getRecordFields();
         } else {
             $all_fields = $this->table->getEditableFields();
         }
         $fail = "";
         //Check if we can create this record.
         foreach ($all_fields as $field) {
             try {
                 $value = $this->form->getInput("field_" . $field->getId());
                 $field->checkValidity($value, $this->record_id);
             } catch (ilDataCollectionInputException $e) {
                 $fail .= $field->getTitle() . ": " . $e . "<br>";
             }
         }
         if ($fail) {
             $this->sendFailure($fail);
             return;
         }
         if (!isset($this->record_id)) {
             if (!$this->table->hasPermissionToAddRecord($this->parent_obj->ref_id)) {
                 $this->accessDenied();
                 return;
             }
             $record_obj->setOwner($this->user->getId());
             $record_obj->setCreateDate($date_obj->get(IL_CAL_DATETIME));
             $record_obj->setTableId($this->table_id);
             $record_obj->doCreate();
             $this->record_id = $record_obj->getId();
             $create_mode = true;
         } else {
             if (!$record_obj->hasPermissionToEdit($this->parent_obj->ref_id)) {
                 $this->accessDenied();
                 return;
             }
         }
         //edit values, they are valid we already checked them above
         foreach ($all_fields as $field) {
             $value = $this->form->getInput("field_" . $field->getId());
             //deletion flag on MOB inputs.
             if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB && $this->form->getItemByPostVar("field_" . $field->getId())->getDeletionFlag()) {
                 $value = -1;
             }
             $record_obj->setRecordFieldValue($field->getId(), $value);
         }
         // Do we need to set a new owner for this record?
         if (!$create_mode) {
             $owner_id = ilObjUser::_lookupId($_POST['field_owner']);
             if (!$owner_id) {
                 $this->sendFailure($this->lng->txt('user_not_known'));
                 return;
             }
             $record_obj->setOwner($owner_id);
         }
         if ($create_mode) {
             ilObjDataCollection::sendNotification("new_record", $this->table_id, $record_obj->getId());
         }
         $record_obj->doUpdate();
         $this->ctrl->setParameter($this, "table_id", $this->table_id);
         $this->ctrl->setParameter($this, "record_id", $this->record_id);
         if (!$this->ctrl->isAsynch()) {
             ilUtil::sendSuccess($this->lng->txt("msg_obj_modified"), true);
         }
         $this->checkAndPerformRedirect();
         if ($this->ctrl->isAsynch()) {
             // If ajax request, return the form in edit mode again
             $this->record_id = $record_obj->getId();
             $this->initForm();
             $this->setFormValues();
             echo $this->tpl->getMessageHTML($this->lng->txt('msg_obj_modified'), 'success') . $this->form->getHTML();
             exit;
         } else {
             $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
         }
     } else {
         // Form not valid...
         $this->form->setValuesByPost();
         if ($this->ctrl->isAsynch()) {
             echo $this->form->getHTML();
             exit;
         } else {
             $this->tpl->setContent($this->form->getHTML());
         }
     }
 }