Controller.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * The class representing a Controller of MVC design pattern.
  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: Controller.php 289084 2009-10-02 06:53:09Z avb $
  21. * @link http://pear.php.net/package/HTML_QuickForm_Controller
  22. */
  23. /**
  24. * The class representing a page of a multipage form.
  25. */
  26. require_once 'HTML/QuickForm/Page.php';
  27. /**
  28. * The class representing a Controller of MVC design pattern.
  29. *
  30. * This class keeps track of pages and (default) action handlers for the form,
  31. * it manages keeping the form values in session, setting defaults and
  32. * constants for the form as a whole and getting its submit values.
  33. *
  34. * Generally you don't need to subclass this.
  35. *
  36. * @category HTML
  37. * @package HTML_QuickForm_Controller
  38. * @author Alexey Borzov <avb@php.net>
  39. * @author Bertrand Mansion <bmansion@mamasam.com>
  40. * @version Release: 1.0.10
  41. */
  42. class HTML_QuickForm_Controller
  43. {
  44. /**
  45. * Contains the pages (HTML_QuickForm_Page objects) of the miultipage form
  46. * @var array
  47. */
  48. var $_pages = array();
  49. /**
  50. * Contains the mapping of actions to corresponding HTML_QuickForm_Action objects
  51. * @var array
  52. */
  53. var $_actions = array();
  54. /**
  55. * Name of the form, used to store the values in session
  56. * @var string
  57. */
  58. var $_name;
  59. /**
  60. * Whether the form is modal
  61. * @var bool
  62. */
  63. var $_modal = true;
  64. /**
  65. * The action extracted from HTTP request: array('page', 'action')
  66. * @var array
  67. */
  68. var $_actionName = null;
  69. /**
  70. * Class constructor.
  71. *
  72. * Sets the form name and modal/non-modal behaviuor. Different multipage
  73. * forms should have different names, as they are used to store form
  74. * values in session. Modal forms allow passing to the next page only when
  75. * all of the previous pages are valid.
  76. *
  77. * @access public
  78. * @param string form name
  79. * @param bool whether the form is modal
  80. */
  81. function HTML_QuickForm_Controller($name, $modal = true)
  82. {
  83. $this->_name = $name;
  84. $this->_modal = $modal;
  85. }
  86. /**
  87. * Returns a reference to a session variable containing the form-page
  88. * values and pages' validation status.
  89. *
  90. * This is a "low-level" method, use exportValues() if you want just to
  91. * get the form's values.
  92. *
  93. * @access public
  94. * @param bool If true, then reset the container: clear all default, constant and submitted values
  95. * @return array
  96. */
  97. function &container($reset = false)
  98. {
  99. $name = '_' . $this->_name . '_container';
  100. if (!isset($_SESSION[$name]) || $reset) {
  101. $_SESSION[$name] = array(
  102. 'defaults' => array(),
  103. 'constants' => array(),
  104. 'values' => array(),
  105. 'valid' => array()
  106. );
  107. }
  108. foreach (array_keys($this->_pages) as $pageName) {
  109. if (!isset($_SESSION[$name]['values'][$pageName])) {
  110. $_SESSION[$name]['values'][$pageName] = array();
  111. $_SESSION[$name]['valid'][$pageName] = null;
  112. }
  113. }
  114. return $_SESSION[$name];
  115. }
  116. /**
  117. * Processes the request.
  118. *
  119. * This finds the current page, the current action and passes the action
  120. * to the page's handle() method.
  121. *
  122. * @access public
  123. * @throws PEAR_Error
  124. */
  125. function run()
  126. {
  127. // the names of the action and page should be saved
  128. list($page, $action) = $this->_actionName = $this->getActionName();
  129. return $this->_pages[$page]->handle($action);
  130. }
  131. /**
  132. * Registers a handler for a specific action.
  133. *
  134. * @access public
  135. * @param string name of the action
  136. * @param HTML_QuickForm_Action the handler for the action
  137. */
  138. function addAction($actionName, &$action)
  139. {
  140. $this->_actions[$actionName] =& $action;
  141. }
  142. /**
  143. * Adds a new page to the form
  144. *
  145. * @access public
  146. * @param HTML_QuickForm_Page
  147. */
  148. function addPage(&$page)
  149. {
  150. $page->controller =& $this;
  151. $this->_pages[$page->getAttribute('id')] =& $page;
  152. }
  153. /**
  154. * Returns a page
  155. *
  156. * @access public
  157. * @param string Name of a page
  158. * @return HTML_QuickForm_Page A reference to the page
  159. * @throws PEAR_Error
  160. */
  161. function &getPage($pageName)
  162. {
  163. if (!isset($this->_pages[$pageName])) {
  164. return PEAR::raiseError('HTML_QuickForm_Controller: Unknown page "' . $pageName . '"');
  165. }
  166. return $this->_pages[$pageName];
  167. }
  168. /**
  169. * Handles an action.
  170. *
  171. * This will be called if the page itself does not have a handler
  172. * to a specific action. The method also loads and uses default handlers
  173. * for common actions, if specific ones were not added.
  174. *
  175. * @access public
  176. * @param HTML_QuickForm_Page The page that failed to handle the action
  177. * @param string Name of the action
  178. * @throws PEAR_Error
  179. */
  180. function handle(&$page, $actionName)
  181. {
  182. if (isset($this->_actions[$actionName])) {
  183. return $this->_actions[$actionName]->perform($page, $actionName);
  184. }
  185. switch ($actionName) {
  186. case 'next':
  187. case 'back':
  188. case 'submit':
  189. case 'display':
  190. case 'jump':
  191. include_once 'HTML/QuickForm/Action/' . ucfirst($actionName) . '.php';
  192. $className = 'HTML_QuickForm_Action_' . $actionName;
  193. $this->_actions[$actionName] =& new $className();
  194. return $this->_actions[$actionName]->perform($page, $actionName);
  195. break;
  196. default:
  197. return PEAR::raiseError('HTML_QuickForm_Controller: Unhandled action "' . $actionName . '" in page "' . $page->getAttribute('id') . '"');
  198. } // switch
  199. }
  200. /**
  201. * Checks whether the form is modal.
  202. *
  203. * @access public
  204. * @return bool
  205. */
  206. function isModal()
  207. {
  208. return $this->_modal;
  209. }
  210. /**
  211. * Checks whether the pages of the controller are valid
  212. *
  213. * @access public
  214. * @param string If set, check only the pages before (not including) that page
  215. * @return bool
  216. * @throws PEAR_Error
  217. */
  218. function isValid($pageName = null)
  219. {
  220. $data =& $this->container();
  221. foreach (array_keys($this->_pages) as $key) {
  222. if (isset($pageName) && $pageName == $key) {
  223. return true;
  224. } elseif (!$data['valid'][$key]) {
  225. // We should handle the possible situation when the user has never
  226. // seen a page of a non-modal multipage form
  227. if (!$this->isModal() && null === $data['valid'][$key]) {
  228. $page =& $this->_pages[$key];
  229. // Fix for bug #8687: the unseen page was considered
  230. // submitted, so defaults for checkboxes and multiselects
  231. // were not used. Shouldn't break anything since this flag
  232. // will be reset right below in loadValues().
  233. $page->_flagSubmitted = false;
  234. // Use controller's defaults and constants, if present
  235. $this->applyDefaults($key);
  236. $page->isFormBuilt() or $page->BuildForm();
  237. // We use defaults and constants as if they were submitted
  238. $data['values'][$key] = $page->exportValues();
  239. $page->loadValues($data['values'][$key]);
  240. // Is the page now valid?
  241. if (PEAR::isError($valid = $page->validate())) {
  242. return $valid;
  243. }
  244. $data['valid'][$key] = $valid;
  245. if (true === $valid) {
  246. continue;
  247. }
  248. }
  249. return false;
  250. }
  251. }
  252. return true;
  253. }
  254. /**
  255. * Returns the name of the page before the given.
  256. *
  257. * @access public
  258. * @param string
  259. * @return string
  260. */
  261. function getPrevName($pageName)
  262. {
  263. $prev = null;
  264. foreach (array_keys($this->_pages) as $key) {
  265. if ($key == $pageName) {
  266. return $prev;
  267. }
  268. $prev = $key;
  269. }
  270. }
  271. /**
  272. * Returns the name of the page after the given.
  273. *
  274. * @access public
  275. * @param string
  276. * @return string
  277. */
  278. function getNextName($pageName)
  279. {
  280. $prev = null;
  281. foreach (array_keys($this->_pages) as $key) {
  282. if ($prev == $pageName) {
  283. return $key;
  284. }
  285. $prev = $key;
  286. }
  287. return null;
  288. }
  289. /**
  290. * Finds the (first) invalid page
  291. *
  292. * @access public
  293. * @return string Name of an invalid page
  294. */
  295. function findInvalid()
  296. {
  297. $data =& $this->container();
  298. foreach (array_keys($this->_pages) as $key) {
  299. if (!$data['valid'][$key]) {
  300. return $key;
  301. }
  302. }
  303. return null;
  304. }
  305. /**
  306. * Extracts the names of the current page and the current action from
  307. * HTTP request data.
  308. *
  309. * @access public
  310. * @return array first element is page name, second is action name
  311. */
  312. function getActionName()
  313. {
  314. if (is_array($this->_actionName)) {
  315. return $this->_actionName;
  316. }
  317. $names = array_map('preg_quote', array_keys($this->_pages));
  318. $regex = '/^_qf_(' . implode('|', $names) . ')_(.+?)(_x)?$/';
  319. foreach (array_keys($_REQUEST) as $key) {
  320. if (preg_match($regex, $key, $matches)) {
  321. return array($matches[1], $matches[2]);
  322. }
  323. }
  324. if (isset($_REQUEST['_qf_default'])) {
  325. $matches = explode(':', $_REQUEST['_qf_default'], 2);
  326. if (isset($this->_pages[$matches[0]])) {
  327. return $matches;
  328. }
  329. }
  330. reset($this->_pages);
  331. return array(key($this->_pages), 'display');
  332. }
  333. /**
  334. * Initializes default form values.
  335. *
  336. * @access public
  337. * @param array default values
  338. * @param mixed filter(s) to apply to default values
  339. * @throws PEAR_Error
  340. */
  341. function setDefaults($defaultValues = null, $filter = null)
  342. {
  343. if (is_array($defaultValues)) {
  344. $data =& $this->container();
  345. return $this->_setDefaultsOrConstants($data['defaults'], $defaultValues, $filter);
  346. }
  347. }
  348. /**
  349. * Initializes constant form values.
  350. * These values won't get overridden by POST or GET vars
  351. *
  352. * @access public
  353. * @param array constant values
  354. * @param mixed filter(s) to apply to constant values
  355. * @throws PEAR_Error
  356. */
  357. function setConstants($constantValues = null, $filter = null)
  358. {
  359. if (is_array($constantValues)) {
  360. $data =& $this->container();
  361. return $this->_setDefaultsOrConstants($data['constants'], $constantValues, $filter);
  362. }
  363. }
  364. /**
  365. * Adds new values to defaults or constants array
  366. *
  367. * @access private
  368. * @param array array to add values to (either defaults or constants)
  369. * @param array values to add
  370. * @param mixed filters to apply to new values
  371. * @throws PEAR_Error
  372. */
  373. function _setDefaultsOrConstants(&$values, $newValues, $filter = null)
  374. {
  375. if (isset($filter)) {
  376. if (is_array($filter) && (2 != count($filter) || !is_callable($filter))) {
  377. foreach ($filter as $val) {
  378. if (!is_callable($val)) {
  379. return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm_Controller::_setDefaultsOrConstants()", 'HTML_QuickForm_Error', true);
  380. } else {
  381. $newValues = $this->_arrayMapRecursive($val, $newValues);
  382. }
  383. }
  384. } elseif (!is_callable($filter)) {
  385. return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm_Controller::_setDefaultsOrConstants()", 'HTML_QuickForm_Error', true);
  386. } else {
  387. $newValues = $this->_arrayMapRecursive($val, $newValues);
  388. }
  389. }
  390. $values = HTML_QuickForm::arrayMerge($values, $newValues);
  391. }
  392. /**
  393. * Recursively applies the callback function to the value
  394. *
  395. * @param mixed Callback function
  396. * @param mixed Value to process
  397. * @access private
  398. * @return mixed Processed values
  399. */
  400. function _arrayMapRecursive($callback, $value)
  401. {
  402. if (!is_array($value)) {
  403. return call_user_func($callback, $value);
  404. } else {
  405. $map = array();
  406. foreach ($value as $k => $v) {
  407. $map[$k] = $this->_arrayMapRecursive($callback, $v);
  408. }
  409. return $map;
  410. }
  411. }
  412. /**
  413. * Sets the default values for the given page
  414. *
  415. * @access public
  416. * @param string Name of a page
  417. */
  418. function applyDefaults($pageName)
  419. {
  420. $data =& $this->container();
  421. if (!empty($data['defaults'])) {
  422. $this->_pages[$pageName]->setDefaults($data['defaults']);
  423. }
  424. if (!empty($data['constants'])) {
  425. $this->_pages[$pageName]->setConstants($data['constants']);
  426. }
  427. }
  428. /**
  429. * Returns the form's values
  430. *
  431. * @access public
  432. * @param string name of the page, if not set then returns values for all pages
  433. * @return array
  434. */
  435. function exportValues($pageName = null)
  436. {
  437. $data =& $this->container();
  438. $values = array();
  439. if (isset($pageName)) {
  440. $pages = array($pageName);
  441. } else {
  442. $pages = array_keys($data['values']);
  443. }
  444. foreach ($pages as $page) {
  445. // skip elements representing actions
  446. foreach ($data['values'][$page] as $key => $value) {
  447. if (0 !== strpos($key, '_qf_')) {
  448. if (isset($values[$key]) && is_array($value)) {
  449. $values[$key] = HTML_QuickForm::arrayMerge($values[$key], $value);
  450. } else {
  451. $values[$key] = $value;
  452. }
  453. }
  454. }
  455. }
  456. return $values;
  457. }
  458. /**
  459. * Returns the element's value
  460. *
  461. * @access public
  462. * @param string name of the page
  463. * @param string name of the element in the page
  464. * @return mixed value for the element
  465. */
  466. function exportValue($pageName, $elementName)
  467. {
  468. $data =& $this->container();
  469. return isset($data['values'][$pageName][$elementName])? $data['values'][$pageName][$elementName]: null;
  470. }
  471. }
  472. ?>