<?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 $permission     = '';
    var $showHeader     = false;
    
    function preHeaders()
    {
        $this->initialize();
    }

    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->forceShowHeader();
            $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->preHeaders();
        $createAccounts->dispatch();
        return;
    }

    function _showPhoneVerificationForm(&$phoneVerificationPlugin)
    {
        $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()) {

                /*
                if ($this->settings->get('E-mail For New Signups') != '') {

                    $fraudmsg = "<br>\n------------------------------<b><br>\n";
                    $fraudmsg .= "Fraud Details<br>\n";
                    $fraudmsg .= "</b>------------------------------<br>\n";

                    $aSupportEmail = explode("\r\n", $this->settings->get('E-mail For New Signups'));
                    foreach ($aSupportEmail as $email)
                    {
                        $email = trim($email);
                        if ($email!="") $mailGateway->mailMessageEmail( array('HTML' => $fraudmsg, 'plainText' => null),
                                                                        $this->settings->get('Support E-mail'),
                                                                        $this->settings->get('Support E-mail'),
                                                                        $email,
                                                                        '',
                                                                        "Fraud Phone Verification Failed",
                                                                        3,
                                                                        0,
                                                                        '',
                                                                        '',
                                                                        MAILGATEWAY_CONTENTTYPE_HTML);
                    }
                }
*/
                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->preHeaders();
                $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->assign("SUBMITFUNCTION","SubmitPreStepFour");
        $this->tpl->assign("BACKFUNCTION","BackPreStepFour");

        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('SIGNUP_3B_PHONE', 'signup_3b_phoneverify');
        $this->tpl->FastPrint("SIGNUP_3B_PHONE");
        $this->tpl->FastPrint("FOOTER");
    }
}

?>
