Array.php
6 years ago
Default.php
6 years ago
Plugin.php
6 years ago
Proxy.php
6 years ago
Smarty.php
6 years ago
Array.php
377 lines
| 1 | <?php |
| 2 | /** |
| 3 | * A renderer for HTML_QuickForm2 building an array of form elements |
| 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 | * @author Thomas Schulz <ths@4bconsult.de> |
| 42 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
| 43 | * @version SVN: $Id: Array.php 294052 2010-01-26 20:00:22Z avb $ |
| 44 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 45 | */ |
| 46 | |
| 47 | /** |
| 48 | * Abstract base class for QuickForm2 renderers |
| 49 | */ |
| 50 | // require_once 'HTML/QuickForm2/Renderer.php'; |
| 51 | |
| 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 | /** |
| 138 | * Array with references to 'elements' fields of currently processed containers |
| 139 | * @var unknown_type |
| 140 | */ |
| 141 | public $containers = array(); |
| 142 | |
| 143 | /** |
| 144 | * Whether the form contains required elements |
| 145 | * @var bool |
| 146 | */ |
| 147 | public $hasRequired = false; |
| 148 | |
| 149 | /** |
| 150 | * Additional style information for elements |
| 151 | * @var array |
| 152 | */ |
| 153 | public $styles = array(); |
| 154 | |
| 155 | /** |
| 156 | * Constructor, adds a new 'static_labels' option |
| 157 | */ |
| 158 | protected function __construct() |
| 159 | { |
| 160 | $this->options['static_labels'] = false; |
| 161 | } |
| 162 | |
| 163 | public function exportMethods() |
| 164 | { |
| 165 | return array( |
| 166 | 'reset', |
| 167 | 'toArray', |
| 168 | 'setStyleForId' |
| 169 | ); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Resets the accumulated data |
| 174 | * |
| 175 | * This method is called automatically by startForm() method, but should |
| 176 | * be called manually before calling other rendering methods separately. |
| 177 | * |
| 178 | * @return HTML_QuickForm2_Renderer_Array |
| 179 | */ |
| 180 | public function reset() |
| 181 | { |
| 182 | $this->array = array(); |
| 183 | $this->containers = array(); |
| 184 | $this->hasRequired = false; |
| 185 | |
| 186 | return $this; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Returns the resultant array |
| 191 | * |
| 192 | * @return array |
| 193 | */ |
| 194 | public function toArray() |
| 195 | { |
| 196 | return $this->array; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Creates an array with fields that are common to all elements |
| 201 | * |
| 202 | * @param HTML_QuickForm2_Node Element being rendered |
| 203 | * @return array |
| 204 | */ |
| 205 | public function buildCommonFields(HTML_QuickForm2_Node $element) |
| 206 | { |
| 207 | $ary = array( |
| 208 | 'id' => $element->getId(), |
| 209 | 'frozen' => $element->toggleFrozen() |
| 210 | ); |
| 211 | if ($labels = $element->getLabel()) { |
| 212 | if (!is_array($labels) || !$this->options['static_labels']) { |
| 213 | $ary['label'] = $labels; |
| 214 | } else { |
| 215 | foreach ($labels as $key => $label) { |
| 216 | $key = is_int($key)? $key + 1: $key; |
| 217 | if (1 === $key) { |
| 218 | $ary['label'] = $label; |
| 219 | } else { |
| 220 | $ary['label_' . $key] = $label; |
| 221 | } |
| 222 | } |
| 223 | } |
| 224 | } |
| 225 | if (($error = $element->getError()) && $this->options['group_errors']) { |
| 226 | $this->array['errors'][$ary['id']] = $error; |
| 227 | } elseif ($error) { |
| 228 | $ary['error'] = $error; |
| 229 | } |
| 230 | if (isset($this->styles[$ary['id']])) { |
| 231 | $ary['style'] = $this->styles[$ary['id']]; |
| 232 | } |
| 233 | if (!$element instanceof HTML_QuickForm2_Container) { |
| 234 | $ary['html'] = $element->__toString(); |
| 235 | } else { |
| 236 | $ary['elements'] = array(); |
| 237 | $ary['attributes'] = $element->getAttributes(true); |
| 238 | } |
| 239 | return $ary; |
| 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Stores an array representing "scalar" element in the form array |
| 244 | * |
| 245 | * @param array |
| 246 | */ |
| 247 | public function pushScalar(array $element) |
| 248 | { |
| 249 | if (!empty($element['required'])) { |
| 250 | $this->hasRequired = true; |
| 251 | } |
| 252 | if (empty($this->containers)) { |
| 253 | $this->array += $element; |
| 254 | } else { |
| 255 | $this->containers[count($this->containers) - 1][] = $element; |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | /** |
| 260 | * Stores an array representing a Container in the form array |
| 261 | * |
| 262 | * @param array |
| 263 | */ |
| 264 | public function pushContainer(array $container) |
| 265 | { |
| 266 | if (!empty($container['required'])) { |
| 267 | $this->hasRequired = true; |
| 268 | } |
| 269 | if (empty($this->containers)) { |
| 270 | $this->array += $container; |
| 271 | $this->containers = array(&$this->array['elements']); |
| 272 | } else { |
| 273 | $cntIndex = count($this->containers) - 1; |
| 274 | $myIndex = count($this->containers[$cntIndex]); |
| 275 | $this->containers[$cntIndex][$myIndex] = $container; |
| 276 | $this->containers[$cntIndex + 1] =& $this->containers[$cntIndex][$myIndex]['elements']; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Sets a style for element rendering |
| 282 | * |
| 283 | * "Style" is some information that is opaque to Array Renderer but may be |
| 284 | * of use to e.g. template engine that receives the resultant array. |
| 285 | * |
| 286 | * @param string|array Element id or array ('element id' => 'style') |
| 287 | * @param sting Element style if $idOrStyles is not an array |
| 288 | * @return HTML_QuickForm2_Renderer_Array |
| 289 | */ |
| 290 | public function setStyleForId($idOrStyles, $style = null) |
| 291 | { |
| 292 | if (is_array($idOrStyles)) { |
| 293 | $this->styles = array_merge($this->styles, $idOrStyles); |
| 294 | } else { |
| 295 | $this->styles[$idOrStyles] = $style; |
| 296 | } |
| 297 | return $this; |
| 298 | } |
| 299 | |
| 300 | /**#@+ |
| 301 | * Implementations of abstract methods from {@link HTML_QuickForm2_Renderer} |
| 302 | */ |
| 303 | public function renderElement(HTML_QuickForm2_Node $element) |
| 304 | { |
| 305 | $ary = $this->buildCommonFields($element) + array( |
| 306 | 'value' => $element->getValue(), |
| 307 | 'type' => $element->getType(), |
| 308 | 'required' => $element->isRequired(), |
| 309 | ); |
| 310 | $this->pushScalar($ary); |
| 311 | } |
| 312 | |
| 313 | public function renderHidden(HTML_QuickForm2_Node $element) |
| 314 | { |
| 315 | if ($this->options['group_hiddens']) { |
| 316 | $this->array['hidden'][] = $element->__toString(); |
| 317 | } else { |
| 318 | $this->renderElement($element); |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | public function startForm(HTML_QuickForm2_Node $form) |
| 323 | { |
| 324 | $this->reset(); |
| 325 | |
| 326 | $this->array = $this->buildCommonFields($form); |
| 327 | if ($this->options['group_errors']) { |
| 328 | $this->array['errors'] = array(); |
| 329 | } |
| 330 | if ($this->options['group_hiddens']) { |
| 331 | $this->array['hidden'] = array(); |
| 332 | } |
| 333 | $this->containers = array(&$this->array['elements']); |
| 334 | } |
| 335 | |
| 336 | public function finishForm(HTML_QuickForm2_Node $form) |
| 337 | { |
| 338 | $this->finishContainer($form); |
| 339 | if ($this->hasRequired) { |
| 340 | $this->array['required_note'] = $this->options['required_note']; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | public function startContainer(HTML_QuickForm2_Node $container) |
| 345 | { |
| 346 | $ary = $this->buildCommonFields($container) + array( |
| 347 | 'required' => $container->isRequired(), |
| 348 | 'type' => $container->getType() |
| 349 | ); |
| 350 | $this->pushContainer($ary); |
| 351 | } |
| 352 | |
| 353 | public function finishContainer(HTML_QuickForm2_Node $container) |
| 354 | { |
| 355 | array_pop($this->containers); |
| 356 | } |
| 357 | |
| 358 | public function startGroup(HTML_QuickForm2_Node $group) |
| 359 | { |
| 360 | $ary = $this->buildCommonFields($group) + array( |
| 361 | 'required' => $group->isRequired(), |
| 362 | 'type' => $group->getType() |
| 363 | ); |
| 364 | if ($separator = $group->getSeparator()) { |
| 365 | $ary['separator'] = $separator; |
| 366 | } |
| 367 | $this->pushContainer($ary); |
| 368 | } |
| 369 | |
| 370 | public function finishGroup(HTML_QuickForm2_Node $group) |
| 371 | { |
| 372 | $this->finishContainer($group); |
| 373 | } |
| 374 | /**#@-*/ |
| 375 | } |
| 376 | ?> |
| 377 |