ILIAS  release_5-0 Revision 5.0.0-1144-gc4397b1f870
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
4require_once("./Modules/DataCollection/classes/class.ilDataCollectionRecord.php");
5require_once("./Modules/DataCollection/classes/class.ilDataCollectionField.php");
6require_once("./Modules/DataCollection/classes/class.ilDataCollectionTable.php");
7require_once("./Modules/DataCollection/classes/class.ilDataCollectionDatatype.php");
8require_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
9require_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
10require_once('./Services/Utilities/classes/class.ilConfirmationGUI.php');
11require_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 $conf->addItem('record_id', $record->getId(), implode(", ", $record->getRecordFieldValues()));
155 $conf->addHiddenItem('table_id', $this->table_id);
156 $conf->setConfirm($this->lng->txt('delete'), 'delete');
157 $conf->setCancel($this->lng->txt('cancel'), 'cancelDelete');
158 $this->tpl->setContent($conf->getHTML());
159 }
160
161
162 public function cancelDelete() {
163 $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
164 }
165
166
167 public function delete() {
169
170 if (!$this->table->hasPermissionToDeleteRecord($this->parent_obj->ref_id, $record)) {
171 $this->accessDenied();
172
173 return;
174 }
175
176 $record->doDelete();
177 ilUtil::sendSuccess($this->lng->txt("dcl_record_deleted"), true);
178 $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
179 }
180
181
190 public function getRecordData($record_id = 0) {
191 $record_id = ($record_id) ? $record_id : $_GET['record_id'];
192 $return = array();
193 if ($record_id) {
195 if (is_object($record)) {
196 $return = $record->getRecordFieldValues();
197 }
198 }
199 if ($this->ctrl->isAsynch()) {
200 echo json_encode($return);
201 exit();
202 }
203
204 return $return;
205 }
206
207
211 public function initForm() {
212 $this->form = new ilPropertyFormGUI();
213 $prefix = ($this->ctrl->isAsynch()) ? 'dclajax' : 'dcl'; // Used by datacolleciton.js to select input elements
214 $this->form->setId($prefix . $this->table_id . $this->record_id);
215
216 $hidden_prop = new ilHiddenInputGUI("table_id");
217 $hidden_prop->setValue($this->table_id);
218 $this->form->addItem($hidden_prop);
219 if ($this->record_id) {
220 $hidden_prop = new ilHiddenInputGUI("record_id");
221 $hidden_prop->setValue($this->record_id);
222 $this->form->addItem($hidden_prop);
223 }
224
225 $this->ctrl->setParameter($this, "record_id", $this->record_id);
226 $this->form->setFormAction($this->ctrl->getFormAction($this));
227 $allFields = $this->table->getRecordFields();
228 $inline_css = '';
229 foreach ($allFields as $field) {
231 if ($item === NULL) {
232 continue; // Fields calculating values at runtime, e.g. ilDataCollectionFormulaField do not have input
233 }
234 if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
235 $fieldref = $field->getFieldRef();
236 $reffield = ilDataCollectionCache::getFieldCache($fieldref);
237 $options = array();
238 if (!$field->isNRef()) {
239 $options[""] = $this->lng->txt('dcl_please_select');
240 }
241 $reftable = ilDataCollectionCache::getTableCache($reffield->getTableId());
242 foreach ($reftable->getRecords() as $record) {
243 // If the referenced field is MOB or FILE, we display the filename in the dropdown
244 switch ($reffield->getDatatypeId()) {
246 $file_obj = new ilObjFile($record->getRecordFieldValue($fieldref), false);
247 $options[$record->getId()] = $file_obj->getFileName();
248 break;
250 $media_obj = new ilObjMediaObject($record->getRecordFieldValue($fieldref), false);
251 $options[$record->getId()] = $media_obj->getTitle();
252 break;
254 $options[$record->getId()] = strtotime($record->getRecordFieldSingleHTML($fieldref));
255 // TT #0019091: options2 are the actual values, options the timestamp for sorting
256 $options2[$record->getId()] = $record->getRecordFieldSingleHTML($fieldref);
257 break;
258 default:
259 $options[$record->getId()] = $record->getRecordFieldValue($fieldref);
260 break;
261 }
262 }
263 asort($options);
264
265 // TT #0019091: restore the actual values after sorting with timestamp
266 if ($reffield->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_DATETIME) {
267 foreach ($options as $key => $opt) {
268 $options[$key] = $options2[$key];
269 }
270 // the option 'please select' messes with the order, therefore we reset it
271 unset($options[""]);
272 $options = array("" => $this->lng->txt('dcl_please_select')) + $options;
273 }
274
275 $item->setOptions($options);
276 if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) { // FSX use this to apply to MultiSelectInputGUI
277 if ($reftable->hasPermissionToAddRecord($_GET['ref_id'])) {
278 $item->addCustomAttribute('data-ref="1"');
279 $item->addCustomAttribute('data-ref-table-id="' . $reftable->getId() . '"');
280 $item->addCustomAttribute('data-ref-field-id="' . $reffield->getId() . '"');
281 }
282 }
283 }
284
285 if ($this->record_id) {
287 }
288
289 $item->setRequired($field->getRequired());
290 //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.
291 if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE
292 || $field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB
293 ) {
294 if ($this->record_id AND $record->getId()) {
295 $field_value = $record->getRecordFieldValue($field->getId());
296 if ($field_value) {
297 $item->setRequired(false);
298 }
299 }
300 // If this is an ajax request to return the form, input files are currently not supported
301 if ($this->ctrl->isAsynch()) {
302 $item->setDisabled(true);
303 }
304 }
305
306 if (!ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id) && $field->getLocked()) {
307 $item->setDisabled(true);
308 }
309 $this->form->addItem($item);
310 }
311
312 $this->tpl->addInlineCss($inline_css);
313
314 // Add possibility to change the owner in edit mode
315 if ($this->record_id) {
316 $ownerField = $this->table->getField('owner');
317 $inputfield = ilDataCollectionDatatype::getInputField($ownerField);
318 $this->form->addItem($inputfield);
319 }
320
321 // save and cancel commands
322 if ($this->record_id) {
323 $this->form->setTitle($this->lng->txt("dcl_update_record"));
324 $this->form->addCommandButton("save", $this->lng->txt("dcl_update_record"));
325 if (!$this->ctrl->isAsynch()) {
326 $this->form->addCommandButton("cancelUpdate", $this->lng->txt("cancel"));
327 }
328 } else {
329 $this->form->setTitle($this->lng->txt("dcl_add_new_record"));
330 $this->form->addCommandButton("save", $this->lng->txt("save"));
331 if (!$this->ctrl->isAsynch()) {
332 $this->form->addCommandButton("cancelSave", $this->lng->txt("cancel"));
333 }
334 }
335 $this->ctrl->setParameter($this, "table_id", $this->table_id);
336 $this->ctrl->setParameter($this, "record_id", $this->record_id);
337 }
338
339
345 public function setFormValues() {
346 //Get Record-Values
347 $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
348 //Get Table Field Definitions
349 $allFields = $this->table->getFields();
350 $values = array();
351 foreach ($allFields as $field) {
352 $value = $record_obj->getRecordFieldFormInput($field->getId());
353 $values['field_' . $field->getId()] = $value;
354 }
355 $values['record_id'] = $record_obj->getId();
356 $this->form->setValuesByArray($values);
357
358 return true;
359 }
360
361
365 public function cancelUpdate() {
366 $this->checkAndPerformRedirect(true);
367 }
368
369
373 public function cancelSave() {
374 $this->cancelUpdate();
375 }
376
377
381 public function save() {
382 $this->initForm();
383
384 $valid = $this->form->checkInput();
385
386
387 $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
388 $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
389 $record_obj->setTableId($this->table_id);
390 $record_obj->setLastUpdate($date_obj->get(IL_CAL_DATETIME));
391 $record_obj->setLastEditBy($this->user->getId());
392
393 $create_mode = false;
394
395 if (ilObjDataCollectionAccess::hasWriteAccess($this->parent_obj->ref_id)) {
396 $all_fields = $this->table->getRecordFields();
397 } else {
398 $all_fields = $this->table->getEditableFields();
399 }
400
401 //Check if we can create this record.
402 foreach ($all_fields as $field) {
403 try {
404 $value = $this->form->getInput("field_" . $field->getId());
405 $field->checkValidity($value, $this->record_id);
406 } catch (ilDataCollectionInputException $e) {
407 $valid = false;
408 $item = $this->form->getItemByPostVar('field_'.$field->getId());
409 $item->setAlert($e);
410 }
411 }
412
413
414 if ($valid) {
415 if (!isset($this->record_id)) {
416 if (!($this->table->hasPermissionToAddRecord($this->parent_obj->ref_id))) {
417 $this->accessDenied();
418
419 return;
420 }
421 $record_obj->setOwner($this->user->getId());
422 $record_obj->setCreateDate($date_obj->get(IL_CAL_DATETIME));
423 $record_obj->setTableId($this->table_id);
424 $record_obj->doCreate();
425 $this->record_id = $record_obj->getId();
426 $create_mode = true;
427 } else {
428 if (!$record_obj->hasPermissionToEdit($this->parent_obj->ref_id)) {
429 $this->accessDenied();
430
431 return;
432 }
433 }
434 //edit values, they are valid we already checked them above
435 foreach ($all_fields as $field) {
436 $value = $this->form->getInput("field_" . $field->getId());
437
438 switch (true) {
439 //deletion flag on MOB inputs.
440 case ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB
441 && $this->form->getItemByPostVar("field_" . $field->getId())->getDeletionFlag()):
442 $value = - 1;
443 $record_obj->setRecordFieldValue($field->getId(), $value);
444 break;
445 // mantis 0018064
446 case ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE && $value['tmp_name']):
447 $record_obj->setRecordFieldValue($field->getId(), $value);
448 break;
449 default:
450 $record_obj->setRecordFieldValue($field->getId(), $value);
451 break;
452 }
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
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 $this->form->setValuesByPost();
492 if ($this->ctrl->isAsynch()) {
493 echo $this->form->getHTML();
494 exit();
495 } else {
496 $this->tpl->setContent($this->form->getHTML());
497 }
498 }
499 }
500
501
506 protected function checkAndPerformRedirect($force_redirect = false) {
507 if ($force_redirect || (isset($_GET['redirect']) && !$this->ctrl->isAsynch())) {
508 switch ((int)$_GET['redirect']) {
510 $this->ctrl->setParameterByClass('ildatacollectionrecordviewgui', 'record_id', $this->record_id);
511 $this->ctrl->setParameterByClass('ildatacollectionrecordviewgui', 'table_id', $this->table_id);
512 $this->ctrl->redirectByClass("ildatacollectionrecordviewgui", "renderRecord");
513 break;
515 $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
516 break;
517 default:
518 $this->ctrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
519 }
520 }
521 }
522
523
524 protected function accessDenied() {
525 if (!$this->ctrl->isAsynch()) {
526 ilUtil::sendFailure($this->lng->txt('dcl_msg_no_perm_edit'), true);
527 $this->ctrl->redirectByClass('ildatacollectionrecordlistgui', 'listRecords');
528 } else {
529 echo $this->lng->txt('dcl_msg_no_perm_edit');
530 exit();
531 }
532 }
533
534
538 protected function sendFailure($message) {
539 $keep = ($this->ctrl->isAsynch()) ? false : true;
540 $this->form->setValuesByPost();
541 if ($this->ctrl->isAsynch()) {
542 echo $this->tpl->getMessageHTML($message, 'failure') . $this->form->getHTML();
543 exit();
544 } else {
545 ilUtil::sendFailure($message, $keep);
546 $this->tpl->setContent($this->form->getHTML());
547 }
548 }
549
550
554 public function searchObjects() {
555 $search = $_POST['search_for'];
556 $dest = $_POST['dest'];
557 $html = "";
558 include_once './Services/Search/classes/class.ilQueryParser.php';
559 $query_parser = new ilQueryParser($search);
560 $query_parser->setMinWordLength(1, true);
561 $query_parser->setCombination(QP_COMBINATION_AND);
562 $query_parser->parse();
563 if (!$query_parser->validate()) {
564 $html .= $query_parser->getMessage() . "<br />";
565 }
566
567 // only like search since fulltext does not support search with less than 3 characters
568 include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
569 $object_search = new ilLikeObjectSearch($query_parser);
570 $res = $object_search->performSearch();
571 //$res->setRequiredPermission('copy');
572 $res->filter(ROOT_FOLDER_ID, true);
573
574 if (!count($results = $res->getResultsByObjId())) {
575 $html .= $this->lng->txt('dcl_no_search_results_found_for') . ' ' . $search . "<br />";
576 }
578
579 foreach ($results as $entry) {
580 $tpl = new ilTemplate("tpl.dcl_tree.html", true, true, "Modules/DataCollection");
581 foreach ((array)$entry['refs'] as $reference) {
582 include_once './Services/Tree/classes/class.ilPathGUI.php';
583 $path = new ilPathGUI();
584 $tpl->setCurrentBlock('result');
585 $tpl->setVariable('RESULT_PATH', $path->getPath(ROOT_FOLDER_ID, $reference) . " ยป " . $entry['title']);
586 $tpl->setVariable('RESULT_REF', $reference);
587 $tpl->setVariable('FIELD_ID', $dest);
588 $tpl->parseCurrentBlock();
589 }
590 $html .= $tpl->get();
591 }
592
593 echo $html;
594 exit;
595 }
596
597
605 protected function parseSearchResults($a_res) {
606 $rows = array();
607 foreach ($a_res as $obj_id => $references) {
608 $r = array();
609 $r['title'] = ilObject::_lookupTitle($obj_id);
610 $r['desc'] = ilObject::_lookupDescription($obj_id);
611 $r['obj_id'] = $obj_id;
612 $r['refs'] = $references;
613 $rows[] = $r;
614 }
615
616 return $rows;
617 }
618}
619
620?>
$_GET["client_id"]
const IL_CAL_UNIX
const IL_CAL_DATETIME
const QP_COMBINATION_AND
Confirmation screen class.
static getRecordCache($record_id=0)
static getInputField(ilDataCollectionField $field)
Class ilDataCollectionRecordEditGUI.
getRecordData($record_id=0)
Return All fields and values from a record ID.
const REDIRECT_RECORD_LIST
Possible redirects after saving/updating a record - use GET['redirect'] to set constants.
__construct(ilObjDataCollectionGUI $parent_obj)
checkAndPerformRedirect($force_redirect=false)
Checkes to what view (table or detail) should be redirected and performs redirect.
setFormValues()
Set values from object to form.
searchObjects()
This function is only used by the ajax request if searching for ILIAS references.
@classDescription Date and time handling
This class represents a hidden form property in a property form.
Class ilObjDataCollectionGUI.
static sendNotification($a_action, $a_table_id, $a_record_id=NULL)
Class ilObjFile.
Class ilObjMediaObject.
static _lookupId($a_user_str)
lookup id by login
static _lookupTitle($a_id)
lookup object title
static _lookupDescription($a_id)
lookup object description
Creates a path for a start and endnode.
This class represents a property form user interface.
special template class to simplify handling of ITX/PEAR
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
$_POST['username']
Definition: cron.php:12
$valid
global $ilCtrl
Definition: ilias.php:18
exit
Definition: login.php:54
$cmd
Definition: sahs_server.php:35
$results
$path
Definition: index.php:22
if($_REQUEST['ilias_path']) define('ILIAS_HTTP_PATH' $_REQUEST['ilias_path']
Definition: index.php:7
if(!is_array($argv)) $options
global $ilUser
Definition: imgupload.php:15