Container
6 years ago
Controller
6 years ago
DataSource
6 years ago
Element
6 years ago
Renderer
6 years ago
Rule
6 years ago
Container.php
6 years ago
Controller.php
6 years ago
DataSource.php
6 years ago
Element.php
6 years ago
Exception.php
6 years ago
Factory.php
6 years ago
JavascriptBuilder.php
6 years ago
Loader.php
6 years ago
Node.php
6 years ago
Renderer.php
6 years ago
Rule.php
6 years ago
Renderer.php
368 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Base class for HTML_QuickForm2 renderers |
| 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: Renderer.php 299706 2010-05-24 18:32:37Z avb $ |
| 43 | * @link http://pear.php.net/package/HTML_QuickForm2 |
| 44 | */ |
| 45 | use Piwik\Plugin; |
| 46 | |
| 47 | /** |
| 48 | * Class with static methods for loading classes and files |
| 49 | */ |
| 50 | // require_once 'HTML/QuickForm2/Loader.php'; |
| 51 | |
| 52 | /** |
| 53 | * Abstract base class for QuickForm2 renderers |
| 54 | * |
| 55 | * This class serves two main purposes: |
| 56 | * <ul> |
| 57 | * <li>Defines the API all renderers should implement (render*() methods);</li> |
| 58 | * <li>Provides static methods for registering renderers and their plugins |
| 59 | * and {@link factory()} method for creating renderer instances.</li> |
| 60 | * </ul> |
| 61 | * |
| 62 | * Note that renderers should always be instantiated through factory(), in the |
| 63 | * other case it will not be possible to add plugins. |
| 64 | * |
| 65 | * @category HTML |
| 66 | * @package HTML_QuickForm2 |
| 67 | * @author Alexey Borzov <avb@php.net> |
| 68 | * @author Bertrand Mansion <golgote@mamasam.com> |
| 69 | * @version Release: @package_version@ |
| 70 | */ |
| 71 | abstract class HTML_QuickForm2_Renderer |
| 72 | { |
| 73 | /** |
| 74 | * List of registered renderer types |
| 75 | * @var array |
| 76 | */ |
| 77 | private static $_types = array( |
| 78 | 'default' => array('HTML_QuickForm2_Renderer_Default', null), |
| 79 | 'array' => array('HTML_QuickForm2_Renderer_Array', null) |
| 80 | ); |
| 81 | |
| 82 | /** |
| 83 | * List of registered renderer plugins |
| 84 | * @var array |
| 85 | */ |
| 86 | private static $_pluginClasses = array( |
| 87 | 'default' => array(), |
| 88 | 'array' => array() |
| 89 | ); |
| 90 | |
| 91 | /** |
| 92 | * Renderer options |
| 93 | * @var array |
| 94 | * @see setOption() |
| 95 | */ |
| 96 | protected $options = array( |
| 97 | 'group_hiddens' => true, |
| 98 | 'required_note' => '<em>*</em> denotes required fields.', |
| 99 | 'errors_prefix' => 'Invalid information entered:', |
| 100 | 'errors_suffix' => 'Please correct these fields.', |
| 101 | 'group_errors' => false |
| 102 | ); |
| 103 | |
| 104 | /** |
| 105 | * Javascript builder object |
| 106 | * @var HTML_QuickForm2_JavascriptBuilder |
| 107 | */ |
| 108 | protected $jsBuilder; |
| 109 | |
| 110 | /** |
| 111 | * Creates a new renderer instance of the given type |
| 112 | * |
| 113 | * A renderer is always wrapped by a Proxy, which handles calling its |
| 114 | * "published" methods and methods of its plugins. Registered plugins are |
| 115 | * added automagically to the existing renderer instances so that |
| 116 | * <code> |
| 117 | * $foo = HTML_QuickForm2_Renderer::factory('foo'); |
| 118 | * // Plugin implementing bar() method |
| 119 | * HTML_QuickForm2_Renderer::registerPlugin('foo', 'Plugin_Foo_Bar'); |
| 120 | * $foo->bar(); |
| 121 | * </code> |
| 122 | * will work. |
| 123 | * |
| 124 | * @param string Type name (treated case-insensitively) |
| 125 | * @return HTML_QuickForm2_Renderer_Proxy A renderer instance of the given |
| 126 | * type wrapped by a Proxy |
| 127 | * @throws HTML_QuickForm2_InvalidArgumentException If type name is unknown |
| 128 | * @throws HTML_QuickForm2_NotFoundException If class for the renderer can |
| 129 | * not be found and/or loaded from file |
| 130 | */ |
| 131 | final public static function factory($type) |
| 132 | { |
| 133 | $type = strtolower($type); |
| 134 | if (!isset(self::$_types[$type])) { |
| 135 | throw new HTML_QuickForm2_InvalidArgumentException( |
| 136 | "Renderer type '$type' is not known" |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | list ($className, $includeFile) = self::$_types[$type]; |
| 141 | if (!class_exists($className)) { |
| 142 | HTML_QuickForm2_Loader::loadClass($className, $includeFile); |
| 143 | } |
| 144 | if (!class_exists('HTML_QuickForm2_Renderer_Proxy')) { |
| 145 | HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_Renderer_Proxy'); |
| 146 | } |
| 147 | return new HTML_QuickForm2_Renderer_Proxy(new $className, self::$_pluginClasses[$type]); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Registers a new renderer type |
| 152 | * |
| 153 | * @param string Type name (treated case-insensitively) |
| 154 | * @param string Class name |
| 155 | * @param string File containing the class, leave empty if class already loaded |
| 156 | * @throws HTML_QuickForm2_InvalidArgumentException if type already registered |
| 157 | */ |
| 158 | final public static function register($type, $className, $includeFile = null) |
| 159 | { |
| 160 | $type = strtolower($type); |
| 161 | if (!empty(self::$_types[$type])) { |
| 162 | throw new HTML_QuickForm2_InvalidArgumentException( |
| 163 | "Renderer type '$type' is already registered" |
| 164 | ); |
| 165 | } |
| 166 | self::$_types[$type] = array($className, $includeFile); |
| 167 | if (empty(self::$_pluginClasses[$type])) { |
| 168 | self::$_pluginClasses[$type] = array(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Registers a plugin for a renderer type |
| 174 | * |
| 175 | * @param string Renderer type name (treated case-insensitively) |
| 176 | * @param string Plugin class name |
| 177 | * @param string File containing the plugin class, leave empty if class already loaded |
| 178 | * @throws HTML_QuickForm2_InvalidArgumentException if plugin is already registered |
| 179 | */ |
| 180 | final public static function registerPlugin($type, $className, $includeFile = null) |
| 181 | { |
| 182 | $type = strtolower($type); |
| 183 | // We don't check self::$_types, since a plugin may be registered |
| 184 | // before renderer itself if it goes with some custom element |
| 185 | if (empty(self::$_pluginClasses[$type])) { |
| 186 | self::$_pluginClasses[$type] = array(array($className, $includeFile)); |
| 187 | } else { |
| 188 | foreach (self::$_pluginClasses[$type] as $plugin) { |
| 189 | if (0 == strcasecmp($plugin[0], $className)) { |
| 190 | throw new HTML_QuickForm2_InvalidArgumentException( |
| 191 | "Plugin '$className' for renderer type '$type' is already registered" |
| 192 | ); |
| 193 | } |
| 194 | } |
| 195 | self::$_pluginClasses[$type][] = array($className, $includeFile); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Constructor |
| 201 | * |
| 202 | * Renderer instances should not be created directly, use {@link factory()} |
| 203 | */ |
| 204 | protected function __construct() |
| 205 | { |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Returns an array of "published" method names that should be callable through proxy |
| 210 | * |
| 211 | * Methods defined in HTML_QuickForm2_Renderer are proxied automatically, |
| 212 | * only additional methods should be returned. |
| 213 | * |
| 214 | * @return array |
| 215 | */ |
| 216 | public function exportMethods() |
| 217 | { |
| 218 | return array(); |
| 219 | } |
| 220 | |
| 221 | /** |
| 222 | * Sets the option(s) affecting renderer behaviour |
| 223 | * |
| 224 | * The following options are available: |
| 225 | * <ul> |
| 226 | * <li>'group_hiddens' - whether to group hidden elements together or |
| 227 | * render them where they were added (boolean)</li> |
| 228 | * <li>'group_errors' - whether to group error messages or render them |
| 229 | * alongside elements they apply to (boolean)</li> |
| 230 | * <li>'errors_prefix' - leading message for grouped errors (string)</li> |
| 231 | * <li>'errors_suffix' - trailing message for grouped errors (string)</li> |
| 232 | * <li>'required_note' - note displayed if the form contains required |
| 233 | * elements (string)</li> |
| 234 | * </ul> |
| 235 | * |
| 236 | * @param string|array option name or array ('option name' => 'option value') |
| 237 | * @param mixed parameter value if $nameOrConfig is not an array |
| 238 | * @return HTML_QuickForm2_Renderer |
| 239 | * @throws HTML_QuickForm2_NotFoundException in case of unknown option |
| 240 | */ |
| 241 | public function setOption($nameOrOptions, $value = null) |
| 242 | { |
| 243 | if (is_array($nameOrOptions)) { |
| 244 | foreach ($nameOrOptions as $name => $value) { |
| 245 | $this->setOption($name, $value); |
| 246 | } |
| 247 | |
| 248 | } else { |
| 249 | if (!array_key_exists($nameOrOptions, $this->options)) { |
| 250 | throw new HTML_QuickForm2_NotFoundException( |
| 251 | "Unknown option '{$nameOrOptions}'" |
| 252 | ); |
| 253 | } |
| 254 | $this->options[$nameOrOptions] = $value; |
| 255 | } |
| 256 | |
| 257 | return $this; |
| 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Returns the value(s) of the renderer option(s) |
| 262 | * |
| 263 | * @param string parameter name |
| 264 | * @return mixed value of $name parameter, array of all configuration |
| 265 | * parameters if $name is not given |
| 266 | * @throws HTML_QuickForm2_NotFoundException in case of unknown option |
| 267 | */ |
| 268 | public function getOption($name = null) |
| 269 | { |
| 270 | if (null === $name) { |
| 271 | return $this->options; |
| 272 | } elseif (!array_key_exists($name, $this->options)) { |
| 273 | throw new HTML_QuickForm2_NotFoundException( |
| 274 | "Unknown option '{$name}'" |
| 275 | ); |
| 276 | } |
| 277 | return $this->options[$name]; |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Returns the javascript builder object |
| 282 | * |
| 283 | * @return HTML_QuickForm2_JavascriptBuilder |
| 284 | */ |
| 285 | public function getJavascriptBuilder() |
| 286 | { |
| 287 | if (empty($this->jsBuilder)) { |
| 288 | if (!class_exists('HTML_QuickForm2_JavascriptBuilder')) { |
| 289 | HTML_QuickForm2_Loader::loadClass('HTML_QuickForm2_JavascriptBuilder'); |
| 290 | } |
| 291 | $this->jsBuilder = new HTML_QuickForm2_JavascriptBuilder(); |
| 292 | } |
| 293 | return $this->jsBuilder; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * Sets the javascript builder object |
| 298 | * |
| 299 | * You may want to reuse the same builder object if outputting several |
| 300 | * forms on one page. |
| 301 | * |
| 302 | * @param HTML_QuickForm2_JavascriptBuilder |
| 303 | * @return HTML_QuickForm2_Renderer |
| 304 | */ |
| 305 | public function setJavascriptBuilder(HTML_QuickForm2_JavascriptBuilder $builder = null) |
| 306 | { |
| 307 | $this->jsBuilder = $builder; |
| 308 | return $this; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Renders a generic element |
| 313 | * |
| 314 | * @param HTML_QuickForm2_Node Element being rendered |
| 315 | */ |
| 316 | abstract public function renderElement(HTML_QuickForm2_Node $element); |
| 317 | |
| 318 | /** |
| 319 | * Renders a hidden element |
| 320 | * |
| 321 | * @param HTML_QuickForm2_Node Hidden element being rendered |
| 322 | */ |
| 323 | abstract public function renderHidden(HTML_QuickForm2_Node $element); |
| 324 | |
| 325 | /** |
| 326 | * Starts rendering a form, called before processing contained elements |
| 327 | * |
| 328 | * @param HTML_QuickForm2_Node Form being rendered |
| 329 | */ |
| 330 | abstract public function startForm(HTML_QuickForm2_Node $form); |
| 331 | |
| 332 | /** |
| 333 | * Finishes rendering a form, called after processing contained elements |
| 334 | * |
| 335 | * @param HTML_QuickForm2_Node Form being rendered |
| 336 | */ |
| 337 | abstract public function finishForm(HTML_QuickForm2_Node $form); |
| 338 | |
| 339 | /** |
| 340 | * Starts rendering a generic container, called before processing contained elements |
| 341 | * |
| 342 | * @param HTML_QuickForm2_Node Container being rendered |
| 343 | */ |
| 344 | abstract public function startContainer(HTML_QuickForm2_Node $container); |
| 345 | |
| 346 | /** |
| 347 | * Finishes rendering a generic container, called after processing contained elements |
| 348 | * |
| 349 | * @param HTML_QuickForm2_Node Container being rendered |
| 350 | */ |
| 351 | abstract public function finishContainer(HTML_QuickForm2_Node $container); |
| 352 | |
| 353 | /** |
| 354 | * Starts rendering a group, called before processing grouped elements |
| 355 | * |
| 356 | * @param HTML_QuickForm2_Node Group being rendered |
| 357 | */ |
| 358 | abstract public function startGroup(HTML_QuickForm2_Node $group); |
| 359 | |
| 360 | /** |
| 361 | * Finishes rendering a group, called after processing grouped elements |
| 362 | * |
| 363 | * @param HTML_QuickForm2_Node Group being rendered |
| 364 | */ |
| 365 | abstract public function finishGroup(HTML_QuickForm2_Node $group); |
| 366 | } |
| 367 | ?> |
| 368 |