Page.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Alexey Borzov <avb@php.net> |
  17. // | Bertrand Mansion <bmansion@mamasam.com> |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Page.php 6184 2005-09-07 10:08:17Z bmol $
  21. require_once 'HTML/QuickForm.php';
  22. /**
  23. * The class represents a page of a multipage form.
  24. *
  25. * Generally you'll need to subclass this and define your buildForm()
  26. * method that will build the form. While it is also possible to instantiate
  27. * this class and build the form manually, this is not the recommended way.
  28. *
  29. * @author Alexey Borzov <avb@php.net>
  30. * @package HTML_QuickForm_Controller
  31. * @version $Revision: 6184 $
  32. */
  33. class HTML_QuickForm_Page extends HTML_QuickForm
  34. {
  35. /**
  36. * Contains the mapping of actions to corresponding HTML_QuickForm_Action objects
  37. * @var array
  38. */
  39. var $_actions = array();
  40. /**
  41. * Contains a reference to a Controller object containing this page
  42. * @var object HTML_QuickForm_Controller
  43. * @access public
  44. */
  45. var $controller = null;
  46. /**
  47. * Should be set to true on first call to buildForm()
  48. * @var bool
  49. */
  50. var $_formBuilt = false;
  51. /**
  52. * Class constructor
  53. *
  54. * @access public
  55. */
  56. function HTML_QuickForm_Page($formName, $method = 'post', $target = '_self', $attributes = null)
  57. {
  58. $this->HTML_QuickForm($formName, $method, '', $target, $attributes);
  59. }
  60. /**
  61. * Registers a handler for a specific action.
  62. *
  63. * @access public
  64. * @param string name of the action
  65. * @param object HTML_QuickForm_Action the handler for the action
  66. */
  67. function addAction($actionName, &$action)
  68. {
  69. $this->_actions[$actionName] =& $action;
  70. }
  71. /**
  72. * Handles an action.
  73. *
  74. * If an Action object was not registered here, controller's handle()
  75. * method will be called.
  76. *
  77. * @access public
  78. * @param string Name of the action
  79. */
  80. function handle($actionName)
  81. {
  82. if (isset($this->_actions[$actionName])) {
  83. return $this->_actions[$actionName]->perform($this, $actionName);
  84. } else {
  85. return $this->controller->handle($this, $actionName);
  86. }
  87. }
  88. /**
  89. * Returns a name for a submit button that will invoke a specific action.
  90. *
  91. * @access public
  92. * @param string Name of the action
  93. * @return string "name" attribute for a submit button
  94. */
  95. function getButtonName($actionName)
  96. {
  97. return '_qf_' . $this->getAttribute('id') . '_' . $actionName;
  98. }
  99. /**
  100. * Loads the submit values from the array.
  101. *
  102. * The method is NOT intended for general usage.
  103. *
  104. * @param array 'submit' values
  105. * @access public
  106. */
  107. function loadValues($values)
  108. {
  109. $this->_submitValues = $values;
  110. foreach (array_keys($this->_elements) as $key) {
  111. $this->_elements[$key]->onQuickFormEvent('updateValue', null, $this);
  112. }
  113. }
  114. /**
  115. * Builds a form.
  116. *
  117. * You should override this method when you subclass HTML_QuickForm_Page,
  118. * it should contain all the necessary addElement(), applyFilter(), addRule()
  119. * and possibly setDefaults() and setConstants() calls. The method will be
  120. * called on demand, so please be sure to set $_formBuilt property to true to
  121. * assure that the method works only once.
  122. *
  123. * @access public
  124. * @abstract
  125. */
  126. function buildForm()
  127. {
  128. $this->_formBuilt = true;
  129. }
  130. /**
  131. * Checks whether the form was already built.
  132. *
  133. * @access public
  134. * @return bool
  135. */
  136. function isFormBuilt()
  137. {
  138. return $this->_formBuilt;
  139. }
  140. /**
  141. * Sets the default action invoked on page-form submit
  142. *
  143. * This is necessary as the user may just press Enter instead of
  144. * clicking one of the named submit buttons and then no action name will
  145. * be passed to the script.
  146. *
  147. * @access public
  148. * @param string default action name
  149. */
  150. function setDefaultAction($actionName)
  151. {
  152. if ($this->elementExists('_qf_default')) {
  153. $element =& $this->getElement('_qf_default');
  154. $element->setValue($this->getAttribute('id') . ':' . $actionName);
  155. } else {
  156. $this->addElement('hidden', '_qf_default', $this->getAttribute('id') . ':' . $actionName);
  157. }
  158. }
  159. }
  160. ?>