<?php

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

/**
* @package Admin
*/
class ViewSignup_0_PackageGroup extends SignupView
{
    var $permission     = '';
    
    function dispatch()
    {

        $this->initialize();

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

        // Set header info

        if ($this->clienttype == 0 && $this->package == 0) {
             $_SESSION['AllowStep0']=true;
        }

        if (!isset($this->currency)) {
            $this->currency = '';
        }

        if (!isset($this->selectedCurrency)) {
            $this->selectedCurrency = '';
        }
        
        // Unset the session used to stop things being added to the cart incase we got back here
        unset($_SESSION['avoidaddpackage']);
        
        // Has somebody specified a domain name in the URL?
        if(isset($_GET['domainName']) && isset($_GET['tld'])) {
        	$_SESSION['domainName'] = $_GET['domainName'];
        	$_SESSION['tld'] = $_GET['tld'];
        }

        // Get the package types
        if ($this->clienttype == 0 && $this->package == 0) {

            //First Time through step 0
            require_once 'modules/admin/models/PackageTypeGateway.php';
            $packageTypes = PackageTypeGateway::getDefaultPackageTypes();
            $plancount = $packageTypes->getNumItems();
            
            //Loop through all the packagetypes
            while($packageType = $packageTypes->fetch()) {

                // Check if the group is a domain group
                if($packageType->fields['type'] == 3) {

                    // Any domain products do not appear as part of a group, they are standalone on the order form

                    // Get the packages for this group
                    $query = "SELECT id FROM package WHERE planid = ? AND showpackage = 1 ORDER BY id";
                    $result = $this->db->query($query, $packageType->fields['id']);
                    while ($row = $result->fetch()) {

                        // Get the info for this package
                        $package = new Package($row['id']);

                        // Add it to the array
                        $this->tplView->productGroups[] = array('id' => $package->id,
                                                                'name' => $package->planname,
                                                                'description' => $package->description,
                                                                'type' => $packageType->fields['type']);
                    }

                } else {

                    // Check to see if this group has any products assigned to it that are set to display in the sign up
                    // Get the packages for this group
                    $query = "SELECT id FROM package WHERE planid = ? AND showpackage = 1 ORDER BY id";
                    $result = $this->db->query($query, $packageType->fields['id']);

                    if($result->getNumRows()) {
                        $this->tplView->productGroups[] = array('id' => $packageType->fields['id'],
                                                                'name' => $packageType->fields['name'],
                                                                'description' => $packageType->fields['description'],
                                                                'type' => $packageType->fields['type']);
                    }
                }
            }
        }

        // Process the progress bar
        $this->tplView->progress = $this->getProgressBar(1);
        $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_0_PackageGroup.tpl.php');

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

        
    }
}

?>
