00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 define( 'EZ_WIZARD_STAGE_PRE', 0 );
00041 define( 'EZ_WIZARD_STAGE_POST', 1 );
00042
00043 class eZWizardBase
00044 {
00045
00046
00047
00048
00049
00050
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
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', EZ_WIZARD_STAGE_PRE );
00081 }
00082 }
00083
00084
00085
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
00100
00101 function hasAttribute( $attr )
00102 {
00103 return in_array( $attr, $this->attributes() );
00104 }
00105
00106
00107
00108
00109 function &attribute( $attr )
00110 {
00111 switch( $attr )
00112 {
00113 case 'error_count':
00114 {
00115 $retValue = count( $this->ErrorList );
00116 } break;
00117
00118 case 'error_list':
00119 {
00120 $retValue = $this->ErrorList;
00121 } break;
00122
00123 case 'warning_count':
00124 {
00125 $retValue = count( $this->WarningList );
00126 } break;
00127
00128 case 'warning_list':
00129 {
00130 $retValue = $this->WarningList;
00131 } break;
00132
00133 case 'step_template':
00134 {
00135 $retValue = $this->stepTemplate();
00136 } break;
00137
00138 case 'variable_list':
00139 {
00140 $retValue = $this->variableList();
00141 } break;
00142
00143 case 'url':
00144 {
00145 $retValue = $this->WizardURL;
00146 } break;
00147
00148 default:
00149 {
00150 eZDebug::writeError( "Attribute '$attr' does not exist", 'eZWizardBase::attribute' );
00151 $retValue = null;
00152 }
00153 break;
00154 }
00155 return $retValue;
00156 }
00157
00158
00159
00160
00161
00162
00163
00164 function run()
00165 {
00166 if ( $this->HTTP->hasPostVariable( 'PreviousButton' ) )
00167 {
00168 return $this->previousStep();
00169 }
00170
00171 switch( $this->metaData( 'current_stage' ) )
00172 {
00173 case EZ_WIZARD_STAGE_PRE:
00174 {
00175 $this->preCheck();
00176 $this->nextStep();
00177 if ( $this->skip() )
00178 {
00179 return $this->nextStep();
00180 }
00181 return $this->process();
00182 } break;
00183
00184 case EZ_WIZARD_STAGE_POST:
00185 {
00186 if ( $this->postCheck() )
00187 {
00188 return $this->nextStep();
00189 }
00190 else
00191 {
00192 return $this->process();
00193 }
00194 } break;
00195 }
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207 function preCheck()
00208 {
00209 return true;
00210 }
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221 function postCheck()
00222 {
00223 if ( $this->HTTP->hasPostVariable( 'NextButton' ) )
00224 {
00225 return true;
00226 }
00227
00228 return false;
00229 }
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 function skip()
00241 {
00242 return false;
00243 }
00244
00245
00246
00247
00248
00249
00250
00251 function process()
00252 {
00253 }
00254
00255
00256
00257
00258
00259
00260
00261 function setMetaData( $key, $value )
00262 {
00263 $this->MetaData[$key] = $value;
00264 eZDebug::writeNotice( 'Set MetaData : [' . $key . '] = ' . $value,
00265 'eZWizardBase::setMetaData()' );
00266 $this->savePersistentData();
00267 }
00268
00269
00270
00271
00272
00273 function metaData( $key )
00274 {
00275 return $this->MetaData[$key];
00276 }
00277
00278
00279
00280
00281 function hasMetaData( $key )
00282 {
00283 return isset( $this->MetaData[$key] );
00284 }
00285
00286
00287
00288
00289
00290
00291
00292 function setVariable( $key, $value )
00293 {
00294 $this->VariableList[$key] = $value;
00295 $this->savePersistentData();
00296 }
00297
00298
00299
00300
00301
00302
00303
00304
00305 function &variable( $key )
00306 {
00307 if ( isset( $this->VariableList[$key] ) )
00308 $retValue = $this->VariableList[$key];
00309 else
00310 $retValue = false;
00311 return $retValue;
00312 }
00313
00314
00315
00316
00317
00318
00319
00320
00321 function hasVariable( $key )
00322 {
00323 return isset( $this->VariableList[$key] );
00324 }
00325
00326
00327
00328
00329
00330
00331 function variableList()
00332 {
00333 return $this->VariableList;
00334 }
00335
00336
00337
00338
00339
00340
00341
00342 function stepTemplate()
00343 {
00344 return $this->StepTemplateBase . '_' . ( $this->metaData( 'current_step' ) + 1 ) . '.tpl';
00345 }
00346
00347
00348
00349
00350 function cleanup()
00351 {
00352 $this->HTTP->removeSessionVariable( $this->StorageName . $this->MetaDataName );
00353 $this->HTTP->removeSessionVariable( $this->StorageName . $this->VariableListName );
00354 $this->MetaData = array();
00355 $this->VariableList = array();
00356 }
00357
00358
00359
00360
00361 function previousStep()
00362 {
00363 $this->setMetaData( 'current_stage', EZ_WIZARD_STAGE_PRE );
00364 $this->setMetaData( 'current_step', $this->metaData( 'current_step' ) - 1 );
00365 $this->savePersistentData();
00366
00367 return $this->Module->redirectTo( $this->WizardURL );
00368 }
00369
00370
00371
00372
00373 function nextStep()
00374 {
00375 if ( $this->metaData( 'current_stage' ) == EZ_WIZARD_STAGE_PRE )
00376 {
00377 $this->setMetaData( 'current_stage', EZ_WIZARD_STAGE_POST );
00378 }
00379 else
00380 {
00381 $this->setMetaData( 'current_stage', EZ_WIZARD_STAGE_PRE );
00382 $this->setMetaData( 'current_step', $this->metaData( 'current_step' ) + 1 );
00383
00384 return $this->Module->redirectTo( $this->WizardURL );
00385 }
00386
00387 $this->savePersistentData();
00388 }
00389
00390
00391
00392
00393 function savePersistentData()
00394 {
00395 $this->HTTP->setSessionVariable( $this->StorageName . $this->MetaDataName, $this->MetaData );
00396 $this->HTTP->setSessionVariable( $this->StorageName . $this->VariableListName, $this->VariableList );
00397 }
00398
00399
00400 var $ErrorList = array();
00401 var $WarningList = array();
00402
00403
00404 var $StepList = array();
00405
00406 var $HTTP;
00407 var $Tpl;
00408 var $Module;
00409 var $WizardURL = '';
00410
00411
00412 var $VariableList = array();
00413 var $MetaData = array();
00414 var $StorageName = 'eZWizard';
00415 var $MetaDataName = '_meta';
00416 var $VariableListName = '_data';
00417
00418
00419 var $StepTemplateBase = 'design:wizard/step';
00420
00421
00422 var $StepArray = array();
00423 }
00424
00425 class eZWizardBaseClassLoader
00426 {
00427
00428
00429
00430
00431 function createClass( &$tpl,
00432 &$module,
00433 $stepArray,
00434 $basePath,
00435 $storageName = false,
00436 $metaData = false )
00437 {
00438 if ( !$storageName )
00439 {
00440 $storageName = 'eZWizard';
00441 }
00442
00443 if ( !$metaData )
00444 {
00445 $http =& eZHTTPTool::instance();
00446 $metaData = $http->sessionVariable( $storageName . '_meta' );
00447 }
00448
00449 if ( !isset( $metaData['current_step'] ) ||
00450 $metaData['current_step'] < 0 )
00451 {
00452 $metaData['current_step'] = 0;
00453 eZDebug::writeNotice( 'Setting wizard step to : ' . $metaData['current_step'],
00454 'eZWizardBaseClassLoader::createClass()' );
00455 }
00456 $currentStep = $metaData['current_step'];
00457
00458 if ( count( $stepArray ) <= $currentStep )
00459 {
00460 eZDebug::writeError( 'Invalid wizard step count: ' . $currentStep,
00461 'eZWizardBaseClassLoader::createClass()' );
00462 return false;
00463 }
00464
00465 $filePath = $basePath . $stepArray[$currentStep]['file'];
00466 if ( !file_exists( $filePath ) )
00467 {
00468 eZDebug::writeError( 'Wizard file not found : ' . $filePath,
00469 'eZWizardBaseClassLoader::createClass()' );
00470 return false;
00471 }
00472
00473 include_once( $filePath );
00474
00475 $className = $stepArray[$currentStep]['class'];
00476 eZDebug::writeNotice( 'Creating class : ' . $className,
00477 'eZWizardBaseClassLoader::createClass()' );
00478 $returnClass = new $className( $tpl, $module, $storageName );
00479
00480 if ( isset( $stepArray[$currentStep]['operation'] ) )
00481 {
00482 $operation = $stepArray[$currentStep]['operation'];
00483 return $returnClass->$operation();
00484 eZDebug::writeNotice( 'Running : "' . $className . '->' . $operation . '()". Specified in StepArray',
00485 'eZWizardBaseClassLoader::createClass()' );
00486 }
00487
00488 if ( isset( $metaData['current_stage'] ) )
00489 {
00490 $returnClass->setMetaData( 'current_stage', $metaData['current_stage'] );
00491 eZDebug::writeNotice( 'Setting wizard stage to : ' . $metaData['current_stage'],
00492 'eZWizardBaseClassLoader::createClass()' );
00493 }
00494
00495 return $returnClass;
00496 }
00497 }
00498
00499 ?>