ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
class.arEditGUI.php
Go to the documentation of this file.
1 <?php
2 require_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
3 require_once('./Services/ActiveRecord/Views/Edit/class.arEditField.php');
4 require_once('./Services/ActiveRecord/Views/Edit/class.arEditFields.php');
5 
14 {
15 
19  protected $ar;
23  protected $parent_gui;
27  protected $ctrl;
31  protected $form_name = "";
35  protected $form_prefix = "";
39  protected $fields;
40 
41 
47  {
48  global $DIC;
49  $ilCtrl = $DIC['ilCtrl'];
50 
51  $this->ar = $ar;
52  $this->parent_gui = $parent_gui;
53  $this->ctrl = $ilCtrl;
54  $this->ctrl->saveParameter($parent_gui, 'ar_id');
55  $this->setFormName(get_class($ar));
56  $this->init();
57  }
58 
59 
65  protected function init()
66  {
67  $this->initFields();
68  $this->initForm();
69  if ($this->ar->getPrimaryFieldValue() != 0) {
70  $this->fillForm();
71  }
72  }
73 
74 
75  protected function initFields()
76  {
77  $this->fields = new arEditFields($this->ar);
78  $this->customizeFields();
79  $this->fields->sortFields();
80  }
81 
82 
83  protected function customizeFields()
84  {
85  }
86 
87 
88  protected function initForm()
89  {
90  $this->BeforeInitForm();
91  $this->initFormAction();
92  $this->initFormTitle();
93  $this->generateFormFields();
94  $this->initCommandButtons();
95  $this->afterInitForm();
96  }
97 
98 
99  protected function beforeInitForm()
100  {
101  }
102 
103 
104  protected function initFormAction()
105  {
106  $this->setFormAction($this->ctrl->getFormAction($this->parent_gui, "index"));
107  }
108 
109 
110  protected function initFormTitle()
111  {
112  $this->setFormPrefix("");
113  if ($this->ar->getPrimaryFieldValue() == 0) {
114  $this->setTitle($this->txt($this->getFormPrefix() . 'create_' . $this->getFormName()));
115  } else {
116  $this->setTitle($this->txt($this->getFormPrefix() . 'edit_' . $this->getFormName()));
117  }
118  }
119 
120 
121  protected function generateFormFields()
122  {
123  foreach ($this->fields->getFields() as $field) {
127  if ($field->getVisible()) {
128  $this->addFormField($field);
129  }
130  }
131  }
132 
133 
137  protected function addFormField(arEditField $field)
138  {
139  $field_element = null;
140  if (!$field->getFormElement()) {
141  switch ($field->getFieldType()) {
142  case 'integer':
143  case 'float':
144  $field->setFormElement($this->addNumbericInputField($field));
145  break;
146  break;
147  case 'date':
148  case 'time':
149  case 'timestamp':
150  $field->setFormElement($this->addDateTimeInputField($field));
151  break;
152  case 'clob':
153  $field->setFormElement($this->addClobInputField($field));
154  break;
155  default:
156  $field->setFormElement($this->addTextInputField($field));
157  }
158  if ($field->getNotNull()) {
159  $field->getFormElement()->setRequired(true);
160  }
161  }
162 
163  if ($field->getFormElement()) {
164  if ($field->getSubelementOf()) {
165  $field->getSubelementOf()->addSubItem($field->getFormElement());
166  } else {
167  $this->addItem($field->getFormElement());
168  }
169  }
170  }
171 
172 
178  protected function addBooleanInputField(arEditField $field)
179  {
180  return new ilCheckboxInputGUI($this->txt($field->getTxt()), $field->getName());
181  }
182 
183 
189  protected function addTextInputField(arEditField $field)
190  {
191  return new ilTextInputGUI($this->txt($field->getTxt()), $field->getName());
192  }
193 
194 
200  protected function addNumbericInputField(arEditField $field)
201  {
202  return new ilNumberInputGUI($this->txt($field->getTxt()), $field->getName());
203  }
204 
205 
211  protected function addDateTimeInputField(arEditField $field)
212  {
213  $date_input = new ilDateTimeInputGUI($this->txt($field->getTxt()), $field->getName());
214  $date_input->setDate(new ilDate(date('Y-m-d H:i:s'), IL_CAL_DATE));
215  $date_input->setShowTime(true);
216 
217  return $date_input;
218  }
219 
220 
226  protected function addClobInputField(arEditField $field)
227  {
228  return new ilTextAreaInputGUI($this->txt($field->getTxt()), $field->getName());
229  }
230 
231 
232  protected function initCommandButtons()
233  {
234  if ($this->ar->getPrimaryFieldValue() == 0) {
235  $this->addCommandButton('create', $this->txt('create', false));
236  } else {
237  $this->addCommandButton('update', $this->txt('save', false));
238  }
239  $this->addCommandButton('index', $this->txt('cancel', false));
240  }
241 
242 
243  protected function afterInitForm()
244  {
245  }
246 
247 
253  public function fillForm()
254  {
255  $this->beforeFillForm();
256  foreach ($this->fields->getFields() as $field) {
260  if ($field->getVisible()) {
261  if ($field->getFormElement()) {
262  $this->fillFormField($field);
263  }
264  }
265  }
266  $this->afterFillForm();
267  }
268 
269 
270  protected function beforeFillForm()
271  {
272  }
273 
274 
275  protected function afterFillForm()
276  {
277  }
278 
279 
283  protected function fillFormField(arEditField $field)
284  {
285  $get_function = $field->getGetFunctionName();
286  switch (get_class($field->getFormElement())) {
287  case 'ilCheckboxInputGUI':
288  $field->getFormElement()->setValue(1);
289  $field->getFormElement()->setChecked($this->ar->$get_function() == 1 ? true : false);
290  break;
291  case 'ilNumberInputGUI':
292  case 'ilSelectInputGUI':
293  case 'ilTextInputGUI':
294  case 'ilTextAreaInputGUI':
295  case 'ilRadioGroupInputGUI':
296  $field->getFormElement()->setValue($this->ar->$get_function());
297  break;
298  case 'ilDateTimeInputGUI':
299  case 'ilDate':
303  $datetime = new ilDateTime($this->ar->$get_function(), IL_CAL_DATETIME);
304  $form_item->setDate($datetime);
305  break;
306  default:
307  $this->fillCustomFormField($field);
308  break;
309  }
310  }
311 
312 
316  protected function fillCustomFormField(arEditField $field)
317  {
318  }
319 
320 
321 
330  public function saveObject()
331  {
332  if (!$this->beforeSave()) {
333  return false;
334  }
335  global $DIC;
336  $ilUser = $DIC['ilUser'];
340  if (!$this->setArFieldsAfterSubmit()) {
341  return false;
342  }
343  $modified_by_field = $this->getFields()->getModifiedByField();
344  if ($modified_by_field) {
345  $set_modified_by_function = $modified_by_field->getSetFunctionName();
346  $this->ar->$set_modified_by_function($ilUser->getId());
347  }
348  $modification_date_field = $this->getFields()->getModificationDateField();
349  if ($modification_date_field) {
350  $set_modification_date_function = $modification_date_field->getSetFunctionName();
351  $datetime = new ilDateTime(time(), IL_CAL_UNIX);
352  $this->ar->$set_modification_date_function($datetime);
353  }
354  if ($this->ar->getPrimaryFieldValue() != 0) {
355  $this->ar->update();
356  } else {
357  $created_by_field = $this->getFields()->getCreatedByField();
358  if ($created_by_field) {
359  $set_created_by_function = $created_by_field->getSetFunctionName();
360  $this->ar->$set_created_by_function($ilUser->getId());
361  }
362  $creation_date_field = $this->getFields()->getCreationDateField();
363  if ($creation_date_field) {
364  $set_creation_date_function = $creation_date_field->getSetFunctionName();
365  $datetime = new ilDateTime(time(), IL_CAL_UNIX);
366  $this->ar->$set_creation_date_function($datetime);
367  }
368  $this->ar->create();
369  }
370 
371  return $this->afterSave();
372  }
373 
374 
375  protected function beforeSave()
376  {
377  return true;
378  }
379 
380 
384  protected function afterSave()
385  {
386  return true;
387  }
388 
389 
393  public function setArFieldsAfterSubmit()
394  {
395  if (!$this->checkInput()) {
396  return false;
397  }
398  if (!$this->afterValidation()) {
399  return false;
400  }
401 
402  foreach ($this->fields->getFields() as $field) {
403  if (!$this->setArFieldAfterSubmit($field)) {
404  return false;
405  }
406  }
407 
408  return true;
409  }
410 
411 
412  protected function afterValidation()
413  {
414  return true;
415  }
416 
417 
423  protected function setArFieldAfterSubmit(arEditField $field)
424  {
428  $valid = false;
429 
430  if ($field->getPrimary()) {
431  $valid = true;
432 
433  return true;
434  }
435  if (array_key_exists($field->getName(), $_POST)) {
436  switch (get_class($field->getFormElement())) {
437  case 'ilNumberInputGUI':
438  case 'ilCheckboxInputGUI':
439  case 'ilSelectInputGUI':
440  case 'ilRadioGroupInputGUI':
441  return $this->setNumericRecordField($field);
442  case 'ilTextInputGUI':
443  case 'ilTextAreaInputGUI':
444  return $this->setTextRecordField($field);
445  case 'ilDateTimeInputGUI':
446  case 'ilDate':
447  return $this->setDateTimeRecordField($field);
448  default:
449  return $this->setCustomRecordField($field);
450  }
451  }
452 
453  return $this->handleEmptyPostValue($field);
454  ;
455  }
456 
457 
463  protected function setNumericRecordField(arEditField $field)
464  {
465  $set_function = $field->getSetFunctionName();
466  $this->ar->$set_function($this->getInput($field->getName()));
467 
468  return true;
469  }
470 
471 
477  protected function setTextRecordField(arEditField $field)
478  {
479  $set_function = $field->getSetFunctionName();
480  $this->ar->$set_function($this->getInput($field->getName()));
481 
482  return true;
483  }
484 
485 
491  protected function setDateTimeRecordField(arEditField $field)
492  {
493  $set_function = $field->getSetFunctionName();
494  $value = $this->getInput($field->getName());
495  if ($value['time']) {
496  $datetime = new ilDateTime($value['date'] . " " . $value['time'], IL_CAL_DATETIME);
497  } else {
498  $datetime = new ilDateTime($value['date'], IL_CAL_DATETIME);
499  }
500  $this->ar->$set_function($datetime);
501 
502  return true;
503  }
504 
505 
511  protected function setCustomRecordField(arEditField $field)
512  {
513  return true;
514  }
515 
516 
522  protected function handleEmptyPostValue(arEditField $field)
523  {
524  return true;
525  }
526 
535  public function setFields(arEditFields $fields)
536  {
537  $this->fields = $fields;
538  }
539 
540 
544  public function getFields()
545  {
546  return $this->fields;
547  }
548 
549 
553  public function getFieldsAsArray()
554  {
555  return $this->getFields()->getFields();
556  }
557 
558 
564  public function getField($field_name)
565  {
566  return $this->getFields()->getField($field_name);
567  }
568 
569 
573  public function addEditField(arEditField $field)
574  {
575  $this->getFields()->addField($field);
576  }
577 
578 
585  protected function txt($txt, $plugin_txt = true)
586  {
587  return $this->parent_gui->txt($txt, $plugin_txt);
588  }
589 
590 
594  public function setFormName($form_name)
595  {
596  $this->form_name = $form_name;
597  }
598 
599 
603  public function getFormName()
604  {
605  return $this->form_name;
606  }
607 
608 
612  public function setFormPrefix($form_prefix)
613  {
614  $this->form_prefix = $form_prefix;
615  }
616 
617 
621  public function getFormPrefix()
622  {
623  return $this->form_prefix;
624  }
625 }
addFormField(arEditField $field)
txt($txt, $plugin_txt=true)
const IL_CAL_DATETIME
setFormPrefix($form_prefix)
This class represents a property form user interface.
Class ActiveRecord.
global $DIC
Definition: saml.php:7
addTextInputField(arEditField $field)
$valid
addBooleanInputField(arEditField $field)
setFormElement($form_element)
This class represents a checkbox property in a property form.
setFormAction($a_formaction)
Set FormAction.
addItem($a_item)
Add Item (Property, SectionHeader).
setCustomRecordField(arEditField $field)
const IL_CAL_UNIX
setFormName($form_name)
setDateTimeRecordField(arEditField $field)
setNumericRecordField(arEditField $field)
This class represents a date/time property in a property form.
GUI-Class arEditFields.
global $ilCtrl
Definition: ilias.php:18
$errors fields
Definition: imgupload.php:51
GUI-Class arEditField.
getField($field_name)
setTitle($a_title)
Set Title.
Class for single dates.
This class represents a number property in a property form.
date( 'd-M-Y', $objPHPExcel->getProperties() ->getCreated())
This class represents a text property in a property form.
setDate(ilDateTime $a_date=null)
set date E.g $dt_form->setDate(new ilDateTime(time(),IL_CAL_UTC)); or $dt_form->setDate(new ilDateTim...
addCommandButton($a_cmd, $a_text, $a_id="")
Add Command button.
checkInput()
Check Post Input.
Date and time handling
$ilUser
Definition: imgupload.php:18
getInput($a_post_var, $ensureValidation=true)
Returns the value of a HTTP-POST variable, identified by the passed id.
addEditField(arEditField $field)
setTextRecordField(arEditField $field)
$txt
Definition: error.php:11
GUI-Class arEditGUI.
const IL_CAL_DATE
__construct(arGUI $parent_gui, ActiveRecord $ar)
setDate($a_date, $a_format)
Set date.
This class represents a text area property in a property form.
addNumbericInputField(arEditField $field)
handleEmptyPostValue(arEditField $field)
addClobInputField(arEditField $field)
fillCustomFormField(arEditField $field)
setFields(arEditFields $fields)
Setters and Getters *********************.
Add data(end) time
Method that wraps PHPs time in order to allow simulations with the workflow.
addDateTimeInputField(arEditField $field)
$_POST["username"]
init()
Form Initialization **********************.