ILIAS  Release_5_0_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
class.arEditGUI.php
Go to the documentation of this file.
1 <?php
2 require_once('./Services/Form/classes/class.ilPropertyFormGUI.php');
3 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/Views/Edit/class.arEditField.php');
4 require_once('./Customizing/global/plugins/Libraries/ActiveRecord/Views/Edit/class.arEditFields.php');
5 
14 
18  protected $ar;
22  protected $parent_gui;
26  protected $ctrl;
30  protected $form_name = "";
34  protected $form_prefix = "";
38  protected $fields;
39 
40 
46  global $ilCtrl;
47 
48  $this->ar = $ar;
49  $this->parent_gui = $parent_gui;
50  $this->ctrl = $ilCtrl;
51  $this->ctrl->saveParameter($parent_gui, 'ar_id');
52  $this->setFormName(get_class($ar));
53  $this->init();
54  }
55 
56 
62  protected function init() {
63  $this->initFields();
64  $this->initForm();
65  if ($this->ar->getPrimaryFieldValue() != 0) {
66  $this->fillForm();
67  }
68  }
69 
70 
71  protected function initFields() {
72  $this->fields = new arEditFields($this->ar);
73  $this->customizeFields();
74  $this->fields->sortFields();
75  }
76 
77 
78  protected function customizeFields() {
79  }
80 
81 
82  protected function initForm() {
83  $this->BeforeInitForm();
84  $this->initFormAction();
85  $this->initFormTitle();
86  $this->generateFormFields();
87  $this->initCommandButtons();
88  $this->afterInitForm();
89  }
90 
91 
92  protected function beforeInitForm() {
93  }
94 
95 
96  protected function initFormAction() {
97  $this->setFormAction($this->ctrl->getFormAction($this->parent_gui, "index"));
98  }
99 
100 
101  protected function initFormTitle() {
102  $this->setFormPrefix("");
103  if ($this->ar->getPrimaryFieldValue() == 0) {
104  $this->setTitle($this->txt($this->getFormPrefix() . 'create_' . $this->getFormName()));
105  } else {
106  $this->setTitle($this->txt($this->getFormPrefix() . 'edit_' . $this->getFormName()));
107  }
108  }
109 
110 
111  protected function generateFormFields() {
112 
113  foreach ($this->fields->getFields() as $field) {
117  if ($field->getVisible()) {
118  $this->addFormField($field);
119  }
120  }
121  }
122 
123 
127  protected function addFormField(arEditField $field) {
128  $field_element = NULL;
129  if (!$field->getFormElement()) {
130  switch ($field->getFieldType()) {
131  case 'integer':
132  case 'float':
133  $field->setFormElement($this->addNumbericInputField($field));
134  break;
135  break;
136  case 'date':
137  case 'time':
138  case 'timestamp':
139  $field->setFormElement($this->addDateTimeInputField($field));
140  break;
141  case 'clob':
142  $field->setFormElement($this->addClobInputField($field));
143  break;
144  default:
145  $field->setFormElement($this->addTextInputField($field));
146  }
147  if ($field->getNotNull()) {
148  $field->getFormElement()->setRequired(true);
149  }
150  }
151 
152  if ($field->getFormElement()) {
153  if ($field->getSubelementOf()) {
154  $field->getSubelementOf()->addSubItem($field->getFormElement());
155  } else {
156  $this->addItem($field->getFormElement());
157  }
158  }
159  }
160 
161 
167  protected function addBooleanInputField(arEditField $field) {
168  return new ilCheckboxInputGUI($this->txt($field->getTxt()), $field->getName());
169  }
170 
171 
177  protected function addTextInputField(arEditField $field) {
178  return new ilTextInputGUI($this->txt($field->getTxt()), $field->getName());
179  }
180 
181 
187  protected function addNumbericInputField(arEditField $field) {
188  return new ilNumberInputGUI($this->txt($field->getTxt()), $field->getName());
189  }
190 
191 
197  protected function addDateTimeInputField(arEditField $field) {
198  $date_input = new ilDateTimeInputGUI($this->txt($field->getTxt()), $field->getName());
199  $date_input->setDate(new ilDate(date('Y-m-d H:i:s'), IL_CAL_DATE));
200  $date_input->setShowTime(true);
201 
202  return $date_input;
203  }
204 
205 
211  protected function addClobInputField(arEditField $field) {
212  return new ilTextAreaInputGUI($this->txt($field->getTxt()), $field->getName());
213  }
214 
215 
216  protected function initCommandButtons() {
217  if ($this->ar->getPrimaryFieldValue() == 0) {
218  $this->addCommandButton('create', $this->txt('create', false));
219  } else {
220  $this->addCommandButton('update', $this->txt('save', false));
221  }
222  $this->addCommandButton('index', $this->txt('cancel', false));
223  }
224 
225 
226  protected function afterInitForm() {
227  }
228 
229 
235  public function fillForm() {
236  $this->beforeFillForm();
237  foreach ($this->fields->getFields() as $field) {
241  if ($field->getVisible()) {
242  if ($field->getFormElement()) {
243  $this->fillFormField($field);
244  }
245  }
246  }
247  $this->afterFillForm();
248  }
249 
250 
251  protected function beforeFillForm() {
252  }
253 
254 
255  protected function afterFillForm() {
256  }
257 
258 
262  protected function fillFormField(arEditField $field) {
263  $get_function = $field->getGetFunctionName();
264  switch (get_class($field->getFormElement())) {
265  case 'ilCheckboxInputGUI':
266  $field->getFormElement()->setValue(1);
267  $field->getFormElement()->setChecked($this->ar->$get_function() == 1 ? true : false);
268  break;
269  case 'ilNumberInputGUI':
270  case 'ilSelectInputGUI':
271  case 'ilTextInputGUI':
272  case 'ilTextAreaInputGUI':
273  case 'ilRadioGroupInputGUI':
274  $field->getFormElement()->setValue($this->ar->$get_function());
275  break;
276  case 'ilDateTimeInputGUI':
277  case 'ilDate':
281  $datetime = new ilDateTime($this->ar->$get_function(), IL_CAL_DATETIME);
282  $form_item->setDate($datetime);
283  break;
284  default:
285  $this->fillCustomFormField($field);
286  break;
287  }
288  }
289 
290 
294  protected function fillCustomFormField(arEditField $field) {
295  }
296 
297 
298 
307  public function saveObject() {
308  if (!$this->beforeSave()) {
309  return false;
310  }
311  global $ilUser;
315  if (!$this->setArFieldsAfterSubmit()) {
316  return false;
317  }
318  $modified_by_field = $this->getFields()->getModifiedByField();
319  if ($modified_by_field) {
320  $set_modified_by_function = $modified_by_field->getSetFunctionName();
321  $this->ar->$set_modified_by_function($ilUser->getId());
322  }
323  $modification_date_field = $this->getFields()->getModificationDateField();
324  if ($modification_date_field) {
325  $set_modification_date_function = $modification_date_field->getSetFunctionName();
326  $datetime = new ilDateTime(time(), IL_CAL_UNIX);
327  $this->ar->$set_modification_date_function($datetime);
328  }
329  if ($this->ar->getPrimaryFieldValue() != 0) {
330  $this->ar->update();
331  } else {
332  $created_by_field = $this->getFields()->getCreatedByField();
333  if ($created_by_field) {
334  $set_created_by_function = $created_by_field->getSetFunctionName();
335  $this->ar->$set_created_by_function($ilUser->getId());
336  }
337  $creation_date_field = $this->getFields()->getCreationDateField();
338  if ($creation_date_field) {
339  $set_creation_date_function = $creation_date_field->getSetFunctionName();
340  $datetime = new ilDateTime(time(), IL_CAL_UNIX);
341  $this->ar->$set_creation_date_function($datetime);
342  }
343  $this->ar->create();
344  }
345 
346  return $this->afterSave();
347  }
348 
349 
350  protected function beforeSave() {
351  return true;
352  }
353 
354 
358  protected function afterSave() {
359  return true;
360  }
361 
362 
366  public function setArFieldsAfterSubmit() {
367  if (!$this->checkInput()) {
368  return false;
369  }
370  if (!$this->afterValidation()) {
371  return false;
372  }
373 
374  foreach ($this->fields->getFields() as $field) {
375  if (!$this->setArFieldAfterSubmit($field)) {
376  return false;
377  }
378  }
379 
380  return true;
381  }
382 
383 
384  protected function afterValidation() {
385  return true;
386  }
387 
388 
394  protected function setArFieldAfterSubmit(arEditField $field) {
398  $valid = false;
399 
400  if ($field->getPrimary()) {
401  $valid = true;
402 
403  return true;
404  }
405  if (array_key_exists($field->getName(), $_POST)) {
406  switch (get_class($field->getFormElement())) {
407  case 'ilNumberInputGUI':
408  case 'ilCheckboxInputGUI':
409  case 'ilSelectInputGUI':
410  case 'ilRadioGroupInputGUI':
411  return $this->setNumericRecordField($field);
412  case 'ilTextInputGUI':
413  case 'ilTextAreaInputGUI':
414  return $this->setTextRecordField($field);
415  case 'ilDateTimeInputGUI':
416  case 'ilDate':
417  return $this->setDateTimeRecordField($field);
418  default:
419  return $this->setCustomRecordField($field);
420  }
421  }
422 
423  return $this->handleEmptyPostValue($field);;
424  }
425 
426 
432  protected function setNumericRecordField(arEditField $field) {
433  $set_function = $field->getSetFunctionName();
434  $this->ar->$set_function($this->getInput($field->getName()));
435 
436  return true;
437  }
438 
439 
445  protected function setTextRecordField(arEditField $field) {
446  $set_function = $field->getSetFunctionName();
447  $this->ar->$set_function($this->getInput($field->getName()));
448 
449  return true;
450  }
451 
452 
458  protected function setDateTimeRecordField(arEditField $field) {
459  $set_function = $field->getSetFunctionName();
460  $value = $this->getInput($field->getName());
461  if ($value['time']) {
462  $datetime = new ilDateTime($value['date'] . " " . $value['time'], IL_CAL_DATETIME);
463  } else {
464  $datetime = new ilDateTime($value['date'], IL_CAL_DATETIME);
465  }
466  $this->ar->$set_function($datetime);
467 
468  return true;
469  }
470 
471 
477  protected function setCustomRecordField(arEditField $field) {
478  return true;
479  }
480 
481 
487  protected function handleEmptyPostValue(arEditField $field) {
488  return true;
489  }
490 
500  $this->fields = $fields;
501  }
502 
503 
507  public function getFields() {
508  return $this->fields;
509  }
510 
511 
515  public function getFieldsAsArray() {
516  return $this->getFields()->getFields();
517  }
518 
519 
525  public function getField($field_name) {
526  return $this->getFields()->getField($field_name);
527  }
528 
529 
533  public function addEditField(arEditField $field) {
534  $this->getFields()->addField($field);
535  }
536 
537 
544  protected function txt($txt, $plugin_txt = true) {
545  return $this->parent_gui->txt($txt, $plugin_txt);
546  }
547 
548 
552  public function setFormName($form_name) {
553  $this->form_name = $form_name;
554  }
555 
556 
560  public function getFormName() {
561  return $this->form_name;
562  }
563 
564 
568  public function setFormPrefix($form_prefix) {
569  $this->form_prefix = $form_prefix;
570  }
571 
572 
576  public function getFormPrefix() {
577  return $this->form_prefix;
578  }
579 }