Page.php 5.7 KB

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