Source for file Controller.php
Documentation is available at Controller.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* The class representing a Controller of MVC design pattern.
* LICENSE: This source file is subject to version 3.01 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_01.txt If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
* @package HTML_QuickForm_Controller
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @copyright 2003-2007 The PHP Group
* @license http://www.php.net/license/3_01.txt PHP License 3.01
* @version CVS: $Id: Controller.php,v 1.13 2007/05/18 09:34:18 avb Exp $
* @link http://pear.php.net/package/HTML_QuickForm_Controller
* The class representing a page of a multipage form.
require_once 'HTML/QuickForm/Page.php';
* The class representing a Controller of MVC design pattern.
* This class keeps track of pages and (default) action handlers for the form,
* it manages keeping the form values in session, setting defaults and
* constants for the form as a whole and getting its submit values.
* Generally you don't need to subclass this.
* @package HTML_QuickForm_Controller
* @author Alexey Borzov <avb@php.net>
* @author Bertrand Mansion <bmansion@mamasam.com>
* @version Release: 1.0.9
* Contains the pages (HTML_QuickForm_Page objects) of the miultipage form
* Contains the mapping of actions to corresponding HTML_QuickForm_Action objects
* Name of the form, used to store the values in session
* Whether the form is modal
* The action extracted from HTTP request: array('page', 'action')
* Sets the form name and modal/non-modal behaviuor. Different multipage
* forms should have different names, as they are used to store form
* values in session. Modal forms allow passing to the next page only when
* all of the previous pages are valid.
* @param string form name
* @param bool whether the form is modal
* Returns a reference to a session variable containing the form-page
* values and pages' validation status.
* This is a "low-level" method, use exportValues() if you want just to
* @param bool If true, then reset the container: clear all default, constant and submitted values
$name = '_' . $this->_name . '_container';
if (!isset ($_SESSION[$name]) || $reset) {
$_SESSION[$name] = array (
foreach (array_keys($this->_pages) as $pageName) {
if (!isset ($_SESSION[$name]['values'][$pageName])) {
$_SESSION[$name]['values'][$pageName] = array ();
$_SESSION[$name]['valid'][$pageName] = null;
* This finds the current page, the current action and passes the action
* to the page's handle() method.
// the names of the action and page should be saved
list ($page, $action) = $this->_actionName = $this->getActionName();
return $this->_pages[$page]->handle ($action);
* Registers a handler for a specific action.
* @param string name of the action
* @param HTML_QuickForm_Action the handler for the action
$this->_actions[$actionName] = & $action;
* Adds a new page to the form
* @param HTML_QuickForm_Page
$page->controller = & $this;
$this->_pages[$page->getAttribute ('id')] = & $page;
* @param string Name of a page
* @return HTML_QuickForm_Page A reference to the page
if (!isset ($this->_pages[$pageName])) {
return PEAR ::raiseError ('HTML_QuickForm_Controller: Unknown page "' . $pageName . '"');
return $this->_pages[$pageName];
* This will be called if the page itself does not have a handler
* to a specific action. The method also loads and uses default handlers
* for common actions, if specific ones were not added.
* @param HTML_QuickForm_Page The page that failed to handle the action
* @param string Name of the action
function handle(&$page, $actionName)
if (isset ($this->_actions[$actionName])) {
return $this->_actions[$actionName]->perform ($page, $actionName);
include_once 'HTML/QuickForm/Action/' . ucfirst($actionName) . '.php';
$className = 'HTML_QuickForm_Action_' . $actionName;
$this->_actions[$actionName] = & new $className();
return $this->_actions[$actionName]->perform ($page, $actionName);
return PEAR ::raiseError ('HTML_QuickForm_Controller: Unhandled action "' . $actionName . '" in page "' . $page->getAttribute ('id') . '"');
* Checks whether the form is modal.
* Checks whether the pages of the controller are valid
* @param string If set, check only the pages before (not including) that page
if (isset ($pageName) && $pageName == $key) {
} elseif (!$data['valid'][$key]) {
// We should handle the possible situation when the user has never
// seen a page of a non-modal multipage form
if (!$this->isModal() && null === $data['valid'][$key]) {
$page = & $this->_pages[$key];
// Fix for bug #8687: the unseen page was considered
// submitted, so defaults for checkboxes and multiselects
// were not used. Shouldn't break anything since this flag
// will be reset right below in loadValues().
$page->_flagSubmitted = false;
// Use controller's defaults and constants, if present
$page->isFormBuilt () or $page->BuildForm ();
// We use defaults and constants as if they were submitted
$data['values'][$key] = $page->exportValues ();
$page->loadValues ($data['values'][$key]);
// Is the page now valid?
if (PEAR ::isError ($valid = $page->validate ())) {
$data['valid'][$key] = $valid;
* Returns the name of the page before the given.
* Returns the name of the page after the given.
if ($prev == $pageName) {
* Finds the (first) invalid page
* @return string Name of an invalid page
if (!$data['valid'][$key]) {
* Extracts the names of the current page and the current action from
* @return array first element is page name, second is action name
return $this->_actionName;
$regex = '/^_qf_(' . implode('|', $names) . ')_(.+?)(_x)?$/';
return array ($matches[1 ], $matches[2 ]);
if (isset ($_REQUEST['_qf_default'])) {
$matches = explode(':', $_REQUEST['_qf_default'], 2 );
if (isset ($this->_pages[$matches[0 ]])) {
return array (key($this->_pages), 'display');
* Initializes default form values.
* @param array default values
* @param mixed filter(s) to apply to default values
function setDefaults($defaultValues = null , $filter = null )
return $this->_setDefaultsOrConstants ($data['defaults'], $defaultValues, $filter);
* Initializes constant form values.
* These values won't get overridden by POST or GET vars
* @param array constant values
* @param mixed filter(s) to apply to constant values
function setConstants($constantValues = null , $filter = null )
return $this->_setDefaultsOrConstants ($data['constants'], $constantValues, $filter);
* Adds new values to defaults or constants array
* @param array array to add values to (either defaults or constants)
* @param array values to add
* @param mixed filters to apply to new values
function _setDefaultsOrConstants (&$values, $newValues, $filter = null )
foreach ($filter as $val) {
return PEAR ::raiseError (null , QUICKFORM_INVALID_FILTER , null , E_USER_WARNING , "Callback function does not exist in QuickForm_Controller::_setDefaultsOrConstants()", 'HTML_QuickForm_Error', true );
$newValues = $this->_arrayMapRecursive ($val, $newValues);
return PEAR ::raiseError (null , QUICKFORM_INVALID_FILTER , null , E_USER_WARNING , "Callback function does not exist in QuickForm_Controller::_setDefaultsOrConstants()", 'HTML_QuickForm_Error', true );
$newValues = $this->_arrayMapRecursive ($val, $newValues);
$values = HTML_QuickForm ::arrayMerge ($values, $newValues);
* Recursively applies the callback function to the value
* @param mixed Callback function
* @param mixed Value to process
* @return mixed Processed values
function _arrayMapRecursive ($callback, $value)
foreach ($value as $k => $v) {
$map[$k] = $this->_arrayMapRecursive ($callback, $v);
* Sets the default values for the given page
* @param string Name of a page
if (!empty ($data['defaults'])) {
$this->_pages[$pageName]->setDefaults ($data['defaults']);
if (!empty ($data['constants'])) {
$this->_pages[$pageName]->setConstants ($data['constants']);
* Returns the form's values
* @param string name of the page, if not set then returns values for all pages
$pages = array ($pageName);
foreach ($pages as $page) {
// skip elements representing actions
foreach ($data['values'][$page] as $key => $value) {
if (0 !== strpos($key, '_qf_')) {
if (isset ($values[$key]) && is_array($value)) {
$values[$key] = HTML_QuickForm ::arrayMerge ($values[$key], $value);
* Returns the element's value
* @param string name of the page
* @param string name of the element in the page
* @return mixed value for the element
return isset ($data['values'][$pageName][$elementName])? $data['values'][$pageName][$elementName]: null;
Documentation generated on Tue, 22 Jul 2008 08:00:05 -0400 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|