ILIAS  release_4-4 Revision
All Data Structures Namespaces Files Functions Variables Modules Pages
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 
20 
21  private $record_id;
22  private $table_id;
23  private $table;
24  private $parent_obj;
25 
26 
32  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
33  $this->form = new ilPropertyFormGUI();
34  $this->parent_obj = $parent_obj;
35  $this->record_id = $_REQUEST['record_id'];
36  $this->table_id = $_GET['table_id'];
37 
38  include_once("class.ilDataCollectionDatatype.php");
39  if ($_REQUEST['table_id']) {
40  $this->table_id = $_REQUEST['table_id'];
41  }
42 
43  $this->table = ilDataCollectionCache::getTableCache($this->table_id);
44  }
45 
46 
50  public function executeCommand() {
51  global $ilCtrl;
52 
53  $cmd = $ilCtrl->getCmd();
54  switch ($cmd) {
55  default:
56  $this->$cmd();
57  break;
58  }
59 
60  return true;
61  }
62 
63 
68  public function create() {
69  global $ilCtrl, $tpl;
70 
71  $this->initForm();
72 
73  $tpl->setContent($this->form->getHTML());
74  }
75 
76 
80  public function edit() {
81  global $tpl, $ilCtrl;
82 
83  $this->initForm("edit");
84  $this->getValues();
85 
86  $tpl->setContent($this->form->getHTML());
87  }
88 
89 
93  public function confirmDelete() {
94  global $ilCtrl, $lng, $tpl;
95 
96  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
97  $conf = new ilConfirmationGUI();
98  $conf->setFormAction($ilCtrl->getFormAction($this));
99  $conf->setHeaderText($lng->txt('dcl_confirm_delete_record'));
100 
101  $record = ilDataCollectionCache::getRecordCache($this->record_id);
102 
103  $conf->addItem('record_id', $record->getId(), implode(", ", $record->getRecordFieldValues()));
104  $conf->addHiddenItem('table_id', $this->table_id);
105 
106  $conf->setConfirm($lng->txt('delete'), 'delete');
107  $conf->setCancel($lng->txt('cancel'), 'cancelDelete');
108 
109  $tpl->setContent($conf->getHTML());
110  }
111 
112 
116  public function cancelDelete() {
117  global $ilCtrl;
118 
119  $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
120  }
121 
122 
123  /*
124  * delete
125  */
126  public function delete() {
127  global $ilCtrl, $lng;
128  $record = ilDataCollectionCache::getRecordCache($this->record_id);
129 
130  if (! $this->table->hasPermissionToDeleteRecord($this->parent_obj->ref_id, $record)) {
131  $this->accessDenied();
132 
133  return;
134  }
135 
136  $record->doDelete();
137  ilUtil::sendSuccess($lng->txt("dcl_record_deleted"), true);
138  $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
139  }
140 
141 
147  public function initForm() {
148  global $lng, $ilCtrl, $tpl;
149 
150  //table_id
151  $hidden_prop = new ilHiddenInputGUI("table_id");
152  $hidden_prop->setValue($this->table_id);
153  $this->form->addItem($hidden_prop);
154 
155  $ilCtrl->setParameter($this, "record_id", $this->record_id);
156  $this->form->setFormAction($ilCtrl->getFormAction($this));
157  $allFields = $this->table->getRecordFields();
158 
159  $inline_css = '';
160  foreach ($allFields as $field) {
162 
163  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE) {
164  $fieldref = $field->getFieldRef();
165  $reffield = ilDataCollectionCache::getFieldCache($fieldref);
166  $options = array();
167  if (! $field->isNRef()) {
168  $options[""] = $lng->txt('dcl_please_select');
169  }
170  $reftable = ilDataCollectionCache::getTableCache($reffield->getTableId());
171  foreach ($reftable->getRecords() as $record) {
172  switch($reffield->getDatatypeId()) {
174  $file_obj = new ilObjFile($record->getRecordFieldValue($fieldref), false);
175  $options[$record->getId()] = $file_obj->getFileName();
176  break;
178  $media_obj = new ilObjMediaObject($record->getRecordFieldValue($fieldref), false);
179  $options[$record->getId()] = $media_obj->getTitle();
180  break;
182  $options[$record->getId()] = $record->getRecordFieldSingleHTML($fieldref);
183  break;
184  default:
185  $options[$record->getId()] = $record->getRecordFieldValue($fieldref);
186  break;
187  }
188 
189  }
190  asort($options);
191  $item->setOptions($options);
192  if($item instanceof ilMultiSelectInputGUI)
193  {
194  $item->setWidth(400);
195  $item->setHeight(100);
196  $inline_css .= 'div#'.$item->getFieldId().'{resize:both;} ';
197  }
198  }
199  $tpl->addInlineCss($inline_css);
200 
201  if ($this->record_id) {
202  $record = ilDataCollectionCache::getRecordCache($this->record_id);
203  }
204 
205  $item->setRequired($field->getRequired());
206  //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.
207  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE
208  || $field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB
209  ) {
210  if ($this->record_id && $record->getId()) {
211  $field_value = $record->getRecordFieldValue($field->getId());
212  if ($field_value) {
213  $item->setRequired(false);
214  }
215  }
216  }
217 
218  if (! ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id) && $field->getLocked()) {
219  $item->setDisabled(true);
220  }
221  $this->form->addItem($item);
222  }
223 
224  // Add possibility to change the owner in edit mode
225  if ($this->record_id) {
226  $ownerField = $this->table->getField('owner');
227  $inputfield = ilDataCollectionDatatype::getInputField($ownerField);
228  $this->form->addItem($inputfield);
229  }
230 
231  // save and cancel commands
232  if (isset($this->record_id)) {
233  $this->form->setTitle($lng->txt("dcl_update_record"));
234  $this->form->addCommandButton("save", $lng->txt("dcl_update_record"));
235  $this->form->addCommandButton("cancelUpdate", $lng->txt("cancel"));
236  } else {
237  $this->form->setTitle($lng->txt("dcl_add_new_record"));
238  $this->form->addCommandButton("save", $lng->txt("save"));
239  $this->form->addCommandButton("cancelSave", $lng->txt("cancel"));
240  }
241 
242  $ilCtrl->setParameter($this, "table_id", $this->table_id);
243  $ilCtrl->setParameter($this, "record_id", $this->record_id);
244  }
245 
246 
251  public function getValues() {
252 
253  //Get Record-Values
254  $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
255 
256  //Get Table Field Definitions
257  $allFields = $this->table->getFields();
258 
259  $values = array();
260  foreach ($allFields as $field) {
261  $value = $record_obj->getRecordFieldFormInput($field->getId());
262  $values['field_' . $field->getId()] = $value;
263  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB) {
265  if ($value) {
266  $this->form->getItemByPostVar('field_' . $field->getId())->setImage($img);
267  }
268  }
269  }
270 
271  $this->form->setValuesByArray($values);
272 
273  return true;
274  }
275 
276 
277  /*
278  * cancelUpdate
279  */
280  public function cancelUpdate() {
281  global $ilCtrl;
282  $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
283  }
284 
285 
286  /*
287  * cancelSave
288  */
289  public function cancelSave() {
290  $this->cancelUpdate();
291  }
292 
293 
299  public function save() {
300  global $tpl, $ilUser, $lng, $ilCtrl;
301 
302  $this->initForm();
303  if ($this->form->checkInput()) {
304  $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
305  $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
306 
307  $record_obj->setTableId($this->table_id);
308  $record_obj->setLastUpdate($date_obj->get(IL_CAL_DATETIME));
309  $record_obj->setLastEditBy($ilUser->getId());
310 
311  $create_mode = false;
312 
313  if (ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id)) {
314  $all_fields = $this->table->getRecordFields();
315  } else {
316  $all_fields = $this->table->getEditableFields();
317  }
318 
319  $fail = "";
320  //Check if we can create this record.
321  foreach ($all_fields as $field) {
322  try {
323  $value = $this->form->getInput("field_" . $field->getId());
324  $field->checkValidity($value, $this->record_id);
325  } catch (ilDataCollectionInputException $e) {
326  $fail .= $field->getTitle() . ": " . $e . "<br>";
327  }
328  }
329 
330  if ($fail) {
331  ilUtil::sendFailure($fail, true);
332  $this->sendFailure();
333 
334  return;
335  }
336 
337  if (! isset($this->record_id)) {
338  if (! ($this->table->hasPermissionToAddRecord($this->parent_obj->ref_id))) {
339  $this->accessDenied();
340 
341  return;
342  }
343  $record_obj->setOwner($ilUser->getId());
344  $record_obj->setCreateDate($date_obj->get(IL_CAL_DATETIME));
345  $record_obj->setTableId($this->table_id);
346  $record_obj->doCreate();
347  $this->record_id = $record_obj->getId();
348  $create_mode = true;
349  } else {
350  if (! $record_obj->hasPermissionToEdit($this->parent_obj->ref_id)) {
351  $this->accessDenied();
352 
353  return;
354  }
355  }
356  //edit values, they are valid we already checked them above
357  foreach ($all_fields as $field) {
358  $value = $this->form->getInput("field_" . $field->getId());
359  //deletion flag on MOB inputs.
360  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB
361  && $this->form->getItemByPostVar("field_" . $field->getId())->getDeletionFlag()
362  ) {
363  $value = - 1;
364  }
365  $record_obj->setRecordFieldValue($field->getId(), $value);
366  }
367 
368  // Do we need to set a new owner for this record?
369  if (! $create_mode) {
370  $owner_id = ilObjUser::_lookupId($_POST['field_owner']);
371  if (! $owner_id) {
372  ilUtil::sendFailure($lng->txt('user_not_known'));
373  $this->sendFailure();
374 
375  return;
376  }
377  $record_obj->setOwner($owner_id);
378  }
379 
380  if ($create_mode) {
381  ilObjDataCollection::sendNotification("new_record", $this->table_id, $record_obj->getId());
382  }
383 
384  $record_obj->doUpdate();
385  ilUtil::sendSuccess($lng->txt("msg_obj_modified"), true);
386 
387  $ilCtrl->setParameter($this, "table_id", $this->table_id);
388  $ilCtrl->setParameter($this, "record_id", $this->record_id);
389  $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
390  } else {
391  global $tpl;
392  $this->form->setValuesByPost();
393  $tpl->setContent($this->form->getHTML());
394  }
395  }
396 
397 
398  /*
399  * accessDenied
400  */
401  private function accessDenied() {
402  global $tpl;
403  $tpl->setContent("Access denied");
404  }
405 
406 
407  /*
408  * sendFailure
409  */
410  private function sendFailure() {
411  global $tpl;
412  $this->form->setValuesByPost();
413  $tpl->setContent($this->form->getHTML());
414  }
415 
416 
420  public function searchObjects() {
421  global $lng;
422 
423  $search = $_POST['search_for'];
424  $dest = $_POST['dest'];
425  $html = "";
426  include_once './Services/Search/classes/class.ilQueryParser.php';
427  $query_parser = new ilQueryParser($search);
428  $query_parser->setMinWordLength(1, true);
429  $query_parser->setCombination(QP_COMBINATION_AND);
430  $query_parser->parse();
431  if (! $query_parser->validate()) {
432  $html .= $query_parser->getMessage() . "<br />";
433  }
434 
435  // only like search since fulltext does not support search with less than 3 characters
436  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
437  $object_search = new ilLikeObjectSearch($query_parser);
438  $res = $object_search->performSearch();
439  //$res->setRequiredPermission('copy');
440  $res->filter(ROOT_FOLDER_ID, true);
441 
442  if (! count($results = $res->getResultsByObjId())) {
443  $html .= $lng->txt('dcl_no_search_results_found_for') . ' ' . $search . "<br />";
444  }
446 
447  foreach ($results as $entry) {
448  $tpl = new ilTemplate("tpl.dcl_tree.html", true, true, "Modules/DataCollection");
449  foreach ((array)$entry['refs'] as $reference) {
450  include_once './Services/Tree/classes/class.ilPathGUI.php';
451  $path = new ilPathGUI();
452 
453  $tpl->setCurrentBlock('result');
454  $tpl->setVariable('RESULT_PATH', $path->getPath(ROOT_FOLDER_ID, $reference) . " » " . $entry['title']);
455  $tpl->setVariable('RESULT_REF', $reference);
456  $tpl->setVariable('FIELD_ID', $dest);
457  $tpl->parseCurrentBlock();
458  }
459  $html .= $tpl->get();
460  }
461 
462  echo $html;
463  exit;
464  }
465 
466 
474  private function parseSearchResults($a_res) {
475  foreach ($a_res as $obj_id => $references) {
476  $r['title'] = ilObject::_lookupTitle($obj_id);
477  $r['desc'] = ilObject::_lookupDescription($obj_id);
478  $r['obj_id'] = $obj_id;
479  $r['refs'] = $references;
480 
481  $rows[] = $r;
482  }
483 
484  return $rows ? $rows : array();
485  }
486 }
487 
488 ?>
static sendSuccess($a_info="", $a_keep=false)
Send Success Message to Screen.
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"]
$cmd
Definition: sahs_server.php:35
static _lookupId($a_user_str)
lookup id by login
static _lookupTitle($a_id)
lookup object title
const IL_CAL_UNIX
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.
This class represents a multi selection list property in a property form.
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
if(isset($_FILES['img_file']['size']) && $_FILES['img_file']['size'] > 0) $tpl
Class ilObjMediaObject.
$results
static getInputField(ilDataCollectionField $field)
static sendFailure($a_info="", $a_keep=false)
Send Failure Message to Screen.
static _lookupItemPath($a_mob_id, $a_url_encode=false, $a_web=true, $a_purpose="")
Get path for item with specific purpose.
global $ilUser
Definition: imgupload.php:15
Class ilDataCollectionRecordEditGUI.
global $lng
Definition: privfeed.php:40
$path
Definition: index.php:22
static sendNotification($a_action, $a_table_id, $a_record_id=NULL)
__construct(ilObjDataCollectionGUI $parent_obj)
Constructor.
const QP_COMBINATION_AND
if($_REQUEST['ilias_path']) define('ILIAS_HTTP_PATH' $_REQUEST['ilias_path']
Definition: index.php:7
Confirmation screen class.
$r