ILIAS  release_4-3 Revision
 All Data Structures Namespaces Files Functions Variables Groups 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 
22  private $record_id;
23  private $table_id;
24  private $table;
25  private $parent_obj;
26 
32  {
33  include_once("Services/Form/classes/class.ilPropertyFormGUI.php");
34  $this->form = new ilPropertyFormGUI();
35  $this->parent_obj = $parent_obj;
36  $this->record_id = $_REQUEST['record_id'];
37  $this->table_id = $_GET['table_id'];
38 
39  include_once("class.ilDataCollectionDatatype.php");
40  if($_REQUEST['table_id'])
41  {
42  $this->table_id = $_REQUEST['table_id'];
43  }
44 
45  $this->table = ilDataCollectionCache::getTableCache($this->table_id);
46  }
47 
48 
52  public function executeCommand()
53  {
54  global $ilCtrl;
55 
56  $cmd = $ilCtrl->getCmd();
57  switch($cmd)
58  {
59  default:
60  $this->$cmd();
61  break;
62  }
63  return true;
64  }
65 
66 
71  public function create()
72  {
73  global $ilCtrl, $tpl;
74 
75  $this->initForm();
76 
77  $tpl->setContent($this->form->getHTML());
78  }
79 
83  public function edit()
84  {
85  global $tpl, $ilCtrl;
86 
87  $this->initForm("edit");
88  $this->getValues();
89 
90  $tpl->setContent($this->form->getHTML());
91  }
92 
96  public function confirmDelete()
97  {
98  global $ilCtrl, $lng, $tpl;
99 
100  include_once './Services/Utilities/classes/class.ilConfirmationGUI.php';
101  $conf = new ilConfirmationGUI();
102  $conf->setFormAction($ilCtrl->getFormAction($this));
103  $conf->setHeaderText($lng->txt('dcl_confirm_delete_record'));
104 
105  $record = ilDataCollectionCache::getRecordCache($this->record_id);
106 
107  $conf->addItem('record_id', $record->getId(), implode(", ", $record->getRecordFieldValues()));
108  $conf->addHiddenItem('table_id', $this->table_id);
109 
110  $conf->setConfirm($lng->txt('delete'), 'delete');
111  $conf->setCancel($lng->txt('cancel'), 'cancelDelete');
112 
113  $tpl->setContent($conf->getHTML());
114  }
115 
119  public function cancelDelete()
120  {
121  global $ilCtrl;
122 
123  $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
124  }
125 
126  /*
127  * delete
128  */
129  public function delete()
130  {
131  global $ilCtrl, $lng;
132  $record = ilDataCollectionCache::getRecordCache($this->record_id);
133 
134  if(!$this->table->hasPermissionToDeleteRecord($this->parent_obj->ref_id, $record))
135  {
136  $this->accessDenied();
137  return;
138  }
139 
140  $record->doDelete();
141  ilUtil::sendSuccess($lng->txt("dcl_record_deleted"), true);
142  $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
143  }
144 
150  public function initForm()
151  {
152  global $lng, $ilCtrl;
153 
154  //table_id
155  $hidden_prop = new ilHiddenInputGUI("table_id");
156  $hidden_prop ->setValue($this->table_id);
157  $this->form->addItem($hidden_prop);
158 
159  $ilCtrl->setParameter($this, "record_id", $this->record_id);
160  $this->form->setFormAction($ilCtrl->getFormAction($this));
161  $allFields = $this->table->getRecordFields();
162 
163  foreach($allFields as $field)
164  {
166 
167  if($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_REFERENCE)
168  {
169  $fieldref = $field->getFieldRef();
170  $reffield = ilDataCollectionCache::getFieldCache($fieldref);
171  $options = array();
172  $options[""] = '--';
173  $reftable = ilDataCollectionCache::getTableCache($reffield->getTableId());
174  foreach($reftable->getRecords() as $record)
175  {
176  // If the referenced field is MOB or FILE, we display the filename in the dropdown
177  if ($reffield->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE) {
178  $file_obj = new ilObjFile($record->getRecordFieldValue($fieldref), false);
179  $options[$record->getId()] = $file_obj->getFileName();
180  } else if ($reffield->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB) {
181  $media_obj = new ilObjMediaObject($record->getRecordFieldValue($fieldref), false);
182  $options[$record->getId()] = $media_obj->getTitle();
183  } else {
184  $options[$record->getId()] = $record->getRecordFieldValue($fieldref);
185  }
186  }
187  asort($options);
188  $item->setOptions($options);
189  }
190  if($this->record_id)
191  {
192  $record = ilDataCollectionCache::getRecordCache($this->record_id);
193  }
194 
195 
196  $item->setRequired($field->getRequired());
197  //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.
198  if ($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_FILE || $field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB) {
199  if ($this->record_id && $record->getId()) {
200  $field_value = $record->getRecordFieldValue($field->getId());
201  if ($field_value) {
202  $item->setRequired(false);
203  }
204  }
205  }
206 
207  if(!ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id) && $field->getLocked())
208  {
209  $item->setDisabled(true);
210  }
211  $this->form->addItem($item);
212  }
213 
214  // Add possibility to change the owner in edit mode
215  if ($this->record_id) {
216  $ownerField = $this->table->getField('owner');
217  $inputfield = ilDataCollectionDatatype::getInputField($ownerField);
218  $this->form->addItem($inputfield);
219  }
220 
221  // save and cancel commands
222  if(isset($this->record_id))
223  {
224  $this->form->setTitle($lng->txt("dcl_update_record"));
225  $this->form->addCommandButton("save", $lng->txt("dcl_update_record"));
226  $this->form->addCommandButton("cancelUpdate", $lng->txt("cancel"));
227  }
228  else
229  {
230  $this->form->setTitle($lng->txt("dcl_add_new_record"));
231  $this->form->addCommandButton("save", $lng->txt("save"));
232  $this->form->addCommandButton("cancelSave", $lng->txt("cancel"));
233  }
234 
235  $ilCtrl->setParameter($this, "table_id", $this->table_id);
236  $ilCtrl->setParameter($this, "record_id", $this->record_id);
237 
238  }
239 
244  public function getValues()
245  {
246 
247  //Get Record-Values
248  $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
249 
250  //Get Table Field Definitions
251  $allFields = $this->table->getFields();
252 
253  $values = array();
254  foreach($allFields as $field)
255  {
256  $value = $record_obj->getRecordFieldFormInput($field->getId(), ilDataCollectionCache::getRecordFieldCache($record_obj, $field));
257  $values['field_'.$field->getId()] = $value;
258  if($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB){
260  if($value)
261  $this->form->getItemByPostVar('field_'.$field->getId())->setImage($img);
262  }
263  }
264 
265  $this->form->setValuesByArray($values);
266  return true;
267  }
268 
269  /*
270  * cancelUpdate
271  */
272  public function cancelUpdate()
273  {
274  global $ilCtrl;
275  $ilCtrl->redirectByClass("ildatacollectionrecordlistgui", "listRecords");
276  }
277 
278  /*
279  * cancelSave
280  */
281  public function cancelSave()
282  {
283  $this->cancelUpdate();
284  }
285 
291  public function save()
292  {
293  global $tpl, $ilUser, $lng, $ilCtrl;
294 
295  $this->initForm();
296  if($this->form->checkInput())
297  {
298  $record_obj = ilDataCollectionCache::getRecordCache($this->record_id);
299  $date_obj = new ilDateTime(time(), IL_CAL_UNIX);
300 
301  $record_obj->setTableId($this->table_id);
302  $record_obj->setLastUpdate($date_obj->get(IL_CAL_DATETIME));
303  $record_obj->setLastEditBy($ilUser->getId());
304 
305  $create_mode = false;
306 
307  if(ilObjDataCollection::_hasWriteAccess($this->parent_obj->ref_id))
308  {
309  $all_fields = $this->table->getRecordFields();
310  }
311  else
312  {
313  $all_fields = $this->table->getEditableFields();
314  }
315 
316  $fail = "";
317  //Check if we can create this record.
318  foreach($all_fields as $field)
319  {
320  try
321  {
322  $value = $this->form->getInput("field_".$field->getId());
323  $field->checkValidity($value, $this->record_id);
325  $fail .= $field->getTitle().": ".$e."<br>";
326  }
327 
328  }
329 
330  if($fail)
331  {
332  ilUtil::sendFailure($fail, true);
333  $this->sendFailure();
334  return;
335  }
336 
337  if(!isset($this->record_id))
338  {
339  if(!($this->table->hasPermissionToAddRecord($this->parent_obj->ref_id)))
340  {
341  $this->accessDenied();
342  return;
343  }
344  $record_obj->setOwner($ilUser->getId());
345  $record_obj->setCreateDate($date_obj->get(IL_CAL_DATETIME));
346  $record_obj->setTableId($this->table_id);
347  $record_obj->doCreate();
348  $this->record_id = $record_obj->getId();
349  $create_mode = true;
350  }
351  else
352  {
353  if(!$record_obj->hasPermissionToEdit($this->parent_obj->ref_id))
354  {
355  $this->accessDenied();
356  return;
357  }
358  }
359  //edit values, they are valid we already checked them above
360  foreach($all_fields as $field)
361  {
362  $value = $this->form->getInput("field_".$field->getId());
363  //deletion flag on MOB inputs.
364  if($field->getDatatypeId() == ilDataCollectionDatatype::INPUTFORMAT_MOB && $this->form->getItemByPostVar("field_".$field->getId())->getDeletionFlag()){
365  $value = -1;
366  }
367  $record_obj->setRecordFieldValue($field->getId(), $value);
368  }
369 
370  if (!$create_mode) {
371  $owner_id = ilObjUser::_lookupId($_POST['field_owner']);
372  if(!$owner_id) {
373  ilUtil::sendFailure($lng->txt('user_not_known'));
374  $this->sendFailure();
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  }
391  else
392  {
393  global $tpl;
394  $this->form->setValuesByPost();
395  $tpl->setContent($this->form->getHTML());
396  }
397 
398  }
399 
400  /*
401  * accessDenied
402  */
403  private function accessDenied()
404  {
405  global $tpl;
406  $tpl->setContent("Access denied");
407  }
408 
409  /*
410  * sendFailure
411  */
412  private function sendFailure()
413  {
414  global $tpl;
415  $this->form->setValuesByPost();
416  $tpl->setContent($this->form->getHTML());
417  }
418 
422  public function searchObjects(){
423  global $lng;
424 
425  $search = $_POST['search_for'];
426  $dest = $_POST['dest'];
427  $html = "";
428  include_once './Services/Search/classes/class.ilQueryParser.php';
429  $query_parser = new ilQueryParser($search);
430  $query_parser->setMinWordLength(1,true);
431  $query_parser->setCombination(QP_COMBINATION_AND);
432  $query_parser->parse();
433  if(!$query_parser->validate())
434  {
435  $html .= $query_parser->getMessage()."<br />";
436  }
437 
438  // only like search since fulltext does not support search with less than 3 characters
439  include_once 'Services/Search/classes/Like/class.ilLikeObjectSearch.php';
440  $object_search = new ilLikeObjectSearch($query_parser);
441  $res = $object_search->performSearch();
442  //$res->setRequiredPermission('copy');
443  $res->filter(ROOT_FOLDER_ID,true);
444 
445  if(!count($results = $res->getResultsByObjId()))
446  {
447  $html .= $lng->txt('dcl_no_search_results_found_for'). $search."<br />";
448  }
450 
451  foreach($results as $entry){
452  $tpl = new ilTemplate("tpl.dcl_tree.html",true, true, "Modules/DataCollection");
453  foreach((array) $entry['refs'] as $reference)
454  {
455  include_once './Services/Tree/classes/class.ilPathGUI.php';
456  $path = new ilPathGUI();
457 
458  $tpl->setCurrentBlock('result');
459  $tpl->setVariable('RESULT_PATH',$path->getPath(ROOT_FOLDER_ID, $reference)." > ".$entry['title']);
460  $tpl->setVariable('RESULT_REF',$reference);
461  $tpl->setVariable('FIELD_ID', $dest);
462  $tpl->parseCurrentBlock();
463  }
464  $html .= $tpl->get();
465  }
466 
467  echo $html;
468  exit;
469  }
470 
471 
477  private function parseSearchResults($a_res)
478  {
479  foreach($a_res as $obj_id => $references)
480  {
481  $r['title'] = ilObject::_lookupTitle($obj_id);
482  $r['desc'] = ilObject::_lookupDescription($obj_id);
483  $r['obj_id'] = $obj_id;
484  $r['refs'] = $references;
485 
486  $rows[] = $r;
487  }
488 
489  return $rows ? $rows : array();
490  }
491 }
492 
493 ?>