QuickHtml.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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: Jason Rust <jrust@rustyparts.com> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: QuickHtml.php 6184 2005-09-07 10:08:17Z bmol $
  20. require_once('HTML/QuickForm/Renderer/Default.php');
  21. /**
  22. * A renderer that makes it quick and easy to create customized forms.
  23. *
  24. * This renderer has three main distinctives: an easy way to create
  25. * custom-looking forms, the ability to separate the creation of form
  26. * elements from their display, and being able to use QuickForm in
  27. * widget-based template systems. See the online docs for more info.
  28. * For a usage example see: docs/renderers/QuickHtml_example.php
  29. *
  30. * @access public
  31. * @package QuickForm
  32. */
  33. class HTML_QuickForm_Renderer_QuickHtml extends HTML_QuickForm_Renderer_Default {
  34. // {{{ properties
  35. /**
  36. * The array of rendered elements
  37. * @var array
  38. */
  39. var $renderedElements = array();
  40. // }}}
  41. // {{{ constructor
  42. /**
  43. * Constructor
  44. *
  45. * @access public
  46. * @return void
  47. */
  48. function HTML_QuickForm_Renderer_QuickHtml()
  49. {
  50. $this->HTML_QuickForm_Renderer_Default();
  51. // The default templates aren't used for this renderer
  52. $this->clearAllTemplates();
  53. } // end constructor
  54. // }}}
  55. // {{{ toHtml()
  56. /**
  57. * returns the HTML generated for the form
  58. *
  59. * @param string $data (optional) Any extra data to put before the end of the form
  60. *
  61. * @access public
  62. * @return string
  63. */
  64. function toHtml($data = '')
  65. {
  66. // Render any elements that haven't been rendered explicitly by elementToHtml()
  67. foreach (array_keys($this->renderedElements) as $key) {
  68. if (!$this->renderedElements[$key]['rendered']) {
  69. $this->renderedElements[$key]['rendered'] = true;
  70. $data .= $this->renderedElements[$key]['html'] . "\n";
  71. }
  72. }
  73. // Insert the extra data and form elements at the end of the form
  74. $this->_html = str_replace('</form>', $data . "\n</form>", $this->_html);
  75. return $this->_html;
  76. } // end func toHtml
  77. // }}}
  78. // {{{ elementToHtml()
  79. /**
  80. * Gets the html for an element and marks it as rendered.
  81. *
  82. * @param string $elementName The element name
  83. * @param string $elementValue (optional) The value of the element. This is only useful
  84. * for elements that have the same name (i.e. radio and checkbox), but
  85. * different values
  86. *
  87. * @access public
  88. * @return string The html for the QuickForm element
  89. */
  90. function elementToHtml($elementName, $elementValue = null)
  91. {
  92. $elementKey = null;
  93. // Find the key for the element
  94. foreach ($this->renderedElements as $key => $data) {
  95. if ($data['name'] == $elementName &&
  96. // See if the value must match as well
  97. (is_null($elementValue) ||
  98. $data['value'] == $elementValue)) {
  99. $elementKey = $key;
  100. break;
  101. }
  102. }
  103. if (is_null($elementKey)) {
  104. $msg = is_null($elementValue) ? "Element $elementName does not exist." :
  105. "Element $elementName with value of $elementValue does not exist.";
  106. return PEAR::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
  107. } else {
  108. if ($this->renderedElements[$elementKey]['rendered']) {
  109. $msg = is_null($elementValue) ? "Element $elementName has already been rendered." :
  110. "Element $elementName with value of $elementValue has already been rendered.";
  111. return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
  112. } else {
  113. $this->renderedElements[$elementKey]['rendered'] = true;
  114. return $this->renderedElements[$elementKey]['html'];
  115. }
  116. }
  117. } // end func elementToHtml
  118. // }}}
  119. // {{{ renderElement()
  120. /**
  121. * Gets the html for an element and adds it to the array by calling
  122. * parent::renderElement()
  123. *
  124. * @param object An HTML_QuickForm_element object
  125. * @param bool Whether an element is required
  126. * @param string An error message associated with an element
  127. *
  128. * @access public
  129. * @return mixed HTML string of element if $immediateRender is set, else we just add the
  130. * html to the global _html string
  131. */
  132. function renderElement(&$element, $required, $error)
  133. {
  134. $this->_html = '';
  135. parent::renderElement($element, $required, $error);
  136. if (!$this->_inGroup) {
  137. $this->renderedElements[] = array(
  138. 'name' => $element->getName(),
  139. 'value' => $element->getValue(),
  140. 'html' => $this->_html,
  141. 'rendered' => false);
  142. }
  143. $this->_html = '';
  144. } // end func renderElement
  145. // }}}
  146. // {{{ renderHidden()
  147. /**
  148. * Gets the html for a hidden element and adds it to the array.
  149. *
  150. * @param object An HTML_QuickForm_hidden object being visited
  151. * @access public
  152. * @return void
  153. */
  154. function renderHidden(&$element)
  155. {
  156. $this->renderedElements[] = array(
  157. 'name' => $element->getName(),
  158. 'value' => $element->getValue(),
  159. 'html' => $element->toHtml(),
  160. 'rendered' => false);
  161. } // end func renderHidden
  162. // }}}
  163. // {{{ finishGroup()
  164. /**
  165. * Gets the html for the group element and adds it to the array by calling
  166. * parent::finishGroup()
  167. *
  168. * @param object An HTML_QuickForm_group object being visited
  169. * @access public
  170. * @return void
  171. */
  172. function finishGroup(&$group)
  173. {
  174. $this->_html = '';
  175. parent::finishGroup($group);
  176. $this->renderedElements[] = array(
  177. 'name' => $group->getName(),
  178. 'value' => $group->getValue(),
  179. 'html' => $this->_html,
  180. 'rendered' => false);
  181. $this->_html = '';
  182. } // end func finishGroup
  183. // }}}
  184. } // end class HTML_QuickForm_Renderer_QuickHtml
  185. ?>