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