ArraySmarty.php 14 KB

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