<?php

require_once 'modules/admin/views/public/SignupView.php';
require_once 'modules/admin/models/PackageTypeGateway.php';
require_once 'modules/billing/models/Currency.php';

/**
* @package Admin
*/
class ViewSignup_2_Domain extends SignupView
{
    var $permission     = '';
    var $currencys    = null;

    function dispatch()
    {

        $this->initialize();

        // Init some vars
        $this->templateInit();
        $this->currencys = new Currency($this->user);
        $this->tplView->bundling = 0;

        // Set header info
        $this->headerlabel = $this->lang("Order - Step 2 - Choose A Domain");

        // Check if we were given a product to configure
        if(!@$_REQUEST['product']) {

            // Redirect back to the first step
            include("modules/admin/views/public/Signup_0_PackageGroup.php");
            ViewSignup_0_PackageGroup::dispatch();
            $this->showFooter(); // Show the footer to set the header vars etc properly
            exit;
        }

        // Check if we have a post of a product ID from a previous form
        if($_POST) {
            
            $itemParams = array('product' => @$_REQUEST['product'], 'term' => @$_REQUEST['priceTerm']);

            // Loop the fields
            $customFields = array();
            $addonFields = array();
            foreach($_POST AS $key => $value) {

                // Explode to look for custom fields
                $explodePost = explode("_", $key);

                // Check for custom fields
                if(@$explodePost[0] == 'PCT') {
                    $customFields[$explodePost[1]] = $value;
                }

                // Check for addons
                if(@$explodePost[0] == 'addonSelect') {
                    $addonFields[$explodePost[1]] = $value;
                }
            }

            // Add the two
            $itemParams['customFields'] = $customFields;
            $itemParams['addons'] = $addonFields;

            // Add the temp item
            $this->addTempItem($itemParams);

            $this->tplView->bundling = 1;

            // Set the $_REQEST to get from the URL
            $_REQUEST['product'] = $_GET['product'];
        }

        // Get the options for this product
        $package = new Package(@$_REQUEST['product']);

        // Set some vars
        $this->tplView->product['name'] = $package->planname;
        $this->tplView->product['description'] = $package->description;
        $this->tplView->product['id'] = @$_REQUEST['product'];

        // Get the TLD's
        $tldData = unserialize($package->pricing);
        $tldData = @$tldData['pricedata'];

        if(is_array($tldData)) {
            foreach($tldData AS $key => $value) {

                if($value['isDefault'] == true && isset($this->tplView->tld)) {
                    $this->tplView->tld = array_pad($this->tplView->tld, -(count($this->tplView->tld) + 1), array('name' => $key));
                } else {
                    $this->tplView->tld[] = array('name' => $key);
                }
            }
        }
        
        // Do we have a manually specified domain
        if(@$_GET['domainName'] && @$_GET['tld']) {
            $this->tplView->triggerSearch = true;
            $this->tplView->triggerDomain = @$_GET['domainName'];
            $this->tplView->triggerTld = @$_GET['tld'];
        }

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

        // Process the cart summary
        $cartSummary = $this->getCartSummary();
        $this->tplView->cartItems = $cartSummary['items'];
        $this->tplView->cartTotal = $cartSummary['total']['price'];
        $this->tplView->cartSummary = $this->tplView->fetch('cartSummary.tpl.php');

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

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

    }
}

?>
