Default.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. // | Adam Daniel <adaniel1@eesus.jnj.com> |
  18. // | Bertrand Mansion <bmansion@mamasam.com> |
  19. // +----------------------------------------------------------------------+
  20. //
  21. // $Id: Default.php 9612 2006-10-20 11:56:44Z bmol $
  22. require_once('HTML/QuickForm/Renderer.php');
  23. /**
  24. * A concrete renderer for HTML_QuickForm,
  25. * based on QuickForm 2.x built-in one
  26. *
  27. * @access public
  28. */
  29. class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer
  30. {
  31. /**
  32. * The HTML of the form
  33. * @var string
  34. * @access private
  35. */
  36. var $_html;
  37. /**
  38. * Header Template string
  39. * @var string
  40. * @access private
  41. */
  42. var $_headerTemplate =
  43. "\n\t<tr>\n\t\t<td style=\"white-space: nowrap; background-color: #CCCCCC;\" align=\"left\" valign=\"top\" colspan=\"2\"><b>{header}</b></td>\n\t</tr>";
  44. /**
  45. * Element template string
  46. * @var string
  47. * @access private
  48. */
  49. var $_elementTemplate =
  50. "\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><span style=\"color: #ff0000\">*</span><!-- END required --><b>{label}</b></td>\n\t\t<td valign=\"top\" align=\"left\"><!-- BEGIN error --><span style=\"color: #ff0000\">{error}</span><br /><!-- END error -->\t{element}</td>\n\t</tr>";
  51. /**
  52. * Form template string
  53. * @var string
  54. * @access private
  55. */
  56. var $_formTemplate =
  57. "\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>";
  58. /**
  59. * Required Note template string
  60. * @var string
  61. * @access private
  62. */
  63. var $_requiredNoteTemplate =
  64. "\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>";
  65. /**
  66. * Array containing the templates for customised elements
  67. * @var array
  68. * @access private
  69. */
  70. var $_templates = array();
  71. /**
  72. * Array containing the templates for group wraps.
  73. *
  74. * These templates are wrapped around group elements and groups' own
  75. * templates wrap around them. This is set by setGroupTemplate().
  76. *
  77. * @var array
  78. * @access private
  79. */
  80. var $_groupWraps = array();
  81. /**
  82. * Array containing the templates for elements within groups
  83. * @var array
  84. * @access private
  85. */
  86. var $_groupTemplates = array();
  87. /**
  88. * True if we are inside a group
  89. * @var bool
  90. * @access private
  91. */
  92. var $_inGroup = false;
  93. /**
  94. * Array with HTML generated for group elements
  95. * @var array
  96. * @access private
  97. */
  98. var $_groupElements = array();
  99. /**
  100. * Template for an element inside a group
  101. * @var string
  102. * @access private
  103. */
  104. var $_groupElementTemplate = '';
  105. /**
  106. * HTML that wraps around the group elements
  107. * @var string
  108. * @access private
  109. */
  110. var $_groupWrap = '';
  111. /**
  112. * HTML for the current group
  113. * @var string
  114. * @access private
  115. */
  116. var $_groupTemplate = '';
  117. /**
  118. * Collected HTML of the hidden fields
  119. * @var string
  120. * @access private
  121. */
  122. var $_hiddenHtml = '';
  123. /**
  124. * Constructor
  125. *
  126. * @access public
  127. */
  128. function HTML_QuickForm_Renderer_Default()
  129. {
  130. $this->HTML_QuickForm_Renderer();
  131. } // end constructor
  132. /**
  133. * returns the HTML generated for the form
  134. *
  135. * @access public
  136. * @return string
  137. */
  138. function toHtml()
  139. {
  140. // _hiddenHtml is cleared in finishForm(), so this only matters when
  141. // finishForm() was not called (e.g. group::toHtml(), bug #3511)
  142. return $this->_hiddenHtml . $this->_html;
  143. } // end func toHtml
  144. /**
  145. * Called when visiting a form, before processing any form elements
  146. *
  147. * @param object An HTML_QuickForm object being visited
  148. * @access public
  149. * @return void
  150. */
  151. function startForm(&$form)
  152. {
  153. $this->_html = '';
  154. $this->_hiddenHtml = '';
  155. } // end func startForm
  156. /**
  157. * Called when visiting a form, after processing all form elements
  158. * Adds required note, form attributes, validation javascript and form content.
  159. *
  160. * @param object An HTML_QuickForm object being visited
  161. * @access public
  162. * @return void
  163. */
  164. function finishForm(&$form)
  165. {
  166. // add a required note, if one is needed
  167. if (!empty($form->_required) && !$form->_freezeAll) {
  168. $this->_html .= str_replace('{requiredNote}', $form->getRequiredNote(), $this->_requiredNoteTemplate);
  169. }
  170. // add form attributes and content
  171. $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
  172. if (strpos($this->_formTemplate, '{hidden}')) {
  173. $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
  174. } else {
  175. $this->_html .= $this->_hiddenHtml;
  176. }
  177. $this->_hiddenHtml = '';
  178. $this->_html = str_replace('{content}', $this->_html, $html);
  179. // add a validation script
  180. if ('' != ($script = $form->getValidationScript())) {
  181. $this->_html = $script . "\n" . $this->_html;
  182. }
  183. } // end func finishForm
  184. /**
  185. * Called when visiting a header element
  186. *
  187. * @param object An HTML_QuickForm_header element being visited
  188. * @access public
  189. * @return void
  190. */
  191. function renderHeader(&$header)
  192. {
  193. $name = $header->getName();
  194. if (!empty($name) && isset($this->_templates[$name])) {
  195. $this->_html .= str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
  196. } else {
  197. $this->_html .= str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
  198. }
  199. } // end func renderHeader
  200. /**
  201. * Helper method for renderElement
  202. *
  203. * @param string Element name
  204. * @param mixed Element label (if using an array of labels, you should set the appropriate template)
  205. * @param bool Whether an element is required
  206. * @param string Error message associated with the element
  207. * @access private
  208. * @see renderElement()
  209. * @return string Html for element
  210. */
  211. function _prepareTemplate($name, $label, $required, $error)
  212. {
  213. if (is_array($label)) {
  214. $nameLabel = array_shift($label);
  215. } else {
  216. $nameLabel = $label;
  217. }
  218. if (isset($this->_templates[$name])) {
  219. $html = str_replace('{label}', $nameLabel, $this->_templates[$name]);
  220. } else {
  221. $html = str_replace('{label}', $nameLabel, $this->_elementTemplate);
  222. }
  223. if ($required) {
  224. $html = str_replace('<!-- BEGIN required -->', '', $html);
  225. $html = str_replace('<!-- END required -->', '', $html);
  226. } else {
  227. $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/iU", '', $html);
  228. }
  229. if (isset($error)) {
  230. $html = str_replace('{error}', $error, $html);
  231. $html = str_replace('<!-- BEGIN error -->', '', $html);
  232. $html = str_replace('<!-- END error -->', '', $html);
  233. } else {
  234. $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/iU", '', $html);
  235. }
  236. if (is_array($label)) {
  237. foreach($label as $key => $text) {
  238. $key = is_int($key)? $key + 2: $key;
  239. $html = str_replace("{label_{$key}}", $text, $html);
  240. $html = str_replace("<!-- BEGIN label_{$key} -->", '', $html);
  241. $html = str_replace("<!-- END label_{$key} -->", '', $html);
  242. }
  243. }
  244. if (strpos($html, '{label_')) {
  245. $html = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/i', '', $html);
  246. }
  247. return $html;
  248. } // end func _prepareTemplate
  249. /**
  250. * Renders an element Html
  251. * Called when visiting an element
  252. *
  253. * @param object An HTML_QuickForm_element object being visited
  254. * @param bool Whether an element is required
  255. * @param string An error message associated with an element
  256. * @access public
  257. * @return void
  258. */
  259. function renderElement(&$element, $required, $error)
  260. {
  261. if (!$this->_inGroup) {
  262. $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
  263. $this->_html .= str_replace('{element}', $element->toHtml(), $html);
  264. } elseif (!empty($this->_groupElementTemplate)) {
  265. $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
  266. if ($required) {
  267. $html = str_replace('<!-- BEGIN required -->', '', $html);
  268. $html = str_replace('<!-- END required -->', '', $html);
  269. } else {
  270. $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->(\s|\S)*<!-- END required -->([ \t\n\r]*)?/iU", '', $html);
  271. }
  272. $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
  273. } else {
  274. $this->_groupElements[] = $element->toHtml();
  275. }
  276. } // end func renderElement
  277. /**
  278. * Renders an hidden element
  279. * Called when visiting a hidden element
  280. *
  281. * @param object An HTML_QuickForm_hidden object being visited
  282. * @access public
  283. * @return void
  284. */
  285. function renderHidden(&$element)
  286. {
  287. $this->_hiddenHtml .= $element->toHtml() . "\n";
  288. } // end func renderHidden
  289. /**
  290. * Called when visiting a raw HTML/text pseudo-element
  291. *
  292. * @param object An HTML_QuickForm_html element being visited
  293. * @access public
  294. * @return void
  295. */
  296. function renderHtml(&$data)
  297. {
  298. $this->_html .= $data->toHtml();
  299. } // end func renderHtml
  300. /**
  301. * Called when visiting a group, before processing any group elements
  302. *
  303. * @param object An HTML_QuickForm_group object being visited
  304. * @param bool Whether a group is required
  305. * @param string An error message associated with a group
  306. * @access public
  307. * @return void
  308. */
  309. function startGroup(&$group, $required, $error)
  310. {
  311. $name = $group->getName();
  312. $this->_groupTemplate = $this->_prepareTemplate($name, $group->getLabel(), $required, $error);
  313. $this->_groupElementTemplate = empty($this->_groupTemplates[$name])? '': $this->_groupTemplates[$name];
  314. $this->_groupWrap = empty($this->_groupWraps[$name])? '': $this->_groupWraps[$name];
  315. $this->_groupElements = array();
  316. $this->_inGroup = true;
  317. } // end func startGroup
  318. /**
  319. * Called when visiting a group, after processing all group elements
  320. *
  321. * @param object An HTML_QuickForm_group object being visited
  322. * @access public
  323. * @return void
  324. */
  325. function finishGroup(&$group)
  326. {
  327. $separator = $group->_separator;
  328. if (is_array($separator)) {
  329. $count = count($separator);
  330. $html = '';
  331. for ($i = 0; $i < count($this->_groupElements); $i++) {
  332. $html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i];
  333. }
  334. } else {
  335. if (is_null($separator)) {
  336. $separator = '&nbsp;';
  337. }
  338. $html = implode((string)$separator, $this->_groupElements);
  339. }
  340. if (!empty($this->_groupWrap)) {
  341. $html = str_replace('{content}', $html, $this->_groupWrap);
  342. }
  343. $this->_html .= str_replace('{element}', $html, $this->_groupTemplate);
  344. $this->_inGroup = false;
  345. } // end func finishGroup
  346. /**
  347. * Sets element template
  348. *
  349. * @param string The HTML surrounding an element
  350. * @param string (optional) Name of the element to apply template for
  351. * @access public
  352. * @return void
  353. */
  354. function setElementTemplate($html, $element = null)
  355. {
  356. if (is_null($element)) {
  357. $this->_elementTemplate = $html;
  358. } else {
  359. $this->_templates[$element] = $html;
  360. }
  361. } // end func setElementTemplate
  362. /**
  363. * Sets template for a group wrapper
  364. *
  365. * This template is contained within a group-as-element template
  366. * set via setTemplate() and contains group's element templates, set
  367. * via setGroupElementTemplate()
  368. *
  369. * @param string The HTML surrounding group elements
  370. * @param string Name of the group to apply template for
  371. * @access public
  372. * @return void
  373. */
  374. function setGroupTemplate($html, $group)
  375. {
  376. $this->_groupWraps[$group] = $html;
  377. } // end func setGroupTemplate
  378. /**
  379. * Sets element template for elements within a group
  380. *
  381. * @param string The HTML surrounding an element
  382. * @param string Name of the group to apply template for
  383. * @access public
  384. * @return void
  385. */
  386. function setGroupElementTemplate($html, $group)
  387. {
  388. $this->_groupTemplates[$group] = $html;
  389. } // end func setGroupElementTemplate
  390. /**
  391. * Sets header template
  392. *
  393. * @param string The HTML surrounding the header
  394. * @access public
  395. * @return void
  396. */
  397. function setHeaderTemplate($html)
  398. {
  399. $this->_headerTemplate = $html;
  400. } // end func setHeaderTemplate
  401. /**
  402. * Sets form template
  403. *
  404. * @param string The HTML surrounding the form tags
  405. * @access public
  406. * @return void
  407. */
  408. function setFormTemplate($html)
  409. {
  410. $this->_formTemplate = $html;
  411. } // end func setFormTemplate
  412. /**
  413. * Sets the note indicating required fields template
  414. *
  415. * @param string The HTML surrounding the required note
  416. * @access public
  417. * @return void
  418. */
  419. function setRequiredNoteTemplate($html)
  420. {
  421. $this->_requiredNoteTemplate = $html;
  422. } // end func setRequiredNoteTemplate
  423. /**
  424. * Clears all the HTML out of the templates that surround notes, elements, etc.
  425. * Useful when you want to use addData() to create a completely custom form look
  426. *
  427. * @access public
  428. * @return void
  429. */
  430. function clearAllTemplates()
  431. {
  432. $this->setElementTemplate('{element}');
  433. $this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n");
  434. $this->setRequiredNoteTemplate('');
  435. $this->_templates = array();
  436. } // end func clearAllTemplates
  437. } // end class HTML_QuickForm_Renderer_Default
  438. ?>