QuickHtml.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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. public 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. }
  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. }
  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. public 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. throw new \Exception($msg);
  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. throw new \Exception($msg);
  112. } else {
  113. $this->renderedElements[$elementKey]['rendered'] = true;
  114. return $this->renderedElements[$elementKey]['html'];
  115. }
  116. }
  117. }
  118. /**
  119. * Gets the html for an element and adds it to the array by calling
  120. * parent::renderElement()
  121. *
  122. * @param HTML_QuickForm_element form element being visited
  123. * @param bool Whether an element is required
  124. * @param string An error message associated with an element
  125. *
  126. * @access public
  127. * @return mixed HTML string of element if $immediateRender is set, else we just add the
  128. * html to the global _html string
  129. */
  130. public function renderElement(&$element, $required, $error)
  131. {
  132. $this->_html = '';
  133. parent::renderElement($element, $required, $error);
  134. if (!$this->_inGroup) {
  135. $this->renderedElements[] = array(
  136. 'name' => $element->getName(),
  137. 'value' => $element->getValue(),
  138. 'html' => $this->_html,
  139. 'rendered' => false);
  140. }
  141. $this->_html = '';
  142. }
  143. /**
  144. * Gets the html for a hidden element and adds it to the array.
  145. *
  146. * @param HTML_QuickForm_element hidden form element being visited
  147. * @access public
  148. * @return void
  149. */
  150. public function renderHidden(&$element)
  151. {
  152. $this->renderedElements[] = array(
  153. 'name' => $element->getName(),
  154. 'value' => $element->getValue(),
  155. 'html' => $element->toHtml(),
  156. 'rendered' => false);
  157. } // end func renderHidden
  158. // }}}
  159. // {{{ finishGroup()
  160. /**
  161. * Gets the html for the group element and adds it to the array by calling
  162. * parent::finishGroup()
  163. *
  164. * @param HTML_QuickForm_group group being visited
  165. * @access public
  166. * @return void
  167. */
  168. public function finishGroup(&$group)
  169. {
  170. $this->_html = '';
  171. parent::finishGroup($group);
  172. $this->renderedElements[] = array(
  173. 'name' => $group->getName(),
  174. 'value' => $group->getValue(),
  175. 'html' => $this->_html,
  176. 'rendered' => false);
  177. $this->_html = '';
  178. }
  179. }