Controller.php 15 KB

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