QuickHtml.php 7.0 KB

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