QuickHtml.php 7.2 KB

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