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