home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / statemachine.php < prev    next >
Encoding:
PHP Script  |  2004-03-24  |  7.8 KB  |  237 lines

  1. <?php
  2. /**
  3.  * Example for HTML_QuickForm_Controller: Statemachine
  4.  * Going to either of the two pages based on user input
  5.  * 
  6.  * @author Donald Lobo <lobo@groundspring.org>
  7.  * 
  8.  * $Id: statemachine.php,v 1.2 2004/03/02 21:15:40 avb Exp $
  9.  */
  10.  
  11. require_once 'HTML/QuickForm/Controller.php';
  12.  
  13. // Load some default action handlers
  14. require_once 'HTML/QuickForm/Action/Next.php';
  15. require_once 'HTML/QuickForm/Action/Back.php';
  16. require_once 'HTML/QuickForm/Action/Jump.php';
  17. require_once 'HTML/QuickForm/Action/Display.php';
  18.  
  19. // Start the session, form-page values will be kept there
  20. session_start();
  21.  
  22. class PageFirstActionNext extends HTML_QuickForm_Action_Next
  23. {
  24.     function perform(&$page, $actionName) 
  25.     {
  26.       // save the form values and validation status to the session
  27.         $page->isFormBuilt() or $page->buildForm();
  28.         $pageName =  $page->getAttribute('id');
  29.         $data     =& $page->controller->container();
  30.         $data['values'][$pageName] = $page->exportValues();
  31.         $data['valid'][$pageName]  = $page->validate();
  32.  
  33.         // Modal form and page is invalid: don't go further
  34.         if ($page->controller->isModal() && !$data['valid'][$pageName]) {
  35.             return $page->handle('display');
  36.         }
  37.  
  38.         $nextName = $data['values'][$pageName]['iradPageAB'];
  39.         if (empty($nextName)) {
  40.             $nextName = 'page1';
  41.         }
  42.         if ($nextName == 'page2a') {
  43.             $data['valid']['page2b'] = true;
  44.         } else {
  45.             $data['valid']['page2a'] = true;
  46.         }
  47.         $next =& $page->controller->getPage($nextName);
  48.         $next->handle('jump');
  49.     }
  50. }
  51.  
  52.  
  53. class PageSecondActionNext extends HTML_QuickForm_Action_Next
  54. {
  55.     function perform(&$page, $actionName)
  56.     { 
  57.       // save the form values and validation status to the session
  58.         $page->isFormBuilt() or $page->buildForm();
  59.         $pageName =  $page->getAttribute('id');
  60.         $data     =& $page->controller->container();
  61.         $data['values'][$pageName] = $page->exportValues();
  62.         $data['valid'][$pageName]  = $page->validate();
  63.  
  64.         // Modal form and page is invalid: don't go further
  65.         if ($page->controller->isModal() && !$data['valid'][$pageName]) {
  66.             return $page->handle('display');
  67.         }
  68.  
  69.         // More pages?
  70.         $next =& $page->controller->getPage('page3');
  71.         $next->handle('jump'); 
  72.     }
  73. }
  74.  
  75.  
  76. class PageSecondActionBack extends HTML_QuickForm_Action_Back
  77. {
  78.     function perform(&$page, $actionName)
  79.     {
  80.         // save the form values and validation status to the session
  81.         $page->isFormBuilt() or $page->buildForm();
  82.         $pageName =  $page->getAttribute('id');
  83.         $data     =& $page->controller->container();
  84.         $data['values'][$pageName] = $page->exportValues();
  85.         if (!$page->controller->isModal()) {
  86.             $data['valid'][$pageName]  = $page->validate();
  87.         }
  88.  
  89.         $prev =& $page->controller->getPage('page1');
  90.         $prev->handle('jump');
  91.     }
  92. }
  93.  
  94.  
  95. class PageThirdActionBack extends HTML_QuickForm_Action_Back
  96. {
  97.     function perform(&$page, $actionName)
  98.     {
  99.         // save the form values and validation status to the session
  100.         $page->isFormBuilt() or $page->buildForm();
  101.         $pageName =  $page->getAttribute('id');
  102.         $data     =& $page->controller->container();
  103.         $data['values'][$pageName] = $page->exportValues();
  104.         if (!$page->controller->isModal()) {
  105.             $data['valid'][$pageName]  = $page->validate();
  106.         }
  107.  
  108.         $prev =& $page->controller->getPage($data['values']['page1']['iradPageAB']);
  109.         $prev->handle('jump');
  110.     }
  111. }
  112.  
  113.  
  114. class PageFirst extends HTML_QuickForm_Page
  115. {
  116.     function buildForm()
  117.     {
  118.         $this->_formBuilt = true;
  119.  
  120.         $this->addElement('header',     null, 'StateMachine page 1 of 3');
  121.  
  122.         $radio[] = &$this->createElement('radio', null, null, 'Page 2A', 'page2a');
  123.         $radio[] = &$this->createElement('radio', null, null, 'Page 2B', 'page2b');
  124.         $this->addGroup($radio, 'iradPageAB', 'Proceed to page:');
  125.  
  126.         $this->addElement('submit',     $this->getButtonName('next'), 'Next >>');
  127.  
  128.         $this->addRule('iradYesNo', 'Select the page', 'required');
  129.  
  130.         $this->setDefaultAction('next');
  131.     }
  132. }
  133.  
  134. class PageSecondAlpha extends HTML_QuickForm_Page
  135. {
  136.     function buildForm()
  137.     {
  138.         $this->_formBuilt = true;
  139.  
  140.         $this->addElement('header',     null, 'StateMachine page 2A of 3');
  141.  
  142.         $name['last']  = &$this->createElement('text', 'last', null, array('size' => 30));
  143.         $name['first'] = &$this->createElement('text', 'first', null, array('size' => 20));
  144.         $this->addGroup($name, 'name', 'Name (last, first):', ', ');
  145.  
  146.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('back'), '<< Back');
  147.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('next'), 'Next >>');
  148.         $this->addGroup($prevnext, null, '', ' ', false);
  149.         
  150.         $this->addGroupRule('name', array('last' => array(array('Last name is required', 'required'))));
  151.  
  152.         $this->setDefaultAction('next');
  153.     }
  154. }
  155.  
  156. class PageSecondBeta extends HTML_QuickForm_Page
  157. {
  158.     function buildForm()
  159.     {
  160.         $this->_formBuilt = true;
  161.  
  162.         $this->addElement('header',     null, 'StateMachine page 2B of 3');
  163.  
  164.         $this->addElement('textarea',   'itxaTest', 'Description:', array('rows' => 5, 'cols' => 40));
  165.  
  166.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('back'), '<< Back');
  167.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('next'), 'Next >>');
  168.         $this->addGroup($prevnext, null, '', ' ', false);
  169.  
  170.         $this->addRule('itxaTest', 'Say something!', 'required');
  171.  
  172.         $this->setDefaultAction('next');
  173.     }
  174. }
  175.  
  176. class PageThird extends HTML_QuickForm_Page
  177. {
  178.     function buildForm()
  179.     {
  180.         $this->_formBuilt = true;
  181.  
  182.         $this->addElement('header',     null, 'StateMachine page 3 of 3');
  183.  
  184.         $this->addElement('text',   'fourthTextBox', 'Final Text:', array('size' => 20, 'maxlen' => 40));
  185.  
  186.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('back'), '<< Back');
  187.         $prevnext[] =& $this->createElement('submit',   $this->getButtonName('next'), 'Finish');
  188.         $this->addGroup($prevnext, null, '', ' ', false);
  189.  
  190.         $this->addRule('fourthTextBox', 'Say something!', 'required');
  191.  
  192.         $this->setDefaultAction('next');
  193.     }
  194. }
  195.  
  196. class ActionProcess extends HTML_QuickForm_Action
  197. {
  198.     function perform(&$page, $actionName)
  199.     {
  200.         echo "Submit successful!<br>\n<pre>\n";
  201.         var_dump($page->controller->exportValues());
  202.         echo "\n</pre>\n";
  203.     }
  204. }
  205.  
  206. $statemachine =& new HTML_QuickForm_Controller('StateMachine');
  207.  
  208. $page1  =& new PageFirst('page1');
  209. $page2a =& new PageSecondAlpha('page2a');
  210. $page2b =& new PageSecondBeta('page2b');
  211. $page3  =& new PageThird('page3');
  212.  
  213. $statemachine->addPage($page1);
  214. $statemachine->addPage($page2a);
  215. $statemachine->addPage($page2b);
  216. $statemachine->addPage($page3);
  217.  
  218. $page1->addAction('next', new PageFirstActionNext());
  219. $page2a->addAction('next', new PageSecondActionNext());
  220. $page2a->addAction('back', new PageSecondActionBack());
  221. $page2b->addAction('next', new PageSecondActionNext());
  222. $page2b->addAction('back', new PageSecondActionBack());
  223. $page3->addAction('back', new PageThirdActionBack());
  224.  
  225. // We actually add these handlers here for the sake of example
  226. // They can be automatically loaded and added by the controller
  227. $statemachine->addAction('display', new HTML_QuickForm_Action_Display());
  228. $statemachine->addAction('next', new HTML_QuickForm_Action_Next());
  229. $statemachine->addAction('back', new HTML_QuickForm_Action_Back());
  230. $statemachine->addAction('jump', new HTML_QuickForm_Action_Jump());
  231.  
  232. // This is the action we should always define ourselves
  233. $statemachine->addAction('process', new ActionProcess());
  234.  
  235. $statemachine->run();
  236. ?>
  237.