Page.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Class representing a page of a multipage form.
  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_Controller
  16. * @author Alexey Borzov <avb@php.net>
  17. * @author Bertrand Mansion <bmansion@mamasam.com>
  18. * @copyright 2003-2009 The PHP Group
  19. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  20. * @version SVN: $Id: Page.php 289084 2009-10-02 06:53:09Z avb $
  21. * @link http://pear.php.net/package/HTML_QuickForm_Controller
  22. */
  23. /**
  24. * Create, validate and process HTML forms
  25. */
  26. require_once 'HTML/QuickForm.php';
  27. /**
  28. * Class representing a page of a multipage form.
  29. *
  30. * Generally you'll need to subclass this and define your buildForm()
  31. * method that will build the form. While it is also possible to instantiate
  32. * this class and build the form manually, this is not the recommended way.
  33. *
  34. * @category HTML
  35. * @package HTML_QuickForm_Controller
  36. * @author Alexey Borzov <avb@php.net>
  37. * @author Bertrand Mansion <bmansion@mamasam.com>
  38. * @version Release: 1.0.10
  39. */
  40. class HTML_QuickForm_Page extends HTML_QuickForm
  41. {
  42. /**
  43. * Contains the mapping of actions to corresponding HTML_QuickForm_Action objects
  44. * @var array
  45. */
  46. var $_actions = array();
  47. /**
  48. * Contains a reference to a Controller object containing this page
  49. * @var HTML_QuickForm_Controller
  50. * @access public
  51. */
  52. var $controller = null;
  53. /**
  54. * Should be set to true on first call to buildForm()
  55. * @var bool
  56. */
  57. var $_formBuilt = false;
  58. /**
  59. * Class constructor
  60. *
  61. * @access public
  62. */
  63. function HTML_QuickForm_Page($formName, $method = 'post', $target = '', $attributes = null)
  64. {
  65. $this->HTML_QuickForm($formName, $method, '', $target, $attributes);
  66. }
  67. /**
  68. * Registers a handler for a specific action.
  69. *
  70. * @access public
  71. * @param string name of the action
  72. * @param HTML_QuickForm_Action the handler for the action
  73. */
  74. function addAction($actionName, &$action)
  75. {
  76. $this->_actions[$actionName] =& $action;
  77. }
  78. /**
  79. * Handles an action.
  80. *
  81. * If an Action object was not registered here, controller's handle()
  82. * method will be called.
  83. *
  84. * @access public
  85. * @param string Name of the action
  86. * @throws PEAR_Error
  87. */
  88. function handle($actionName)
  89. {
  90. if (isset($this->_actions[$actionName])) {
  91. return $this->_actions[$actionName]->perform($this, $actionName);
  92. } else {
  93. return $this->controller->handle($this, $actionName);
  94. }
  95. }
  96. /**
  97. * Returns a name for a submit button that will invoke a specific action.
  98. *
  99. * @access public
  100. * @param string Name of the action
  101. * @return string "name" attribute for a submit button
  102. */
  103. function getButtonName($actionName)
  104. {
  105. return '_qf_' . $this->getAttribute('id') . '_' . $actionName;
  106. }
  107. /**
  108. * Loads the submit values from the array.
  109. *
  110. * The method is NOT intended for general usage.
  111. *
  112. * @param array 'submit' values
  113. * @access public
  114. */
  115. function loadValues($values)
  116. {
  117. $this->_flagSubmitted = true;
  118. $this->_submitValues = $values;
  119. foreach (array_keys($this->_elements) as $key) {
  120. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  121. }
  122. }
  123. /**
  124. * Builds a form.
  125. *
  126. * You should override this method when you subclass HTML_QuickForm_Page,
  127. * it should contain all the necessary addElement(), applyFilter(), addRule()
  128. * and possibly setDefaults() and setConstants() calls. The method will be
  129. * called on demand, so please be sure to set $_formBuilt property to true to
  130. * assure that the method works only once.
  131. *
  132. * @access public
  133. * @abstract
  134. */
  135. function buildForm()
  136. {
  137. $this->_formBuilt = true;
  138. }
  139. /**
  140. * Checks whether the form was already built.
  141. *
  142. * @access public
  143. * @return bool
  144. */
  145. function isFormBuilt()
  146. {
  147. return $this->_formBuilt;
  148. }
  149. /**
  150. * Sets the default action invoked on page-form submit
  151. *
  152. * This is necessary as the user may just press Enter instead of
  153. * clicking one of the named submit buttons and then no action name will
  154. * be passed to the script.
  155. *
  156. * @access public
  157. * @param string default action name
  158. */
  159. function setDefaultAction($actionName)
  160. {
  161. if ($this->elementExists('_qf_default')) {
  162. $element =& $this->getElement('_qf_default');
  163. $element->setValue($this->getAttribute('id') . ':' . $actionName);
  164. } else {
  165. $this->addElement('hidden', '_qf_default', $this->getAttribute('id') . ':' . $actionName);
  166. }
  167. }
  168. /**
  169. * Returns 'safe' elements' values
  170. *
  171. * @param mixed Array/string of element names, whose values we want. If not set then return all elements.
  172. * @param bool Whether to remove internal (_qf_...) values from the resultant array
  173. */
  174. function exportValues($elementList = null, $filterInternal = false)
  175. {
  176. $values = parent::exportValues($elementList);
  177. if ($filterInternal) {
  178. foreach (array_keys($values) as $key) {
  179. if (0 === strpos($key, '_qf_')) {
  180. unset($values[$key]);
  181. }
  182. }
  183. }
  184. return $values;
  185. }
  186. }
  187. ?>