1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Nip_Form_Element_CheckboxGroup |
5
|
|
|
* |
6
|
|
|
* @method Nip_Form_Element_Checkbox[] getElements() |
7
|
|
|
*/ |
8
|
|
|
class Nip_Form_Element_CheckboxGroup extends Nip_Form_Element_Input_Group |
|
|
|
|
9
|
|
|
{ |
10
|
|
|
protected $_type = 'checkboxGroup'; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @param string $requester |
14
|
|
|
* @return null |
15
|
|
|
*/ |
16
|
|
|
public function getValue($requester = 'abstract') |
|
|
|
|
17
|
|
|
{ |
18
|
|
|
$elements = $this->getElements(); |
19
|
|
|
$data = []; |
20
|
|
|
if ($elements) { |
|
|
|
|
21
|
|
|
foreach ($elements as $element) { |
22
|
|
|
if ($element->isChecked()) { |
23
|
|
|
$data[] = $element->getValue(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
return $data; |
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
32
|
|
|
* @inheritdoc |
33
|
|
|
*/ |
34
|
|
|
public function setValue($value) |
35
|
|
|
{ |
36
|
|
|
return $this->getDataFromRequest($value); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** @noinspection PhpMissingParentCallCommonInspection |
40
|
|
|
* @inheritdoc |
41
|
|
|
*/ |
42
|
|
|
public function getDataFromRequest($request) |
43
|
|
|
{ |
44
|
|
|
$elements = $this->getElements(); |
45
|
|
|
if (is_array($request)) { |
46
|
|
|
foreach ($elements as $key => $element) { |
47
|
|
|
$element->setChecked(in_array($key, $request)); |
48
|
|
|
} |
49
|
|
|
} else { |
50
|
|
|
foreach ($elements as $key => $element) { |
51
|
|
|
$element->setChecked(false); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @return Nip_Form_Element_Abstract |
60
|
|
|
*/ |
61
|
|
|
public function getNewElement() |
62
|
|
|
{ |
63
|
|
|
$element = $this->getForm()->getNewElement('checkbox'); |
64
|
|
|
$name = $this->getName(); |
|
|
|
|
65
|
|
|
if (!strpos($name, '[]')) { |
66
|
|
|
$name = $name.'[]'; |
67
|
|
|
} |
68
|
|
|
$element->setName($name); |
69
|
|
|
|
70
|
|
|
return $element; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @return bool |
75
|
|
|
*/ |
76
|
|
|
public function isRequestArray() |
77
|
|
|
{ |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.