ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilAsyncPropertyFormGUI.php
Go to the documentation of this file.
1 <?php
2 require_once("./Services/Form/classes/class.ilPropertyFormGUI.php");
3 
14  protected static $js_path = "./Modules/StudyProgramme/templates/js/";
15 
19  protected static $default_from_name = "async_form";
20 
24  protected static $js_on_load_added = array();
25 
29  protected $has_errors = false;
30 
34  protected $is_async = true;
35 
36  public function __construct(array $config = array(), $is_async = true) {
37  parent::__construct();
38 
39  foreach($config as $key=>$value) {
40  $setterMethod = "set".ucfirst($key);
41  if(method_exists($this, $setterMethod)) {
42  $setterMethod($value);
43  }
44  }
45 
46  $this->setAsync($is_async);
47  $this->setName(self::$default_from_name);
48  }
49 
50 
58  public static function addJavaScript($add_form_loader = false, $js_base_path = null) {
59  global $DIC;
60  $tpl = $DIC['tpl'];
61 
62  $js_path = (isset($js_base_path))? $js_base_path : self::$js_path;
63 
64  $tpl->addJavaScript($js_path.'ilAsyncPropertyFormGUI.js');
65 
66  $global_config = "$.ilAsyncPropertyForm.global_config.error_message_template = '".self::getErrorMessageTemplate()."'; $.ilAsyncPropertyForm.global_config.async_form_name = '".self::$default_from_name."';";
67  self::addOnLoadCode('global_config', $global_config);
68 
69  if($add_form_loader) {
70  self::addOnLoadCode('form_loader', '$("body").ilAsyncPropertyForm();');
71  }
72  }
73 
74 
80  public function checkInput() {
81  $result = parent::checkInput();
82  $this->has_errors = $result;
83 
84  return $result;
85  }
86 
92  public function getErrors() {
93  if(!$this->check_input_called) {
94  $this->checkInput();
95  }
96 
97  $errors = array();
98  foreach($this->getItems() as $item) {
99  // We call method exists as there are items in the form (ilFormSectionHeaderGUI)
100  // that do not have alerts. (#16956)
101  if(method_exists($item, "getAlert") && $item->getAlert() != "") {
102  $errors[] = array('key'=>$item->getFieldId(), 'message'=>$item->getAlert());
103  }
104  }
105  return $errors;
106  }
107 
113  public function hasErrors() {
114  return $this->has_errors;
115  }
116 
117 
123  public static function getErrorMessageTemplate() {
124  global $DIC;
125  $lng = $DIC['lng'];
126 
127  $tpl = new ilTemplate("tpl.property_form.html", true, true, "Services/Form");
128 
129  $tpl->setCurrentBlock("alert");
130  $tpl->setVariable("IMG_ALERT", ilUtil::getImagePath("icon_alert.svg"));
131  $tpl->setVariable("ALT_ALERT", $lng->txt("alert"));
132  $tpl->setVariable("TXT_ALERT", "[TXT_ALERT]");
133  $tpl->parseCurrentBlock();
134  $content = trim($tpl->get("alert"));
135 
136  return $content;
137  }
138 
147  public function cloneForm(ilPropertyFormGUI $form_to_clone) {
148  if(count($this->getItems()) > 0) {
149  throw new ilException("You cannot clone into a already filled form!");
150  }
151 
152  $reflect = new ReflectionClass($this);
153  $properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
154 
155  foreach($properties as $property) {
156  $this->{$property->getName()} = $property->getValue($form_to_clone);
157  }
158 
159  foreach($form_to_clone->getItems() as $item) {
160  $this->addItem($item);
161  }
162 
163  foreach($form_to_clone->getCommandButtons() as $button) {
164  $this->addCommandButton($button['cmd'], $button['text']);
165  }
166 
167  return $this;
168  }
169 
170 
177  protected static function addOnLoadCode($id, $content) {
178  global $DIC;
179  $tpl = $DIC['tpl'];
180 
181  if(!isset(self::$js_on_load_added[$id])) {
182  $tpl->addOnLoadCode($content);
183  self::$js_on_load_added[$id] = $content;
184  }
185  }
186 
192  public function getHTML() {
193  self::addJavaScript($this->isAsync());
194 
195  return parent::getHTML();
196  }
197 
203  public function isSubmitted() {
204  if(isset($_POST['cmd'])) {
205  return true;
206  }
207  return false;
208  }
209 
210 
217  public function setFormAction($a_formaction) {
218  if($this->isAsync()) {
219  $a_formaction .= "&cmdMode=asynch";
220  }
221 
222  $this->formaction = $a_formaction;
223  }
224 
228  public function getJsPath() {
229  return $this->js_path;
230  }
231 
235  public function setJsPath($js_path) {
236  $this->js_path = $js_path;
237  }
238 
242  public function getDefaultFormName() {
243  return self::$default_from_name;
244  }
245 
249  public function isAsync() {
250  return $this->is_async;
251  }
252 
253 
257  public function setAsync($is_async) {
258  $this->is_async = $is_async;
259  }
260 
261 
265  public function setName($a_name) {
266  self::$default_from_name = $a_name;
267 
268  parent::setName($a_name);
269  }
270 }
Base class for ILIAS Exception handling.
$result
This class represents a property form user interface.
getHTML()
Returns the rendered form content.
checkInput()
Saves the change input result into a property.
addItem($a_item)
Add Item (Property, SectionHeader).
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($add_form_loader=false, $js_base_path=null)
Adds all needed js By default is called by ilAsyncPropertyFormGUI::getHTML()
global $tpl
Definition: ilias.php:8
static getImagePath($img, $module_path="", $mode="output", $offline=false)
get image path (for images located in a template directory)
special template class to simplify handling of ITX/PEAR
isSubmitted()
Checks if the form was submitted.
addCommandButton($a_cmd, $a_text, $a_id="")
Add Command button.
__construct(array $config=array(), $is_async=true)
getCommandButtons()
Return all Command buttons.
getErrors()
Return errors of the form as array.
Create styles array
The data for the language used.
$errors
global $lng
Definition: privfeed.php:17
Class ilAsyncPropertyFormGUI.
global $DIC
setFormAction($a_formaction)
Sets the form action If the form is set to async, the cmdMode=asynch is added to the url...
hasErrors()
Return if there were errors on the last checkInput call.
$_POST["username"]
static addOnLoadCode($id, $content)
Adds onload code to the template.