ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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  throw new InvalidArgumentException(
209  sprintf("Please pass a value of type 'boolean' to specify whether the form is not to be validated when it is submitted")
210  );
211  }
212 
213  $this->form_novalidate = $form_novalidate;
214  return $this;
215  }
216 
220  public function getFormTarget()
221  {
222  return $this->form_target;
223  }
224 
234  public function setFormTarget($form_target)
235  {
236  if (!in_array($form_target, self::getValidFormTargets())) {
237  throw new InvalidArgumentException(
238  sprintf(
239  "Invalid form target passed, must be one of these: %s",
240  implode(', ', self::getValidFormTargets())
241  )
242  );
243  }
244 
245  $this->form_target = $form_target;
246  return $this;
247  }
248 
252  public function getFormMethod()
253  {
254  return $this->form_method;
255  }
256 
263  public function setFormMethod($form_method)
264  {
265  if (!in_array($form_method, self::getValidFormMethods())) {
266  throw new InvalidArgumentException(
267  sprintf(
268  "Invalid form method passed, must be one of these: %s",
269  implode(', ', self::getValidFormMethods())
270  )
271  );
272  }
273 
274  $this->form_method = $form_method;
275  return $this;
276  }
277 
281  public function getFormEncType()
282  {
283  return $this->form_enc_type;
284  }
285 
293  {
294  if (!in_array($form_enc_type, self::getValidFormEncTypes())) {
295  throw new InvalidArgumentException(
296  sprintf(
297  "Invalid form enc type passed, must be one of these: %s",
298  implode(', ', self::getValidFormEncTypes())
299  )
300  );
301  }
302 
303  $this->form_enc_type = $form_enc_type;
304  return $this;
305  }
306 
310  public function getFormAction()
311  {
312  return $this->form_action;
313  }
314 
322  public function setFormAction($form_action)
323  {
324  if (!is_string($form_action)) {
325  throw new InvalidArgumentException(
326  sprintf("The form action must be of type 'string'")
327  );
328  }
329 
330  $this->form_action = $form_action;
331  return $this;
332  }
333 
337  public function getForm()
338  {
339  return $this->form;
340  }
341 
352  public function setForm($form)
353  {
354  if (!is_string($form)) {
355  throw new InvalidArgumentException(
356  sprintf("The form id must be of type 'string'")
357  );
358  }
359 
360  $this->form = $form;
361  return $this;
362  }
363 
367  public function getValue()
368  {
369  return $this->value;
370  }
371 
378  public function setValue($value)
379  {
380  if (!is_string($value)) {
381  throw new InvalidArgumentException(
382  sprintf("The initial value of the button must be of type 'string'")
383  );
384  }
385 
386  $this->value = $value;
387  return $this;
388  }
389 
393  public function getName()
394  {
395  return $this->name;
396  }
397 
405  public function setName($name, $is_command = true)
406  {
407  if (!is_string($name)) {
408  throw new InvalidArgumentException(
409  sprintf("The name of the button must be of type 'string'")
410  );
411  }
412 
413  $this->name = $is_command ? 'cmd[' . $name . ']' : $name;
414  return $this;
415  }
416 
420  public function getButtonType()
421  {
422  return $this->button_type;
423  }
424 
430  public function setButtonType($button_type)
431  {
432  if (!in_array($button_type, self::getValidButtonTypes())) {
433  throw new InvalidArgumentException(
434  sprintf(
435  "Invalid button type passed, must be one of these: %s",
436  implode(', ', self::getValidButtonTypes())
437  )
438  );
439  }
440 
441  $this->button_type = $button_type;
442  return $this;
443  }
444 
449  public function render()
450  {
451  $this->prepareRender();
452 
453  $attr = array();
454  $attr['type'] = $this->getButtonType();
455  $attr['name'] = $this->getName();
456  $attr['value'] = $this->getValue();
457  $attr['form'] = $this->getForm();
458  $attr['formaction'] = $this->getFormAction();
459  $attr['formmethod'] = $this->getFormMethod();
460  $attr['formenctype'] = $this->getFormEncType();
461  $attr['formtarget'] = $this->getFormTarget();
462  $attr['formnovalidate'] = $this->isFormNovalidate() ? var_export($this->isFormNovalidate(), 1) : null;
463 
464  if (self::FORM_TARGET_BLANK === $this->getFormTarget()) {
465  $attr['rel'] = 'noopener';
466  }
467 
468  return '<button' . $this->renderAttributes(array_filter($attr)) . '>' . $this->getCaption() . '</button>';
469  }
470 }
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).
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.