Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php
/**
* Base class for HTML_QuickForm2 groups
*
* PHP version 5
*
* LICENSE:
*
* Copyright (c) 2006-2010, Alexey Borzov <avb@php.net>,
* Bertrand Mansion <golgote@mamasam.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* * The names of the authors may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @category HTML
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version SVN: $Id: Group.php 294057 2010-01-26 21:10:28Z avb $
* @link http://pear.php.net/package/HTML_QuickForm2
*/
/**
* Base class for all HTML_QuickForm2 containers
*/
// require_once 'HTML/QuickForm2/Container.php';
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
* Base class for QuickForm2 groups of elements
*
* @category HTML
* @package HTML_QuickForm2
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <golgote@mamasam.com>
* @version Release: @package_version@
*/
class HTML_QuickForm2_Container_Group extends HTML_QuickForm2_Container
{
/**
* Group name
* If set, group name will be used as prefix for contained
* element names, like groupname[elementname].
* @var string
*/
protected $name;
/**
* Previous group name
* Stores the previous group name when the group name is changed.
* Used to restore children names if necessary.
* @var string
*/
protected $previousName;
public function getType()
{
return 'group';
}
protected function prependsName()
{
return strlen($this->name) > 0;
}
public function getValue()
{
$value = parent::getValue();
if (!$this->prependsName()) {
return $value;
} elseif (!strpos($this->getName(), '[')) {
return isset($value[$this->getName()])? $value[$this->getName()]: null;
} else {
$tokens = explode('[', str_replace(']', '', $this->getName()));
$valueAry =& $value;
do {
$token = array_shift($tokens);
if (!isset($valueAry[$token])) {
return null;
}
$valueAry =& $valueAry[$token];
} while ($tokens);
return $valueAry;
}
}
public function setValue($value)
{
// Prepare a mapper for element names as array
if ($this->prependsName()) {
$prefix = explode('[', str_replace(']', '', $this->getName()));
}
$elements = array();
foreach ($this as $child) {
$tokens = explode('[', str_replace(']', '', $child->getName()));
if (!empty($prefix)) {
$tokens = array_slice($tokens, count($prefix));
}
$elements[] = $tokens;
}
// Iterate over values to find corresponding element
$index = 0;
foreach ($value as $k => $v) {
$val = array($k => $v);
$found = null;
foreach ($elements as $i => $tokens) {
do {
$token = array_shift($tokens);
$numeric = false;
if ($token == "") {
// Deal with numeric indexes in values
$token = $index;
$numeric = true;
}
if (isset($val[$token])) {
// Found a value
$val = $val[$token];
$found = $val;
if ($numeric) {
$index += 1;
}
} else {
// Not found, skip next iterations
$found = null;
break;
}
} while (!empty($tokens));
if (!is_null($found)) {
// Found a value corresponding to element name
$child = $this->elements[$i];
$child->setValue($val);
unset($val);
if (!($child instanceof HTML_QuickForm2_Container_Group)) {
// Speed up next iterations
unset($elements[$i]);
}
break;
}
}
}
}
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->previousName = $this->name;
$this->name = $name;
foreach ($this as $child) {
$this->renameChild($child);
}
return $this;
}
protected function renameChild(HTML_QuickForm2_Node $element)
{
$tokens = explode('[', str_replace(']', '', $element->getName()));
if ($this === $element->getContainer()) {
// Child has already been renamed by its group before
if (!is_null($this->previousName) &&
$this->previousName !== '') {
$gtokens = explode('[', str_replace(']', '', $this->previousName));
$pos = array_search(end($gtokens), $tokens);
if (!is_null($pos)) {
$tokens = array_slice($tokens, $pos+1);
}
}
}
if (is_null($this->name) || $this->name === '') {
if (is_null($this->previousName) || $this->previousName === '') {
return $element;
} else {
$elname = $tokens[0];
unset($tokens[0]);
foreach ($tokens as $v) {
$elname .= '['.$v.']';
}
}
} else {
$elname = $this->getName().'['.implode('][', $tokens).']';
}
$element->setName($elname);
return $element;
}
/**
* Appends an element to the container
*
* If the element was previously added to the container or to another
* container, it is first removed there.
*
* @param HTML_QuickForm2_Node Element to add
* @return HTML_QuickForm2_Node Added element
* @throws HTML_QuickForm2_InvalidArgumentException
*/
public function appendChild(HTML_QuickForm2_Node $element)
{
if (null !== ($container = $element->getContainer())) {
$container->removeChild($element);
}
// Element can be renamed only after being removed from container
$this->renameChild($element);
$element->setContainer($this);
$this->elements[] = $element;
return $element;
}
/**
* Removes the element from this container
*
* If the reference object is not given, the element will be appended.
*
* @param HTML_QuickForm2_Node Element to remove
* @return HTML_QuickForm2_Node Removed object
*/
public function removeChild(HTML_QuickForm2_Node $element)
{
$element = parent::removeChild($element);
if ($this->prependsName()) {
$name = preg_replace('/^' . $this->getName() . '\[([^\]]*)\]/', '\1', $element->getName());
$element->setName($name);
}
return $element;
}
/**
* Inserts an element in the container
*
* If the reference object is not given, the element will be appended.
*
* @param HTML_QuickForm2_Node Element to insert
* @param HTML_QuickForm2_Node Reference to insert before
* @return HTML_QuickForm2_Node Inserted element
*/
public function insertBefore(HTML_QuickForm2_Node $element, HTML_QuickForm2_Node $reference = null)
{
if (null === $reference) {
return $this->appendChild($element);
}
return parent::insertBefore($this->renameChild($element), $reference);
}
/**
* Sets string(s) to separate grouped elements
*
* @param string|array Use a string for one separator, array for
* alternating separators
* @return HTML_QuickForm2_Container_Group
*/
public function setSeparator($separator)
{
$this->data['separator'] = $separator;
return $this;
}
/**
* Returns string(s) to separate grouped elements
*
* @return string|array Separator, null if not set
*/
public function getSeparator()
{
return isset($this->data['separator'])? $this->data['separator']: null;
}
/**
* Renders the group using the given renderer
*
* @param HTML_QuickForm2_Renderer Renderer instance
* @return HTML_QuickForm2_Renderer
*/
public function render(HTML_QuickForm2_Renderer $renderer)
{
$renderer->startGroup($this);
foreach ($this as $element) {
$element->render($renderer);
}
$renderer->finishGroup($this);
return $renderer;
}
public function __toString()
{
// require_once 'HTML/QuickForm2/Renderer.php';
return $this->render(
HTML_QuickForm2_Renderer::factory('default')
->setTemplateForId($this->getId(), '{content}')
)->__toString();
}
}