Dissecting ZervWizard.class.php for the good of many!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dissonantallure
Forum Newbie
Posts: 21
Joined: Tue Feb 03, 2009 7:48 pm

Dissecting ZervWizard.class.php for the good of many!

Post by dissonantallure »

I have been working with a PHP class called ZervWizard.class.php which used for Creating Multi-Step Forms And Wizards In PHP. You can find more information as well as the source here:

http://www.phpriot.com/articles/multi-step-wizards

As you can tell there are many possibilities with this class however, I found that the instructions are lacking. I also have read countless posts on many different forums begging for decent advice on implementing Javascript, check boxes, radio buttons, and most of all, a way to skip between steps depending on a variables value. For instance,

Choose one:

Boy (step 1)
Girl (step 2)
Other (step 3)

I have successfully implemented check boxes and radio buttons and I am more than happy to provide the code:

Code: Select all

//Radio Input SC Wizzard
<?php foreach ($wizard->countries as $k => $v) { ?>
<?= $v ?>
<input type="radio" name="country" value="<?= $k ?>"  <?php if ($wizard->getValue('country') == $k) { ?>checked="checked"<?php } ?>/>
<?php } ?>
 
<?php if ($wizard->isError('country')) { ?>
<?= $wizard->getError('country') ?>
<?php } ?>
 

Code: Select all

// CheckBox SC Wizzard
<?php
$Check = $this->coalesce($form['Check']);
if (strlen($Check) > 0)
$this->setValue('Check', $Check);
            
if(is_null($Check))
$this->addError('Check', 'Please make a selection');
?>
 
<label><input type="checkbox" name="Check[]" value="Shopping Cart" />Shopping Cart</label><br/>
<label><input type="checkbox" name="Check[]" value="Message Board" />Message Board</label><br/>
<label><input type="checkbox" name="Check[]" value="Realtime Web Site Tracking" />Realtime Web Site Tracking</label><br/>
<label><input type="checkbox" name="Check[]" value="SSL Encryption" />SSL Encryption</label><br/>
<label><input type="checkbox" name="Check[]" value="Password Protected Logins" />Password Protected Logins</label><br/>
<label><input type="checkbox" name="Check[]" value="Flash Animation" />Flash Animation</label><br/>
<label><input type="checkbox" name="Check[]" value="Content Management System" />Content Management System</label><br/>
<label><input type="checkbox" name="Check[]" value="Spam Free Contact Form" />Spam Free Contact Form</label><br/>
<label><input type="checkbox" name="Check[]" value="News Letter" />News Letter</label><br/>
<label><input type="checkbox" name="Check[]" value="RSS Feed" />RSS Feed</label><br/>
<label><input type="checkbox" name="Check[]" value="None" />None</label><br />
<label><input type="checkbox" name="Check[]" value="Other1"  onclick="SCdiv('Oth1');"  />Other</label><br />
 
<?php if ($wizard->isError('Check')) { ?>
<?= $wizard->getError('Check') ?>
<?php } ?>
As you may tell I am a Novice PHP coder and I definitely need help in conquering the last problem "skipping between steps".

I have the following code which I feel is very close but I can't figure out how to set the variable $step.

Allow me to first show some of the functions which are used to create the steps(please follow the link and download the source for a better understanding too long to post it all)

Code: Select all

function process($action, &$form, $process = true)
        {
            if ($action == $this->resetAction) {
                $this->clearContainer();
                $this->setCurrentStep($this->getFirstIncompleteStep());
            }
            else if (isset($form['previous']) && !$this->isFirstStep()) {
                // clear out errors
                $this->_errors = array();
 
                $this->setCurrentStep($this->getPreviousStep($action));
                $this->doRedirect();
            }
            else {
                $proceed = false;
 
                // check if the step to be processed is valid
                if (strlen($action) == 0)
                    $action = $this->getExpectedStep();
 
                if ($this->stepCanBeProcessed($action)) {
                    if ($this->getStepNumber($action) <= $this->getStepNumber($this->getExpectedStep()))
                        $proceed = true;
                    else
                        $proceed = false;
                }
 
                if ($proceed) {
 
                    if ($process) {
                        // clear out errors
                        $this->_errors = array();
 
                        // processing callback must exist and validate to proceed
                        $callback = 'process_' . $action;
                        $complete = method_exists($this, $callback) && $this->$callback($form);
 
                        $this->container[$this->_step_status_key][$action] = $complete;
 
                        if ($complete)
                            $this->setCurrentStep($this->getFollowingStep($action)); // all ok, go to next step
                        else
                            $this->setCurrentStep($action); // error occurred, redo step
 
                        // final processing once complete
                        if ($this->isComplete())
                            $this->completeCallback();
 
                        $this->doRedirect();
                    }
                    else
                        $this->setCurrentStep($action);
                }
                else // when initally starting the wizard
                    $this->setCurrentStep($this->getFirstIncompleteStep());
            }
 
            // setup any required data for this step
            $callback = 'prepare_' . $this->getStepName();
            if (method_exists($this, $callback))
                $this->$callback();
 
        }
 
 
        /**
         * completeCallback
         *
         * Function to run once the final step has been processed and is valid.
         * This should be overwritten in child classes
         */
        function completeCallback()
        { }
 
 
        function doRedirect()
        {
            if ($this->coalesce($this->options['redirectAfterPost'], false)) {
                $redir = $_SERVER['REQUEST_URI'];
                $redir = preg_replace('/\?' . preg_quote($_SERVER['QUERY_STRING'], '/') . '$/', '', $redir);
                header('Location: ' . $redir);
                exit;
            }
        }
        /**
         * isComplete
         *
         * Check if the form is complete. This can only be properly determined
         * after process() has been called.
         *
         * @return  bool    True if the form is complete and valid, false if not
         */
        function isComplete()
        {
            return $this->_complete;
        }
 
 
        /**
         * setCurrentStep
         *
         * Sets the current step in the form. This should generally only be
         * called internally but you may have reason to change the current
         * step.
         *
         * @param   string  $step   The step to set as current
         */
        function setCurrentStep($step)
        {
            if (is_null($step) || !$this->stepExists($step)) {
                $this->_complete = true;
                $this->container[$this->_step_expected_key] = null;
            }
            else {
                $this->_currentStep = $step;
                $this->container[$this->_step_expected_key] = $step;
            }
        }
Now take a look at my code and see if you can help me figure out how to define the proper $step value;

Code: Select all

<?php
    require_once('ZervWizard.class.php');
 
    class SubatomicCreations extends ZervWizard
    {
        function SubatomicCreations()
        {
            // start the session and initialize the wizard
            session_start();
            parent::ZervWizard($_SESSION, __CLASS__);
 
 
            // create the steps, we're only making a simple 3 step form
            $this->addStep('step1', 'Step1');
            $this->addStep('step2', 'step2');
            $this->addStep('step3', 'step3');
            $this->addStep('confirm', 'confirm');
        }
 
        // here we prepare the user details step. all we really need to
        // do for this step is generate the list of times.
 
    
 
        // now we process the first step. we've simplified things, so we're
        // only collecting, name, email address and country
 
        function process_step1(&$form)
        {
            $Check = $this->coalesce($form['Check']);
            
            if (strlen($Check) > 0)
            $this->setValue('Check', $Check);
            
            elseif (is_null($Check))
            $this->addError('Check', 'Please make a selection');
            
            elseif (array_key_exists('key', $Check))
            // DEFINE $STEP HERE
 
            elseif (array_key_exists('key', $Check))
            // DEFINE $STEP HERE
            
            return !$this->isError();
        }
 
    function process_step2(&$form)
        {
        return !$this->isError();
        }
 
        function process_step3(&$form)
        {
         return !$this->isError();
        }
           
        // the final step is the confirmation step. there's nothing to prepare here
        // as we're just asking for final acceptance from the user
 
        function process_confirm(&$form)
        {
            $confirm = (bool) $this->coalesce($form['confirm'], true);
 
            return $confirm;
        }
Please help me solve this problem so I may write a small tutorial on how to use this class to its fullest potential. There are 20 or so functions in ZervWizard.class.php and I would like to dissect and understand all of them. At this point I am thinking I might need to create a new function in ZervWizard.class.php in order to skip steps depending on a variables value. Thanks to anyone who even read my post! I look forward to everyone's reply's!
Post Reply