PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/campsite/src/include/pear/HTML/QuickForm/Renderer/Array.php

https://github.com/joechrysler/Campsite
PHP | 340 lines | 127 code | 31 blank | 182 comment | 20 complexity | 3d0c41285bd432c09ce431b1f11e29e6 MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, LGPL-2.1, Apache-2.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * A concrete renderer for HTML_QuickForm, makes an array of form contents
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Alexey Borzov <avb@php.net>
  17. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  18. * @author Bertrand Mansion <bmansion@mamasam.com>
  19. * @author Thomas Schulz <ths@4bconsult.de>
  20. * @copyright 2001-2009 The PHP Group
  21. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  22. * @version CVS: $Id: Array.php,v 1.11 2009/04/04 21:34:04 avb Exp $
  23. * @link http://pear.php.net/package/HTML_QuickForm
  24. */
  25. /**
  26. * An abstract base class for QuickForm renderers
  27. */
  28. require_once 'HTML/QuickForm/Renderer.php';
  29. /**
  30. * A concrete renderer for HTML_QuickForm, makes an array of form contents
  31. *
  32. * Based on old HTML_QuickForm::toArray() code.
  33. *
  34. * The form array structure is the following:
  35. * <pre>
  36. * array(
  37. * 'frozen' => 'whether the form is frozen',
  38. * 'javascript' => 'javascript for client-side validation',
  39. * 'attributes' => 'attributes for <form> tag',
  40. * 'requirednote => 'note about the required elements',
  41. * // if we set the option to collect hidden elements
  42. * 'hidden' => 'collected html of all hidden elements',
  43. * // if there were some validation errors:
  44. * 'errors' => array(
  45. * '1st element name' => 'Error for the 1st element',
  46. * ...
  47. * 'nth element name' => 'Error for the nth element'
  48. * ),
  49. * // if there are no headers in the form:
  50. * 'elements' => array(
  51. * element_1,
  52. * ...
  53. * element_N
  54. * )
  55. * // if there are headers in the form:
  56. * 'sections' => array(
  57. * array(
  58. * 'header' => 'Header text for the first header',
  59. * 'name' => 'Header name for the first header',
  60. * 'elements' => array(
  61. * element_1,
  62. * ...
  63. * element_K1
  64. * )
  65. * ),
  66. * ...
  67. * array(
  68. * 'header' => 'Header text for the Mth header',
  69. * 'name' => 'Header name for the Mth header',
  70. * 'elements' => array(
  71. * element_1,
  72. * ...
  73. * element_KM
  74. * )
  75. * )
  76. * )
  77. * );
  78. * </pre>
  79. *
  80. * where element_i is an array of the form:
  81. * <pre>
  82. * array(
  83. * 'name' => 'element name',
  84. * 'value' => 'element value',
  85. * 'type' => 'type of the element',
  86. * 'frozen' => 'whether element is frozen',
  87. * 'label' => 'label for the element',
  88. * 'required' => 'whether element is required',
  89. * 'error' => 'error associated with the element',
  90. * 'style' => 'some information about element style (e.g. for Smarty)',
  91. * // if element is not a group
  92. * 'html' => 'HTML for the element'
  93. * // if element is a group
  94. * 'separator' => 'separator for group elements',
  95. * 'elements' => array(
  96. * element_1,
  97. * ...
  98. * element_N
  99. * )
  100. * );
  101. * </pre>
  102. *
  103. * @category HTML
  104. * @package HTML_QuickForm
  105. * @author Alexey Borzov <avb@php.net>
  106. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  107. * @author Bertrand Mansion <bmansion@mamasam.com>
  108. * @author Thomas Schulz <ths@4bconsult.de>
  109. * @version Release: 3.2.11
  110. * @since 3.0
  111. */
  112. class HTML_QuickForm_Renderer_Array extends HTML_QuickForm_Renderer
  113. {
  114. /**#@+
  115. * @access private
  116. */
  117. /**
  118. * An array being generated
  119. * @var array
  120. */
  121. var $_ary;
  122. /**
  123. * Number of sections in the form (i.e. number of headers in it)
  124. * @var integer
  125. */
  126. var $_sectionCount;
  127. /**
  128. * Current section number
  129. * @var integer
  130. */
  131. var $_currentSection;
  132. /**
  133. * Array representing current group
  134. * @var array
  135. */
  136. var $_currentGroup = null;
  137. /**
  138. * Additional style information for different elements
  139. * @var array
  140. */
  141. var $_elementStyles = array();
  142. /**
  143. * true: collect all hidden elements into string; false: process them as usual form elements
  144. * @var bool
  145. */
  146. var $_collectHidden = false;
  147. /**
  148. * true: render an array of labels to many labels, $key 0 named 'label', the rest "label_$key"
  149. * false: leave labels as defined
  150. * @var bool
  151. */
  152. var $_staticLabels = false;
  153. /**#@-*/
  154. /**
  155. * Constructor
  156. *
  157. * @param bool true: collect all hidden elements into string; false: process them as usual form elements
  158. * @param bool true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key"
  159. * @access public
  160. */
  161. function HTML_QuickForm_Renderer_Array($collectHidden = false, $staticLabels = false)
  162. {
  163. $this->HTML_QuickForm_Renderer();
  164. $this->_collectHidden = $collectHidden;
  165. $this->_staticLabels = $staticLabels;
  166. } // end constructor
  167. /**
  168. * Returns the resultant array
  169. *
  170. * @access public
  171. * @return array
  172. */
  173. function toArray()
  174. {
  175. return $this->_ary;
  176. }
  177. function startForm(&$form)
  178. {
  179. $this->_ary = array(
  180. 'frozen' => $form->isFrozen(),
  181. 'javascript' => $form->getValidationScript(),
  182. 'attributes' => $form->getAttributes(true),
  183. 'requirednote' => $form->getRequiredNote(),
  184. 'errors' => array()
  185. );
  186. if ($this->_collectHidden) {
  187. $this->_ary['hidden'] = '';
  188. }
  189. $this->_elementIdx = 1;
  190. $this->_currentSection = null;
  191. $this->_sectionCount = 0;
  192. } // end func startForm
  193. function renderHeader(&$header)
  194. {
  195. $this->_ary['sections'][$this->_sectionCount] = array(
  196. 'header' => $header->toHtml(),
  197. 'name' => $header->getName()
  198. );
  199. $this->_currentSection = $this->_sectionCount++;
  200. } // end func renderHeader
  201. function renderElement(&$element, $required, $error)
  202. {
  203. $elAry = $this->_elementToArray($element, $required, $error);
  204. if (!empty($error)) {
  205. $this->_ary['errors'][$elAry['name']] = $error;
  206. }
  207. $this->_storeArray($elAry);
  208. } // end func renderElement
  209. function renderHidden(&$element)
  210. {
  211. if ($this->_collectHidden) {
  212. $this->_ary['hidden'] .= $element->toHtml() . "\n";
  213. } else {
  214. $this->renderElement($element, false, null);
  215. }
  216. } // end func renderHidden
  217. function startGroup(&$group, $required, $error)
  218. {
  219. $this->_currentGroup = $this->_elementToArray($group, $required, $error);
  220. if (!empty($error)) {
  221. $this->_ary['errors'][$this->_currentGroup['name']] = $error;
  222. }
  223. } // end func startGroup
  224. function finishGroup(&$group)
  225. {
  226. $this->_storeArray($this->_currentGroup);
  227. $this->_currentGroup = null;
  228. } // end func finishGroup
  229. /**
  230. * Creates an array representing an element
  231. *
  232. * @access private
  233. * @param HTML_QuickForm_element element being processed
  234. * @param bool Whether an element is required
  235. * @param string Error associated with the element
  236. * @return array
  237. */
  238. function _elementToArray(&$element, $required, $error)
  239. {
  240. $ret = array(
  241. 'name' => $element->getName(),
  242. 'value' => $element->getValue(),
  243. 'type' => $element->getType(),
  244. 'frozen' => $element->isFrozen(),
  245. 'required' => $required,
  246. 'error' => $error
  247. );
  248. // render label(s)
  249. $labels = $element->getLabel();
  250. if (is_array($labels) && $this->_staticLabels) {
  251. foreach($labels as $key => $label) {
  252. $key = is_int($key)? $key + 1: $key;
  253. if (1 === $key) {
  254. $ret['label'] = $label;
  255. } else {
  256. $ret['label_' . $key] = $label;
  257. }
  258. }
  259. } else {
  260. $ret['label'] = $labels;
  261. }
  262. // set the style for the element
  263. if (isset($this->_elementStyles[$ret['name']])) {
  264. $ret['style'] = $this->_elementStyles[$ret['name']];
  265. }
  266. if ('group' == $ret['type']) {
  267. $ret['separator'] = $element->_separator;
  268. $ret['elements'] = array();
  269. } else {
  270. $ret['html'] = $element->toHtml();
  271. }
  272. return $ret;
  273. }
  274. /**
  275. * Stores an array representation of an element in the form array
  276. *
  277. * @access private
  278. * @param array Array representation of an element
  279. * @return void
  280. */
  281. function _storeArray($elAry)
  282. {
  283. // where should we put this element...
  284. if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) {
  285. $this->_currentGroup['elements'][] = $elAry;
  286. } elseif (isset($this->_currentSection)) {
  287. $this->_ary['sections'][$this->_currentSection]['elements'][] = $elAry;
  288. } else {
  289. $this->_ary['elements'][] = $elAry;
  290. }
  291. }
  292. /**
  293. * Sets a style to use for element rendering
  294. *
  295. * @param mixed element name or array ('element name' => 'style name')
  296. * @param string style name if $elementName is not an array
  297. * @access public
  298. * @return void
  299. */
  300. function setElementStyle($elementName, $styleName = null)
  301. {
  302. if (is_array($elementName)) {
  303. $this->_elementStyles = array_merge($this->_elementStyles, $elementName);
  304. } else {
  305. $this->_elementStyles[$elementName] = $styleName;
  306. }
  307. }
  308. }
  309. ?>