ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
class.ilButton.php
Go to the documentation of this file.
1 <?php
2 
24 class ilButton extends ilButtonBase
25 {
30  public const BUTTON_TYPE_BUTTON = 'button';
31 
36  public const BUTTON_TYPE_SUBMIT = 'submit';
37 
41  public const BUTTON_TYPE_RESET = 'reset';
42 
46  public const FORM_ENC_TYPE_APPLICATION = 'application/x-www-form-urlencoded';
47 
51  public const FORM_ENC_TYPE_MULTI_PART = 'multipart/form-data';
52 
53  public const FORM_ENC_TYPE_PLAIN = 'text/plain';
54  public const FORM_METHOD_POST = 'POST';
55  public const FORM_METHOD_GET = 'GET';
56 
61  public const FORM_TARGET_SELF = '_self';
62 
66  public const FORM_TARGET_BLANK = '_blank';
67 
72  public const FORM_TARGET_PARENT = '_parent';
73 
79  public const FORM_TARGET_TOP = '_top';
80 
81  protected string $button_type = self::BUTTON_TYPE_SUBMIT;
82  protected ?string $name = null;
83  protected ?string $value = null;
84  protected ?string $form = null;
85  protected ?string $form_action = null;
86  protected ?string $form_enc_type = null;
87  protected ?string $form_method = null;
88  protected ?string $form_target = null;
89  protected bool $form_novalidate = false;
90 
91  public static function getInstance(): self
92  {
93  return new self(self::TYPE_BUTTON);
94  }
95 
96  public static function getValidFormTargets(): array
97  {
98  return array(
99  self::FORM_TARGET_BLANK,
100  self::FORM_TARGET_PARENT,
101  self::FORM_TARGET_SELF,
102  self::FORM_TARGET_TOP
103  );
104  }
105 
106  public static function getValidFormMethods(): array
107  {
108  return array(
109  self::FORM_METHOD_POST,
110  self::FORM_METHOD_GET
111  );
112  }
113 
114  public static function getValidFormEncTypes(): array
115  {
116  return array(
117  self::FORM_ENC_TYPE_APPLICATION,
118  self::FORM_ENC_TYPE_MULTI_PART,
119  self::FORM_ENC_TYPE_PLAIN
120  );
121  }
122 
123  public static function getValidButtonTypes(): array
124  {
125  return array(
126  self::BUTTON_TYPE_SUBMIT,
127  self::BUTTON_TYPE_BUTTON,
128  self::BUTTON_TYPE_RESET
129  );
130  }
131 
132  public function isFormNovalidate(): ?bool
133  {
134  return $this->form_novalidate;
135  }
136 
142  public function setFormNovalidate(bool $form_novalidate): self
143  {
144  if (!is_bool($form_novalidate)) {
145  throw new InvalidArgumentException(
146  "Please pass a value of type 'boolean' to specify whether the form is not to be validated when it is submitted"
147  );
148  }
149 
150  $this->form_novalidate = $form_novalidate;
151  return $this;
152  }
153 
154  public function getFormTarget(): ?string
155  {
156  return $this->form_target;
157  }
158 
166  public function setFormTarget(string $form_target): self
167  {
168  if (!in_array($form_target, self::getValidFormTargets())) {
169  throw new InvalidArgumentException(
170  sprintf(
171  "Invalid form target passed, must be one of these: %s",
172  implode(', ', self::getValidFormTargets())
173  )
174  );
175  }
176 
177  $this->form_target = $form_target;
178  return $this;
179  }
180 
181  public function getFormMethod(): ?string
182  {
183  return $this->form_method;
184  }
185 
190  public function setFormMethod(string $form_method): self
191  {
192  if (!in_array($form_method, self::getValidFormMethods())) {
193  throw new InvalidArgumentException(
194  sprintf(
195  "Invalid form method passed, must be one of these: %s",
196  implode(', ', self::getValidFormMethods())
197  )
198  );
199  }
200 
201  $this->form_method = $form_method;
202  return $this;
203  }
204 
205  public function getFormEncType(): ?string
206  {
207  return $this->form_enc_type;
208  }
209 
214  public function setFormEncType(string $form_enc_type): self
215  {
216  if (!in_array($form_enc_type, self::getValidFormEncTypes())) {
217  throw new InvalidArgumentException(
218  sprintf(
219  "Invalid form enc type passed, must be one of these: %s",
220  implode(', ', self::getValidFormEncTypes())
221  )
222  );
223  }
224 
225  $this->form_enc_type = $form_enc_type;
226  return $this;
227  }
228 
229  public function getFormAction(): ?string
230  {
231  return $this->form_action;
232  }
233 
239  public function setFormAction(string $form_action): self
240  {
241  if (!is_string($form_action)) {
242  throw new InvalidArgumentException(
243  "The form action must be of type 'string'"
244  );
245  }
246 
247  $this->form_action = $form_action;
248  return $this;
249  }
250 
251  public function getForm(): string
252  {
253  return $this->form;
254  }
255 
264  public function setForm(string $form): self
265  {
266  if (!is_string($form)) {
267  throw new InvalidArgumentException(
268  "The form id must be of type 'string'"
269  );
270  }
271 
272  $this->form = $form;
273  return $this;
274  }
275 
276  public function getValue(): ?string
277  {
278  return $this->value;
279  }
280 
285  public function setValue(string $value): self
286  {
287  if (!is_string($value)) {
288  throw new InvalidArgumentException(
289  "The initial value of the button must be of type 'string'"
290  );
291  }
292 
293  $this->value = $value;
294  return $this;
295  }
296 
297  public function getName(): ?string
298  {
299  return $this->name;
300  }
301 
307  public function setName(string $name, bool $is_command = true): self
308  {
309  if (!is_string($name)) {
310  throw new InvalidArgumentException(
311  "The name of the button must be of type 'string'"
312  );
313  }
314 
315  $this->name = $is_command ? 'cmd[' . $name . ']' : $name;
316  return $this;
317  }
318 
319  public function getButtonType(): string
320  {
321  return $this->button_type;
322  }
323 
327  public function setButtonType(string $button_type): self
328  {
329  if (!in_array($button_type, self::getValidButtonTypes())) {
330  throw new InvalidArgumentException(
331  sprintf(
332  "Invalid button type passed, must be one of these: %s",
333  implode(', ', self::getValidButtonTypes())
334  )
335  );
336  }
337 
338  $this->button_type = $button_type;
339  return $this;
340  }
341 
342  public function render(): string
343  {
344  $this->prepareRender();
345 
346  $attr = [];
347  $attr['type'] = $this->getButtonType() ?? '';
348  $attr['name'] = $this->getName() ?? '';
349  $attr['value'] = $this->getValue() ?? '';
350  $attr['form'] = $this->getForm() ?? '';
351  $attr['formaction'] = $this->getFormAction() ?? '';
352  $attr['formmethod'] = $this->getFormMethod() ?? '';
353  $attr['formenctype'] = $this->getFormEncType() ?? '';
354  $attr['formtarget'] = $this->getFormTarget() ?? '';
355  $attr['formnovalidate'] = $this->isFormNovalidate() ? var_export($this->isFormNovalidate(), 1) : null;
356 
357  if (self::FORM_TARGET_BLANK === $this->getFormTarget()) {
358  $attr['rel'] = 'noopener';
359  }
360 
361  return '<button' . $this->renderAttributes(array_filter($attr)) . '>' . $this->getCaption() . '</button>';
362  }
363 }
setButtonType(string $button_type)
string $form_target
setFormEncType(string $form_enc_type)
If this attribute is specified, it overrides the enctype attribute of the button&#39;s form owner...
string $form_enc_type
static getValidFormMethods()
setValue(string $value)
The initial value of the button.
static getInstance()
setForm(string $form)
The form element that the button is associated with (its form owner).
const FORM_ENC_TYPE_MULTI_PART
Use this value if you are using an <input> element with the type attribute set to file...
const FORM_METHOD_GET
const FORM_TARGET_TOP
Load the response into the top-level browsing context (that is, the browsing context that is an ances...
setFormMethod(string $form_method)
If the button is a submit button, this attribute specifies the HTTP method that the browser uses to s...
const FORM_ENC_TYPE_PLAIN
const FORM_TARGET_BLANK
Load the response into a new unnamed browsing context.
const BUTTON_TYPE_RESET
The button resets all the controls to their initial values.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
bool $form_novalidate
setFormTarget(string $form_target)
If the button is a submit button, this attribute is a name or keyword indicating where to display the...
string $form
const BUTTON_TYPE_BUTTON
The button has no default behavior.
string $value
const FORM_TARGET_PARENT
Load the response into the parent browsing context of the current one.
string $form_action
string $button_type
setFormAction(string $form_action)
The URI of a program that processes the information submitted by the button.
form( $class_path, string $cmd)
setFormNovalidate(bool $form_novalidate)
If the button is a submit button, this Boolean attribute specifies that the form is not to be validat...
const FORM_ENC_TYPE_APPLICATION
The default value if the attribute is not specified.
const FORM_METHOD_POST
static getValidButtonTypes()
static getValidFormEncTypes()
renderAttributes(array $a_additional_attr=null)
Render current HTML attributes.
string $form_method
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
static getValidFormTargets()
getCaption(bool $a_translate=true)
const BUTTON_TYPE_SUBMIT
The button submits the form data to the server.
setName(string $name, bool $is_command=true)
The name of the button, which is submitted with the form data.
const FORM_TARGET_SELF
Load the response into the same browsing context as the current one.
string $name