[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/controller/ -> sequentialcontroller.class.php (source)

   1  <?php
   2      
   3      lt_include( PLOG_CLASS_PATH."class/controller/controller.class.php" );
   4      lt_include( PLOG_CLASS_PATH."class/net/http/session/sessionmanager.class.php" );
   5      lt_include( PLOG_CLASS_PATH."class/net/http/httpvars.class.php" );    
   6      
   7      define( "SEQUENTIAL_CONTROLLER_SESSION_PARAMETER", "ltSeqControllerCurrentStep" );
   8  
   9      /**

  10       * \ingroup Controller

  11       *

  12       * This is a simplified version of the Controller pattern that executes its actions in a

  13       * pre-defined sequence. This allows to easily create multi-step forms that are executed

  14       * only in the pre-defined order.

  15       *

  16       * This controller uses the session to store its current position in the list of steps/action

  17       * and it will use it to compute its next step after the next request. Support for sessions 

  18       * on both the client side and the server side is required.

  19       *

  20       * The next step in the sequence will be calculated based on the outcome of the action that

  21       * was last executed: if the validate() method or the perform() method both returned true, then

  22       * the controller will proceed to execute the next action. If either of the conditions above

  23       * was not true, this controller will keep retrying the current step until successful. If you are

  24       * having problems with this controller executing the same action class over and over again,

  25       * make sure you are not returning 'false' from any of those two methods.

  26       *

  27       * If you'd like to restart the sequence of this controller, please include a "start=1" parameter

  28       * in any request.

  29       *

  30       * Please see the RegistrationController class for a simple example of how to use

  31       * this controller.

  32       *

  33       * @author The LifeType Project

  34       * @see RegistrationController

  35       */
  36      class SequentialController extends Controller
  37      {
  38      
  39          var $_steps;
  40          
  41          /**

  42           * Constructor of the class.

  43           *

  44           * @param steps An array containing the name of the action classes that are going to be

  45           * executedin sequence. Please do not use an associative array but a normal array, since

  46           * controller will internally use an integer counter to store the current position.

  47           * @return Nothing

  48           */
  49  		function SequentialController( $steps )
  50          {
  51              $this->Controller( Array());        
  52              $this->_steps = $steps;
  53          }
  54          
  55          /**

  56           * Returns the next step in the sequence based on the current step

  57           *

  58           * @param currentStep The current step in the sequence

  59           * @private

  60           * @return

  61           */
  62  		function getNextStep( $currentStep )
  63          {
  64              return(($currentStep + 1) % count($this->_steps ));
  65          }
  66          
  67          /**

  68           * Returns the current step, based on the contents from the session. If there is

  69           * no information in the session, processing will start at the action class whose

  70           * index in the sequence array is '0'

  71           *

  72           * @return The current step in the sequence

  73           */
  74  		function getCurrentStep()
  75          {
  76              $curStep = SessionManager::getSessionValue( SEQUENTIAL_CONTROLLER_SESSION_PARAMETER );
  77              $request = HttpVars::getRequest();
  78              if( empty($request["start"]) )
  79                  $start = 0;
  80              else
  81                  $start = $request["start"];
  82  
  83              if( !$curStep || $start == "1" ) {
  84                  $curStep = 0;
  85              }
  86              
  87              return( $curStep );
  88          }
  89  
  90          /**

  91           * Main method of the controller, as it processes the current HTTP request. Since this is a sequential

  92           * controller, it does not take into account any of the parameters in the request (as opposed to a normal

  93           * Controller class, which would for example check the "op" parameter) but it instead relies on its

  94           * internal counter and the outcome of the last action to find out which action to execute next.

  95           *

  96           * @param httpRequest The current HTTP Request

  97           * @return Always true

  98           */
  99  		function process( $httpRequest )
 100          {
 101              global $_plogController_previousAction;
 102  
 103              lt_include( PLOG_CLASS_PATH."class/net/request.class.php" );
 104  
 105              // get the name of the action

 106              $request = new Request( $httpRequest );
 107  
 108              lt_include( PLOG_CLASS_PATH."class/action/actioninfo.class.php" );
 109              
 110              $currentStep = $this->getCurrentStep();
 111              $actionClass = $this->_steps[ $currentStep ];
 112              
 113              $this->loadActionClass( $actionClass );
 114  
 115              $actionInfo   = new ActionInfo( $this->_actionParam, "sequential" );
 116              $actionObject = new $actionClass( $actionInfo, $httpRequest );
 117              $actionObject->setPreviousAction( $_plogController_previousAction );
 118  
 119              // we can use the validate method to check the values of the form variables. If validate()

 120              // returns 'true', then we call the 'perform' method. If not, then we won't :)

 121              if( $actionObject->validate()) {
 122                  if( $actionObject->perform()) {
 123                      // if everything went ok, let's move to the next step

 124                      $actionObject->setSuccess( true );
 125                      $nextStep = $this->getNextStep( $currentStep );
 126                  }
 127                  else {
 128                      // if there was an issue in the perform() method, let's retry the current step

 129                      $actionObject->setSuccess( false );
 130                      $nextStep = $currentStep;                        
 131                  }
 132              }
 133              else {
 134                  // retry the current step if there was a validation issue

 135                  $nextStep = $currentStep;    
 136              }
 137  
 138              // store the next step in the sequence, regardless of what it was

 139              SessionManager::setSessionValue( SEQUENTIAL_CONTROLLER_SESSION_PARAMETER, $nextStep );            
 140              
 141              $view = $actionObject->getView();
 142  
 143              if( empty( $view )) {
 144                  $e = new Exception( 'The view is empty after calling the perform method.' );
 145                  throw( $e );
 146              }
 147              else
 148                  $view->render();
 149          }
 150      }
 151  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics