PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/libs/HTML/QuickForm2.php

https://github.com/CodeYellowBV/piwik
PHP | 224 lines | 103 code | 16 blank | 105 comment | 15 complexity | aa4307cba861837825a3ded27a0b6b0b 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. * Class representing a HTML form
  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: QuickForm2.php 299706 2010-05-24 18:32:37Z avb $
  43. * @link http://pear.php.net/package/HTML_QuickForm2
  44. */
  45. /**
  46. * Abstract base class for QuickForm2 containers
  47. */
  48. // require_once 'HTML/QuickForm2/Container.php';
  49. /**
  50. * Data source for HTML_QuickForm2 objects based on superglobal arrays
  51. */
  52. // require_once 'HTML/QuickForm2/DataSource/SuperGlobal.php';
  53. /**
  54. * Class representing a HTML form
  55. *
  56. * @category HTML
  57. * @package HTML_QuickForm2
  58. * @author Alexey Borzov <avb@php.net>
  59. * @author Bertrand Mansion <golgote@mamasam.com>
  60. * @version Release: @package_version@
  61. */
  62. class HTML_QuickForm2 extends HTML_QuickForm2_Container
  63. {
  64. /**
  65. * Data sources providing values for form elements
  66. * @var array
  67. */
  68. protected $datasources = array();
  69. /**
  70. * We do not allow setting "method" and "id" other than through constructor
  71. * @var array
  72. */
  73. protected $watchedAttributes = array('id', 'method');
  74. /**
  75. * Class constructor, form's "id" and "method" attributes can only be set here
  76. *
  77. * @param string "id" attribute of <form> tag
  78. * @param string HTTP method used to submit the form
  79. * @param mixed Additional attributes (either a string or an array)
  80. * @param bool Whether to track if the form was submitted by adding
  81. * a special hidden field
  82. */
  83. public function __construct($id, $method = 'post', $attributes = null, $trackSubmit = true)
  84. {
  85. $method = ('GET' == strtoupper($method))? 'get': 'post';
  86. if (empty($id)) {
  87. $id = self::generateId('');
  88. $trackSubmit = false;
  89. } else {
  90. self::storeId($id);
  91. }
  92. $this->attributes = array_merge(
  93. self::prepareAttributes($attributes),
  94. array('id' => (string)$id, 'method' => $method)
  95. );
  96. if (!isset($this->attributes['action'])) {
  97. $this->attributes['action'] = $_SERVER['PHP_SELF'];
  98. }
  99. if ($trackSubmit && isset($_REQUEST['_qf__' . $id]) ||
  100. !$trackSubmit && ('get' == $method && !empty($_GET) ||
  101. 'post' == $method && (!empty($_POST) || !empty($_FILES))))
  102. {
  103. $this->addDataSource(new HTML_QuickForm2_DataSource_SuperGlobal(
  104. $method, get_magic_quotes_gpc()
  105. ));
  106. }
  107. if ($trackSubmit) {
  108. $this->appendChild(HTML_QuickForm2_Factory::createElement(
  109. 'hidden', '_qf__' . $id
  110. ));
  111. }
  112. }
  113. protected function onAttributeChange($name, $value = null)
  114. {
  115. throw new HTML_QuickForm2_InvalidArgumentException(
  116. 'Attribute \'' . $name . '\' is read-only'
  117. );
  118. }
  119. protected function setContainer(HTML_QuickForm2_Container $container = null)
  120. {
  121. throw new HTML_QuickForm2_Exception('Form cannot be added to container');
  122. }
  123. public function setId($id = null)
  124. {
  125. throw new HTML_QuickForm2_InvalidArgumentException(
  126. "Attribute 'id' is read-only"
  127. );
  128. }
  129. /**
  130. * Adds a new data source to the form
  131. *
  132. * @param HTML_QuickForm2_DataSource Data source
  133. */
  134. public function addDataSource(HTML_QuickForm2_DataSource $datasource)
  135. {
  136. $this->datasources[] = $datasource;
  137. $this->updateValue();
  138. }
  139. /**
  140. * Replaces the list of form's data sources with a completely new one
  141. *
  142. * @param array A new data source list
  143. * @throws HTML_QuickForm2_InvalidArgumentException if given array
  144. * contains something that is not a valid data source
  145. */
  146. public function setDataSources(array $datasources)
  147. {
  148. foreach ($datasources as $ds) {
  149. if (!$ds instanceof HTML_QuickForm2_DataSource) {
  150. throw new HTML_QuickForm2_InvalidArgumentException(
  151. 'Array should contain only DataSource instances'
  152. );
  153. }
  154. }
  155. $this->datasources = $datasources;
  156. $this->updateValue();
  157. }
  158. /**
  159. * Returns the list of data sources attached to the form
  160. *
  161. * @return array
  162. */
  163. public function getDataSources()
  164. {
  165. return $this->datasources;
  166. }
  167. public function getType()
  168. {
  169. return 'form';
  170. }
  171. public function setValue($value)
  172. {
  173. throw new HTML_QuickForm2_Exception('Not implemented');
  174. }
  175. /**
  176. * Performs the server-side validation
  177. *
  178. * @return boolean Whether all form's elements are valid
  179. */
  180. public function validate()
  181. {
  182. $isSubmitted = false;
  183. foreach ($this->datasources as $ds) {
  184. if ($ds instanceof HTML_QuickForm2_DataSource_Submit) {
  185. $isSubmitted = true;
  186. break;
  187. }
  188. }
  189. return $isSubmitted? parent::validate(): false;
  190. }
  191. /**
  192. * Renders the form using the given renderer
  193. *
  194. * @param HTML_QuickForm2_Renderer Renderer instance
  195. * @return HTML_QuickForm2_Renderer
  196. */
  197. public function render(HTML_QuickForm2_Renderer $renderer)
  198. {
  199. $renderer->startForm($this);
  200. $renderer->getJavascriptBuilder()->startForm($this);
  201. foreach ($this as $element) {
  202. $element->render($renderer);
  203. }
  204. $renderer->finishForm($this);
  205. return $renderer;
  206. }
  207. }
  208. ?>