ILIAS  release_8 Revision v8.24
class.ilAsyncPropertyFormGUI.php
Go to the documentation of this file.
1<?php
2
3declare(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 = '" .
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 {
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 {
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}
setVariable($variable, $value='')
Sets a variable value.
Definition: IT.php:514
Class ilAsyncPropertyFormGUI.
static addOnLoadCode(string $id, string $content)
Adds onload code to the template.
isSubmitted()
Checks if the form was submitted.
checkInput()
Saves the change input result into a property.
setFormAction(string $a_formaction)
Sets the form action If the form is set to async, the cmdMode=asynch is added to the url.
cloneForm(ilPropertyFormGUI $form_to_clone)
Copies form items, buttons and properties from another form.
hasErrors()
Return if there were errors on the last checkInput call.
__construct(RequestWrapper $request_wrapper, array $config=array(), bool $is_async=true)
getHTML()
Returns the rendered form content.
static getErrorMessageTemplate()
Returns the error-message template for the client-side validation.
getErrors()
Return errors of the form as array.
static addJavaScript(bool $add_form_loader=false, string $js_base_path=null)
Adds all needed js By default is called by ilAsyncPropertyFormGUI::getHTML()
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
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...
This class represents a property form user interface.
addCommandButton(string $a_cmd, string $a_text, string $a_id="")
special template class to simplify handling of ITX/PEAR
setCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
get(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
Returns a block with all replacements done.
parseCurrentBlock(string $part=ilGlobalTemplateInterface::DEFAULT_BLOCK)
static getImagePath(string $img, string $module_path="", string $mode="output", bool $offline=false)
get image path (for images located in a template directory)
global $DIC
Definition: feed.php:28
$errors
Definition: imgupload.php:65
Interface RequestWrapper.
if(!array_key_exists('PATH_INFO', $_SERVER)) $config
Definition: metadata.php:85
__construct(Container $dic, ilPlugin $plugin)
@inheritDoc
string $key
Consumer key/client ID value.
Definition: System.php:193