PageRenderTime 38ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/pear/HTML/QuickForm/Renderer/ArraySmarty.php

https://bitbucket.org/blackriver/openx
PHP | 402 lines | 134 code | 19 blank | 249 comment | 35 complexity | 1b64d6029b23f31e675495ac448446af MD5 | raw file
  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-2007 The PHP Group
  21. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  22. * @version CVS: $Id: ArraySmarty.php 22781 2008-07-16 16:52:20Z chris.nutting@openx.org $
  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.10
  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. * @access public
  129. */
  130. function HTML_QuickForm_Renderer_ArraySmarty(&$tpl, $staticLabels = false)
  131. {
  132. $this->HTML_QuickForm_Renderer_Array(true, $staticLabels);
  133. $this->_tpl =& $tpl;
  134. } // end constructor
  135. /**
  136. * Called when visiting a header element
  137. *
  138. * @param HTML_QuickForm_header header element being visited
  139. * @access public
  140. * @return void
  141. */
  142. function renderHeader(&$header)
  143. {
  144. if ($name = $header->getName()) {
  145. $this->_ary['header'][$name] = $header->toHtml();
  146. } else {
  147. $this->_ary['header'][$this->_sectionCount] = $header->toHtml();
  148. }
  149. $this->_currentSection = $this->_sectionCount++;
  150. } // end func renderHeader
  151. /**
  152. * Called when visiting a group, before processing any group elements
  153. *
  154. * @param HTML_QuickForm_group group being visited
  155. * @param bool Whether a group is required
  156. * @param string An error message associated with a group
  157. * @access public
  158. * @return void
  159. */
  160. function startGroup(&$group, $required, $error)
  161. {
  162. parent::startGroup($group, $required, $error);
  163. $this->_groupElementIdx = 1;
  164. } // end func startGroup
  165. /**
  166. * Creates an array representing an element containing
  167. * the key for storing this
  168. *
  169. * @access private
  170. * @param HTML_QuickForm_element form element being visited
  171. * @param bool Whether an element is required
  172. * @param string Error associated with the element
  173. * @return array
  174. */
  175. function _elementToArray(&$element, $required, $error)
  176. {
  177. $ret = parent::_elementToArray($element, $required, $error);
  178. if ('group' == $ret['type']) {
  179. $ret['html'] = $element->toHtml();
  180. // we don't need the elements, see the array structure
  181. unset($ret['elements']);
  182. }
  183. if (($required || $error) && !empty($this->_required)){
  184. $this->_renderRequired($ret['label'], $ret['html'], $required, $error);
  185. }
  186. if ($error && !empty($this->_error)) {
  187. $this->_renderError($ret['label'], $ret['html'], $error);
  188. $ret['error'] = $error;
  189. }
  190. // create keys for elements grouped by native group or name
  191. if (strstr($ret['name'], '[') or $this->_currentGroup) {
  192. // Fix for bug #8123: escape backslashes and quotes to prevent errors
  193. // in eval(). The code below seems to handle the case where element
  194. // name has unbalanced square brackets. Dunno whether we really
  195. // need this after the fix for #8123, but I'm wary of making big
  196. // changes to this code.
  197. preg_match('/([^]]*)\\[([^]]*)\\]/', $ret['name'], $matches);
  198. if (isset($matches[1])) {
  199. $sKeysSub = substr_replace($ret['name'], '', 0, strlen($matches[1]));
  200. $sKeysSub = str_replace(
  201. array('\\', '\'', '[' , ']', '[\'\']'),
  202. array('\\\\', '\\\'', '[\'', '\']', '[]' ),
  203. $sKeysSub
  204. );
  205. $sKeys = '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $matches[1]) . '\']' . $sKeysSub;
  206. } else {
  207. $sKeys = '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret['name']) . '\']';
  208. }
  209. // special handling for elements in native groups
  210. if ($this->_currentGroup) {
  211. // skip unnamed group items unless radios: no name -> no static access
  212. // identification: have the same key string as the parent group
  213. if ($this->_currentGroup['keys'] == $sKeys and 'radio' != $ret['type']) {
  214. return false;
  215. }
  216. // reduce string of keys by remove leading group keys
  217. if (0 === strpos($sKeys, $this->_currentGroup['keys'])) {
  218. $sKeys = substr_replace($sKeys, '', 0, strlen($this->_currentGroup['keys']));
  219. }
  220. }
  221. // element without a name
  222. } elseif ($ret['name'] == '') {
  223. $sKeys = '[\'element_' . $this->_elementIdx . '\']';
  224. // other elements
  225. } else {
  226. $sKeys = '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret['name']) . '\']';
  227. }
  228. // for radios: add extra key from value
  229. if ('radio' == $ret['type'] and substr($sKeys, -2) != '[]') {
  230. $sKeys .= '[\'' . str_replace(array('\\', '\''), array('\\\\', '\\\''), $ret['value']) . '\']';
  231. }
  232. $this->_elementIdx++;
  233. $ret['keys'] = $sKeys;
  234. return $ret;
  235. } // end func _elementToArray
  236. /**
  237. * Stores an array representation of an element in the form array
  238. *
  239. * @access private
  240. * @param array Array representation of an element
  241. * @return void
  242. */
  243. function _storeArray($elAry)
  244. {
  245. if ($elAry) {
  246. $sKeys = $elAry['keys'];
  247. unset($elAry['keys']);
  248. // where should we put this element...
  249. if (is_array($this->_currentGroup) && ('group' != $elAry['type'])) {
  250. $toEval = '$this->_currentGroup' . $sKeys . ' = $elAry;';
  251. } else {
  252. $toEval = '$this->_ary' . $sKeys . ' = $elAry;';
  253. }
  254. eval($toEval);
  255. }
  256. return;
  257. }
  258. /**
  259. * Called when an element is required
  260. *
  261. * This method will add the required tag to the element label and/or the element html
  262. * such as defined with the method setRequiredTemplate.
  263. *
  264. * @param string The element label
  265. * @param string The element html rendering
  266. * @param boolean The element required
  267. * @param string The element error
  268. * @see setRequiredTemplate()
  269. * @access private
  270. * @return void
  271. */
  272. function _renderRequired(&$label, &$html, &$required, &$error)
  273. {
  274. $this->_tpl->assign(array(
  275. 'label' => $label,
  276. 'html' => $html,
  277. 'required' => $required,
  278. 'error' => $error
  279. ));
  280. if (!empty($label) && strpos($this->_required, $this->_tpl->left_delimiter . '$label') !== false) {
  281. $label = $this->_tplFetch($this->_required);
  282. }
  283. if (!empty($html) && strpos($this->_required, $this->_tpl->left_delimiter . '$html') !== false) {
  284. $html = $this->_tplFetch($this->_required);
  285. }
  286. $this->_tpl->clear_assign(array('label', 'html', 'required'));
  287. } // end func _renderRequired
  288. /**
  289. * Called when an element has a validation error
  290. *
  291. * This method will add the error message to the element label or the element html
  292. * such as defined with the method setErrorTemplate. If the error placeholder is not found
  293. * in the template, the error will be displayed in the form error block.
  294. *
  295. * @param string The element label
  296. * @param string The element html rendering
  297. * @param string The element error
  298. * @see setErrorTemplate()
  299. * @access private
  300. * @return void
  301. */
  302. function _renderError(&$label, &$html, &$error)
  303. {
  304. $this->_tpl->assign(array('label' => '', 'html' => '', 'error' => $error));
  305. $error = $this->_tplFetch($this->_error);
  306. $this->_tpl->assign(array('label' => $label, 'html' => $html));
  307. if (!empty($label) && strpos($this->_error, $this->_tpl->left_delimiter . '$label') !== false) {
  308. $label = $this->_tplFetch($this->_error);
  309. } elseif (!empty($html) && strpos($this->_error, $this->_tpl->left_delimiter . '$html') !== false) {
  310. $html = $this->_tplFetch($this->_error);
  311. }
  312. $this->_tpl->clear_assign(array('label', 'html', 'error'));
  313. } // end func _renderError
  314. /**
  315. * Process an template sourced in a string with Smarty
  316. *
  317. * Smarty has no core function to render a template given as a string.
  318. * So we use the smarty eval plugin function to do this.
  319. *
  320. * @param string The template source
  321. * @access private
  322. * @return void
  323. */
  324. function _tplFetch($tplSource)
  325. {
  326. if (!function_exists('smarty_function_eval')) {
  327. require SMARTY_DIR . '/plugins/function.eval.php';
  328. }
  329. return smarty_function_eval(array('var' => $tplSource), $this->_tpl);
  330. }// end func _tplFetch
  331. /**
  332. * Sets the way required elements are rendered
  333. *
  334. * You can use {$label} or {$html} placeholders to let the renderer know where
  335. * where the element label or the element html are positionned according to the
  336. * required tag. They will be replaced accordingly with the right value. You
  337. * can use the full smarty syntax here, especially a custom modifier for I18N.
  338. * For example:
  339. * {if $required}<span style="color: red;">*</span>{/if}{$label|translate}
  340. * will put a red star in front of the label if the element is required and
  341. * translate the label.
  342. *
  343. *
  344. * @param string The required element template
  345. * @access public
  346. * @return void
  347. */
  348. function setRequiredTemplate($template)
  349. {
  350. $this->_required = $template;
  351. } // end func setRequiredTemplate
  352. /**
  353. * Sets the way elements with validation errors are rendered
  354. *
  355. * You can use {$label} or {$html} placeholders to let the renderer know where
  356. * where the element label or the element html are positionned according to the
  357. * error message. They will be replaced accordingly with the right value.
  358. * The error message will replace the {$error} placeholder.
  359. * For example:
  360. * {if $error}<span style="color: red;">{$error}</span>{/if}<br />{$html}
  361. * will put the error message in red on top of the element html.
  362. *
  363. * If you want all error messages to be output in the main error block, use
  364. * the {$form.errors} part of the rendered array that collects all raw error
  365. * messages.
  366. *
  367. * If you want to place all error messages manually, do not specify {$html}
  368. * nor {$label}.
  369. *
  370. * Groups can have special layouts. With this kind of groups, you have to
  371. * place the formated error message manually. In this case, use {$form.group.error}
  372. * where you want the formated error message to appear in the form.
  373. *
  374. * @param string The element error template
  375. * @access public
  376. * @return void
  377. */
  378. function setErrorTemplate($template)
  379. {
  380. $this->_error = $template;
  381. } // end func setErrorTemplate
  382. }
  383. ?>