Default.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * A concrete renderer for HTML_QuickForm, based on QuickForm 2.x built-in one
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Alexey Borzov <avb@php.net>
  17. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  18. * @author Bertrand Mansion <bmansion@mamasam.com>
  19. * @copyright 2001-2009 The PHP Group
  20. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  21. * @version CVS: $Id$
  22. * @link http://pear.php.net/package/HTML_QuickForm
  23. */
  24. /**
  25. * A concrete renderer for HTML_QuickForm, based on QuickForm 2.x built-in one
  26. *
  27. * @category HTML
  28. * @package HTML_QuickForm
  29. * @author Alexey Borzov <avb@php.net>
  30. * @author Adam Daniel <adaniel1@eesus.jnj.com>
  31. * @author Bertrand Mansion <bmansion@mamasam.com>
  32. * @version Release: 3.2.11
  33. * @since 3.0
  34. */
  35. class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer
  36. {
  37. private $form;
  38. private $customElementTemplate;
  39. /**
  40. * @return mixed
  41. */
  42. public function getCustomElementTemplate()
  43. {
  44. return $this->customElementTemplate;
  45. }
  46. /**
  47. * This template will be taken instead of the default templates by element
  48. * @param string $customElementTemplate
  49. */
  50. public function setCustomElementTemplate($customElementTemplate)
  51. {
  52. $this->customElementTemplate = $customElementTemplate;
  53. }
  54. /**
  55. * The HTML of the form
  56. * @var string
  57. * @access private
  58. */
  59. var $_html;
  60. /**
  61. * Header Template string
  62. * @var string
  63. * @access private
  64. */
  65. var $_headerTemplate =
  66. "\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>";
  67. /**
  68. * Element template string
  69. * @var string
  70. * @access private
  71. */
  72. var $_elementTemplate =
  73. "\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>";
  74. /**
  75. * Form template string
  76. * @var string
  77. * @access private
  78. */
  79. var $_formTemplate =
  80. "\n<form{attributes}>\n<div>\n{hidden}<table border=\"0\">\n{content}\n</table>\n</div>\n</form>";
  81. /**
  82. * Required Note template string
  83. * @var string
  84. * @access private
  85. */
  86. var $_requiredNoteTemplate =
  87. "\n\t<tr>\n\t\t<td></td>\n\t<td align=\"left\" valign=\"top\">{requiredNote}</td>\n\t</tr>";
  88. /**
  89. * Array containing the templates for customised elements
  90. * @var array
  91. * @access private
  92. */
  93. var $_templates = array();
  94. /**
  95. * Array containing the templates for group wraps.
  96. *
  97. * These templates are wrapped around group elements and groups' own
  98. * templates wrap around them. This is set by setGroupTemplate().
  99. *
  100. * @var array
  101. * @access private
  102. */
  103. var $_groupWraps = array();
  104. /**
  105. * Array containing the templates for elements within groups
  106. * @var array
  107. * @access private
  108. */
  109. var $_groupTemplates = array();
  110. /**
  111. * True if we are inside a group
  112. * @var bool
  113. * @access private
  114. */
  115. var $_inGroup = false;
  116. /**
  117. * Array with HTML generated for group elements
  118. * @var array
  119. * @access private
  120. */
  121. var $_groupElements = array();
  122. /**
  123. * Template for an element inside a group
  124. * @var string
  125. * @access private
  126. */
  127. var $_groupElementTemplate = '';
  128. /**
  129. * HTML that wraps around the group elements
  130. * @var string
  131. * @access private
  132. */
  133. var $_groupWrap = '';
  134. /**
  135. * HTML for the current group
  136. * @var string
  137. * @access private
  138. */
  139. var $_groupTemplate = '';
  140. /**
  141. * Collected HTML of the hidden fields
  142. * @var string
  143. * @access private
  144. */
  145. var $_hiddenHtml = '';
  146. /**
  147. * Constructor
  148. *
  149. * @access public
  150. */
  151. public function HTML_QuickForm_Renderer_Default()
  152. {
  153. parent::__construct();
  154. } // end constructor
  155. /**
  156. * returns the HTML generated for the form
  157. *
  158. * @access public
  159. * @return string
  160. */
  161. public function toHtml()
  162. {
  163. // _hiddenHtml is cleared in finishForm(), so this only matters when
  164. // finishForm() was not called (e.g. group::toHtml(), bug #3511)
  165. return $this->_hiddenHtml . $this->_html;
  166. } // end func toHtml
  167. /**
  168. * Called when visiting a form, before processing any form elements
  169. *
  170. * @param HTML_QuickForm form object being visited
  171. * @access public
  172. * @return void
  173. */
  174. function startForm(&$form)
  175. {
  176. $this->setForm($form);
  177. $this->_html = '';
  178. $this->_hiddenHtml = '';
  179. }
  180. /**
  181. * @return FormValidator
  182. */
  183. public function getForm()
  184. {
  185. return $this->form;
  186. }
  187. /**
  188. * @param mixed $form
  189. */
  190. public function setForm($form)
  191. {
  192. $this->form = $form;
  193. } // end func startForm
  194. /**
  195. * Called when visiting a form, after processing all form elements
  196. * Adds required note, form attributes, validation javascript and form content.
  197. *
  198. * @param HTML_QuickForm form object being visited
  199. * @access public
  200. * @return void
  201. */
  202. public function finishForm(&$form)
  203. {
  204. // add a required note, if one is needed
  205. if (!empty($form->_required) && !$form->_freezeAll) {
  206. $this->_html .= str_replace('{requiredNote}', $form->getRequiredNote(), $this->_requiredNoteTemplate);
  207. }
  208. // add form attributes and content
  209. $html = str_replace('{attributes}', $form->getAttributes(true), $this->_formTemplate);
  210. if (strpos($this->_formTemplate, '{hidden}')) {
  211. $html = str_replace('{hidden}', $this->_hiddenHtml, $html);
  212. } else {
  213. $this->_html .= $this->_hiddenHtml;
  214. }
  215. $this->_hiddenHtml = '';
  216. $this->_html = str_replace('{content}', $this->_html, $html);
  217. // add a validation script
  218. if ('' != ($script = $form->getValidationScript())) {
  219. $this->_html = $script . "\n" . $this->_html;
  220. }
  221. } // end func finishForm
  222. /**
  223. * Called when visiting a header element
  224. *
  225. * @param HTML_QuickForm_header header element being visited
  226. * @access public
  227. * @return void
  228. */
  229. function renderHeader(&$header)
  230. {
  231. $name = $header->getName();
  232. if (!empty($name) && isset($this->_templates[$name])) {
  233. $this->_html .= str_replace('{header}', $header->toHtml(), $this->_templates[$name]);
  234. } else {
  235. $this->_html .= str_replace('{header}', $header->toHtml(), $this->_headerTemplate);
  236. }
  237. } // end func renderHeader
  238. /**
  239. * Helper method for renderElement
  240. *
  241. * @param HTML_QuickForm_element $element
  242. * @param bool Whether an element is required
  243. * @param string $required Error message associated with the element
  244. * @param string $error Label for ID
  245. * @access private
  246. * @see renderElement()
  247. * @return string Html for element
  248. */
  249. private function _prepareTemplate(HTML_QuickForm_element $element, $required, $error)
  250. {
  251. $name = $element->getName();
  252. $label = $element->getLabel();
  253. $labelForId = $element->getLabelFor();
  254. $icon = $element->getIconToHtml();
  255. if (is_array($label)) {
  256. $nameLabel = array_shift($label);
  257. } else {
  258. $nameLabel = $label;
  259. }
  260. $labelFor = !empty($labelForId) ? 'for="' . $labelForId . '"' : '';
  261. if (isset($this->_templates[$name])) {
  262. // Custom template
  263. $html = str_replace('{label}', $nameLabel, $this->_templates[$name]);
  264. } else {
  265. $customElementTemplate = $this->getCustomElementTemplate();
  266. if (empty($customElementTemplate)) {
  267. if (method_exists($element, 'getTemplate')) {
  268. $template = $element->getTemplate(
  269. $this->getForm()->getLayout()
  270. );
  271. } else {
  272. $template = $this->getForm()->getDefaultElementTemplate();
  273. }
  274. } else {
  275. $template = $customElementTemplate;
  276. }
  277. $html = str_replace('{label}', $nameLabel, $template);
  278. }
  279. $html = str_replace('{label-for}', $labelFor, $html);
  280. $html = str_replace('{icon}', $icon, $html);
  281. if ($required) {
  282. $html = str_replace('<!-- BEGIN required -->', '', $html);
  283. $html = str_replace('<!-- END required -->', '', $html);
  284. } else {
  285. $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->.*<!-- END required -->([ \t\n\r]*)?/isU", '', $html);
  286. }
  287. if (isset($error)) {
  288. $html = str_replace('{error}', $error, $html);
  289. $html = str_replace('{error_class}', 'error has-error', $html);
  290. $html = str_replace('<!-- BEGIN error -->', '', $html);
  291. $html = str_replace('<!-- END error -->', '', $html);
  292. } else {
  293. $html = str_replace('{error_class}', '', $html);
  294. $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->.*<!-- END error -->([ \t\n\r]*)?/isU", '', $html);
  295. }
  296. if (is_array($label)) {
  297. foreach ($label as $key => $text) {
  298. $key = is_int($key)? $key + 2: $key;
  299. $html = str_replace("{label_{$key}}", $text, $html);
  300. $html = str_replace("<!-- BEGIN label_{$key} -->", '', $html);
  301. $html = str_replace("<!-- END label_{$key} -->", '', $html);
  302. }
  303. }
  304. if (strpos($html, '{label_')) {
  305. $html = preg_replace('/\s*<!-- BEGIN label_(\S+) -->.*<!-- END label_\1 -->\s*/is', '', $html);
  306. }
  307. return $html;
  308. } // end func _prepareTemplate
  309. /**
  310. * Renders an element Html
  311. * Called when visiting an element
  312. *
  313. * @param HTML_QuickForm_element form element being visited
  314. * @param bool Whether an element is required
  315. * @param string An error message associated with an element
  316. * @access public
  317. * @return void
  318. */
  319. public function renderElement(&$element, $required, $error)
  320. {
  321. if (!$this->_inGroup) {
  322. $html = $this->_prepareTemplate(
  323. $element,
  324. $required,
  325. $error
  326. );
  327. $this->_html .= str_replace('{element}', $element->toHtml(), $html);
  328. } elseif (!empty($this->_groupElementTemplate)) {
  329. $html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
  330. $html = str_replace('{label-for}', $element->getLabelFor(), $this->_groupElementTemplate);
  331. if ($required) {
  332. $html = str_replace('<!-- BEGIN required -->', '', $html);
  333. $html = str_replace('<!-- END required -->', '', $html);
  334. } else {
  335. $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN required -->.*<!-- END required -->([ \t\n\r]*)?/isU", '', $html);
  336. }
  337. $this->_groupElements[] = str_replace('{element}', $element->toHtml(), $html);
  338. } else {
  339. $this->_groupElements[] = $element->toHtml();
  340. }
  341. } // end func renderElement
  342. /**
  343. * Renders an hidden element
  344. * Called when visiting a hidden element
  345. *
  346. * @param HTML_QuickForm_element form element being visited
  347. * @access public
  348. * @return void
  349. */
  350. function renderHidden(&$element)
  351. {
  352. $this->_hiddenHtml .= $element->toHtml() . "\n";
  353. } // end func renderHidden
  354. /**
  355. * Called when visiting a raw HTML/text pseudo-element
  356. *
  357. * @param HTML_QuickForm_html element being visited
  358. * @access public
  359. * @return void
  360. */
  361. function renderHtml(&$data)
  362. {
  363. $this->_html .= $data->toHtml();
  364. } // end func renderHtml
  365. /**
  366. * Called when visiting a group, before processing any group elements
  367. *
  368. * @param HTML_QuickForm_group group being visited
  369. * @param bool Whether a group is required
  370. * @param string An error message associated with a group
  371. * @access public
  372. * @return void
  373. */
  374. function startGroup(&$group, $required, $error)
  375. {
  376. $name = $group->getName();
  377. $this->_groupTemplate = $this->_prepareTemplate($group, $required, $error);
  378. $this->_groupElementTemplate = empty($this->_groupTemplates[$name])? '': $this->_groupTemplates[$name];
  379. $this->_groupWrap = empty($this->_groupWraps[$name])? '': $this->_groupWraps[$name];
  380. $this->_groupElements = array();
  381. $this->_inGroup = true;
  382. } // end func startGroup
  383. /**
  384. * Called when visiting a group, after processing all group elements
  385. *
  386. * @param HTML_QuickForm_group group being visited
  387. * @access public
  388. * @return void
  389. */
  390. function finishGroup(&$group)
  391. {
  392. $separator = $group->_separator;
  393. if (is_array($separator)) {
  394. $count = count($separator);
  395. $html = '';
  396. for ($i = 0; $i < count($this->_groupElements); $i++) {
  397. $html .= (0 == $i? '': $separator[($i - 1) % $count]) . $this->_groupElements[$i];
  398. }
  399. } else {
  400. if (is_null($separator)) {
  401. $separator = '&nbsp;';
  402. }
  403. $html = implode((string)$separator, $this->_groupElements);
  404. }
  405. if (!empty($this->_groupWrap)) {
  406. $html = str_replace('{content}', $html, $this->_groupWrap);
  407. }
  408. $this->_html .= str_replace('{element}', $html, $this->_groupTemplate);
  409. $this->_inGroup = false;
  410. } // end func finishGroup
  411. /**
  412. * Sets element template
  413. *
  414. * @param string The HTML surrounding an element
  415. * @param string (optional) Name of the element to apply template for
  416. * @access public
  417. * @return void
  418. */
  419. function setElementTemplate($html, $element = null)
  420. {
  421. if (is_null($element)) {
  422. $this->_elementTemplate = $html;
  423. } else {
  424. $this->_templates[$element] = $html;
  425. }
  426. } // end func setElementTemplate
  427. /**
  428. * Sets template for a group wrapper
  429. *
  430. * This template is contained within a group-as-element template
  431. * set via setTemplate() and contains group's element templates, set
  432. * via setGroupElementTemplate()
  433. *
  434. * @param string The HTML surrounding group elements
  435. * @param string Name of the group to apply template for
  436. * @access public
  437. * @return void
  438. */
  439. function setGroupTemplate($html, $group)
  440. {
  441. $this->_groupWraps[$group] = $html;
  442. } // end func setGroupTemplate
  443. /**
  444. * Sets element template for elements within a group
  445. *
  446. * @param string The HTML surrounding an element
  447. * @param string Name of the group to apply template for
  448. * @access public
  449. * @return void
  450. */
  451. function setGroupElementTemplate($html, $group)
  452. {
  453. $this->_groupTemplates[$group] = $html;
  454. } // end func setGroupElementTemplate
  455. /**
  456. * Sets header template
  457. *
  458. * @param string The HTML surrounding the header
  459. * @access public
  460. * @return void
  461. */
  462. function setHeaderTemplate($html)
  463. {
  464. $this->_headerTemplate = $html;
  465. } // end func setHeaderTemplate
  466. /**
  467. * Sets form template
  468. *
  469. * @param string The HTML surrounding the form tags
  470. * @access public
  471. * @return void
  472. */
  473. function setFormTemplate($html) {
  474. $this->_formTemplate = $html;
  475. } // end func setFormTemplate
  476. /**
  477. * Sets the note indicating required fields template
  478. *
  479. * @param string The HTML surrounding the required note
  480. * @access public
  481. * @return void
  482. */
  483. function setRequiredNoteTemplate($html)
  484. {
  485. $this->_requiredNoteTemplate = $html;
  486. } // end func setRequiredNoteTemplate
  487. /**
  488. * Clears all the HTML out of the templates that surround notes, elements, etc.
  489. * Useful when you want to use addData() to create a completely custom form look
  490. *
  491. * @access public
  492. * @return void
  493. */
  494. function clearAllTemplates()
  495. {
  496. $this->setElementTemplate('{element}');
  497. $this->setFormTemplate("\n\t<form{attributes}>{content}\n\t</form>\n");
  498. $this->setRequiredNoteTemplate('');
  499. $this->_templates = array();
  500. } // end func clearAllTemplates
  501. } // end class HTML_QuickForm_Renderer_Default