ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 $tpl;
60 
61  $js_path = (isset($js_base_path))? $js_base_path : self::$js_path;
62 
63  $tpl->addJavaScript($js_path.'ilAsyncPropertyFormGUI.js');
64 
65  $global_config = "$.ilAsyncPropertyForm.global_config.error_message_template = '".self::getErrorMessageTemplate()."'; $.ilAsyncPropertyForm.global_config.async_form_name = '".self::$default_from_name."';";
66  self::addOnLoadCode('global_config', $global_config);
67 
68  if($add_form_loader) {
69  self::addOnLoadCode('form_loader', '$("body").ilAsyncPropertyForm();');
70  }
71  }
72 
73 
79  public function checkInput() {
80  $result = parent::checkInput();
81  $this->has_errors = $result;
82 
83  return $result;
84  }
85 
91  public function getErrors() {
92  if(!$this->check_input_called) {
93  $this->checkInput();
94  }
95 
96  $errors = array();
97  foreach($this->getItems() as $item) {
98  // We call method exists as there are items in the form (ilFormSectionHeaderGUI)
99  // that do not have alerts. (#16956)
100  if(method_exists($item, "getAlert") && $item->getAlert() != "") {
101  $errors[] = array('key'=>$item->getFieldId(), 'message'=>$item->getAlert());
102  }
103  }
104  return $errors;
105  }
106 
112  public function hasErrors() {
113  return $this->has_errors;
114  }
115 
116 
122  public function getErrorMessageTemplate() {
123  global $lng;
124 
125  $tpl = new ilTemplate("tpl.property_form.html", true, true, "Services/Form");
126 
127  $tpl->setCurrentBlock("alert");
128  $tpl->setVariable("IMG_ALERT", ilUtil::getImagePath("icon_alert.svg"));
129  $tpl->setVariable("ALT_ALERT", $lng->txt("alert"));
130  $tpl->setVariable("TXT_ALERT", "[TXT_ALERT]");
131  $tpl->parseCurrentBlock();
132  $content = trim($tpl->get("alert"));
133 
134  return $content;
135  }
136 
145  public function cloneForm(ilPropertyFormGUI $form_to_clone) {
146  if(count($this->getItems()) > 0) {
147  throw new ilException("You cannot clone into a already filled form!");
148  }
149 
150  $reflect = new ReflectionClass($this);
151  $properties = $reflect->getProperties(ReflectionProperty::IS_PUBLIC);
152 
153  foreach($properties as $property) {
154  $this->{$property->getName()} = $property->getValue($form_to_clone);
155  }
156 
157  foreach($form_to_clone->getItems() as $item) {
158  $this->addItem($item);
159  }
160 
161  foreach($form_to_clone->getCommandButtons() as $button) {
162  $this->addCommandButton($button['cmd'], $button['text']);
163  }
164 
165  return $this;
166  }
167 
168 
175  protected function addOnLoadCode($id, $content) {
176  global $tpl;
177 
178  if(!isset(self::$js_on_load_added[$id])) {
179  $tpl->addOnLoadCode($content);
180  self::$js_on_load_added[$id] = $content;
181  }
182  }
183 
189  public function getHTML() {
190  self::addJavaScript($this->isAsync());
191 
192  return parent::getHTML();
193  }
194 
200  public function isSubmitted() {
201  if(isset($_POST['cmd'])) {
202  return true;
203  }
204  return false;
205  }
206 
207 
214  public function setFormAction($a_formaction) {
215  if($this->isAsync()) {
216  $a_formaction .= "&cmdMode=asynch";
217  }
218 
219  $this->formaction = $a_formaction;
220  }
221 
225  public function getJsPath() {
226  return $this->js_path;
227  }
228 
232  public function setJsPath($js_path) {
233  $this->js_path = $js_path;
234  }
235 
239  public function getDefaultFormName() {
240  return self::$default_from_name;
241  }
242 
246  public function isAsync() {
247  return $this->is_async;
248  }
249 
250 
254  public function setAsync($is_async) {
255  $this->is_async = $is_async;
256  }
257 
258 
262  public function setName($a_name) {
263  self::$default_from_name = $a_name;
264 
265  parent::setName($a_name);
266  }
267 }
addOnLoadCode($id, $content)
Adds onload code to the template.
Base class for ILIAS Exception handling.
$_POST['username']
Definition: cron.php:12
$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).
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
getErrorMessageTemplate()
Returns the error-message template for the client-side validation.
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.
__construct(array $config=array(), $is_async=true)
getCommandButtons()
Return all Command buttons.
getErrors()
Return errors of the form as array.
addCommandButton($a_cmd, $a_text)
Add Command button.
$errors
global $lng
Definition: privfeed.php:40
Class ilAsyncPropertyFormGUI.
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.