QuickHtml.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. {
  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. * returns the HTML generated for the form
  60. *
  61. * @param string $data (optional) Any extra data to put before the end of the form
  62. *
  63. * @access public
  64. * @return string
  65. */
  66. function toHtml($data = '')
  67. {
  68. // Render any elements that haven't been rendered explicitly by elementToHtml()
  69. foreach (array_keys($this->renderedElements) as $key) {
  70. if (!$this->renderedElements[$key]['rendered']) {
  71. $this->renderedElements[$key]['rendered'] = true;
  72. $data .= $this->renderedElements[$key]['html'] . "\n";
  73. }
  74. }
  75. // Insert the extra data and form elements at the end of the form
  76. $this->_html = str_replace('</form>', $data . "\n</form>", $this->_html);
  77. return $this->_html;
  78. } // end func toHtml
  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. * @throws HTML_QuickForm_Error
  90. */
  91. function elementToHtml($elementName, $elementValue = null)
  92. {
  93. $elementKey = null;
  94. // Find the key for the element
  95. foreach ($this->renderedElements as $key => $data) {
  96. if ($data['name'] == $elementName &&
  97. // See if the value must match as well
  98. (is_null($elementValue) ||
  99. $data['value'] == $elementValue)) {
  100. $elementKey = $key;
  101. break;
  102. }
  103. }
  104. if (is_null($elementKey)) {
  105. $msg = is_null($elementValue) ? "Element $elementName does not exist." :
  106. "Element $elementName with value of $elementValue does not exist.";
  107. return PEAR::raiseError(null, QUICKFORM_UNREGISTERED_ELEMENT, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
  108. } else {
  109. if ($this->renderedElements[$elementKey]['rendered']) {
  110. $msg = is_null($elementValue) ? "Element $elementName has already been rendered." :
  111. "Element $elementName with value of $elementValue has already been rendered.";
  112. return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, $msg, 'HTML_QuickForm_Error', true);
  113. } else {
  114. $this->renderedElements[$elementKey]['rendered'] = true;
  115. return $this->renderedElements[$elementKey]['html'];
  116. }
  117. }
  118. } // end func elementToHtml
  119. /**
  120. * Gets the html for an element and adds it to the array by calling
  121. * parent::renderElement()
  122. *
  123. * @param HTML_QuickForm_element form element being visited
  124. * @param bool Whether an element is required
  125. * @param string An error message associated with an element
  126. *
  127. * @access public
  128. * @return mixed HTML string of element if $immediateRender is set, else we just add the
  129. * html to the global _html string
  130. */
  131. function renderElement(&$element, $required, $error)
  132. {
  133. $this->_html = '';
  134. parent::renderElement($element, $required, $error);
  135. if (!$this->_inGroup) {
  136. $this->renderedElements[] = array(
  137. 'name' => $element->getName(),
  138. 'value' => $element->getValue(),
  139. 'html' => $this->_html,
  140. 'rendered' => false);
  141. }
  142. $this->_html = '';
  143. } // end func renderElement
  144. /**
  145. * Gets the html for a hidden element and adds it to the array.
  146. *
  147. * @param HTML_QuickForm_element hidden form element being visited
  148. * @access public
  149. * @return void
  150. */
  151. function renderHidden(&$element)
  152. {
  153. $this->renderedElements[] = array(
  154. 'name' => $element->getName(),
  155. 'value' => $element->getValue(),
  156. 'html' => $element->toHtml(),
  157. 'rendered' => false);
  158. } // end func renderHidden
  159. // }}}
  160. // {{{ finishGroup()
  161. /**
  162. * Gets the html for the group element and adds it to the array by calling
  163. * parent::finishGroup()
  164. *
  165. * @param HTML_QuickForm_group group being visited
  166. * @access public
  167. * @return void
  168. */
  169. function finishGroup(&$group)
  170. {
  171. $this->_html = '';
  172. parent::finishGroup($group);
  173. $this->renderedElements[] = array(
  174. 'name' => $group->getName(),
  175. 'value' => $group->getValue(),
  176. 'html' => $this->_html,
  177. 'rendered' => false);
  178. $this->_html = '';
  179. } // end func finishGroup
  180. // }}}
  181. } // end class HTML_QuickForm_Renderer_QuickHtml
  182. ?>