eZ Publish  [trunk]
ezwizardbase.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZWizardBase class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZWizardBase ezwizardbase.php
00013   \brief The class eZWizardBase does
00014 
00015 */
00016 
00017 class eZWizardBase
00018 {
00019     const STAGE_PRE = 0;
00020     const STAGE_POST = 1;
00021 
00022     /*!
00023      Constructor
00024 
00025      \param $tpl Template class
00026      \param $module Module
00027      \param $storageName Storage Name, optional.
00028     */
00029     function eZWizardBase( $tpl, &$module, $storageName = false )
00030     {
00031         if ( $storageName )
00032         {
00033             $this->StorageName = $storageName;
00034         }
00035 
00036         $this->TPL = $tpl;
00037         $this->Module = $module;
00038         $this->HTTP = eZHTTPTool::instance();
00039         $this->VariableList = $this->HTTP->sessionVariable( $this->StorageName . $this->VariableListName );
00040         $this->MetaData = $this->HTTP->sessionVariable( $this->StorageName . $this->MetaDataName );
00041 
00042         $this->initialize();
00043     }
00044 
00045     /*!
00046      Set needed variables.
00047     */
00048     function initialize()
00049     {
00050         if ( !$this->hasMetaData( 'current_step' ) )
00051         {
00052             $this->setMetaData( 'current_step', 0 );
00053         }
00054 
00055         if ( !$this->hasMetaData( 'current_stage' ) )
00056         {
00057             $this->setMetaData( 'current_stage', eZWizardBase::STAGE_PRE );
00058         }
00059     }
00060 
00061     function attributes()
00062     {
00063         return array( 'error_count',
00064                       'error_list',
00065                       'warning_count',
00066                       'warning_list',
00067                       'step_template',
00068                       'variable_list',
00069                       'url' );
00070     }
00071 
00072     function hasAttribute( $attr )
00073     {
00074         return in_array( $attr, $this->attributes() );
00075     }
00076 
00077     function attribute( $attr )
00078     {
00079         switch( $attr )
00080         {
00081             case 'error_count':
00082             {
00083                 return count( $this->ErrorList );
00084             } break;
00085 
00086             case 'error_list':
00087             {
00088                 return $this->ErrorList;
00089             } break;
00090 
00091             case 'warning_count':
00092             {
00093                 return count( $this->WarningList );
00094             } break;
00095 
00096             case 'warning_list':
00097             {
00098                 return $this->WarningList;
00099             } break;
00100 
00101             case 'step_template':
00102             {
00103                 return $this->stepTemplate();
00104             } break;
00105 
00106             case 'variable_list':
00107             {
00108                 return $this->variableList();
00109             } break;
00110 
00111             case 'url':
00112             {
00113                 return $this->WizardURL;
00114             } break;
00115 
00116             default:
00117             {
00118                 eZDebug::writeError( "Attribute '$attr' does not exist", __METHOD__ );
00119                 return null;
00120             }
00121             break;
00122         }
00123     }
00124 
00125     /*!
00126      Will run the wizard, and continue from the current step.
00127      This method will run postCheck, redirect to next, etc. depending on the current state.
00128 
00129      return Module Result or module redirect.
00130     */
00131     function run()
00132     {
00133         if ( $this->HTTP->hasPostVariable( 'PreviousButton' ) )
00134         {
00135             return $this->previousStep();
00136         }
00137 
00138         switch( $this->metaData( 'current_stage' ) )
00139         {
00140             case eZWizardBase::STAGE_PRE:
00141             {
00142                 $this->preCheck();
00143                 $this->nextStep();
00144                 if ( $this->skip() )
00145                 {
00146                     return $this->nextStep();
00147                 }
00148                 return $this->process();
00149             } break;
00150 
00151             case eZWizardBase::STAGE_POST:
00152             {
00153                 if ( $this->postCheck() )
00154                 {
00155                     return $this->nextStep();
00156                 }
00157                 else
00158                 {
00159                     return $this->process();
00160                 }
00161             } break;
00162         }
00163     }
00164 
00165     /*!
00166      \private
00167      \virtual
00168      Pre check current step to check that it's safe to execute current step.
00169      Return false if current step should not be processed, and set warning message
00170 
00171      \return true if everything ok
00172              false if not
00173     */
00174     function preCheck()
00175     {
00176         return true;
00177     }
00178 
00179     /*!
00180      \private
00181      \virtual
00182      Post check current step to check that it's safe to continue to next step.
00183      Return false if current step should be processed once again, and set warning message
00184 
00185      \return true if everything ok
00186              false if not
00187     */
00188     function postCheck()
00189     {
00190         if ( $this->HTTP->hasPostVariable( 'NextButton' ) )
00191         {
00192             return true;
00193         }
00194 
00195         return false;
00196     }
00197 
00198     /*!
00199      \private
00200      \virtual
00201      Return true to skip current step.
00202      Current step will not be processed.
00203 
00204      \return true skip current step.
00205              false - perform current step.
00206     */
00207     function skip()
00208     {
00209         return false;
00210     }
00211 
00212     /*!
00213      \virtual
00214      Process the current step, and present the HTML.
00215 
00216      \return Module Result
00217     */
00218     function process()
00219     {
00220     }
00221 
00222     /*!
00223      Store meta data.
00224 
00225      \param key
00226      \param value
00227     */
00228     function setMetaData( $key, $value )
00229     {
00230         $this->MetaData[$key] = $value;
00231         eZDebug::writeNotice( 'Set MetaData : [' . $key . '] = ' . $value, __METHOD__ );
00232         $this->savePersistentData();
00233     }
00234 
00235     /*!
00236      Get metadata
00237 
00238     */
00239     function metaData( $key )
00240     {
00241         return $this->MetaData[$key];
00242     }
00243 
00244     /*!
00245      Check if has metadata value
00246     */
00247     function hasMetaData( $key )
00248     {
00249         return isset( $this->MetaData[$key] );
00250     }
00251 
00252     /*!
00253      Store variable. Variable/value will be available in current and next wizard steps.
00254 
00255      \param key
00256      \param value
00257     */
00258     function setVariable( $key, $value )
00259     {
00260         $this->VariableList[$key] = $value;
00261         $this->savePersistentData();
00262     }
00263 
00264     /*!
00265      Get stored wizard values.
00266 
00267      \param key
00268 
00269      \return value
00270     */
00271     function &variable( $key )
00272     {
00273         if ( isset( $this->VariableList[$key] ) )
00274             $retValue = $this->VariableList[$key];
00275         else
00276             $retValue = false;
00277         return $retValue;
00278     }
00279 
00280     /*!
00281      Check if wizard variable exists
00282 
00283      \param $key key of the variable
00284 
00285      \return variable value
00286     */
00287     function hasVariable( $key )
00288     {
00289         return isset( $this->VariableList[$key] );
00290     }
00291 
00292     /*!
00293      Return variable list.
00294 
00295      \return variable list
00296     */
00297     function variableList()
00298     {
00299         return $this->VariableList;
00300     }
00301 
00302     /*!
00303      \private
00304      Get Step template name.
00305 
00306      \return current step template
00307     */
00308     function stepTemplate()
00309     {
00310         return $this->StepTemplateBase . '_' . ( $this->metaData( 'current_step' ) + 1 ) . '.tpl';
00311     }
00312 
00313     /*!
00314      Cleanup variables used during wizard
00315     */
00316     function cleanup()
00317     {
00318         $this->HTTP->removeSessionVariable( $this->StorageName . $this->MetaDataName );
00319         $this->HTTP->removeSessionVariable( $this->StorageName . $this->VariableListName );
00320         $this->MetaData = array();
00321         $this->VariableList = array();
00322     }
00323 
00324     /*!
00325      Go back to previous step
00326     */
00327     function previousStep()
00328     {
00329         $this->setMetaData( 'current_stage', eZWizardBase::STAGE_PRE );
00330         $this->setMetaData( 'current_step', $this->metaData( 'current_step' ) - 1 );
00331         $this->savePersistentData();
00332 
00333         return $this->Module->redirectTo( $this->WizardURL );
00334     }
00335 
00336     /*!
00337      Increate Step counter
00338     */
00339     function nextStep()
00340     {
00341         if ( $this->metaData( 'current_stage' ) == eZWizardBase::STAGE_PRE )
00342         {
00343             $this->setMetaData( 'current_stage', eZWizardBase::STAGE_POST );
00344         }
00345         else
00346         {
00347             $this->setMetaData( 'current_stage', eZWizardBase::STAGE_PRE );
00348             $this->setMetaData( 'current_step', $this->metaData( 'current_step' ) + 1 );
00349 
00350             return $this->Module->redirectTo( $this->WizardURL );
00351         }
00352 
00353         $this->savePersistentData();
00354     }
00355 
00356     /*!
00357      Save persistent data
00358     */
00359     function savePersistentData()
00360     {
00361         $this->HTTP->setSessionVariable( $this->StorageName . $this->MetaDataName, $this->MetaData );
00362         $this->HTTP->setSessionVariable( $this->StorageName . $this->VariableListName, $this->VariableList );
00363     }
00364 
00365     /* Private messages */
00366     public $ErrorList = array();
00367     public $WarningList = array();
00368 
00369     /* Step list, used to determine wizard steps */
00370     public $StepList = array();
00371 
00372     public $HTTP;
00373     public $Tpl;
00374     public $Module;
00375     public $WizardURL = ''; /* url to wizard */
00376 
00377     /* Array used to store wizzard values */
00378     public $VariableList = array();
00379     public $MetaData = array();
00380     public $StorageName = 'eZWizard';
00381     public $MetaDataName = '_meta';
00382     public $VariableListName = '_data';
00383 
00384     /* Step templates */
00385     public $StepTemplateBase = 'design:wizard/step';
00386 
00387     /* Array containing the wizard steps */
00388     public $StepArray = array();
00389 }
00390 
00391 ?>