<?php
require_once 'modules/clients/models/user.php';
require_once 'modules/admin/models/ServicePlugin.php';
require_once 'modules/admin/models/Package.php';

/**
* @package Plugins
*/
class PluginDomainupdater extends ServicePlugin
{
    /**
     * All plugin variables/settings to be used for this particular service.
     *
     * @return array The plugin variables.
     */
    function getVariables()
    {
        $variables = array(
            /*T*/'Plugin Name'/*/T*/   => array(
                'type'          => 'hidden',
                'description'   => /*T*/''/*/T*/,
                'value'         => /*T*/'Domain Updater'/*/T*/,
            ),
            /*T*/'Enabled'/*/T*/       => array(
                'type'          => 'yesno',
                'description'   => /*T*/'When enabled, this service will check all active domains and update the internal expiration date of the domains.  This date is used to send expiration notices among other things.'/*/T*/,
                'value'         => '0',
            ),
            /*T*/'Run schedule - Minute'/*/T*/  => array(
                'type'          => 'text',
                'description'   => /*T*/'Enter number, range, list or steps'/*/T*/,
                'value'         => '30',
                'helpid'        => '8',
            ),
            /*T*/'Run schedule - Hour'/*/T*/  => array(
                'type'          => 'text',
                'description'   => /*T*/'Enter number, range, list or steps'/*/T*/,
                'value'         => '*',
            ),
            /*T*/'Run schedule - Day'/*/T*/  => array(
                'type'          => 'text',
                'description'   => /*T*/'Enter number, range, list or steps'/*/T*/,
                'value'         => '*',
            ),
            /*T*/'Run schedule - Month'/*/T*/  => array(
                'type'          => 'text',
                'description'   => /*T*/'Enter number, range, list or steps'/*/T*/,
                'value'         => '*',
            ),
            /*T*/'Run schedule - Day of the week'/*/T*/  => array(
                'type'          => 'text',
                'description'   => /*T*/'Enter number in range 0-6 (0 is Sunday) or a 3 letter shortcut (e.g. sun)'/*/T*/,
                'value'         => '*',
            ),
        );

        return $variables;
    }


    /**
     * Execute the domain update.  
     * This service will update all domains internal status to show when a domain is going to expire. 
     *
     * @return void
     */
    function execute() {
        $domainNameGateway = new DomainNameGateway($this->user);
        
        // get all active domains
        $query = "SELECT d.id FROM domains d, package p, promotion pr WHERE d.status=? AND d.Plan=p.id AND p.planid=pr.id AND pr.type=3";
        $result = $this->db->query($query, PACKAGE_STATUS_ACTIVE);
        while ( $row = $result->fetch() ) {
            $userPackage = new UserPackage($row['id']);
            try {
                $domainInfo = $domainNameGateway->getGeneralInfoViaPlugin($userPackage);
            } catch ( Exception $e ) { }
            
            $numberOfDaysTillExpires = (int)$domainNameGateway->getExpiresInDays($domainInfo['expires']);
            $userPackage->setCustomField('Plugin Status', $domainNameGateway->getPluginStatusByDays($numberOfDaysTillExpires));
            $userPackage->setCustomField('Expiration Date', strtotime($domainInfo['expires']));
            $userPackage->setCustomField('Auto Renew', $domainInfo['auto_renew']);
        }
    }

    function output() { }
    function dashboard() { }
}
?>
