ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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
4require_once 'Services/UIComponent/Button/classes/class.ilButtonBase.php';
5
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
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 {
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
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
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 {
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
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
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}
renderAttributes(array $a_additional_attr=null)
Render current HTML attributes.
prepareRender()
Prepare render
getCaption($a_translate=true)
Get caption.
static getValidFormTargets()
render()
Render HTML.
static getValidFormEncTypes()
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...
const FORM_TARGET_TOP
setValue($value)
The initial value of the button.
const BUTTON_TYPE_RESET
setName($name, $is_command=true)
The name of the button, which is submitted with the form data.
setFormMethod($form_method)
If the button is a submit button, this attribute specifies the HTTP method that the browser uses to s...
setForm($form)
The form element that the button is associated with (its form owner).
setFormEncType($form_enc_type)
If this attribute is specified, it overrides the enctype attribute of the button's form owner.
const FORM_METHOD_GET
const FORM_TARGET_PARENT
const BUTTON_TYPE_SUBMIT
const FORM_ENC_TYPE_APPLICATION
const FORM_METHOD_POST
setFormNovalidate($form_novalidate)
If the button is a submit button, this Boolean attribute specifies that the form is not to be validat...
setButtonType($button_type)
const FORM_ENC_TYPE_MULTI_PART
const BUTTON_TYPE_BUTTON
const FORM_ENC_TYPE_PLAIN
setFormAction($form_action)
The URI of a program that processes the information submitted by the button.
static getValidFormMethods()
static getValidButtonTypes()
static getInstance()
const FORM_TARGET_BLANK