ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilADTBasedObjectGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 /* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
5 
12 abstract class ilADTBasedObjectGUI
13 {
14  private ilObjectGUI $gui;
15  protected ?ilADTBasedObject $object = null;
16 
18  protected ilLanguage $lng;
19  protected ilCtrl $ctrl;
20 
26  public function __construct(ilObjectGUI $a_parent_gui)
27  {
28  global $DIC;
29  $this->tpl = $DIC->ui()->mainTemplate();
30  $this->lng = $DIC->language();
31  $this->ctrl = $DIC->ctrl();
32 
33  $this->gui = $a_parent_gui;
34  $this->object = $this->initObject();
35  }
36 
40  abstract protected function initObject(): ilADTBasedObject;
41 
42 
43  //
44  // VERY BASIC EXAMPLE OF FORM HANDLING
45  //
46 
47  public function editAction(ilADTGroupFormBridge $a_form = null): bool
48  {
49  if (!$a_form) {
50  $a_form = $this->initForm();
51  }
52 
53  $this->tpl->setContent($a_form->getForm()->getHTML());
54  return true;
55  }
56 
61  abstract protected function prepareFormElements(ilADTGroupFormBridge $a_adt_form): void;
62 
67  protected function initForm(): ilADTFormBridge
68  {
69  $form = new ilPropertyFormGUI();
70  $form->setFormAction($this->ctrl->getFormAction($this->gui, "updateAction"));
71 
72  $adt_form = ilADTFactory::getInstance()->getFormBridgeForInstance($this->object->getProperties());
73 
74  // has to be done BEFORE prepareFormElements() ...
75  $adt_form->setForm($form);
76 
78  $this->prepareFormElements($adt_form);
79 
80  $adt_form->addToForm();
81  $adt_form->addJS($this->tpl);
82 
83  // :TODO:
84  $form->addCommandButton("updateAction", $this->lng->txt("save"));
85 
86  return $adt_form;
87  }
88 
93  public function updateAction(): bool
94  {
95  $adt_form = $this->initForm();
96  $valid = $adt_form->getForm()->checkInput();
97 
98  $old_chksum = $this->object->getProperties()->getCheckSum();
99 
100  $adt_form->importFromPost();
101  $valid = $adt_form->validate();
102 
103  $changed = ($old_chksum != $this->object->getProperties()->getCheckSum());
104 
105  // validation errors have top priority
106  if (!$valid) {
107  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("form_input_not_valid"));
108  return $this->editAction($adt_form);
109  }
110 
111  // :TODO: experimental, update only if necessary
112  if ($changed) {
113  if ($this->object->update()) {
114  $this->tpl->setOnScreenMessage('success', $this->lng->txt("settings_saved"), true);
115  } else {
116  // error occured in db-layer (primary/unique)
117  foreach ($this->object->getDBErrors() as $element_id => $codes) {
118  $element = $adt_form->getElement($element_id);
119  if ($element) {
120  $element->setExternalErrors($this->object->translateDBErrorCodes($codes));
121  }
122  }
123 
124  $this->tpl->setOnScreenMessage('failure', $this->lng->txt("form_input_not_valid"));
125  return $this->editAction($adt_form);
126  }
127  }
128 
129  $this->ctrl->redirect($this->gui, "edit");
130  return true;
131  }
132 }
ADT form bridge base class.
$valid
global $DIC
Definition: feed.php:28
prepareFormElements(ilADTGroupFormBridge $a_adt_form)
Prepare/customize form elements.
updateAction()
Parse incoming values and update if valid PhpParamsInspection.
Class ilObjectGUI Basic methods of all Output classes.
__construct(ilObjectGUI $a_parent_gui)
Constructor Parent GUI is just needed for testing (ilCtrl)
initObject()
Init ADT-based object.
editAction(ilADTGroupFormBridge $a_form=null)
initForm()
Init ADT-based form.
ADT based-object base class Currently "mixed" with ActiveRecord-pattern, could be splitted...
ilGlobalTemplateInterface $tpl
ADT based-object GUI base class.