Array.php 9.7 KB

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