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
Smarty.php
277 lines
| 1 | <?php |
| 2 | |
| 3 | namespace { |
| 4 | /** |
| 5 | * A renderer for HTML_QuickForm2 suitable for using with the Smarty template engine. |
| 6 | * See: http://www.smarty.net/ |
| 7 | * |
| 8 | * PHP version 5 |
| 9 | * |
| 10 | * LICENSE: |
| 11 | * |
| 12 | * Copyright (c) 2009, Alain D D Williams <addw@phcomp.co.uk> |
| 13 | * Based on the QuickForm2 Array renderer. |
| 14 | * |
| 15 | * All rights reserved. |
| 16 | * |
| 17 | * Redistribution and use in source and binary forms, with or without |
| 18 | * modification, are permitted provided that the following conditions |
| 19 | * are met: |
| 20 | * |
| 21 | * * Redistributions of source code must retain the above copyright |
| 22 | * notice, this list of conditions and the following disclaimer. |
| 23 | * * Redistributions in binary form must reproduce the above copyright |
| 24 | * notice, this list of conditions and the following disclaimer in the |
| 25 | * documentation and/or other materials provided with the distribution. |
| 26 | * * The names of the authors may not be used to endorse or promote products |
| 27 | * derived from this software without specific prior written permission. |
| 28 | * |
| 29 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 30 | * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 31 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 32 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 33 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 34 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 35 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 36 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 37 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 38 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 39 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 40 | * |
| 41 | * @category HTML |
| 42 | * @package HTML_QuickForm2 |
| 43 | * @author Alain D D Williams <addw@phcomp.co.uk> |
| 44 | * @license http://opensource.org/licenses/bsd-license.php New BSD License |
| 45 | * @version SCCS: %W% %G% %U% |
| 46 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 47 | */ |
| 48 | /** |
| 49 | * This generates an array, bring in the array renderer and extend it a bit: |
| 50 | */ |
| 51 | // require_once 'HTML/QuickForm2/Renderer/Array.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 | * 'requirednote' => note about the required elements (string), |
| 66 | * NB: no '_' in the middle |
| 67 | * In old_compat this is a span style="font-size:80%;" |
| 68 | * with the '*' also color:#ff0000; |
| 69 | * Not old_compat it is in a div class="reqnote" |
| 70 | * // if 'group_hiddens' option is true: |
| 71 | * 'hidden' => array with html of hidden elements (array), |
| 72 | * // if 'group_errors' option is true: |
| 73 | * 'errors' => array( |
| 74 | * '1st element id' => 'Error for the 1st element', |
| 75 | * ... |
| 76 | * 'nth element id' => 'Error for the nth element' |
| 77 | * ), |
| 78 | * 'elements' => array( |
| 79 | * element_1, |
| 80 | * ... |
| 81 | * element_N |
| 82 | * ) |
| 83 | * '1st_elements_name' => array for the 1st element, |
| 84 | * ... references into 'elements' above |
| 85 | * 'nth_elements_name' => array for the nth element, |
| 86 | * ) |
| 87 | * ); |
| 88 | * </pre> |
| 89 | * Where element_i is an array of the form |
| 90 | * <pre> |
| 91 | * array( |
| 92 | * 'id' => element id (string), |
| 93 | * 'name' => element name (string), |
| 94 | * 'type' => type of the element (string), |
| 95 | * 'frozen' => whether element is frozen (bool), |
| 96 | * // if element has a label: |
| 97 | * 'label' => 'label for the element', |
| 98 | * // note that if 'static_labels' option is true and element's label is an |
| 99 | * // array then there will be several 'label_*' keys corresponding to |
| 100 | * // labels' array keys |
| 101 | * 'required' => whether element is required (bool), |
| 102 | * // if a validation error is present and 'group_errors' option is false: |
| 103 | * 'error' => error associated with the element (string), |
| 104 | * // if some style was associated with an element: |
| 105 | * 'style' => 'some information about element style (e.g. for Smarty)', |
| 106 | * |
| 107 | * // if element is not a Container |
| 108 | * 'value' => element value (mixed), |
| 109 | * 'html' => HTML for the element (string), |
| 110 | * |
| 111 | * // if element is a Container |
| 112 | * 'attributes' => container attributes (string) |
| 113 | * // only for groups, if separator is set: |
| 114 | * 'separator' => separator for group elements (mixed), |
| 115 | * 'elements' => array( |
| 116 | * element_1, |
| 117 | * ... |
| 118 | * element_N |
| 119 | * ) |
| 120 | * |
| 121 | * If the type is 'radio' an element (type = 'radio') is created for each choice that the user has, |
| 122 | * keyed by the 'id' value. An 'element' will be created having an array with one element key '0'. |
| 123 | * The 'id' above will be set to the value in 'name'. |
| 124 | * The 'type' of each element will be 'radio' |
| 125 | * ); |
| 126 | * </pre> |
| 127 | * |
| 128 | * The following additional options are available: |
| 129 | * <ul> |
| 130 | * <li>'old_compat' - generate something compatible with an old renderer</li> |
| 131 | * <li>'key_id' - the key to elements is the field's 'id' rather than 'name'</li> |
| 132 | * </ul> |
| 133 | * |
| 134 | * While almost everything in this class is defined as public, its properties |
| 135 | * and those methods that are not published (i.e. not in array returned by |
| 136 | * exportMethods()) will be available to renderer plugins only. |
| 137 | * |
| 138 | * The following methods are published: |
| 139 | * - {@link reset()} |
| 140 | * - {@link toArray()} |
| 141 | * - {@link setStyleForId()} |
| 142 | * |
| 143 | * @category HTML |
| 144 | * @package HTML_QuickForm2 |
| 145 | * @author Alain D D Williams <addw@phcomp.co.uk> |
| 146 | * @version Release: SCCS: %W% %G% %U% |
| 147 | */ |
| 148 | class HTML_QuickForm2_Renderer_Smarty extends \HTML_QuickForm2_Renderer_Array |
| 149 | { |
| 150 | /** |
| 151 | * Constructor, adds new options |
| 152 | */ |
| 153 | protected function __construct() |
| 154 | { |
| 155 | parent::__construct(); |
| 156 | $this->options += array('old_compat' => \false, 'key_id' => \false); |
| 157 | } |
| 158 | /** |
| 159 | * Creates an array with fields that are common to all elements |
| 160 | * |
| 161 | * @param HTML_QuickForm2_Node Element being rendered |
| 162 | * |
| 163 | * @return array |
| 164 | */ |
| 165 | public function buildCommonFields(\HTML_QuickForm2_Node $element) |
| 166 | { |
| 167 | $keyn = $this->options['key_id'] ? 'id' : 'name'; |
| 168 | $ary = array('id' => $element->getId(), 'frozen' => $element->toggleFrozen(), 'name' => $element->getName()); |
| 169 | // Key that we use for putting into arrays so that smarty can extract them. |
| 170 | // Note that the name may be empty. |
| 171 | $key_val = $ary[$keyn]; |
| 172 | if ($key_val == '') { |
| 173 | $key_val = $ary['id']; |
| 174 | } |
| 175 | if ($labels = $element->getLabel()) { |
| 176 | if (!\is_array($labels) || !$this->options['static_labels']) { |
| 177 | $ary['label'] = $labels; |
| 178 | } else { |
| 179 | foreach ($labels as $key => $label) { |
| 180 | $key = \is_int($key) ? $key + 1 : $key; |
| 181 | if (1 === $key) { |
| 182 | $ary['label'] = $label; |
| 183 | } else { |
| 184 | $ary['label_' . $key] = $label; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | // Smarty: group_errors under 'name' or 'id' depending on key_id option: |
| 190 | if (($error = $element->getError()) && $this->options['group_errors']) { |
| 191 | $this->array['errors'][$key_val] = $error; |
| 192 | } elseif ($error) { |
| 193 | $ary['error'] = $error; |
| 194 | } |
| 195 | if (isset($this->styles[$key_val])) { |
| 196 | $ary['style'] = $this->styles[$key_val]; |
| 197 | } |
| 198 | if (!$element instanceof \HTML_QuickForm2_Container) { |
| 199 | $ary['html'] = $element->__toString(); |
| 200 | } else { |
| 201 | $ary['elements'] = array(); |
| 202 | $ary['attributes'] = $element->getAttributes(\true); |
| 203 | } |
| 204 | return $ary; |
| 205 | } |
| 206 | public function startForm(\HTML_QuickForm2_Node $form) |
| 207 | { |
| 208 | if ($this->options['old_compat']) { |
| 209 | $this->options['group_hiddens'] = \true; |
| 210 | } |
| 211 | parent::startForm($form); |
| 212 | } |
| 213 | public function finishForm(\HTML_QuickForm2_Node $form) |
| 214 | { |
| 215 | parent::finishForm($form); |
| 216 | if ($this->hasRequired) { |
| 217 | // Create element 'requirednote' - note no '_' |
| 218 | if ($this->options['old_compat']) { |
| 219 | // Old QuickForm had the requirednote styled & a different name: |
| 220 | $this->array['requirednote'] = \preg_replace('|<em>([^<]+)</em>(.*)|', '<span style="font-size:80%; color:#ff0000;">$1</span><span style="font-size:80%;">$2</span>', $this->options['required_note']); |
| 221 | } else { |
| 222 | $this->array['requirednote'] = '<div class="reqnote">' . $this->options['required_note'] . '</div>'; |
| 223 | } |
| 224 | } |
| 225 | // Create top level elements keyed by form field 'name' or 'id' |
| 226 | if (isset($this->array['elements']['0'])) { |
| 227 | $this->linkToLevelAbove($this->array, $this->array['elements']); |
| 228 | } |
| 229 | // For compat: it is expected that 'hidden' is a string, not an array: |
| 230 | if ($this->options['old_compat'] && isset($this->array['hidden']) && \is_array($this->array['hidden'])) { |
| 231 | $this->array['hidden'] = \join(' ', $this->array['hidden']); |
| 232 | } |
| 233 | } |
| 234 | // Look through the elements (numerically indexed) array, make fields |
| 235 | // members of the level above. This is so that they can be easily accessed by smarty templates. |
| 236 | // If we find a group, recurse down. Used for smarty only. |
| 237 | // Key is 'name' or 'id'. |
| 238 | private function linkToLevelAbove(&$top, $elements, $inGroup = \false) |
| 239 | { |
| 240 | $key = $this->options['key_id'] ? 'id' : 'name'; |
| 241 | foreach ($elements as &$elem) { |
| 242 | $top_key = $elem[$key]; |
| 243 | // If in a group, convert something like inGrp[F4grp][F4_1] to F4_1 |
| 244 | // Don't do if key_id as the value is a straight id. |
| 245 | if (!$this->options['key_id'] && $inGroup && $top_key != '') { |
| 246 | if (!\preg_match("/\\[?([\\w_]+)\\]?\$/i", $top_key, $match)) { |
| 247 | throw new \HTML_QuickForm2_InvalidArgumentException("linkToLevelAbove can't obtain the name from '{$top_key}'"); |
| 248 | } |
| 249 | $top_key = $match[1]; |
| 250 | } |
| 251 | // Radio buttons: several elements with the same name, make an array |
| 252 | if (isset($elem['type']) && $elem['type'] == 'radio') { |
| 253 | if (!isset($top[$top_key])) { |
| 254 | $top[$top_key] = array('id' => $top_key, 'type' => 'radio', 'elements' => array(0)); |
| 255 | } |
| 256 | $top[$top_key][$elem['id']] =& $elem; |
| 257 | } else { |
| 258 | // Normal field, just link into the level above. |
| 259 | if (!isset($top[$top_key])) { |
| 260 | $top[$top_key] =& $elem; |
| 261 | } |
| 262 | } |
| 263 | // Link into the level above |
| 264 | // If we have a group link its fields up to this level: |
| 265 | if (isset($elem['elements']['0'])) { |
| 266 | $this->linkToLevelAbove($elem, $elem['elements'], \true); |
| 267 | } |
| 268 | // Link errors to the top level: |
| 269 | if (isset($elem['error']) && isset($this->array[$elem['error']])) { |
| 270 | $this->array['errors'][$top_key] = $this->array[$elem['error']]; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | /**#@-*/ |
| 275 | } |
| 276 | } |
| 277 |