Array.php
2 years ago
Default.php
2 years ago
Plugin.php
2 years ago
Proxy.php
1 year ago
Smarty.php
2 years ago
Array.php
339 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * A renderer for HTML_QuickForm2 building an array of form elements |
| 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 | * @author Thomas Schulz <ths@4bconsult.de> |
| 44 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
| 45 | * @version SVN: $Id: Array.php 294052 2010-01-26 20:00:22Z avb $ |
| 46 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 47 | */ |
| 48 | /** |
| 49 | * Abstract base class for QuickForm2 renderers |
| 50 | */ |
| 51 | // require_once 'HTML/QuickForm2/Renderer.php'; |
| 52 | /** |
| 53 | * A renderer for HTML_QuickForm2 building an array of form elements |
| 54 | * |
| 55 | * Based on Array renderer from HTML_QuickForm 3.x package |
| 56 | * |
| 57 | * The form array structure is the following: |
| 58 | * <pre> |
| 59 | * array( |
| 60 | * 'id' => form's "id" attribute (string), |
| 61 | * 'frozen' => whether the form is frozen (bool), |
| 62 | * 'attributes' => attributes for <form> tag (string), |
| 63 | * // if form contains required elements: |
| 64 | * 'required_note' => note about the required elements (string), |
| 65 | * // if 'group_hiddens' option is true: |
| 66 | * 'hidden' => array with html of hidden elements (array), |
| 67 | * // if 'group_errors' option is true: |
| 68 | * 'errors' => array( |
| 69 | * '1st element id' => 'Error for the 1st element', |
| 70 | * ... |
| 71 | * 'nth element id' => 'Error for the nth element' |
| 72 | * ), |
| 73 | * 'elements' => array( |
| 74 | * element_1, |
| 75 | * ... |
| 76 | * element_N |
| 77 | * ) |
| 78 | * ); |
| 79 | * </pre> |
| 80 | * Where element_i is an array of the form |
| 81 | * <pre> |
| 82 | * array( |
| 83 | * 'id' => element id (string), |
| 84 | * 'type' => type of the element (string), |
| 85 | * 'frozen' => whether element is frozen (bool), |
| 86 | * // if element has a label: |
| 87 | * 'label' => 'label for the element', |
| 88 | * // note that if 'static_labels' option is true and element's label is an |
| 89 | * // array then there will be several 'label_*' keys corresponding to |
| 90 | * // labels' array keys |
| 91 | * 'required' => whether element is required (bool), |
| 92 | * // if a validation error is present and 'group_errors' option is false: |
| 93 | * 'error' => error associated with the element (string), |
| 94 | * // if some style was associated with an element: |
| 95 | * 'style' => 'some information about element style (e.g. for Smarty)', |
| 96 | * |
| 97 | * // if element is not a Container |
| 98 | * 'value' => element value (mixed), |
| 99 | * 'html' => HTML for the element (string), |
| 100 | * |
| 101 | * // if element is a Container |
| 102 | * 'attributes' => container attributes (string) |
| 103 | * // only for groups, if separator is set: |
| 104 | * 'separator' => separator for group elements (mixed), |
| 105 | * 'elements' => array( |
| 106 | * element_1, |
| 107 | * ... |
| 108 | * element_N |
| 109 | * ) |
| 110 | * ); |
| 111 | * </pre> |
| 112 | * |
| 113 | * While almost everything in this class is defined as public, its properties |
| 114 | * and those methods that are not published (i.e. not in array returned by |
| 115 | * exportMethods()) will be available to renderer plugins only. |
| 116 | * |
| 117 | * The following methods are published: |
| 118 | * - {@link reset()} |
| 119 | * - {@link toArray()} |
| 120 | * - {@link setStyleForId()} |
| 121 | * |
| 122 | * @category HTML |
| 123 | * @package HTML_QuickForm2 |
| 124 | * @author Alexey Borzov <avb@php.net> |
| 125 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 126 | * @author Thomas Schulz <ths@4bconsult.de> |
| 127 | * @version Release: @package_version@ |
| 128 | */ |
| 129 | class HTML_QuickForm2_Renderer_Array extends \HTML_QuickForm2_Renderer |
| 130 | { |
| 131 | /** |
| 132 | * An array being generated |
| 133 | * @var array |
| 134 | */ |
| 135 | public $array = array(); |
| 136 | /** |
| 137 | * Array with references to 'elements' fields of currently processed containers |
| 138 | * @var unknown_type |
| 139 | */ |
| 140 | public $containers = array(); |
| 141 | /** |
| 142 | * Whether the form contains required elements |
| 143 | * @var bool |
| 144 | */ |
| 145 | public $hasRequired = \false; |
| 146 | /** |
| 147 | * Additional style information for elements |
| 148 | * @var array |
| 149 | */ |
| 150 | public $styles = array(); |
| 151 | /** |
| 152 | * Constructor, adds a new 'static_labels' option |
| 153 | */ |
| 154 | protected function __construct() |
| 155 | { |
| 156 | $this->options['static_labels'] = \false; |
| 157 | } |
| 158 | public function exportMethods() |
| 159 | { |
| 160 | return array('reset', 'toArray', 'setStyleForId'); |
| 161 | } |
| 162 | /** |
| 163 | * Resets the accumulated data |
| 164 | * |
| 165 | * This method is called automatically by startForm() method, but should |
| 166 | * be called manually before calling other rendering methods separately. |
| 167 | * |
| 168 | * @return HTML_QuickForm2_Renderer_Array |
| 169 | */ |
| 170 | public function reset() |
| 171 | { |
| 172 | $this->array = array(); |
| 173 | $this->containers = array(); |
| 174 | $this->hasRequired = \false; |
| 175 | return $this; |
| 176 | } |
| 177 | /** |
| 178 | * Returns the resultant array |
| 179 | * |
| 180 | * @return array |
| 181 | */ |
| 182 | public function toArray() |
| 183 | { |
| 184 | return $this->array; |
| 185 | } |
| 186 | /** |
| 187 | * Creates an array with fields that are common to all elements |
| 188 | * |
| 189 | * @param HTML_QuickForm2_Node Element being rendered |
| 190 | * @return array |
| 191 | */ |
| 192 | public function buildCommonFields(\HTML_QuickForm2_Node $element) |
| 193 | { |
| 194 | $ary = array('id' => $element->getId(), 'frozen' => $element->toggleFrozen()); |
| 195 | if ($labels = $element->getLabel()) { |
| 196 | if (!\is_array($labels) || !$this->options['static_labels']) { |
| 197 | $ary['label'] = $labels; |
| 198 | } else { |
| 199 | foreach ($labels as $key => $label) { |
| 200 | $key = \is_int($key) ? $key + 1 : $key; |
| 201 | if (1 === $key) { |
| 202 | $ary['label'] = $label; |
| 203 | } else { |
| 204 | $ary['label_' . $key] = $label; |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | if (($error = $element->getError()) && $this->options['group_errors']) { |
| 210 | $this->array['errors'][$ary['id']] = $error; |
| 211 | } elseif ($error) { |
| 212 | $ary['error'] = $error; |
| 213 | } |
| 214 | if (isset($this->styles[$ary['id']])) { |
| 215 | $ary['style'] = $this->styles[$ary['id']]; |
| 216 | } |
| 217 | if (!$element instanceof \HTML_QuickForm2_Container) { |
| 218 | $ary['html'] = $element->__toString(); |
| 219 | } else { |
| 220 | $ary['elements'] = array(); |
| 221 | $ary['attributes'] = $element->getAttributes(\true); |
| 222 | } |
| 223 | return $ary; |
| 224 | } |
| 225 | /** |
| 226 | * Stores an array representing "scalar" element in the form array |
| 227 | * |
| 228 | * @param array |
| 229 | */ |
| 230 | public function pushScalar(array $element) |
| 231 | { |
| 232 | if (!empty($element['required'])) { |
| 233 | $this->hasRequired = \true; |
| 234 | } |
| 235 | if (empty($this->containers)) { |
| 236 | $this->array += $element; |
| 237 | } else { |
| 238 | $this->containers[\count($this->containers) - 1][] = $element; |
| 239 | } |
| 240 | } |
| 241 | /** |
| 242 | * Stores an array representing a Container in the form array |
| 243 | * |
| 244 | * @param array |
| 245 | */ |
| 246 | public function pushContainer(array $container) |
| 247 | { |
| 248 | if (!empty($container['required'])) { |
| 249 | $this->hasRequired = \true; |
| 250 | } |
| 251 | if (empty($this->containers)) { |
| 252 | $this->array += $container; |
| 253 | $this->containers = array(&$this->array['elements']); |
| 254 | } else { |
| 255 | $cntIndex = \count($this->containers) - 1; |
| 256 | $myIndex = \count($this->containers[$cntIndex]); |
| 257 | $this->containers[$cntIndex][$myIndex] = $container; |
| 258 | $this->containers[$cntIndex + 1] =& $this->containers[$cntIndex][$myIndex]['elements']; |
| 259 | } |
| 260 | } |
| 261 | /** |
| 262 | * Sets a style for element rendering |
| 263 | * |
| 264 | * "Style" is some information that is opaque to Array Renderer but may be |
| 265 | * of use to e.g. template engine that receives the resultant array. |
| 266 | * |
| 267 | * @param string|array Element id or array ('element id' => 'style') |
| 268 | * @param sting Element style if $idOrStyles is not an array |
| 269 | * @return HTML_QuickForm2_Renderer_Array |
| 270 | */ |
| 271 | public function setStyleForId($idOrStyles, $style = null) |
| 272 | { |
| 273 | if (\is_array($idOrStyles)) { |
| 274 | $this->styles = \array_merge($this->styles, $idOrStyles); |
| 275 | } else { |
| 276 | $this->styles[$idOrStyles] = $style; |
| 277 | } |
| 278 | return $this; |
| 279 | } |
| 280 | /**#@+ |
| 281 | * Implementations of abstract methods from {@link HTML_QuickForm2_Renderer} |
| 282 | */ |
| 283 | public function renderElement(\HTML_QuickForm2_Node $element) |
| 284 | { |
| 285 | $ary = $this->buildCommonFields($element) + array('value' => $element->getValue(), 'type' => $element->getType(), 'required' => $element->isRequired()); |
| 286 | $this->pushScalar($ary); |
| 287 | } |
| 288 | public function renderHidden(\HTML_QuickForm2_Node $element) |
| 289 | { |
| 290 | if ($this->options['group_hiddens']) { |
| 291 | $this->array['hidden'][] = $element->__toString(); |
| 292 | } else { |
| 293 | $this->renderElement($element); |
| 294 | } |
| 295 | } |
| 296 | public function startForm(\HTML_QuickForm2_Node $form) |
| 297 | { |
| 298 | $this->reset(); |
| 299 | $this->array = $this->buildCommonFields($form); |
| 300 | if ($this->options['group_errors']) { |
| 301 | $this->array['errors'] = array(); |
| 302 | } |
| 303 | if ($this->options['group_hiddens']) { |
| 304 | $this->array['hidden'] = array(); |
| 305 | } |
| 306 | $this->containers = array(&$this->array['elements']); |
| 307 | } |
| 308 | public function finishForm(\HTML_QuickForm2_Node $form) |
| 309 | { |
| 310 | $this->finishContainer($form); |
| 311 | if ($this->hasRequired) { |
| 312 | $this->array['required_note'] = $this->options['required_note']; |
| 313 | } |
| 314 | } |
| 315 | public function startContainer(\HTML_QuickForm2_Node $container) |
| 316 | { |
| 317 | $ary = $this->buildCommonFields($container) + array('required' => $container->isRequired(), 'type' => $container->getType()); |
| 318 | $this->pushContainer($ary); |
| 319 | } |
| 320 | public function finishContainer(\HTML_QuickForm2_Node $container) |
| 321 | { |
| 322 | \array_pop($this->containers); |
| 323 | } |
| 324 | public function startGroup(\HTML_QuickForm2_Node $group) |
| 325 | { |
| 326 | $ary = $this->buildCommonFields($group) + array('required' => $group->isRequired(), 'type' => $group->getType()); |
| 327 | if ($separator = $group->getSeparator()) { |
| 328 | $ary['separator'] = $separator; |
| 329 | } |
| 330 | $this->pushContainer($ary); |
| 331 | } |
| 332 | public function finishGroup(\HTML_QuickForm2_Node $group) |
| 333 | { |
| 334 | $this->finishContainer($group); |
| 335 | } |
| 336 | /**#@-*/ |
| 337 | } |
| 338 | } |
| 339 |