Main Page | Class Hierarchy | Class List | File List | Class Members | File Members | Related Pages

wizard-james.class.php

Go to the documentation of this file.
00001 <?
00002 
00003 include_once("core/html/dialog-james.class.php");
00004 include_once("core/i18n/I18N.class.php");
00005 
00006 define ("WIZARD_START_TEXT",gettext(" Start "));
00007 define ("WIZARD_START_NAME", "start");
00008 define ("WIZARD_START_TYPE", "submit");
00009 define ("WIZARD_START_ONCLICK", "");
00010 
00011 define ("WIZARD_END_TEXT",gettext(" End "));
00012 define ("WIZARD_END_NAME", "end");
00013 define ("WIZARD_END_TYPE", "submit");
00014 define ("WIZARD_END_ONCLICK", "");
00015 
00016 define ("WIZARD_NEXT_TEXT",gettext(" Next "));
00017 define ("WIZARD_NEXT_NAME", "next");
00018 define ("WIZARD_NEXT_TYPE", "submit");
00019 define ("WIZARD_NEXT_ONCLICK", "");
00020 
00021 
00023 
00036 class WizardJames{
00037 
00038         var $debug = "ON";
00039                 
00040         var $dialog;
00041         var $end;
00042         var $endCaption;
00043         var $steps;
00044         var $curStep;
00045         var $title;
00046         var $vars;
00047         var $error = false;
00048         var $errorMsg;
00049 
00050         function WizardJames($title=0){
00051                 $this->steps = array();
00052                 $this->vars = array();
00053 
00054                 
00055                 // Following If is refered whereas we are starting 
00056                 // the wizard.
00057 
00058                 // NOTE: When we push the button, we have a variable called
00059                 // state with value execution; this means that we have to execute
00060                 // the handling function.
00061 
00062                 if ($title){
00063                         // If title exist, we are starting the wizard
00064                         // so we must initialize all the stuff
00065                         $this->title = $title;
00066                         $this->end = 0;
00067                         $this->dialog = new DialogJames($title);
00068                         $this->AddStep($_SERVER["PHP_SELF"]);
00069                         $this->SetCurrentStep();
00070                         $this->SaveState();
00071                 }else{
00072                         // If title NOT exist, we are "in the middle" of the wizard
00073                         // so we must load state from sesion
00074                         $this->LoadState();
00075                         $this->SetCurrentStep();
00076                         $this->GetRequestVars();
00077                         //$this->Debug("WizardJames: this",$this);
00078                         if($_REQUEST["WJ_ExecStep"] == $this->curStep){         
00079                                 $this->error = !$this->ExecHandler();
00080                                 if(!$this->error){              
00081                                         $this->RedirectNextStep();
00082                                 }
00083                         }
00084                 }
00085         }
00086 
00087         function RedirectNextStep(){
00088                 
00089                 $nextURL = $this->GetNextURLStep();
00090 
00091                 $this->curStep++;
00092                 $this->SaveState();
00093 
00094                 header("location: ".$nextURL);
00095                 die();
00096 
00097         }
00098 
00099 
00100         function NumSteps(){
00101                 return count($this->steps);
00102         }
00103 
00104         function SetCurrentStep(){
00105                 $this->curStep = array_search(basename($_SERVER["PHP_SELF"]),$this->steps);
00106                 if ( ($this->curStep < 0) || ($this->curStep > $this->NumSteps()) )
00107                         die(gettext("Error getting the Step number"));
00108         }
00109 
00110         function GetCurURLStep(){
00111                 return $this->steps[$this->curStep];
00112         }
00113 
00114         
00115         // Returns the next "nominal" URL, it won't be
00116         // loaded until "header" 
00117         function GetNextURLStep(){
00118                 $NextURL = $this->GetCurURLStep(); 
00119                 if (!$this->IsLastStep())
00120                         $NextURL =  $this->steps[$this->curStep+1];
00121                 else{
00122                         if ($this->end)
00123                                 $NextURL = $this->end;
00124                 }
00125                 return $NextURL;
00126         }
00127 
00128         // Returns the next Page that will be loaded, perharps
00129         // only for make a "header" statement that will redirect
00130         // to another page
00131         // At the moment, is for working around the first Step.
00132         function GetRealNextURLStep(){
00133                 if (!$this->IsFirstStep())
00134                         $RealNextURL = $this->GetCurURLStep();
00135                 else{
00136                         $RealNextURL = $this->GetNextURLStep();
00137                 }
00138                 return $RealNextURL;
00139         }
00140 
00141 
00142         function IsFirstStep(){
00143                 return ($this->curStep == 0);
00144         }
00145 
00146         function IsLastStep(){
00147                 return ($this->curStep == $this->NumSteps()-1);
00148         }
00149 
00150         function PrintHeader(){ 
00151                 $this->SetHeader();
00152                 echo "<form enctype='multipart/form-data' method='POST' action='".$this->GetRealNextURLStep()."'>";
00153                 echo "<input type='hidden' name='WJ_ExecStep' value='".$this->curStep."'>";
00154                 $this->dialog->PrintHeader();
00155                 $this->PrintError(); //... if needed
00156         }       
00157 
00158         function PrintError(){
00159                 if($this->error){
00160                         echo "<hr class='black'>";
00161                         if($this->errorMsg){
00162                                 echo "<center><b>Missing or wrong data, please correct mistake; error details:</b></center>";
00163                                 echo "<br>";
00164                                 echo "<font class='error'>".$this->errorMsg."</font>";
00165                                 echo "<br>";
00166                                 $this->errorMsg = "";
00167                         }else{
00168                                 echo "<center><b>Missing or wrong data, please correct mistake</b></center>";
00169                         }
00170 
00171                         echo "<hr class='black'>";
00172                         $this->error = false;
00173                 }
00174         }
00175 
00176         function SetEnd($end,$caption=""){
00177                 $this->end = $end;
00178                 if ($caption)
00179                         $this->endCaption = $caption;
00180         }
00181 
00182         function SetFoot(){
00183                 if ($this->IsLastStep()){
00184                         $text = WIZARD_END_TEXT;
00185                         if($this->endCaption)
00186                                 $text = $this->endCaption;
00187                         $this->dialog->setOK($text,WIZARD_END_NAME,WIZARD_END_TYPE,WIZARD_END_ONCLICK);
00188                 }else if ($this->IsFirstStep()) {
00189                         $this->dialog->setOK(WIZARD_START_TEXT,WIZARD_START_NAME,WIZARD_START_TYPE,WIZARD_START_ONCLICK);
00190                 }else{
00191                         $this->dialog->setOK(WIZARD_NEXT_TEXT,WIZARD_NEXT_NAME,WIZARD_NEXT_TYPE,WIZARD_NEXT_ONCLICK);
00192                 }
00193         }
00194 
00195         function SetHeader(){
00196                 $this->dialog->title    = $this->title
00197                                         . " (".gettext("Step")." "
00198                                         . ($this->curStep+1)
00199                                         . " ".gettext("of")." "
00200                                         . $this->NumSteps()
00201                                         . ")";
00202         }
00203 
00204         function PrintFoot(){
00205                 $this->SetFoot();
00206                 $this->dialog->PrintFoot();
00207                 echo "</form>";
00208                 $this->SaveState();
00209         }       
00210 
00211         function GetSecVar($name){
00212                 if ($this->vars[$name])
00213                         return $this->vars[$name];
00214                 else
00215                         return "none";
00216         }
00217 
00218         function GetVar($name){
00219                 return $this->vars[$name];              
00220         }
00221 
00222         function SetVar($name,$value){
00223                 $this->vars[$name] = $value;            
00224         }
00225 
00226         function PrintVar($name,$type,$default=""){
00227                 if($this->GetVar($name)){
00228                         $default = $this->GetVar($name);
00229                 }
00230                 echo "<input name='WJVar[$name]' type='$type' value='$default' class='wizard'>";
00231         }
00232 
00233         function SaveState(){
00234                 // Save data (and state) of wizard 
00235                 @session_start();
00236                 $_SESSION["WizardJames"] = $this;
00237         }
00238 
00239         function LoadState(){
00240                 // Save data (and state) of wizard 
00241                 @session_start();
00242 
00243                 //$this->Debug("LoadState",$_SESSION);
00244 
00245                 $wizard = $_SESSION["WizardJames"];
00246                 if ($wizard){
00247                         $this->copyAttributes($wizard);
00248                 }else{
00249                         // If there isn't in session... something wrong is
00250                         // going on...
00251                         die(gettext("Couldn't Load Wizard State and Information from Session. Please Return to first page."));
00252                 }
00253         }
00254 
00255         function copyAttributes($obj){
00256 
00257                 
00258                 // Elegant copy of attributes...
00259                 $classAttributes = get_class_vars(get_class($this));
00260                 foreach ($classAttributes as $name=>$value) {
00261                         $estatement = '$this->'.$name.' = $obj->'.$name.';';
00262                         eval($estatement);
00263                 }       
00264 
00265                 // In some versions of PHP above code doesn't work so...
00266                 if($this->title != $obj->title){
00267 
00268                         // We do it by "brute" style...
00269                         $this->title = $obj->title;
00270                         $this->end = $obj->end;
00271                         $this->endCaption = $obj->endCaption;
00272                         $this->vars = $obj->vars;
00273                         $this->dialog = $obj->dialog;
00274                         $this->steps = $obj->steps;
00275                 }
00276 
00277         }
00278 
00279         function GetRequestVars(){
00280 
00281                 if($_REQUEST["WJVar"]){
00282                         foreach($_REQUEST["WJVar"] as $var=>$val){
00283                                 $this->vars[$var] = $val;
00284                         }
00285                 }
00286                 
00287 
00288         }
00289 
00290         function AddStep($phpfile,$i=-1){
00291                 // A little of caution...
00292                 $phpfile = basename($phpfile);
00293 
00294                 if ($i>=0)
00295                         $this->steps[$i] = $phpfile;
00296                 else
00297                         $this->steps[] = $phpfile;
00298         }
00299 
00300         function ExecHandler(){
00301                 if(function_exists("WizardHandler")){
00302                         return WizardHandler(&$this);           
00303                 }else{
00304                         return true;
00305                 }       
00306         }
00307 
00308 
00309         function Debug($pre,$v){
00310                 
00311                 if ($this->debug == "ON"){
00312                         echo "<pre style='font-size: 11px'>";
00313                         echo $pre.":\n";
00314                         print_r($v);
00315                         echo "</pre>";
00316                 }
00317         }       
00318 
00319         function Dump(){
00320                 echo "<pre style='font-size: 11px'>";
00321                 print_r($this);
00322                 echo "</pre>";
00323         }       
00324 }
00325 
00326 ?>

Generated on Wed Nov 19 20:29:35 2003 for James by doxygen 1.3.4