ITStatic.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * A static renderer for HTML_QuickForm compatible
  5. * with HTML_Template_IT and HTML_Template_Sigma.
  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 Bertrand Mansion <bmansion@mamasam.com>
  18. * @copyright 2001-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version CVS: $Id: ITStatic.php,v 1.9 2009/04/04 21:34:04 avb Exp $
  21. * @link http://pear.php.net/package/HTML_QuickForm
  22. */
  23. /**
  24. * A static renderer for HTML_QuickForm compatible
  25. * with HTML_Template_IT and HTML_Template_Sigma.
  26. *
  27. * As opposed to the dynamic renderer, this renderer needs
  28. * every elements and labels in the form to be specified by
  29. * placeholders at the position you want them to be displayed.
  30. *
  31. * @category HTML
  32. * @package HTML_QuickForm
  33. * @author Bertrand Mansion <bmansion@mamasam.com>
  34. * @version Release: 3.2.11
  35. * @since 3.0
  36. */
  37. class HTML_QuickForm_Renderer_ITStatic extends HTML_QuickForm_Renderer
  38. {
  39. /**#@+
  40. * @access private
  41. */
  42. /**
  43. * An HTML_Template_IT or some other API compatible Template instance
  44. * @var object
  45. */
  46. var $_tpl = null;
  47. /**
  48. * Rendered form name
  49. * @var string
  50. */
  51. var $_formName = 'form';
  52. /**
  53. * The errors that were not shown near concrete fields go here
  54. * @var array
  55. */
  56. var $_errors = array();
  57. /**
  58. * Show the block with required note?
  59. * @var bool
  60. */
  61. var $_showRequired = false;
  62. /**
  63. * Which group are we currently parsing ?
  64. * @var string
  65. */
  66. var $_inGroup;
  67. /**
  68. * Index of the element in its group
  69. * @var int
  70. */
  71. var $_elementIndex = 0;
  72. /**
  73. * If elements have been added with the same name
  74. * @var array
  75. */
  76. var $_duplicateElements = array();
  77. /**
  78. * How to handle the required tag for required fields
  79. * @var string
  80. */
  81. var $_required = '{label}<font size="1" color="red">*</font>';
  82. /**
  83. * How to handle error messages in form validation
  84. * @var string
  85. */
  86. var $_error = '<font color="red">{error}</font><br />{html}';
  87. /**
  88. * Collected HTML for hidden elements, if needed
  89. * @var string
  90. */
  91. var $_hidden = '';
  92. /**#@-*/
  93. /**
  94. * Constructor
  95. *
  96. * @param HTML_Template_IT|HTML_Template_Sigma Template object to use
  97. */
  98. function HTML_QuickForm_Renderer_ITStatic(&$tpl)
  99. {
  100. $this->HTML_QuickForm_Renderer();
  101. $this->_tpl =& $tpl;
  102. } // end constructor
  103. /**
  104. * Called when visiting a form, before processing any form elements
  105. *
  106. * @param HTML_QuickForm form object being visited
  107. * @access public
  108. * @return void
  109. */
  110. function startForm(&$form)
  111. {
  112. $this->_formName = $form->getAttribute('id');
  113. if (count($form->_duplicateIndex) > 0) {
  114. // Take care of duplicate elements
  115. foreach ($form->_duplicateIndex as $elementName => $indexes) {
  116. $this->_duplicateElements[$elementName] = 0;
  117. }
  118. }
  119. } // end func startForm
  120. /**
  121. * Called when visiting a form, after processing all form elements
  122. *
  123. * @param HTML_QuickForm form object being visited
  124. * @access public
  125. * @return void
  126. */
  127. function finishForm(&$form)
  128. {
  129. // display errors above form
  130. if (!empty($this->_errors) && $this->_tpl->blockExists($this->_formName.'_error_loop')) {
  131. foreach ($this->_errors as $error) {
  132. $this->_tpl->setVariable($this->_formName.'_error', $error);
  133. $this->_tpl->parse($this->_formName.'_error_loop');
  134. }
  135. }
  136. // show required note
  137. if ($this->_showRequired) {
  138. $this->_tpl->setVariable($this->_formName.'_required_note', $form->getRequiredNote());
  139. }
  140. // add hidden elements, if collected
  141. if (!empty($this->_hidden)) {
  142. $this->_tpl->setVariable($this->_formName . '_hidden', $this->_hidden);
  143. }
  144. // assign form attributes
  145. $this->_tpl->setVariable($this->_formName.'_attributes', $form->getAttributes(true));
  146. // assign javascript validation rules
  147. $this->_tpl->setVariable($this->_formName.'_javascript', $form->getValidationScript());
  148. } // end func finishForm
  149. /**
  150. * Called when visiting a header element
  151. *
  152. * @param HTML_QuickForm_header header element being visited
  153. * @access public
  154. * @return void
  155. */
  156. function renderHeader(&$header)
  157. {
  158. $name = $header->getName();
  159. $varName = $this->_formName.'_header';
  160. // Find placeHolder
  161. if (!empty($name) && $this->_tpl->placeHolderExists($this->_formName.'_header_'.$name)) {
  162. $varName = $this->_formName.'_header_'.$name;
  163. }
  164. $this->_tpl->setVariable($varName, $header->toHtml());
  165. } // end func renderHeader
  166. /**
  167. * Called when visiting an element
  168. *
  169. * @param HTML_QuickForm_element form element being visited
  170. * @param bool Whether an element is required
  171. * @param string An error message associated with an element
  172. * @access public
  173. * @return void
  174. */
  175. function renderElement(&$element, $required, $error)
  176. {
  177. $name = $element->getName();
  178. // are we inside a group?
  179. if (!empty($this->_inGroup)) {
  180. $varName = $this->_formName.'_'.str_replace(array('[', ']'), '_', $name);
  181. if (substr($varName, -2) == '__') {
  182. // element name is of type : group[]
  183. $varName = $this->_inGroup.'_'.$this->_elementIndex.'_';
  184. $this->_elementIndex++;
  185. }
  186. if ($varName != $this->_inGroup) {
  187. $varName .= '_' == substr($varName, -1)? '': '_';
  188. // element name is of type : group[name]
  189. $label = $element->getLabel();
  190. $html = $element->toHtml();
  191. if ($required && !$element->isFrozen()) {
  192. $this->_renderRequired($label, $html);
  193. $this->_showRequired = true;
  194. }
  195. if (!empty($label)) {
  196. if (is_array($label)) {
  197. foreach ($label as $key => $value) {
  198. $this->_tpl->setVariable($varName.'label_'.$key, $value);
  199. }
  200. } else {
  201. $this->_tpl->setVariable($varName.'label', $label);
  202. }
  203. }
  204. $this->_tpl->setVariable($varName.'html', $html);
  205. }
  206. } else {
  207. $name = str_replace(array('[', ']'), array('_', ''), $name);
  208. if (isset($this->_duplicateElements[$name])) {
  209. // Element is a duplicate
  210. $varName = $this->_formName.'_'.$name.'_'.$this->_duplicateElements[$name];
  211. $this->_duplicateElements[$name]++;
  212. } else {
  213. $varName = $this->_formName.'_'.$name;
  214. }
  215. $label = $element->getLabel();
  216. $html = $element->toHtml();
  217. if ($required) {
  218. $this->_showRequired = true;
  219. $this->_renderRequired($label, $html);
  220. }
  221. if (!empty($error)) {
  222. $this->_renderError($label, $html, $error);
  223. }
  224. if (is_array($label)) {
  225. foreach ($label as $key => $value) {
  226. $this->_tpl->setVariable($varName.'_label_'.$key, $value);
  227. }
  228. } else {
  229. $this->_tpl->setVariable($varName.'_label', $label);
  230. }
  231. $this->_tpl->setVariable($varName.'_html', $html);
  232. }
  233. } // end func renderElement
  234. /**
  235. * Called when visiting a hidden element
  236. *
  237. * @param HTML_QuickForm_element hidden element being visited
  238. * @access public
  239. * @return void
  240. */
  241. function renderHidden(&$element)
  242. {
  243. if ($this->_tpl->placeholderExists($this->_formName . '_hidden')) {
  244. $this->_hidden .= $element->toHtml();
  245. } else {
  246. $name = $element->getName();
  247. $name = str_replace(array('[', ']'), array('_', ''), $name);
  248. $this->_tpl->setVariable($this->_formName.'_'.$name.'_html', $element->toHtml());
  249. }
  250. } // end func renderHidden
  251. /**
  252. * Called when visiting a group, before processing any group elements
  253. *
  254. * @param HTML_QuickForm_group group being visited
  255. * @param bool Whether a group is required
  256. * @param string An error message associated with a group
  257. * @access public
  258. * @return void
  259. */
  260. function startGroup(&$group, $required, $error)
  261. {
  262. $name = $group->getName();
  263. $varName = $this->_formName.'_'.$name;
  264. $this->_elementIndex = 0;
  265. $html = $this->_tpl->placeholderExists($varName.'_html') ? $group->toHtml() : '';
  266. $label = $group->getLabel();
  267. if ($required) {
  268. $this->_renderRequired($label, $html);
  269. }
  270. if (!empty($error)) {
  271. $this->_renderError($label, $html, $error);
  272. }
  273. if (!empty($html)) {
  274. $this->_tpl->setVariable($varName.'_html', $html);
  275. } else {
  276. // Uses error blocks to set the special groups layout error
  277. // <!-- BEGIN form_group_error -->{form_group_error}<!-- END form_group_error -->
  278. if (!empty($error)) {
  279. if ($this->_tpl->placeholderExists($varName.'_error')) {
  280. if ($this->_tpl->blockExists($this->_formName . '_error_block')) {
  281. $this->_tpl->setVariable($this->_formName . '_error', $error);
  282. $error = $this->_getTplBlock($this->_formName . '_error_block');
  283. } elseif (strpos($this->_error, '{html}') !== false || strpos($this->_error, '{label}') !== false) {
  284. $error = str_replace('{error}', $error, $this->_error);
  285. }
  286. }
  287. $this->_tpl->setVariable($varName . '_error', $error);
  288. array_pop($this->_errors);
  289. }
  290. }
  291. if (is_array($label)) {
  292. foreach ($label as $key => $value) {
  293. $this->_tpl->setVariable($varName.'_label_'.$key, $value);
  294. }
  295. } else {
  296. $this->_tpl->setVariable($varName.'_label', $label);
  297. }
  298. $this->_inGroup = $varName;
  299. } // end func startGroup
  300. /**
  301. * Called when visiting a group, after processing all group elements
  302. *
  303. * @param HTML_QuickForm_group group being visited
  304. * @access public
  305. * @return void
  306. */
  307. function finishGroup(&$group)
  308. {
  309. $this->_inGroup = '';
  310. } // end func finishGroup
  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.
  317. * For example:
  318. * <font color="red">*</font>{label}
  319. * will put a red star in front of the label if the element is required.
  320. *
  321. * @param string The required element template
  322. * @access public
  323. * @return void
  324. */
  325. function setRequiredTemplate($template)
  326. {
  327. $this->_required = $template;
  328. } // end func setRequiredTemplate
  329. /**
  330. * Sets the way elements with validation errors are rendered
  331. *
  332. * You can use {label} or {html} placeholders to let the renderer know where
  333. * where the element label or the element html are positionned according to the
  334. * error message. They will be replaced accordingly with the right value.
  335. * The error message will replace the {error} place holder.
  336. * For example:
  337. * <font color="red">{error}</font><br />{html}
  338. * will put the error message in red on top of the element html.
  339. *
  340. * If you want all error messages to be output in the main error block, do not specify
  341. * {html} nor {label}.
  342. *
  343. * Groups can have special layouts. With this kind of groups, the renderer will need
  344. * to know where to place the error message. In this case, use error blocks like:
  345. * <!-- BEGIN form_group_error -->{form_group_error}<!-- END form_group_error -->
  346. * where you want the error message to appear in the form.
  347. *
  348. * @param string The element error template
  349. * @access public
  350. * @return void
  351. */
  352. function setErrorTemplate($template)
  353. {
  354. $this->_error = $template;
  355. } // end func setErrorTemplate
  356. /**
  357. * Called when an element is required
  358. *
  359. * This method will add the required tag to the element label and/or the element html
  360. * such as defined with the method setRequiredTemplate
  361. *
  362. * @param string The element label
  363. * @param string The element html rendering
  364. * @see setRequiredTemplate()
  365. * @access private
  366. * @return void
  367. */
  368. function _renderRequired(&$label, &$html)
  369. {
  370. if ($this->_tpl->blockExists($tplBlock = $this->_formName . '_required_block')) {
  371. if (!empty($label) && $this->_tpl->placeholderExists($this->_formName . '_label', $tplBlock)) {
  372. $this->_tpl->setVariable($this->_formName . '_label', is_array($label)? $label[0]: $label);
  373. if (is_array($label)) {
  374. $label[0] = $this->_getTplBlock($tplBlock);
  375. } else {
  376. $label = $this->_getTplBlock($tplBlock);
  377. }
  378. }
  379. if (!empty($html) && $this->_tpl->placeholderExists($this->_formName . '_html', $tplBlock)) {
  380. $this->_tpl->setVariable($this->_formName . '_html', $html);
  381. $html = $this->_getTplBlock($tplBlock);
  382. }
  383. } else {
  384. if (!empty($label) && strpos($this->_required, '{label}') !== false) {
  385. if (is_array($label)) {
  386. $label[0] = str_replace('{label}', $label[0], $this->_required);
  387. } else {
  388. $label = str_replace('{label}', $label, $this->_required);
  389. }
  390. }
  391. if (!empty($html) && strpos($this->_required, '{html}') !== false) {
  392. $html = str_replace('{html}', $html, $this->_required);
  393. }
  394. }
  395. } // end func _renderRequired
  396. /**
  397. * Called when an element has a validation error
  398. *
  399. * This method will add the error message to the element label or the element html
  400. * such as defined with the method setErrorTemplate. If the error placeholder is not found
  401. * in the template, the error will be displayed in the form error block.
  402. *
  403. * @param string The element label
  404. * @param string The element html rendering
  405. * @param string The element error
  406. * @see setErrorTemplate()
  407. * @access private
  408. * @return void
  409. */
  410. function _renderError(&$label, &$html, $error)
  411. {
  412. if ($this->_tpl->blockExists($tplBlock = $this->_formName . '_error_block')) {
  413. $this->_tpl->setVariable($this->_formName . '_error', $error);
  414. if (!empty($label) && $this->_tpl->placeholderExists($this->_formName . '_label', $tplBlock)) {
  415. $this->_tpl->setVariable($this->_formName . '_label', is_array($label)? $label[0]: $label);
  416. if (is_array($label)) {
  417. $label[0] = $this->_getTplBlock($tplBlock);
  418. } else {
  419. $label = $this->_getTplBlock($tplBlock);
  420. }
  421. } elseif (!empty($html) && $this->_tpl->placeholderExists($this->_formName . '_html', $tplBlock)) {
  422. $this->_tpl->setVariable($this->_formName . '_html', $html);
  423. $html = $this->_getTplBlock($tplBlock);
  424. }
  425. // clean up after ourselves
  426. $this->_tpl->setVariable($this->_formName . '_error', null);
  427. } elseif (!empty($label) && strpos($this->_error, '{label}') !== false) {
  428. if (is_array($label)) {
  429. $label[0] = str_replace(array('{label}', '{error}'), array($label[0], $error), $this->_error);
  430. } else {
  431. $label = str_replace(array('{label}', '{error}'), array($label, $error), $this->_error);
  432. }
  433. } elseif (!empty($html) && strpos($this->_error, '{html}') !== false) {
  434. $html = str_replace(array('{html}', '{error}'), array($html, $error), $this->_error);
  435. } else {
  436. $this->_errors[] = $error;
  437. }
  438. }// end func _renderError
  439. /**
  440. * Returns the block's contents
  441. *
  442. * The method is needed because ITX and Sigma implement clearing
  443. * the block contents on get() a bit differently
  444. *
  445. * @param string Block name
  446. * @return string Block contents
  447. */
  448. function _getTplBlock($block)
  449. {
  450. $this->_tpl->parse($block);
  451. if (is_a($this->_tpl, 'html_template_sigma')) {
  452. $ret = $this->_tpl->get($block, true);
  453. } else {
  454. $oldClear = $this->_tpl->clearCache;
  455. $this->_tpl->clearCache = true;
  456. $ret = $this->_tpl->get($block);
  457. $this->_tpl->clearCache = $oldClear;
  458. }
  459. return $ret;
  460. }
  461. } // end class HTML_QuickForm_Renderer_ITStatic
  462. ?>