<?php
QUnit_Global::includeClass('QUnit_UI_TemplatePage');

class Affiliate_Merchants_Views_SetupWizard extends QUnit_UI_TemplatePage
{
    var $currStep;
    var $totalSteps = 3;
    function Affiliate_Merchants_Views_SetupWizard() {
        $this->navigationAddURL(L_G_HOME,'index.php?md=home');
        $this->navigationAddURL(L_G_PZONE,'javascript:;');
        $this->navigationAddURL(L_G_SETUPWIZARD,'index.php?md=Affiliate_Merchants_Views_SetupWizard');
        $this->currStep = (int) $_POST['step'];
        $this->currStep = empty($this->currStep)?1:$this->currStep;
        $this->currStep = $this->currStep<0?1:$this->currStep;
    }

    //--------------------------------------------------------------------------
    function process(){
        $this->addContent('wizard_js');
        if($_POST['commited']=="yes"){
            $funcString = sprintf('$this->processSaveStep%s();',$this->currStep);
            eval($funcString);
            return;
        }
        $this->showCurrentStep();
    }
    //--------------------------------------------------------------------------
    function processSaveStep1(){
        if(!$this->checkForm1()){
            $this->currStep--;
            $_POST['finish']='no';
        }else{
            $sitename = preg_replace('/[\'\"]/', '', $_POST['sitename']);
            $main_site_url = preg_replace('/[\'\"]/', '', $_POST['main_site_url']);
            $system_email = preg_replace('/[\'\"]/', '', $_POST['system_email']);
            $notifications_email = preg_replace('/[\'\"]/', '', $_POST['notifications_email']);

            $squery[] = 'update `wd_pz_settings` set `svalue`='._q($sitename).' where `code`="sitename"';
            $squery[] = 'update `wd_g_settings` set `value`='._q($main_site_url).' where `code`="Aff_main_site_url" and `accountid`='._q($GLOBALS['Auth']->getAccountID());
            $squery[] = 'update `wd_g_settings` set `value`='._q($system_email).' where `code`="Aff_system_email" and `accountid`='._q($GLOBALS['Auth']->getAccountID());
            $squery[] = 'update `wd_g_settings` set `value`='._q($notifications_email).' where `code`="Aff_notifications_email" and `accountid`='._q($GLOBALS['Auth']->getAccountID());

            foreach($squery as $query){
                $GLOBALS['db']->Execute($query);
                $GLOBALS['dbrequests']++;
            }
        }
        if($_POST['finish']=='yes') $this->currStep = $this->totalSteps+1;
        $this->showNextStep();
    }
    function processSaveStep2(){
        if(!$this->checkForm2()){
            $this->currStep--;
            $_POST['finish']='no';
        }else{
            $middle_msg = $_POST['login_text_in_the_middle_msg'];//preg_replace('/[\'\"]/', '', $_POST['login_text_in_the_middle_msg']);
            $signup_terms =  $_POST['signup_terms_conditions'];// preg_replace('/[\'\"]/', '', $_POST['signup_terms_conditions']);

            $squery[] = 'update `wd_g_settings` set `value`='._q($middle_msg).' where `code`="Aff_login_text_in_the_middle_msg" and `accountid`='._q($GLOBALS['Auth']->getAccountID());
            $squery[] = 'update `wd_g_settings` set `value`='._q($signup_terms).' where `code`="Aff_signup_terms_conditions" and `accountid`='._q($GLOBALS['Auth']->getAccountID());

            foreach($squery as $query){
                $GLOBALS['db']->Execute($query);
                $GLOBALS['dbrequests']++;
            }
        }
        if($_POST['finish']=='yes') $this->currStep = $this->totalSteps+1;
        $this->showNextStep();
    }
    function processSaveStep3(){
        if(!$this->checkForm3()){
            $this->currStep--;
            $_POST['finish']='no';
        }else{
            $name = preg_replace('/[\'\"]/', '', $_POST['name']);
            $username = preg_replace('/[\'\"]/', '', $_POST['username']);
            $pwd = preg_replace('/[\'\"]/', '', $_POST['pwd']);

            list($usec, $sec) = explode(" ",microtime());
            $smicrotime = (float)$usec + (float)$sec;
            $ID = substr(md5($smicrotime),-8);
            $query = 'insert into `wd_g_users` (userid,accountid,username,rpassword,name,rstatus,rtype,dateinserted)values("%s","%s","%s","%s","%s",2,4,NOW())';
            $query = sprintf($query,$ID,$GLOBALS['Auth']->getAccountID(),$username,$pwd,$name);
            $GLOBALS['db']->Execute($query);
            $GLOBALS['dbrequests']++;
        }
        if($_POST['finish']=='yes') $this->currStep = $this->totalSteps+1;
        $this->showNextStep();
    }
    //--------------------------------------------------------------------------
    function checkForm1(){
        return true;
    }
    function checkForm2(){
        $flg = true;
        if(empty($_POST['signup_terms_conditions'])) {$this->addErrorMessage(L_G_REGRULEEMPTY);$flg=false;}
        return $flg;
    }
    function checkForm3(){
        $flg = true;
        if(empty($_POST['name'])) {$this->addErrorMessage(L_G_UNAMEEMPTY);$flg=false;}
        if(empty($_POST['username'])) {$this->addErrorMessage(L_G_EMAILEMPTY);$flg=false;}
        if(empty($_POST['pwd'])) {$this->addErrorMessage(L_G_PWD1EMPTY);$flg=false;}
        return $flg;
    }
    //--------------------------------------------------------------------------
    function showCurrentStep(){
        if($this->currStep>$this->totalSteps){
            $this->addOKMessage(L_G_SETUPWIZARD_SUCCESS);
            $this->addContent('wizard_finish');
        }else
            $this->addContent('wizard_step'.$this->currStep);
    }
    //--------------------------------------------------------------------------
    function showNextStep(){
        if($this->currStep>=$this->totalSteps){
            $this->addContent('wizard_finish');
            $this->addOKMessage(L_G_SETUPWIZARD_SUCCESS);
        }else
            $this->addContent('wizard_step'.($this->currStep+1));
    }
    //--------------------------------------------------------------------------
}
?>
