<?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' => @$_POST['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;
            
            // Get this products advanced features as we may need them for subdomains
            $initialPackage = new Package($itemParams['product']);
            $advancedSettings = @unserialize($initialPackage->advanced);
            
            // 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'];
        if ( isset($_POST['product']) ) {
            $this->tplView->hosting_product = $_POST['product'];
        }

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

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

                if(isset($value['isDefault']) && $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((isset($_GET['domainName']) && isset($_GET['tld'])) || (isset($_SESSION['domainName']) && isset($_SESSION['tld']))) {
            $this->tplView->triggerSearch = true;
            $this->tplView->triggerDomain = (isset($_GET['domainName']))?$_GET['domainName']:$_SESSION['domainName'];
            $this->tplView->triggerTld = (isset($_GET['tld']))?$_GET['tld']:$_SESSION['tld'];
            unset($_SESSION['domainName']);
            unset($_SESSION['tld']);
        }
        
        // Handle adding subdomains to the dropdown if we have any
        if(isset($advancedSettings['subdomain']) && $advancedSettings['subdomain'] != '') {
        
            // Explode the subdomains
            $subDomains = explode(";", $advancedSettings['subdomain']);

			// Add the default seperator for the subdomains to the TLD list
			//$this->tplView->tld[] = array('name' => '------------', 'subdomain' => true);
			
            // Populate the subdomains
            if(is_array($subDomains)) {
            	
            	// Add the subdomains to the TLD list
                foreach($subDomains AS $key) {
                    $this->tplView->subDomains[] = array('name' => $key);
                }
            } else {
                $this->tplView->subDomains[] = array('name' => $advancedSettings['subdomain']);
            }
        }
        
        // Get the advanced settings for the domain product
        $advancedSettings = @unserialize($package->advanced);
        $this->tplView->allowTransfer = (isset($advancedSettings['allowTransfer']))?true:false;
        $this->tplView->allowSelfManage = (isset($advancedSettings['allowSelfManage']))?true:false;

        // 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');

    }
}

?>
