ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
class.ilDataCollectionRecordEditGUI.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("./Modules/DataCollection/classes/class.ilDataCollectionRecord.php");
5 require_once("./Modules/DataCollection/classes/class.ilDataCollectionField.php");
6 require_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
7 require_once("./Modules/DataCollection/classes/class.ilDataCollectionDatatype.php");
8 require_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
9 require_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
10 require_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
11 require_once('./Services/UIComponent/Overlay/classes/class.ilOverlayGUI.php');
12 
25 
31  const REDIRECT_DETAIL = 2;
35  protected $record_id;
39  protected $table_id;
43  protected $table;
47  protected $parent_obj;
51  protected $record;
55  protected $ctrl;
59  protected $tpl;
63  protected $lng;
67  protected $user;
71  protected $form;
72 
73 
78  global $ilCtrl, $tpl, $lng, $ilUser;
79 
80  $this->ctrl = $ilCtrl;
81  $this->tpl = $tpl;
82  $this->lng = $lng;
83  $this->user = $ilUser;
84  $this->parent_obj = $parent_obj;
85  $this->record_id = $_REQUEST['record_id'];
86  $this->table_id = $_REQUEST['table_id'];
87  }
88 
89 
93  public function executeCommand() {
94  if ($_GET['mode']) {
95  $this->ctrl->saveParameter($this, 'mode');
96  $this->ctrl->setParameterByClass("ildatacollectionrecordlistgui", "mode", $_GET['mode']);
97  }
98  $this->ctrl->saveParameter($this, 'redirect');
99  if ($this->record_id) {
100  $this->record = ilDataCollectionCache::getRecordCache($this->record_id);
101  if (!$this->record->hasPermissionToEdit($this->parent_obj->ref_id) OR !$this->record->hasPermissionToView($this->parent_obj->ref_id)) {
102  $this->accessDenied();
103  }
104  $this->table = $this->record->getTable();
105  $this->table_id = $this->table->getId();
106  } else {
107  $this->table = ilDataCollectionCache::getTableCache($this->table_id);
109  $this->accessDenied();
110  }
111  }
112 
113  $cmd = $this->ctrl->getCmd();
114  switch ($cmd) {
115  default:
116  $this->$cmd();
117  break;
118  }
119 
120  return true;
121  }
122 
123 
124  public function create() {
125  $this->initForm();
126  if ($this->ctrl->isAsynch()) {
127  echo $this->form->getHTML();
128  exit();
129  } else {
130  $this->tpl->setContent("<script>ilDataCollection.strings.add_value='" . $this->lng->txt('add_value') . "';</script>"
131  . $this->form->getHTML());
132  }
133  }
134 
135 
136  public function edit() {
137  $this->initForm();
138  $this->setFormValues();
139  if ($this->ctrl->isAsynch()) {
140  echo $this->form->getHTML();
141  exit();
142  } else {
143  $this->tpl->setContent("<script>ilDataCollection.strings.add_value='" . $this->lng->txt('add_value') . "';</script>"
144  . $this->form->getHTML());
145  }
146  }
147 
148 
149  public function confirmDelete() {
150  $conf = new ilConfirmationGUI();
151  $conf->setFormAction($this->ctrl->getFormAction($this));
152  $conf->setHeaderText($this->lng->txt('dcl_confirm_delete_record'));
154  $text = '';
155  foreach ($record->getRecordFields() as $record_field) {
156  $value = $record_field->getExportValue();
157  // cut long texts
158  if (strlen($value) > 150) {
159  $value = substr($value, 0, 100) . ' ...';
160  }
161  $text .= $record_field->getField()->getTitle() . ': ' . $value . "<br>";
162  }
163  $conf->addItem('record_id', $record->getId(), $text);
164  $conf->addHiddenItem('table_id', $this->table_id);
165  $conf->setConfirm($this->lng->txt('delete'), 'delete');
166  $conf->setCancel($this->lng->txt('cancel'), 'cancelDelete');
167  $this->tpl->setContent($conf->getHTML());
168  }
169 
170 
171  public function cancelDelete() {
172  $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
173  }
174 
175 
176  public function delete() {
178 
179  if (!$this->table->hasPermissionToDeleteRecord($this->parent_obj->ref_id, $record)) {
180  $this->accessDenied();
181 
182  return;
183  }
184 
185  $record->doDelete();
186  ilUtil::sendSuccess($this->lng->txt("dcl_record_deleted"), true);
187  $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
188  }
189 
190 
199  public function getRecordData($record_id = 0) {
200  $record_id = ($record_id) ? $record_id : $_GET['record_id'];
201  $return = array();
202  if ($record_id) {
204  if (is_object($record)) {
205  $return = $record->getRecordFieldValues();
206  }
207  }
208  if ($this->ctrl->isAsynch()) {
209  echo json_encode($return);
210  exit();
211  }
212 
213  return $return;
214  }
215 
216 
220  public function initForm() {
221  $this->form = new ilPropertyFormGUI();
222  $prefix = ($this->ctrl->isAsynch()) ? 'dclajax' : 'dcl'; // Used by datacolleciton.js to select input elements
223  $this->form->setId($prefix . $this->table_id . $this->record_id);
224 
225  $hidden_prop = new ilHiddenInputGUI("table_id");
226  $hidden_prop->setValue($this->table_id);
227  $this->form->addItem($hidden_prop);
228  if ($this->record_id) {
229  $hidden_prop = new ilHiddenInputGUI("record_id");
230  $hidden_prop->setValue($this->record_id);
231  $this->form->addItem($hidden_prop);
232  }
233 
234  $this->ctrl->setParameter($this, "record_id", $this->record_id);
235  $this->form->setFormAction($this->ctrl->getFormAction($this));
236  $allFields = $this->table->getRecordFields();
237 
238  $inline_css = '';
239  foreach ($allFields as $field) {
241  if ($item === NULL) {
242  continue; // Fields calculating values at runtime, e.g. ilDataCollectionFormulaField do not have input
243  }
244  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
245  $fieldref = $field->getFieldRef();
246  $reffield = ilDataCollectionCache::getFieldCache($fieldref);
247  $options = array();
248  if (!$field->isNRef()) {
249  $options[""] = $this->lng->txt('dcl_please_select');
250  }
251  $reftable = ilDataCollectionCache::getTableCache($reffield->getTableId());
252  foreach ($reftable->getRecords() as $record) {
253  // If the referenced field is MOB or FILE, we display the filename in the dropdown
254  switch ($reffield->getDatatypeId()) {
256  $file_obj = new ilObjFile($record->getRecordFieldValue($fieldref), false);
257  $options[$record->getId()] = $file_obj->getFileName();
258  break;
260  $media_obj = new ilObjMediaObject($record->getRecordFieldValue($fieldref), false);
261  $options[$record->getId()] = $media_obj->getTitle();
262  break;
264  $options[$record->getId()] = strtotime($record->getRecordFieldSingleHTML($fieldref));
265  // TT #0019091: options2 are the actual values, options the timestamp for sorting
266  $options2[$record->getId()] = $record->getRecordFieldSingleHTML($fieldref);
267  break;
269  $value = $record->getRecordFieldValue($fieldref);
270  if (($json = json_decode($value)) && (json_decode($value) instanceof stdClass)) {
271  $value = $json->title ? $json->title : $json->link;
272  }
273  $options[$record->getId()] = $value;
274  break;
275  default:
276  $options[$record->getId()] = $record->getRecordFieldValue($fieldref);
277  break;
278  }
279  }
280  asort($options);
281 
282  // TT #0019091: restore the actual values after sorting with timestamp
283  if ($reffield->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_DATETIME) {
284  foreach ($options as $key => $opt) {
285  $options[$key] = $options2[$key];
286  }
287  // the option 'please select' messes with the order, therefore we reset it
288  unset($options[""]);
289  $options = array("" => $this->lng->txt('dcl_please_select')) + $options;
290  }
291 
292  $item->setOptions($options);
293  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) { // FSX use this to apply to MultiSelectInputGUI
294  if ($reftable->hasPermissionToAddRecord($_GET['ref_id'])) {
295  $item->addCustomAttribute('data-ref="1"');
296  $item->addCustomAttribute('data-ref-table-id="' . $reftable->getId() . '"');
297  $item->addCustomAttribute('data-ref-field-id="' . $reffield->getId() . '"');
298  }
299  }
300  }
301 
302  if ($this->record_id) {
304  }
305 
306  $item->setRequired($field->getRequired());
307  //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.
308  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE
309  || $field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB
310  ) {
311  if ($this->record_id AND $record->getId()) {
312  $field_value = $record->getRecordFieldValue($field->getId());
313  if ($field_value) {
314  $item->setRequired(false);
315  }
316  }
317  // If this is an ajax request to return the form, input files are currently not supported
318  if ($this->ctrl->isAsynch()) {
319  $item->setDisabled(true);
320  }
321  }
322 
323  if (!ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id) && $field->getLocked()) {
324  $item->setDisabled(true);
325  }
326  $this->form->addItem($item);
327  }
328 
329  $this->tpl->addInlineCss($inline_css);
330 
331  // Add possibility to change the owner in edit mode
332  if ($this->record_id) {
333  $ownerField = $this->table->getField('owner');
334  $inputfield = ilDataCollectionDatatype::getInputField($ownerField);
335  $this->form->addItem($inputfield);
336  }
337 
338  // save and cancel commands
339  if ($this->record_id) {
340  $this->form->setTitle($this->lng->txt("dcl_update_record"));
341  $this->form->addCommandButton("save", $this->lng->txt("dcl_update_record"));
342  if (!$this->ctrl->isAsynch()) {
343  $this->form->addCommandButton("cancelUpdate", $this->lng->txt("cancel"));
344  }
345  } else {
346  $this->form->setTitle($this->lng->txt("dcl_add_new_record"));
347  $this->form->addCommandButton("save", $this->lng->txt("save"));
348  if (!$this->ctrl->isAsynch()) {
349  $this->form->addCommandButton("cancelSave", $this->lng->txt("cancel"));
350  }
351  }
352  $this->ctrl->setParameter($this, "table_id", $this->table_id);
353  $this->ctrl->setParameter($this, "record_id", $this->record_id);
354  }
355 
356 
362  public function setFormValues() {
363  //Get Record-Values
364  $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
365  if ($record_obj->getId()) {
366  //Get Table Field Definitions
367  $allFields = $this->table->getFields();
368  foreach ($allFields as $field) {
369  $record_obj->fillRecordFieldFormInput($field->getId(), $this->form);
370  }
371  } else {
372  $this->form->setValuesByPost();
373  }
374 
375  return true;
376  }
377 
378 
382  public function cancelUpdate() {
383  $this->checkAndPerformRedirect(true);
384  }
385 
386 
390  public function cancelSave() {
391  $this->cancelUpdate();
392  }
393 
394 
398  public function save() {
399  $this->initForm();
400 
401  $valid = $this->form->checkInput();
402 
403  $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
404  $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
405  $record_obj->setTableId($this->table_id);
406  $record_obj->setLastUpdate($date_obj->get(IL_CAL_DATETIME));
407  $record_obj->setLastEditBy($this->user->getId());
408 
409  $create_mode = false;
410 
411  if (ilObjDataCollectionAccess::hasWriteAccess($this->parent_obj->ref_id)) {
412  $all_fields = $this->table->getRecordFields();
413  } else {
414  $all_fields = $this->table->getEditableFields();
415  }
416 
417  //Check if we can create this record.
418  foreach ($all_fields as $field) {
419  try {
420  $value = $this->form->getInput("field_" . $field->getId());
421  $field->checkValidity($value, $this->record_id);
422  } catch (ilDataCollectionInputException $e) {
423  $valid = false;
424  $item = $this->form->getItemByPostVar('field_'.$field->getId());
425  $item->setAlert($e);
426  }
427  }
428 
429  if ($valid) {
430  if (!isset($this->record_id)) {
431  if (!($this->table->hasPermissionToAddRecord($this->parent_obj->ref_id))) {
432  $this->accessDenied();
433 
434  return;
435  }
436  $record_obj->setOwner($this->user->getId());
437  $record_obj->setCreateDate($date_obj->get(IL_CAL_DATETIME));
438  $record_obj->setTableId($this->table_id);
439  $record_obj->doCreate();
440  $this->record_id = $record_obj->getId();
441  $create_mode = true;
442  } else {
443  if (!$record_obj->hasPermissionToEdit($this->parent_obj->ref_id)) {
444  $this->accessDenied();
445 
446  return;
447  }
448  }
449 
450  //edit values, they are valid we already checked them above
451  foreach ($all_fields as $field) {
452  $record_obj->setRecordFieldValueFromForm($field->getId(), $this->form);
453  }
454 
455  // Do we need to set a new owner for this record?
456  if (!$create_mode) {
457  $owner_id = ilObjUser::_lookupId($_POST['field_owner']);
458  if (!$owner_id) {
459  $this->sendFailure($this->lng->txt('user_not_known'));
460 
461  return;
462  }
463  $record_obj->setOwner($owner_id);
464  }
465 
466  if ($create_mode) {
467  ilObjDataCollection::sendNotification("new_record", $this->table_id, $record_obj->getId());
468  }
469  $record_obj->doUpdate();
470 
471  $this->ctrl->setParameter($this, "table_id", $this->table_id);
472  $this->ctrl->setParameter($this, "record_id", $this->record_id);
473 
474  if (!$this->ctrl->isAsynch()) {
475  ilUtil::sendSuccess($this->lng->txt("msg_obj_modified"), true);
476  }
477 
478  $this->checkAndPerformRedirect();
479  if ($this->ctrl->isAsynch()) {
480  // If ajax request, return the form in edit mode again
481  $this->record_id = $record_obj->getId();
482  $this->initForm();
483  $this->setFormValues();
484  echo $this->tpl->getMessageHTML($this->lng->txt('msg_obj_modified'), 'success') . $this->form->getHTML();
485  exit();
486  } else {
487  $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
488  }
489  } else {
490  // Form not valid...
491  //TODO: URL title flushes on invalid form
492  $this->form->setValuesByPost();
493  if ($this->ctrl->isAsynch()) {
494  echo $this->form->getHTML();
495  exit();
496  } else {
497  $this->tpl->setContent($this->form->getHTML());
498  }
499  }
500  }
501 
502 
507  protected function checkAndPerformRedirect($force_redirect = false) {
508  if ($force_redirect || (isset($_GET['redirect']) && !$this->ctrl->isAsynch())) {
509  switch ((int)$_GET['redirect']) {
510  case self::REDIRECT_DETAIL:
511  $this->ctrl->setParameterByClass('ildatacollectionrecordviewgui', 'record_id', $this->record_id);
512  $this->ctrl->setParameterByClass('ildatacollectionrecordviewgui', 'table_id', $this->table_id);
513  $this->ctrl->redirectByClass("ildatacollectionrecordviewgui", "renderRecord");
514  break;
515  case self::REDIRECT_RECORD_LIST:
516  $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
517  break;
518  default:
519  $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
520  }
521  }
522  }
523 
524 
525  protected function accessDenied() {
526  if (!$this->ctrl->isAsynch()) {
527  ilUtil::sendFailure($this->lng->txt('dcl_msg_no_perm_edit'), true);
528  $this->ctrl->redirectByClass('ildatacollectionrecordlistgui', 'listRecords');
529  } else {
530  echo $this->lng->txt('dcl_msg_no_perm_edit');
531  exit();
532  }
533  }
534 
535 
539  protected function sendFailure($message) {
540  $keep = ($this->ctrl->isAsynch()) ? false : true;
541  $this->form->setValuesByPost();
542  if ($this->ctrl->isAsynch()) {
543  echo $this->tpl->getMessageHTML($message, 'failure') . $this->form->getHTML();
544  exit();
545  } else {
546  ilUtil::sendFailure($message, $keep);
547  $this->tpl->setContent($this->form->getHTML());
548  }
549  }
550 
551 
555  public function searchObjects() {
556  $search = $_POST['search_for'];
557  $dest = $_POST['dest'];
558  $html = "";
559  include_once './Services/Search/classes/class.ilQueryParser.php';
560  $query_parser = new ilQueryParser($search);
561  $query_parser->setMinWordLength(1, true);
562  $query_parser->setCombination(QP_COMBINATION_AND);
563  $query_parser->parse();
564  if (!$query_parser->validate()) {
565  $html .= $query_parser->getMessage() . "<br />";
566  }
567 
568  // only like search since fulltext does not support search with less than 3 characters
569  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
570  $object_search = new ilLikeObjectSearch($query_parser);
571  $res = $object_search->performSearch();
572  //$res->setRequiredPermission('copy');
573  $res->filter(ROOT_FOLDER_ID, true);
574 
575  if (!count($results = $res->getResultsByObjId())) {
576  $html .= $this->lng->txt('dcl_no_search_results_found_for') . ' ' . $search . "<br />";
577  }
579 
580  foreach ($results as $entry) {
581  $tpl = new ilTemplate("tpl.dcl_tree.html", true, true, "Modules/DataCollection");
582  foreach ((array)$entry['refs'] as $reference) {
583  include_once './Services/Tree/classes/class.ilPathGUI.php';
584  $path = new ilPathGUI();
585  $tpl->setCurrentBlock('result');
586  $tpl->setVariable('RESULT_PATH', $path->getPath(ROOT_FOLDER_ID, $reference) . " ยป " . $entry['title']);
587  $tpl->setVariable('RESULT_REF', $reference);
588  $tpl->setVariable('FIELD_ID', $dest);
589  $tpl->parseCurrentBlock();
590  }
591  $html .= $tpl->get();
592  }
593 
594  echo $html;
595  exit;
596  }
597 
598 
606  protected function parseSearchResults($a_res) {
607  $rows = array();
608  foreach ($a_res as $obj_id => $references) {
609  $r = array();
610  $r['title'] = ilObject::_lookupTitle($obj_id);
611  $r['desc'] = ilObject::_lookupDescription($obj_id);
612  $r['obj_id'] = $obj_id;
613  $r['refs'] = $references;
614  $rows[] = $r;
615  }
616 
617  return $rows;
618  }
619 }
620 
621 ?>
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
setFormValues()
Set values from object to form.
static getRecordCache($record_id=0)
Creates a path for a start and endnode.
exit
Definition: login.php:54
$_POST['username']
Definition: cron.php:12
const IL_CAL_DATETIME
This class represents a property form user interface.
$_GET["client_id"]
$valid
$cmd
Definition: sahs_server.php:35
static _lookupId($a_user_str)
Lookup id by login.
static _lookupTitle($a_id)
lookup object title
checkAndPerformRedirect($force_redirect=false)
Checkes to what view (table or detail) should be redirected and performs redirect.
const IL_CAL_UNIX
const REDIRECT_RECORD_LIST
Possible redirects after saving/updating a record - use GET[&#39;redirect&#39;] to set constants.
searchObjects()
This function is only used by the ajax request if searching for ILIAS references. ...
global $ilCtrl
Definition: ilias.php:18
This class represents a hidden form property in a property form.
Class ilObjDataCollectionGUI.
$r
Definition: example_031.php:79
if(!is_array($argv)) $options
static _lookupDescription($a_id)
lookup object description
special template class to simplify handling of ITX/PEAR
Class ilObjFile.
Date and time handling
Class ilObjMediaObject.
$results
static getInputField(ilDataCollectionField $field)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
global $ilUser
Definition: imgupload.php:15
Class ilDataCollectionRecordEditGUI.
$path
Definition: index.php:22
$text
static sendNotification($a_action, $a_table_id, $a_record_id=NULL)
__construct(ilObjDataCollectionGUI $parent_obj)
const QP_COMBINATION_AND
if($_REQUEST['ilias_path']) define('ILIAS_HTTP_PATH' $_REQUEST['ilias_path']
Definition: index.php:7
$html
Definition: example_001.php:87
getRecordData($record_id=0)
Return All fields and values from a record ID.
Confirmation screen class.