<?php

require_once 'modules/admin/views/public/SignupView.php';

/**
* @package Admin
*/
class ViewSignup_PhoneVerification extends SignupView
{
    var $permission     = '';
    var $requiresCall = false;
    var $phonePlugin = null;

    function dispatch()
    {

        $this->initialize();
        $this->currencys = new Currency($this->user);

        // Init some vars
        $this->templateInit();

        // Set header info
        $this->headerlabel = $this->lang("Order - Step 4 - Phone Verification");

        // Process the progress bar
        $this->tplView->progress = $this->getProgressBar(4);
        $this->tplView->progressBar = $this->tplView->fetch('progressBar.tpl.php');

        // Get the cart summary so that we can use the invoice total
        $cartSummary = $this->getCartSummary();
        
        $phoneVerificationPlugins = new NE_PluginCollection('phoneverification', $this->user);
        while ($phoneVerificationPlugin = $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") > @$cartSummary['total']['truePrice'] && (isset($_POST['fraudScore']) && $this->settings->get("plugin_".$phoneVerificationPlugin->getInternalName()."_Minimum Fraud Score to Trigger Telephone Verification") > $_POST['fraudScore'])) {
                break;
            }

            $this->requiresCall = true;
            $this->phonePlugin = $phoneVerificationPlugin;
        }

        // Does the user require a phone call?
        if($this->requiresCall) {

            // Check if the users phine number is the same now as it wsa before
            $alreadyCalled = false;
            if(isset($_SESSION['phoneNum'])) {
                if($_SESSION['phoneNum'] == @$_REQUEST['CT_8']) {
                    $alreadyCalled = true;
                }
            }

            // Do we have a session set already with the number?
            if(isset($_SESSION['phoneChecksum']) && $alreadyCalled == 1) {

                // Stops people re-loading the page by accident and having a new code generated
                $this->tplView->isCalled = true;

                // Set the existing code
                $this->tplView->codeChecksum = $_SESSION['phoneChecksum'];

            } else {

                // Generate a code
                $phoneCode = rand(1000, 9999);
                
                // Set the code up
                $_SESSION['phoneChecksum'] = Clientexec::encryptString($phoneCode);
                $this->tplView->codeChecksum = $_SESSION['phoneChecksum'];

                // Make vars
                $this->tplView->isCalled = false;

                // Get the phone number & country
                $this->phonePlugin->setPhoneNumber(@$_REQUEST['CT_8']);
                $_SESSION['phoneNum'] = @$_REQUEST['CT_8'];
                $this->phonePlugin->setLanguage(@$_REQUEST['CT_15']);
                $result = $this->phonePlugin->execute($phoneCode);
                $this->tplView->phoneNumber = @$_REQUEST['CT_8'];

            }

            // Handle the hidden fields
            foreach($_POST AS $key => $value) {
                $this->tplView->hiddenFields[$key] = $value;
            }
            $this->tplView->hiddenFields['phoneCalled'] = true;

            // Output the template
            $this->tplView->display('signup_PhoneVerification.tpl.php');

            // Show the footer
            $this->tplView->display('signup_footer.tpl.php');


        } else {

            $this->log($this->signupLogLevel, "Signup: All checks passed.");

            echo "passed";
exit;
            // If we are still here then we passed validations and we don't have any phone
            // verifications to handle. So silently move on to creating accounts.
            $_REQUEST['signupChecksPassed'] = $this->signupChecksPassed;
            include("modules/admin/views/public/Signup_CreateAccounts.php");
            ViewSignup_CreateAccounts::dispatch($this->existingUserId);

        }

    }
}

?>
