PageRenderTime 55ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/HTML/QuickForm2/Renderer.php

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