<?php

require_once 'modules/admin/views/SignupView.php';
require_once 'newedge/classes/NE_PluginCollection.php';

/**
* @package Admin
*/
class Signup_3b_PhoneVerification extends SignupView
{
    var $phoneVerificationPlugins;
    
    var $jsLibs = array(    'newedge/javascript/prototype.php',
    );

    function dispatch()
    {
        $fraudmsg = '';
        if(isset($_REQUEST['fraudmsg'])) {
            $fraudmsg = $_REQUEST['fraudmsg'];
        }

        $this->phoneVerificationPlugins = new NE_PluginCollection('phoneverification', $this->user);
        while ($phoneVerificationPlugin = $this->phoneVerificationPlugins->getNext()) {

            // if plugin is not enabled, continue with next one
            if (!$this->settings->get("plugin_".$phoneVerificationPlugin->getInternalName()."_Enabled")) {
                continue;
            }


            // if plugin is enabled but the conditions to trigger it are not met,
            // then break the loop and continue with the next signup step.
            if ($this->settings->get("plugin_".$phoneVerificationPlugin->getInternalName()."_Minimum Bill Amount to Trigger Telephone Verification") > $_POST['totalBill']
                && (    isset($_POST['fraudScore'])
                        && $this->settings->get("plugin_".$phoneVerificationPlugin->getInternalName()."_Minimum Fraud Score to Trigger Telephone Verification") > $_POST['fraudScore']))
            {
                break;
            }

            $this->_showPhoneVerificationForm($phoneVerificationPlugin);
            exit;
        }

        $fraudmsg = '';
        if(isset($_REQUEST['fraudmsg'])) $fraudmsg = $_REQUEST['fraudmsg'];
        require_once 'modules/admin/views/Signup_4_CreateAccounts.php';
        $createAccounts = new Signup_4_CreateAccounts('admin', 'Signup_4_CreateAccounts', $this->settings);
        $createAccounts->setTpl($this->tpl);
        $createAccounts->setUser($this->user);
        $createAccounts->setCustomer($this->customer);
        $createAccounts->dispatch();
        return;
    }

    function _showPhoneVerificationForm(&$phoneVerificationPlugin)
    {
        $this->initialize();

        $phonenum = '';
        foreach ($_POST as $key => $val) {
            if ($key == 'phoneField') {
                $phoneVerificationPlugin->setPhoneNumber($phonenum = $_REQUEST[$val]);
            }
            if ($key == 'langField' && in_array($_REQUEST[$val], $phoneVerificationPlugin->getSupportedLanguages())) {
                $phoneVerificationPlugin->setLanguage($_REQUEST[$val]);
            }
        }
        
        $fraudmsg = '';
        if(isset($_REQUEST['fraudmsg'])) $fraudmsg = $_REQUEST['fraudmsg'];

        /* Need to check if we've already called in case they refresh the page
         * Don't want to waste all our phone credits on the same customer.
         * Clicking the back button or closing the browser will clear the session variable
         */
        if (!isset($_SESSION['phoneverification'])) {
            $_SESSION['phoneverification'] = rand(1000, 9999);
            $result = $phoneVerificationPlugin->execute($_SESSION['phoneverification']);
            if ($failureMessages = $phoneVerificationPlugin->getFailureMessages()) {
                require_once 'modules/admin/views/Signup_2_OrderForm.php';
                $orderForm = new Signup_2_OrderForm('admin', 'Signup_2_OrderForm', $this->settings);
                $orderForm->setTpl($this->tpl);
                $orderForm->setUser($this->user);
                $orderForm->dispatch($failureMessages);
                return;
            }

            $outputkeys = array_keys($result);
            $numoutputkeys = count($result);
            for ($i = 0; $i < $numoutputkeys; $i++) {
                $key = $outputkeys[$i];
                $value = $result[$key];
                $fraudmsg .= $key . " = " . $value . "\n";
            }

            $phoned = 0;
        } else {
            $phoned = 1;
        }
        $this->templateInit();

        $this->tpl->define(array( "phonecode_form"    => "signup/include/phone_verif.tpl"));
        //Testing Only
        //echo $_SESSION['phoneverification'];
        //End Testing Only
        $this->showProgress($this->lang('Phone Verification'),95);
        $this->tpl->assign("SUBMITFUNCTION","SubmitPreStepFour");
        $this->tpl->assign("BACKFUNCTION","BackPreStepFour");
        $this->tpl->assign("BACKINPUTNAME","back");
        $this->tpl->assign("BACKINPUTVALUE","1");
        $this->tpl->assign("SIZEOFBOX","65");
        $this->tpl->assign("INPUTNAME","next");
        $this->tpl->assign("WIDTHOFBOX","500");
        $this->tpl->assign("INPUTVALUE","1");
        $this->tpl->assign("SIGNUPBLOCKDESCRIPTION",$this->lang("Telephone Verification"));

        if (!$phoned) {
            $this->tpl->assign(array(
                'verifBox1'     => $this->lang("Please wait, you will be called at"),
                'phoneNum'      => $phonenum,
                'verifBox2'     => $this->lang("within 30 seconds.<br>"),
                'verifBox3'     => $this->lang("Please enter the code given to you by the robotic system below."),
            ));
            $this->tpl->parse('trash', 'first_verification');
        } else {
            $this->tpl->assign(array(
                'verifPostCall' => $this->lang("According to our records we have previously phoned you.  If you did not receive a call within 30 seconds please click the back button and verify that your phone number is in the correct format. Please be aware that a new code will be issued"),
            ));
            $this->tpl->parse('trash', 'already_called');
        }

        //populate all the fields passed from previous step
        $_POST['step'] = 4;
        foreach ($_POST as $key=>$val) {
            $this->tpl->assign(array(
                'INPUTNAME'     => $key,
                'INPUTVALUE'    => $val,
            ));
            $this->tpl->parse("HIDDEN_INPUTS", ".form_hidden_inputs");
        }
        $this->tpl->assign(array(
            'INPUTNAME'     => 'fraudmsg',
            'INPUTVALUE'    => $fraudmsg,
        ));
        $this->tpl->parse("HIDDEN_INPUTS", ".form_hidden_inputs");

        $this->tpl->parse('SIGNUPBLOCKCONTENT', 'phonecode_form');
        $this->tpl->parse("BOXSUBMIT", "boxsubmitback");
        $this->tpl->FastPrint("BOXSUBMIT");
        $this->tpl->FastPrint("FOOTER");
    }
}

?>
