<?php

require_once 'modules/admin/views/public/SignupView.php';
require_once 'modules/admin/models/Package.php';
require_once 'modules/billing/models/Currency.php';
include_once 'modules/admin/models/PluginGateway.php';
include_once 'modules/admin/models/ServerGateway.php';

/**
* @package Admin
*/
class ViewsubDomainSearchResults extends SignupView
{
    var $permission     = '';
    var $showHeader = false;

    /**
    * @return int possible return values:
    *       -1:    SubDomain name already has an account in this system
    *       0:     SubDomain available
    */

    function dispatch()
    {
        $this->initialize();

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

        // Check the domain
        if(!@$_REQUEST['name'] || !@$_REQUEST['tld']) {
            // Set error and stop execution
            $this->tplView->subDomainError = $this->lang("Please ensure that you have entered a subdomain to search for.");
            $this->tplView->display('subDomainSearchResults.tpl.php');
            exit;
        }

        // Preset some global TPL vars
        $this->tplView->subDomainError = false;
        $this->tplView->showContinueButton = (isset($_REQUEST['showContinueButton']))? 1 : 0;

        if($_REQUEST['tld'] != 'false'){
            $this->tplView->subDomainName = htmlentities(addslashes($_REQUEST['name'].".".$_REQUEST['tld']));

            // Start logging
            $this->logger->log(4, "Checking SubDomain ('".$_REQUEST['name']."', '".$_REQUEST['tld']."')");
        }else{
            $this->tplView->subDomainName = htmlentities(addslashes($_REQUEST['name']));

            // Start logging
            $this->logger->log(4, "Checking Domain ('".$_REQUEST['name']."')");
        }
        $this->tplView->requestTld = $_REQUEST['tld'];
        $this->tplView->subDomainStatus = '-2';

        $domainNameDuplicatesHandling = $this->settings->get('Domain name duplicates handling');

        // Work out if we are even checking the subdomain information or just pretending its valid
        if ($this->settings->get('Check Domain Availability') == 1
          && in_array($domainNameDuplicatesHandling, array(2, 3))) {
            include_once 'modules/clients/models/UserPackageGateway.php';
            if(UserPackageGateway::CheckSubDomainDuplicate($this->tplView->subDomainName, ($domainNameDuplicatesHandling == 2))){
                $this->tplView->subDomainStatus = '-1';
            }else{
                $this->tplView->subDomainStatus = '0';
            }
        } else {
            // Set the domain status to available manually
            $this->tplView->subDomainStatus = '0';
        }

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