Browse | Submit New Snippet | Create Package

 

FormWizard

Type:
Class
Category:
Other
License:
MIT/X Consortium License
Language:
PHP
 
Description:
A component for making form wizards

Versions Of This Snippet::

Simon Jaillet
Snippet ID Download Version Date Posted Author Delete
1581.02006-07-03 23:10Simon Jaillet

Download a raw-text version of this code by clicking on "Download Version"

 


Latest Snippet Version: :1.0

<?php

/**
 * FormWizard
 *
 *  Usage :
 *  
 *    1- ) set your ways
 *    ie:
 *    var $ways=array("way1" => array("start","sell0","recap"),
 *                              "way2" => array("start","buy0","buy1","recap"));
 *     
 *   
 *    2- ) for each "step", the controller should contain 2 functions     
 *    ie :
 *    start();startValidate();
 *    sell0();sell0Validate();
 *    buy0();buy0Validate();
 *    buy1();buy1Validate();
 *    recap();
 *    
 *   PS: if you need private access to each steps, you can use "way1" =>array("_start","_sell0","_recap")...
 *
 *
 *    3- )  for each "step", you need 1 view:
 *    ie:
 *    start.thtml , sell0.thtml, buy0.thtml, buy1.thtml, recap.thtml
 *    
 *
 *
 *    4- ) the entry point...
 *
 *    In the wizard controller you need:
 *    - Loading the FormWizard component
 *    - an entry point.
 *
 *    ie :
 *     
 *      var $components = array('FormWizard')
 *
 *      function register ()
 *      {
 *         $ways=array("way1" => array("start","sell0","recap"),
 *                              "way2" => array("start","buy0","buy1","recap"));
 *
 *         $this->FormWizard->initWizard($ways);
 *      }
 *     
 *    you could enter to the register wizard with the url : mydomain/yourwizardcontroller/register
 *
 *    
 *    4- ) changing way...
 *    you can change way according user entries in all xxxValidate functions
 *      
 *    ie:
 *      function startValidate()
 *     {
 *         if($this->User->validates($this->data))
 *         {
 *           if(isset($this->data["User"]["status"]) && $this->data["User"]["status"]=="seller")
 *             $this->FormWizard->changeWay("way1");  // you change to the first way
 *     
 *           if(isset($this->data["User"]["status"]) && $this->data["User"]["status"]=="buyer")
 *             $this->FormWizard->changeWay("way2");  // you change to the second way
 *           return true;
 *         }
 *       else
 *         {
 *           return false;
 *         }
 *      }
 *
 *
 *   4- ) last step
 *   Read User Entries
 *
 *   Ps: the wizard session vars are automatically cleared in the last step
 *   
 *   ie:
 *   function recap()
 *  {
 *    print_r($this->FormWizard->getWizardVars());
 *  }
 *   
 *  Example of a view file:
 *
 * <?php
 *    formTag(”/register/register/”,”post”);
 *      echo $html->input(’User/name’);
 *      echo $html->submit(”Previous Step”,array(”name”=>”previous”));
 *      echo $html->submit(”Next Step”,array(”name”=>”next”));
 *    </form>
 * ?>
 *
 *
 */
 
 class FormWizardComponent extends Object {

  var $ways=null;
  
  var $_prefixWizard;
  var $_wizardData=array();
  var $_way=null;
  var $_fctname;
  var $_curstep;
  
  var $controller;
  var $params;
  var $data;
  var $Session;
  
  /**
 * startup the Wizard
 *
 * @param array $ways An array containing all the wizard ways
 * @param string $prefix Wizard session var names  are prefixed by  $prefix
 * @return 
 */
 function startup(&$controller) {
    $this->controller= &$controller;
    $this->Session= &$controller->Session;
    $this->params = &$controller->params;
    $this->data = &$controller->data;
}

function initWizard(&$ways,$prefix="_Wiz") {
     
    $this->_prefixWizard=$prefix;;
    $this->ways=$ways;
    
    if($this->Session->check($this->_prefixWizard."Data"))
      $this->_wizardData=$this->Session->read($this->_prefixWizard."Data");
      
    if($this->Session->check($this->_prefixWizard."Way"))
      $this->_way=$this->Session->read($this->_prefixWizard."Way");
    else
      {
         $var=each($this->ways);
         $this->changeWay($var[0]);
       }

    if (!$this->Session->check($this->_prefixWizard."Step"))
      {
        $this->_initStep();
      }
    else
      {
        $this->_curstep=$this->Session->read($this->_prefixWizard."Step");

        if(isset($this->params['form']['previous']))
          {
            $this->_previousStep();
          }
        else
          {
            if(isset($this->params['form']['next']))
            {
              $this->_nextStep();
            }
            else
            {
              $this->_initStep($this->_curstep);
            }
          }
      }
      
      //$this->params['_WizardToken']['curstep'] = $this->_curstep;
      $this->Session->write($this->_prefixWizard."Step",$this->_curstep);        
      
      //  Last step reached... delete sessions vars 
      if($this->_curstep>=count($this->ways[$this->_way])-1)
        {
             $this->clearWizard($prefix);
        }

      $this->controller->render($this->_fctname);     
  }
  
  /**
 * Changing the Way in the wizard
 *
 * @param string $way The name of the new way to follow
 * @return 
 */
  function changeWay($way)
  {
    $this->_way=$way;
    $this->Session->write($this->_prefixWizard."Way",$this->_way);
  }
  
  
 /**
 * Read the values entered in the Wizard
 *
 * @return an array() containing all the values entered in the Wizard
 */
  function getWizardVars()
  {
     $vars=array();
     foreach($this->ways[$this->_way] as $step)
     {
        if(isset($this->_wizardData[$step]))
          {
                foreach($this->_wizardData[$step] as $key => $arr)
                {
                    if(isset($vars[$key]))
                      $vars[$key]=array_merge($vars[$key],$arr);
                    else
                      $vars[$key]=$arr;
                }
          }
      }
      return $vars;
  }
  
  
  /**
 * Return the actual way
 *
 * @return the actual way
 */
  function getWay()
  {
     return $this->_way;
  }
  
  /**
 * Clear the Wizard
 *
 * @return 
 */
   function clearWizard($prefix="_Wizard")
  {
    $this->clearStep($prefix);
    $this->clearData($prefix);
    $this->Session->delete($prefix."Way");
  }
  
  /**
 * Clear the CurStep
 *
 * @return 
 */
   function clearStep($prefix="_Wizard")
  {
    $this->Session->delete($prefix."Step");
  }
    /**
 * Clear the Wizard
 *
 * @return 
 */
   function clearData($prefix="_Wizard")
  {
    $this->Session->delete($prefix."Data");
  }
  
  //--------  Private Functions
  
  function _call($fctname,$suffix="")
  {
    $fct=$fctname.$suffix;
    if (method_exists($this->controller, $fct))
      {
       $fct=$fctname.$suffix;
       return $this->controller->$fct();
      }
     else
       return 1; 
  }

  function _updatePost($fctname)
  {
    if(isset($this->_wizardData[$fctname]))
    {
      foreach ($this->_wizardData[$fctname] as $model => $values)
      {
        foreach ($values as $field => $val)
        {
          if(!isset($this->params['data'][$model][$field]))
            {
              $this->params['data'][$model][$field]=$val;
            }
        }
      }
    }
  }
   
  function _initStep($step=0)
  {
     $this->_fctname=$this->ways[$this->_way][$step];
     $this->_updatePost($this->_fctname);   
     $this->_call($this->_fctname);
     $this->_curstep=$step; 
     if(isset($this->params['data']))
     {
        $this->_call($this->_fctname,"Validate");
     }
  }
  
  function _previousStep()
  {                       
    $this->_fctname=$this->ways[$this->_way][$this->_curstep];
    if(isset($this->params['data']))
    {
      $this->_wizardData[$this->_fctname]=$this->params['data'];
      $this->Session->write($this->_prefixWizard."Data",$this->_wizardData);
    }
    $this->_curstep=max(0,$this->_curstep-1);
    $this->_fctname=$this->ways[$this->_way][$this->_curstep];
    $this->_updatePost($this->_fctname);
    $this->_call($this->_fctname);  
  }

  function _nextStep()
  {      
    $this->_curstep=min(count($this->ways[$this->_way])-1,$this->_curstep);
    $this->_fctname=$this->ways[$this->_way][$this->_curstep];
    $this->_updatePost($this->_fctname);                            

    if(isset($this->params['data']) && $this->_call($this->_fctname,"Validate"))
      {
        $this->_wizardData[$this->_fctname]=$this->params['data'];
        $this->Session->write($this->_prefixWizard."Data",$this->_wizardData);
        $this->_curstep=min(count($this->ways[$this->_way])-1,$this->_curstep+1);
        $this->_fctname=$this->ways[$this->_way][$this->_curstep];
        $this->params['data']=array();
        $this->data=array();
        $this->_updatePost($this->_fctname);      
      }
     $this->_call($this->_fctname);
  }
}
?>
		

Submit a new version

You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..