PageRenderTime 24ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/HTML/QuickForm2/Renderer.php

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