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