ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilAsyncPropertyFormGUI.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
22 
30 {
34  protected static string $js_path = "./Modules/StudyProgramme/templates/js/";
35 
39  protected static string $default_from_name = "async_form";
40 
44  protected static array $js_on_load_added = array();
45 
49  protected bool $has_errors = false;
50 
54  protected bool $is_async = true;
55 
57 
58  public function __construct(RequestWrapper $request_wrapper, array $config = array(), bool $is_async = true)
59  {
61 
62  foreach ($config as $key => $value) {
63  $setterMethod = "set" . ucfirst($key);
64  if (method_exists($this, $setterMethod)) {
65  $setterMethod($value);
66  }
67  }
68 
69  $this->request_wrapper = $request_wrapper;
70  $this->setAsync($is_async);
71  $this->setName(self::$default_from_name);
72  }
73 
74 
79  public static function addJavaScript(bool $add_form_loader = false, string $js_base_path = null): void
80  {
81  global $DIC;
82  $tpl = $DIC['tpl'];
83 
84  $js_path = $js_base_path ?? self::$js_path;
85 
86  $tpl->addJavaScript($js_path . 'ilAsyncPropertyFormGUI.js');
87 
88  $global_config =
89  "$.ilAsyncPropertyForm.global_config.error_message_template = '" .
90  self::getErrorMessageTemplate() .
91  "'; $.ilAsyncPropertyForm.global_config.async_form_name = '" .
92  self::$default_from_name . "';"
93  ;
94 
95  self::addOnLoadCode('global_config', $global_config);
96 
97  if ($add_form_loader) {
98  self::addOnLoadCode('form_loader', '$("body").ilAsyncPropertyForm();');
99  }
100  }
101 
105  public function checkInput(): bool
106  {
107  $result = parent::checkInput();
108  $this->has_errors = $result;
109 
110  return $result;
111  }
112 
118  public function getErrors(): array
119  {
120  if (!$this->check_input_called) {
121  $this->checkInput();
122  }
123 
124  $errors = array();
125  foreach ($this->getItems() as $item) {
126  // We call method exists as there are items in the form (ilFormSectionHeaderGUI)
127  // that do not have alerts. (#16956)
128  if (method_exists($item, "getAlert") && $item->getAlert() !== "") {
129  $errors[] = array('key' => $item->getFieldId(), 'message' => $item->getAlert());
130  }
131  }
132  return $errors;
133  }
134 
138  public function hasErrors(): bool
139  {
140  return $this->has_errors;
141  }
142 
146  public static function getErrorMessageTemplate(): string
147  {
148  global $DIC;
149  $lng = $DIC['lng'];
150 
151  $tpl = new ilTemplate("tpl.property_form.html", true, true, "Services/Form");
152 
153  $tpl->setCurrentBlock("alert");
154  // TODO: DW -> refactor getImagePath
155  $tpl->setVariable("IMG_ALERT", ilUtil::getImagePath("icon_alert.svg"));
156  $tpl->setVariable("ALT_ALERT", $lng->txt("alert"));
157  $tpl->setVariable("TXT_ALERT", "[TXT_ALERT]");
159  return trim($tpl->get("alert"));
160  }
161 
165  public function cloneForm(ilPropertyFormGUI $form_to_clone): ilAsyncPropertyFormGUI
166  {
167  if (count($this->getItems()) > 0) {
168  throw new ilException("You cannot clone into a already filled form!");
169  }
170 
171  $reflect = new ReflectionClass($this);
172  $properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
173 
174  foreach ($properties as $property) {
175  $this->{$property->getName()} = $property->getValue($form_to_clone);
176  }
177 
178  foreach ($form_to_clone->getItems() as $item) {
179  $this->addItem($item);
180  }
181 
182  foreach ($form_to_clone->getCommandButtons() as $button) {
183  $this->addCommandButton($button['cmd'], $button['text']);
184  }
185 
186  return $this;
187  }
188 
192  protected static function addOnLoadCode(string $id, string $content): void
193  {
194  global $DIC;
195  $tpl = $DIC['tpl'];
196 
197  if (!isset(self::$js_on_load_added[$id])) {
198  $tpl->addOnLoadCode($content);
199  self::$js_on_load_added[$id] = $content;
200  }
201  }
202 
206  public function getHTML(): string
207  {
208  self::addJavaScript($this->isAsync());
209 
210  return parent::getHTML();
211  }
212 
216  public function isSubmitted(): bool
217  {
218  if ($this->request_wrapper->has("cmd")) {
219  return true;
220  }
221  return false;
222  }
223 
228  public function setFormAction(string $a_formaction): void
229  {
230  if ($this->isAsync()) {
231  $a_formaction .= "&cmdMode=asynch";
232  }
233 
234  $this->formaction = $a_formaction;
235  }
236 
237  public function getJsPath(): ?string
238  {
239  return self::$js_path;
240  }
241 
242  public function setJsPath(string $js_path): void
243  {
244  self::$js_path = $js_path;
245  }
246 
247  public function getDefaultFormName(): string
248  {
249  return self::$default_from_name;
250  }
251 
252  public function isAsync(): bool
253  {
254  return $this->is_async;
255  }
256 
257  public function setAsync(bool $is_async): void
258  {
259  $this->is_async = $is_async;
260  }
261 
265  public function setName(string $a_name): void
266  {
267  self::$default_from_name = $a_name;
268 
269  parent::setName($a_name);
270  }
271 }
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
get(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
txt(string $a_topic, string $a_default_lang_fallback_mod="")
gets the text for a given topic if the topic is not in the list, the topic itself with "-" will be re...
$errors
Definition: imgupload.php:65
getHTML()
Returns the rendered form content.
checkInput()
Saves the change input result into a property.
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
setFormAction(string $a_formaction)
Sets the form action If the form is set to async, the cmdMode=asynch is added to the url...
static getErrorMessageTemplate()
Returns the error-message template for the client-side validation.
cloneForm(ilPropertyFormGUI $form_to_clone)
Copies form items, buttons and properties from another form.
static addJavaScript(bool $add_form_loader=false, string $js_base_path=null)
Adds all needed js By default is called by ilAsyncPropertyFormGUI::getHTML()
global $DIC
Definition: feed.php:28
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
Interface RequestWrapper.
string $key
Consumer key/client ID value.
Definition: System.php:193
isSubmitted()
Checks if the form was submitted.
getErrors()
Return errors of the form as array.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
__construct(RequestWrapper $request_wrapper, array $config=array(), bool $is_async=true)
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
__construct(Container $dic, ilPlugin $plugin)
Class ilAsyncPropertyFormGUI.
static addOnLoadCode(string $id, string $content)
Adds onload code to the template.
hasErrors()
Return if there were errors on the last checkInput call.