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