ArraySmarty.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * A static renderer for HTML_QuickForm, makes an array of form content
  5. * useful for a Smarty template
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * LICENSE: This source file is subject to version 3.01 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category HTML
  16. * @package HTML_QuickForm
  17. * @author Alexey Borzov <avb@php.net>
  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: ArraySmarty.php,v 1.14 2009/04/06 12:02:08 avb Exp $
  23. * @link http://pear.php.net/package/HTML_QuickForm
  24. */
  25. /**
  26. * A static renderer for HTML_QuickForm, makes an array of form content
  27. * useful for a Smarty template
  28. *
  29. * Based on old HTML_QuickForm::toArray() code and ITStatic renderer.
  30. *
  31. * The form array structure is the following:
  32. * <pre>
  33. * Array (
  34. * [frozen] => whether the complete form is frozen'
  35. * [javascript] => javascript for client-side validation
  36. * [attributes] => attributes for <form> tag
  37. * [hidden] => html of all hidden elements
  38. * [requirednote] => note about the required elements
  39. * [errors] => Array
  40. * (
  41. * [1st_element_name] => Error for the 1st element
  42. * ...
  43. * [nth_element_name] => Error for the nth element
  44. * )
  45. *
  46. * [header] => Array
  47. * (
  48. * [1st_header_name] => Header text for the 1st header
  49. * ...
  50. * [nth_header_name] => Header text for the nth header
  51. * )
  52. *
  53. * [1st_element_name] => Array for the 1st element
  54. * ...
  55. * [nth_element_name] => Array for the nth element
  56. * </pre>
  57. *
  58. * where an element array has the form:
  59. * <pre>
  60. * (
  61. * [name] => element name
  62. * [value] => element value,
  63. * [type] => type of the element
  64. * [frozen] => whether element is frozen
  65. * [label] => label for the element
  66. * [required] => whether element is required
  67. * // if element is not a group:
  68. * [html] => HTML for the element
  69. * // if element is a group:
  70. * [separator] => separator for group elements
  71. * [1st_gitem_name] => Array for the 1st element in group
  72. * ...
  73. * [nth_gitem_name] => Array for the nth element in group
  74. * )
  75. * )
  76. * </pre>
  77. *
  78. * @category HTML
  79. * @package HTML_QuickForm
  80. * @author Alexey Borzov <avb@php.net>
  81. * @author Bertrand Mansion <bmansion@mamasam.com>
  82. * @author Thomas Schulz <ths@4bconsult.de>
  83. * @version Release: 3.2.11
  84. * @since 3.0
  85. */
  86. class HTML_QuickForm_Renderer_ArraySmarty extends HTML_QuickForm_Renderer_Array
  87. {
  88. /**#@+
  89. * @access private
  90. */
  91. /**
  92. * The Smarty template engine instance
  93. * @var object
  94. */
  95. var $_tpl = null;
  96. /**
  97. * Current element index
  98. * @var integer
  99. */
  100. var $_elementIdx = 0;
  101. /**
  102. * The current element index inside a group
  103. * @var integer
  104. */
  105. var $_groupElementIdx = 0;
  106. /**
  107. * How to handle the required tag for required fields
  108. * @var string
  109. * @see setRequiredTemplate()
  110. */
  111. var $_required = '';
  112. /**
  113. * How to handle error messages in form validation
  114. * @var string
  115. * @see setErrorTemplate()
  116. */
  117. var $_error = '';
  118. /**#@-*/
  119. /**
  120. * Constructor
  121. *
  122. * @param Smarty reference to the Smarty template engine instance
  123. * @param bool true: render an array of labels to many labels, $key 0 to 'label' and the oterh to "label_$key"
  124. * @param bool true: collect all hidden elements into string; false: process them as usual form elements
  125. * @access public
  126. */
  127. function HTML_QuickForm_Renderer_ArraySmarty(&$tpl, $staticLabels = false, $collectHidden = true)
  128. {
  129. $this->HTML_QuickForm_Renderer_Array($collectHidden, $staticLabels);
  130. $this->_tpl =& $tpl;
  131. } // end constructor
  132. /**
  133. * Called when visiting a header element
  134. *
  135. * @param HTML_QuickForm_header header element being visited
  136. * @access public
  137. * @return void
  138. */
  139. function renderHeader(&$header)
  140. {
  141. if ($name = $header->getName()) {
  142. $this->_ary['header'][$name] = $header->toHtml();
  143. } else {
  144. $this->_ary['header'][$this->_sectionCount] = $header->toHtml();
  145. }
  146. $this->_currentSection = $this->_sectionCount++;
  147. } // end func renderHeader
  148. /**
  149. * Called when visiting a group, before processing any group elements
  150. *
  151. * @param HTML_QuickForm_group group being visited
  152. * @param bool Whether a group is required
  153. * @param string An error message associated with a group
  154. * @access public
  155. * @return void
  156. */
  157. function startGroup(&$group, $required, $error)
  158. {
  159. parent::startGroup($group, $required, $error);
  160. $this->_groupElementIdx = 1;
  161. } // end func startGroup
  162. /**
  163. * Creates an array representing an element containing
  164. * the key for storing this
  165. *
  166. * @access private
  167. * @param HTML_QuickForm_element form element being visited
  168. * @param bool Whether an element is required
  169. * @param string Error associated with the element
  170. * @return array
  171. */
  172. function _elementToArray(&$element, $required, $error)
  173. {
  174. $ret = parent::_elementToArray($element, $required, $error);
  175. if ('group' == $ret['type']) {
  176. $ret['html'] = $element->toHtml();
  177. // we don't need the elements, see the array structure
  178. unset($ret['elements']);
  179. }
  180. if (($required || $error) && !empty($this->_required)){
  181. $this->_renderRequired($ret['label'], $ret['html'], $required, $error);
  182. }
  183. if ($error && !empty($this->_error)) {
  184. $this->_renderError($ret['label'], $ret['html'], $error);
  185. $ret['error'] = $error;
  186. }
  187. // create keys for elements grouped by native group or name
  188. if (strstr($ret['name'], '[') or $this->_currentGroup) {
  189. // Fix for bug #8123: escape backslashes and quotes to prevent errors
  190. // in eval(). The code below seems to handle the case where element
  191. // name has unbalanced square brackets. Dunno whether we really
  192. // need this after the fix for #8123, but I'm wary of making big
  193. // changes to this code.
  194. preg_match('/([^]]*)\\[([^]]*)\\]/', $ret['name'], $matches);
  195. if (isset($matches[1])) {
  196. $sKeysSub = substr_replace($ret['name'], '', 0, strlen($matches[1]));
  197. $sKeysSub = str_replace(
  198. array('\\', '\'', '[' , ']', '[\'\']'),
  199. array('\\\\', '\\\'', '[\'', '\']', '[]' ),
  200. $sKeysSub
  201. );
  202. $sKeys = '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $matches[1]) . '\']' . $sKeysSub;
  203. } else {
  204. $sKeys = '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret['name']) . '\']';
  205. }
  206. // special handling for elements in native groups
  207. if ($this->_currentGroup) {
  208. // skip unnamed group items unless radios: no name -> no static access
  209. // identification: have the same key string as the parent group
  210. if ($this->_currentGroup['keys'] == $sKeys and 'radio' != $ret['type']) {
  211. return false;
  212. }
  213. // reduce string of keys by remove leading group keys
  214. if (0 === strpos($sKeys, $this->_currentGroup['keys'])) {
  215. $sKeys = substr_replace($sKeys, '', 0, strlen($this->_currentGroup['keys']));
  216. }
  217. }
  218. // element without a name
  219. } elseif ($ret['name'] == '') {
  220. $sKeys = '[\'element_' . $this->_elementIdx . '\']';
  221. // other elements
  222. } else {
  223. $sKeys = '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret['name']) . '\']';
  224. }
  225. // for radios: add extra key from value
  226. if ('radio' == $ret['type'] and substr($sKeys, -2) != '[]') {
  227. $sKeys .= '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret['value']) . '\']';
  228. }
  229. $this->_elementIdx++;
  230. $ret['keys'] = $sKeys;
  231. return $ret;
  232. } // end func _elementToArray
  233. /**
  234. * Stores an array representation of an element in the form array
  235. *
  236. * @access private
  237. * @param array Array representation of an element
  238. * @return void
  239. */
  240. function _storeArray($elAry)
  241. {
  242. if ($elAry) {
  243. $sKeys = $elAry['keys'];
  244. unset($elAry['keys']);
  245. // where should we put this element...
  246. if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) {
  247. $toEval = '$this->_currentGroup' . $sKeys . ' = $elAry;';
  248. } else {
  249. $toEval = '$this->_ary' . $sKeys . ' = $elAry;';
  250. }
  251. eval($toEval);
  252. }
  253. return;
  254. }
  255. /**
  256. * Called when an element is required
  257. *
  258. * This method will add the required tag to the element label and/or the element html
  259. * such as defined with the method setRequiredTemplate.
  260. *
  261. * @param string The element label
  262. * @param string The element html rendering
  263. * @param boolean The element required
  264. * @param string The element error
  265. * @see setRequiredTemplate()
  266. * @access private
  267. * @return void
  268. */
  269. function _renderRequired(&$label, &$html, &$required, &$error)
  270. {
  271. $this->_tpl->assign(array(
  272. 'label' => $label,
  273. 'html' => $html,
  274. 'required' => $required,
  275. 'error' => $error
  276. ));
  277. if (!empty($label) && strpos($this->_required, $this->_tpl->left_delimiter . '$label') !== false) {
  278. $label = $this->_tplFetch($this->_required);
  279. }
  280. if (!empty($html) && strpos($this->_required, $this->_tpl->left_delimiter . '$html') !== false) {
  281. $html = $this->_tplFetch($this->_required);
  282. }
  283. $this->_tpl->clear_assign(array('label', 'html', 'required'));
  284. } // end func _renderRequired
  285. /**
  286. * Called when an element has a validation error
  287. *
  288. * This method will add the error message to the element label or the element html
  289. * such as defined with the method setErrorTemplate. If the error placeholder is not found
  290. * in the template, the error will be displayed in the form error block.
  291. *
  292. * @param string The element label
  293. * @param string The element html rendering
  294. * @param string The element error
  295. * @see setErrorTemplate()
  296. * @access private
  297. * @return void
  298. */
  299. function _renderError(&$label, &$html, &$error)
  300. {
  301. $this->_tpl->assign(array('label' => '', 'html' => '', 'error' => $error));
  302. $error = $this->_tplFetch($this->_error);
  303. $this->_tpl->assign(array('label' => $label, 'html' => $html));
  304. if (!empty($label) && strpos($this->_error, $this->_tpl->left_delimiter . '$label') !== false) {
  305. $label = $this->_tplFetch($this->_error);
  306. } elseif (!empty($html) && strpos($this->_error, $this->_tpl->left_delimiter . '$html') !== false) {
  307. $html = $this->_tplFetch($this->_error);
  308. }
  309. $this->_tpl->clear_assign(array('label', 'html', 'error'));
  310. } // end func _renderError
  311. /**
  312. * Process an template sourced in a string with Smarty
  313. *
  314. * Smarty has no core function to render a template given as a string.
  315. * So we use the smarty eval plugin function to do this.
  316. *
  317. * @param string The template source
  318. * @access private
  319. * @return void
  320. */
  321. function _tplFetch($tplSource)
  322. {
  323. if (!function_exists('smarty_function_eval')) {
  324. require SMARTY_DIR . '/plugins/function.eval.php';
  325. }
  326. return smarty_function_eval(array('var' => $tplSource), $this->_tpl);
  327. }// end func _tplFetch
  328. /**
  329. * Sets the way required elements are rendered
  330. *
  331. * You can use {$label} or {$html} placeholders to let the renderer know where
  332. * where the element label or the element html are positionned according to the
  333. * required tag. They will be replaced accordingly with the right value. You
  334. * can use the full smarty syntax here, especially a custom modifier for I18N.
  335. * For example:
  336. * {if $required}<span style="color: red;">*</span>{/if}{$label|translate}
  337. * will put a red star in front of the label if the element is required and
  338. * translate the label.
  339. *
  340. *
  341. * @param string The required element template
  342. * @access public
  343. * @return void
  344. */
  345. function setRequiredTemplate($template)
  346. {
  347. $this->_required = $template;
  348. } // end func setRequiredTemplate
  349. /**
  350. * Sets the way elements with validation errors are rendered
  351. *
  352. * You can use {$label} or {$html} placeholders to let the renderer know where
  353. * where the element label or the element html are positionned according to the
  354. * error message. They will be replaced accordingly with the right value.
  355. * The error message will replace the {$error} placeholder.
  356. * For example:
  357. * {if $error}<span style="color: red;">{$error}</span>{/if}<br />{$html}
  358. * will put the error message in red on top of the element html.
  359. *
  360. * If you want all error messages to be output in the main error block, use
  361. * the {$form.errors} part of the rendered array that collects all raw error
  362. * messages.
  363. *
  364. * If you want to place all error messages manually, do not specify {$html}
  365. * nor {$label}.
  366. *
  367. * Groups can have special layouts. With this kind of groups, you have to
  368. * place the formated error message manually. In this case, use {$form.group.error}
  369. * where you want the formated error message to appear in the form.
  370. *
  371. * @param string The element error template
  372. * @access public
  373. * @return void
  374. */
  375. function setErrorTemplate($template)
  376. {
  377. $this->_error = $template;
  378. } // end func setErrorTemplate
  379. }
  380. ?>