<?php
require_once 'modules/admin/models/ImportPlugin.php';
require_once 'modules/domains/models/ICanImportDomains.php';
require_once 'modules/admin/models/PluginGateway.php';

class PluginDomains_registrar extends ImportPlugin
{
    var $_title = /*T*/'Registrar Domains'/*/T*/;  
    var $description = /*T*/"This import plugin imports domains and customers (if they don't already exist in CE) from your registrar account."/*/T*/;

    /**
     * Returns form for domain importing
     *
     * @return html
     */
    function getForm()
    {
        $this->tpl->define(array('view'  => '../../plugins/import/domains_registrar/PluginDomainsregistrar.tpl'));
        
        if (!isset($this->configuration['modules']['domains']['installedVersion'])) {
            $this->tpl->parse('view', 'noDomainsModule');
            return $this->tpl->subst('PluginDomainsregistrar_out', 'view');
        }
        $this->tpl->assign('CE_VERSION', $this->settings->get('ClientExec Version'));
        
        $count = 0;
        $showWarning = true;
        $pluginGateway = new PluginGateway();
        $query = "SELECT package.pricing FROM package, promotion WHERE package.planid = promotion.id AND promotion.type=3";
        $result = $this->db->query($query);
        $addedTLD = array();
        while ($row = $result->fetch()) {

            // Unserialize the TLD's
            $tldPricing = @unserialize($row['pricing']);

            // Loop the pricing
            if (@is_array($tldPricing['pricedata'])) {
                foreach ($tldPricing['pricedata'] AS $key) {
                    //determine if I have settings defined before hand
                    if (isset($key['registrar']) && strlen($key['registrar']) != 0) {
                        $plugin = $pluginGateway->getPluginByName("registrars", $key['registrar']);
                        //check to see if this plugin supports import functionality
                        if (($plugin instanceof ICanImportDomains)  && !@$addedTLD[$key['registrar']]) {
                            $this->tpl->setKey(array('REGISTRAR' => $key['registrar']));
                            $this->tpl->parse('view', '.registrar');
                            $count++;
                            $addedTLD[@$key['registrar']] = 1;
                        }
                    }
                }
            }
        }

        if ($count > 0) {
            $this->tpl->parse('view', 'registrarDropdown');
        } else {
            $this->tpl->assign('noRegistrarsText', $this->user->lang('You must first %sconfigure a registrar plugin%s and then assign TLDs to that plugin via the Domain Pricing tab for a %sdomains registration product%s.', '<a href="index.php?fuse=admin&amp;view=Registrars">', '</a>', '<a href="index.php?fuse=admin&amp;view=viewProducts">', '</a>'));
            $this->tpl->parse('view', 'noRegistrars');
            $showWarning = false;
        }

        if ($showWarning) {
            $this->tpl->parse('hasRegistrars_out', 'hasRegistrars');
        }
        $this->tpl->parse('view', 'hasDomainsModule');
        return $this->tpl->subst('PluginDomainsregistrar_out', 'view');
    }
    
}
?>
