ArraySmarty.php 15 KB

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