ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
class.ilButton.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3 
4 require_once 'Services/UIComponent/Button/classes/class.ilButtonBase.php';
5 
11 class ilButton extends ilButtonBase
12 {
18  const BUTTON_TYPE_BUTTON = 'button';
19 
25  const BUTTON_TYPE_SUBMIT = 'submit';
26 
31  const BUTTON_TYPE_RESET = 'reset';
32 
37  const FORM_ENC_TYPE_APPLICATION = 'application/x-www-form-urlencoded';
38 
43  const FORM_ENC_TYPE_MULTI_PART = 'multipart/form-data';
44 
48  const FORM_ENC_TYPE_PLAIN = 'text/plain';
49 
53  const FORM_METHOD_POST = 'POST';
54 
58  const FORM_METHOD_GET = 'GET';
59 
65  const FORM_TARGET_SELF = '_self';
66 
71  const FORM_TARGET_BLANK = '_blank';
72 
78  const FORM_TARGET_PARENT = '_parent';
79 
86  const FORM_TARGET_TOP = '_top';
87 
92  protected $button_type = self::BUTTON_TYPE_SUBMIT;
93 
97  protected $name = null;
98 
102  protected $value = null;
103 
107  protected $form = null;
108 
112  protected $form_action = null;
113 
117  protected $form_enc_type = null;
118 
122  protected $form_method = null;
123 
127  protected $form_target = null;
128 
132  protected $form_novalidate = null;
133 
137  public static function getInstance()
138  {
139  return new self(self::TYPE_BUTTON);
140  }
141 
145  public static function getValidFormTargets()
146  {
147  return array(
148  self::FORM_TARGET_BLANK,
149  self::FORM_TARGET_PARENT,
150  self::FORM_TARGET_SELF,
151  self::FORM_TARGET_TOP
152  );
153  }
154 
158  public static function getValidFormMethods()
159  {
160  return array(
161  self::FORM_METHOD_POST,
162  self::FORM_METHOD_GET
163  );
164  }
165 
169  public static function getValidFormEncTypes()
170  {
171  return array(
172  self::FORM_ENC_TYPE_APPLICATION,
173  self::FORM_ENC_TYPE_MULTI_PART,
174  self::FORM_ENC_TYPE_PLAIN
175  );
176  }
177 
181  public static function getValidButtonTypes()
182  {
183  return array(
184  self::BUTTON_TYPE_SUBMIT,
185  self::BUTTON_TYPE_BUTTON,
186  self::BUTTON_TYPE_RESET
187  );
188  }
189 
193  public function isFormNovalidate()
194  {
195  return $this->form_novalidate;
196  }
197 
206  {
207  if(!is_bool($form_novalidate))
208  {
209  throw new InvalidArgumentException(
210  sprintf("Please pass a value of type 'boolean' to specify whether the form is not to be validated when it is submitted")
211  );
212  }
213 
214  $this->form_novalidate = $form_novalidate;
215  return $this;
216  }
217 
221  public function getFormTarget()
222  {
223  return $this->form_target;
224  }
225 
235  public function setFormTarget($form_target)
236  {
237  if(!in_array($form_target, self::getValidFormTargets()))
238  {
239  throw new InvalidArgumentException(
240  sprintf(
241  "Invalid form target passed, must be one of these: %s",
242  implode(', ', self::getValidFormTargets())
243  )
244  );
245  }
246 
247  $this->form_target = $form_target;
248  return $this;
249  }
250 
254  public function getFormMethod()
255  {
256  return $this->form_method;
257  }
258 
265  public function setFormMethod($form_method)
266  {
267  if(!in_array($form_method, self::getValidFormMethods()))
268  {
269  throw new InvalidArgumentException(
270  sprintf(
271  "Invalid form method passed, must be one of these: %s",
272  implode(', ', self::getValidFormMethods())
273  )
274  );
275  }
276 
277  $this->form_method = $form_method;
278  return $this;
279  }
280 
284  public function getFormEncType()
285  {
286  return $this->form_enc_type;
287  }
288 
296  {
297  if(!in_array($form_enc_type, self::getValidFormEncTypes()))
298  {
299  throw new InvalidArgumentException(
300  sprintf(
301  "Invalid form enc type passed, must be one of these: %s",
302  implode(', ', self::getValidFormEncTypes())
303  )
304  );
305  }
306 
307  $this->form_enc_type = $form_enc_type;
308  return $this;
309  }
310 
314  public function getFormAction()
315  {
316  return $this->form_action;
317  }
318 
326  public function setFormAction($form_action)
327  {
328  if(!is_string($form_action))
329  {
330  throw new InvalidArgumentException(
331  sprintf("The form action must be of type 'string'")
332  );
333  }
334 
335  $this->form_action = $form_action;
336  return $this;
337  }
338 
342  public function getForm()
343  {
344  return $this->form;
345  }
346 
357  public function setForm($form)
358  {
359  if(!is_string($form))
360  {
361  throw new InvalidArgumentException(
362  sprintf("The form id must be of type 'string'")
363  );
364  }
365 
366  $this->form = $form;
367  return $this;
368  }
369 
373  public function getValue()
374  {
375  return $this->value;
376  }
377 
384  public function setValue($value)
385  {
386  if(!is_string($value))
387  {
388  throw new InvalidArgumentException(
389  sprintf("The initial value of the button must be of type 'string'")
390  );
391  }
392 
393  $this->value = $value;
394  return $this;
395  }
396 
400  public function getName()
401  {
402  return $this->name;
403  }
404 
412  public function setName($name, $is_command = true)
413  {
414  if(!is_string($name))
415  {
416  throw new InvalidArgumentException(
417  sprintf("The name of the button must be of type 'string'")
418  );
419  }
420 
421  $this->name = $is_command ? 'cmd[' . $name . ']' : $name;
422  return $this;
423  }
424 
428  public function getButtonType()
429  {
430  return $this->button_type;
431  }
432 
438  public function setButtonType($button_type)
439  {
440  if(!in_array($button_type, self::getValidButtonTypes()))
441  {
442  throw new InvalidArgumentException(
443  sprintf(
444  "Invalid button type passed, must be one of these: %s",
445  implode(', ', self::getValidButtonTypes())
446  )
447  );
448  }
449 
450  $this->button_type = $button_type;
451  return $this;
452  }
453 
458  public function render()
459  {
460  $this->prepareRender();
461 
462  $attr = array();
463  $attr['type'] = $this->getButtonType();
464  $attr['name'] = $this->getName();
465  $attr['value'] = $this->getValue();
466  $attr['form'] = $this->getForm();
467  $attr['formaction'] = $this->getFormAction();
468  $attr['formmethod'] = $this->getFormMethod();
469  $attr['formenctype'] = $this->getFormEncType();
470  $attr['formtarget'] = $this->getFormTarget();
471  $attr['formnovalidate'] = $this->isFormNovalidate() ? var_export($this->isFormNovalidate(), 1) : null;
472 
473  if (self::FORM_TARGET_BLANK === $this->getFormTarget()) {
474  $attr['rel'] = 'noopener';
475  }
476 
477  return '<button' . $this->renderAttributes(array_filter($attr)) . '>' . $this->getCaption() . '</button>';
478  }
479 }
setFormAction($form_action)
The URI of a program that processes the information submitted by the button.
static getValidFormMethods()
setName($name, $is_command=true)
The name of the button, which is submitted with the form data.
static getInstance()
const FORM_ENC_TYPE_MULTI_PART
const FORM_METHOD_GET
const FORM_TARGET_TOP
const FORM_ENC_TYPE_PLAIN
const FORM_TARGET_BLANK
const BUTTON_TYPE_RESET
setFormNovalidate($form_novalidate)
If the button is a submit button, this Boolean attribute specifies that the form is not to be validat...
getCaption($a_translate=true)
Get caption.
prepareRender()
Prepare render.
const BUTTON_TYPE_BUTTON
setButtonType($button_type)
const FORM_TARGET_PARENT
setFormEncType($form_enc_type)
If this attribute is specified, it overrides the enctype attribute of the button&#39;s form owner...
setForm($form)
The form element that the button is associated with (its form owner).
Create styles array
The data for the language used.
const FORM_ENC_TYPE_APPLICATION
const FORM_METHOD_POST
static getValidButtonTypes()
static getValidFormEncTypes()
renderAttributes(array $a_additional_attr=null)
Render current HTML attributes.
setValue($value)
The initial value of the button.
static getValidFormTargets()
setFormMethod($form_method)
If the button is a submit button, this attribute specifies the HTTP method that the browser uses to s...
const BUTTON_TYPE_SUBMIT
const FORM_TARGET_SELF
setFormTarget($form_target)
If the button is a submit button, this attribute is a name or keyword indicating where to display the...
render()
Render HTML.