Array.php 9.7 KB

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