1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
You may not change or alter any portion of this comment or credits |
4
|
|
|
of supporting developers from this source code or any supporting source code |
5
|
|
|
which is considered copyrighted (c) material of the original comment or credit authors. |
6
|
|
|
|
7
|
|
|
This program is distributed in the hope that it will be useful, |
8
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
9
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Xoops\Form; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* ElementTray - a group of form elements |
16
|
|
|
* |
17
|
|
|
* @category Xoops\Form\ElementTray |
18
|
|
|
* @package Xoops\Form |
19
|
|
|
* @author Kazumi Ono <[email protected]> |
20
|
|
|
* @copyright 2001-2016 XOOPS Project (http://xoops.org) |
21
|
|
|
* @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html) |
22
|
|
|
* @link http://xoops.org |
23
|
|
|
*/ |
24
|
|
|
class ElementTray extends Element implements ContainerInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* array of form element objects |
28
|
|
|
* |
29
|
|
|
* @var Element[] |
30
|
|
|
*/ |
31
|
|
|
protected $elements = array(); |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* __construct |
35
|
|
|
* |
36
|
|
|
* @param string|array $caption caption or array of all attributes |
37
|
|
|
* Control attributes: |
38
|
|
|
* :joiner joiner for elements in tray |
39
|
|
|
* @param string $joiner joiner for elements in tray |
40
|
|
|
* @param string $name name |
41
|
|
|
*/ |
42
|
15 |
|
public function __construct($caption, $joiner = ' ', $name = '') |
43
|
|
|
{ |
44
|
15 |
|
if (is_array($caption)) { |
45
|
4 |
|
parent::__construct($caption); |
46
|
4 |
|
$this->setIfNotSet(':joiner', ' '); |
47
|
|
|
} else { |
48
|
13 |
|
parent::__construct(); |
49
|
13 |
|
$this->setName($name); |
50
|
13 |
|
$this->setCaption($caption); |
51
|
13 |
|
$this->set(':joiner', $joiner); |
52
|
|
|
} |
53
|
15 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Are there are required elements? |
57
|
|
|
* |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
1 |
|
public function isRequired() |
61
|
|
|
{ |
62
|
1 |
|
foreach ($this->elements as $el) { |
63
|
1 |
|
if ($el->isRequired()) { |
64
|
1 |
|
return true; |
65
|
|
|
} |
66
|
|
|
} |
67
|
1 |
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add an element to the tray |
72
|
|
|
* |
73
|
|
|
* @param Element $formElement Element to add |
74
|
|
|
* @param boolean $required true = entry required |
75
|
|
|
* |
76
|
|
|
* @return void |
77
|
|
|
*/ |
78
|
11 |
|
public function addElement(Element $formElement, $required = false) |
79
|
|
|
{ |
80
|
11 |
|
$this->elements[] = $formElement; |
81
|
11 |
|
if ($required) { |
82
|
1 |
|
$formElement->setRequired(); |
83
|
|
|
} |
84
|
11 |
|
} |
85
|
|
|
|
86
|
|
|
// ContainerInterface |
87
|
|
|
/** |
88
|
|
|
* get an array of "required" form elements |
89
|
|
|
* |
90
|
|
|
* @return array array of Element objects |
91
|
|
|
*/ |
92
|
1 |
|
public function getRequired() |
93
|
|
|
{ |
94
|
1 |
|
$required = []; |
95
|
1 |
|
foreach ($this->elements as $el) { |
96
|
1 |
|
if ($el->isRequired()) { |
97
|
1 |
|
$required[] = $el; |
98
|
|
|
} |
99
|
|
|
} |
100
|
1 |
|
return $required; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Get an array of the elements in this group |
105
|
|
|
* |
106
|
|
|
* @param bool $recurse get elements recursively? |
107
|
|
|
* |
108
|
|
|
* @return array Array of Element objects. |
109
|
|
|
*/ |
110
|
9 |
|
public function getElements($recurse = false) |
111
|
|
|
{ |
112
|
9 |
|
if (!$recurse) { |
113
|
9 |
|
return $this->elements; |
114
|
|
|
} else { |
115
|
1 |
|
$ret = array(); |
116
|
1 |
|
foreach ($this->elements as $ele) { |
117
|
1 |
|
if ($ele instanceof ContainerInterface) { |
118
|
|
|
/* @var ContainerInterface $ele */ |
119
|
|
|
$elements = $ele->getElements(true); |
120
|
|
|
foreach ($elements as $ele2) { |
121
|
|
|
$ret[] = $ele2; |
122
|
|
|
} |
123
|
|
|
unset($elements); |
124
|
|
|
unset($ele2); |
125
|
|
|
} else { |
126
|
1 |
|
$ret[] = $ele; |
127
|
|
|
} |
128
|
1 |
|
unset($ele); |
129
|
|
|
} |
130
|
1 |
|
return $ret; |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Get the delimiter of this group |
136
|
|
|
* |
137
|
|
|
* @param boolean $encode True to encode special characters |
138
|
|
|
* |
139
|
|
|
* @return string The delimiter |
140
|
|
|
*/ |
141
|
4 |
|
protected function getJoiner($encode = false) |
142
|
|
|
{ |
143
|
4 |
|
$joiner = $this->get(':joiner'); |
144
|
4 |
|
return $encode ? htmlspecialchars(str_replace(' ', ' ', $joiner)) : $joiner; |
|
|
|
|
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* defaultRender |
149
|
|
|
* |
150
|
|
|
* @return string rendered form element |
151
|
|
|
*/ |
152
|
7 |
|
public function defaultRender() |
153
|
|
|
{ |
154
|
7 |
|
$count = 0; |
155
|
7 |
|
$ret = "<div class=\"form-inline\">"; |
156
|
7 |
|
foreach ($this->getElements() as $ele) { |
157
|
|
|
/* @var Element $ele */ |
158
|
6 |
|
if ($count > 0) { |
159
|
3 |
|
$ret .= $this->getJoiner(); |
160
|
|
|
} |
161
|
6 |
|
if ($ele->getCaption() != '') { |
162
|
1 |
|
$ret .= '<div class="form-group">'; |
163
|
1 |
|
$ret .= '<label class="control-label">' . $ele->getCaption() . "</label> "; |
164
|
1 |
|
$ret .= '</div>'; |
165
|
|
|
} |
166
|
6 |
|
$ret .= $ele->render() . "\n"; |
167
|
6 |
|
if (!$ele->isHidden()) { |
168
|
6 |
|
++$count; |
169
|
|
|
} |
170
|
|
|
} |
171
|
7 |
|
$ret .= '</div>'; |
172
|
7 |
|
return $ret; |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
|
If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.