1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Anax\HTMLForm; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Form element |
7
|
|
|
*/ |
8
|
|
|
class FormElementCheckboxMultiple extends FormElement |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Constructor |
13
|
|
|
* |
14
|
|
|
* @param string $name of the element. |
15
|
|
|
* @param array $attributes to set to the element. Default is an empty array. |
16
|
|
|
*/ |
17
|
4 |
|
public function __construct($name, $attributes = []) |
18
|
|
|
{ |
19
|
4 |
|
parent::__construct($name, $attributes); |
20
|
4 |
|
$this['type'] = 'checkbox-multiple'; |
21
|
|
|
|
22
|
4 |
|
if (!isset($this['values'])) { |
23
|
1 |
|
throw new Exception("Missing values for the checkbox."); |
24
|
|
|
} |
25
|
3 |
|
} |
26
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the value of the form element. |
31
|
|
|
* |
32
|
|
|
* @return array the checked values of the form element. |
33
|
|
|
*/ |
34
|
|
|
public function value() |
35
|
|
|
{ |
36
|
|
|
return $this['checked']; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get HTML code for a element. |
43
|
|
|
* |
44
|
|
|
* @return string HTML code for the element. |
45
|
|
|
* |
46
|
|
|
* @SuppressWarnings(PHPMD.UnusedLocalVariable) |
47
|
|
|
*/ |
48
|
2 |
|
public function getHTML() |
49
|
|
|
{ |
50
|
2 |
|
$details = $this->getHTMLDetails(); |
51
|
2 |
|
extract($details); |
|
|
|
|
52
|
|
|
|
53
|
2 |
|
$type = "type='checkbox'"; |
54
|
2 |
|
$name = " name='{$this['name']}[]'"; |
55
|
2 |
|
$ret = null; |
56
|
2 |
|
$checkedValues = is_array($this['checked']) ? $this['checked'] : []; |
57
|
|
|
|
58
|
2 |
|
foreach ($this['values'] as $val) { |
59
|
1 |
|
$id .= $val; |
60
|
1 |
|
$item = $onlyValue = htmlentities($val, ENT_QUOTES, $this->characterEncoding); |
61
|
1 |
|
$value = " value='{$onlyValue}'"; |
62
|
1 |
|
$checked = in_array($val, $checkedValues) |
|
|
|
|
63
|
|
|
? " checked='checked'" |
64
|
1 |
|
: null; |
65
|
|
|
|
66
|
|
|
$ret .= <<<EOD |
67
|
|
|
<p> |
68
|
1 |
|
<input id='$id'{$type}{$class}{$name}{$value}{$autofocus}{$readonly}{$checked}{$title} /> |
69
|
1 |
|
<label for='$id'>$item</label> |
70
|
1 |
|
{$messages} |
71
|
|
|
</p> |
72
|
|
|
EOD; |
73
|
|
|
} |
74
|
|
|
return <<<EOD |
|
|
|
|
75
|
|
|
<div> |
76
|
2 |
|
<p>{$label}</p> |
77
|
2 |
|
{$ret} |
78
|
2 |
|
<p class='cf-desc'>{$description}</p> |
79
|
|
|
</div> |
80
|
|
|
EOD; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|