<?php

# $Id: UpCoupon.module.php 11 2011-10-22 12:12:38Z arnoud $ 

#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Module: UpCoupon (c) 2011 by Arnoud van Susteren (arnoud@upservice.nl)
#
# An addon module for CMS Made Simple to setup a Coupon Codes Lottery
# 
#-------------------------------------------------------------------------
# CMS - CMS Made Simple is (c) 2005 by Ted Kulp (wishy@cmsmadesimple.org)
# This project's homepage is: http://www.cmsmadesimple.org
#
#-------------------------------------------------------------------------
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# However, as a special exception to the GPL, this software is distributed
# as an addon module to CMS Made Simple.  You may not use this software
# in any Non GPL version of CMS Made simple, or in any version of CMS
# Made simple that does not indicate clearly and obviously in its admin 
# section that the site was built with CMS Made simple.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# Or read it online: http://www.gnu.org/licenses/licenses.html#GPL
#
#-------------------------------------------------------------------------
#END_LICENSE

$uputils = cms_join_path($gCms->config['root_path'],'modules', 'UpUtils','UpUtils.module.php');

if ( !is_readable($uputils) ) {
    echo '<h1><font color="red">ERROR: The UpUtils module could not be found.</font></h1>';
    return;
}

require_once($uputils);

define('UPCOUPON_ACTION_TABLE', cms_db_prefix().'module_upcoupon_action');
define('UPCOUPON_PERIOD_TABLE', cms_db_prefix().'module_upcoupon_period');
define('UPCOUPON_PRIZE_TABLE', cms_db_prefix().'module_upcoupon_prize');
define('UPCOUPON_CUSTOMER_TABLE', cms_db_prefix().'module_upcoupon_customer');
define('UPCOUPON_CODE_TABLE', cms_db_prefix().'module_upcoupon_code');

class UpCoupon extends CMSModule {
                
    public static $frontend_actions = array (
        'show_register' => 'show_register',
        'show_prize'    => 'show_prize',
        'show_winner'   => 'show_winner'
    );
    
    public static $frontend_template = array (
        'default'   => 'default',        
    );
        
    ###########################################################################
    # cmsms
    ###########################################################################    
    
    /**
     * Constructor
     * @ignore 
     */
    function __construct() {
        
        spl_autoload_register(array($this,'autoload'));
        parent::__construct();
    
        $obj = new UpSmarty($this);
        $smarty = cmsms()->GetSmarty();
        $smarty->assign('upcoupon', $obj);
        
    }
    
    /**
     * autoload method
     * @param $classname - string
     * @return true|false
     */
    public function autoload($classname) {
     
        if ( !is_object($this) ) {
            return false;
        }
        
        // UpUtils libs
        
        $_module_path = $this->GetModulePath();
        $_module_path = str_replace('UpCoupon', 'UpUtils', $_module_path);
        $fn = $_module_path."/lib/classes/{$classname}.class.php";
        
        if ( file_exists($fn) ) {
            require_once($fn);            
            return true;
        }
        
        // UpCoupon libs
        
        if (preg_match('/DAO$/',$classname) ) {
            $classname = 'UpCouponDAO';
        }

        $fn = $this->GetModulePath()."/lib/classes/{$classname}.class.php";
        
        if ( file_exists($fn) ) {
            require_once($fn);
            return true;
        }
        
    }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetName
     * @ignore
     */
    public function GetName() { return 'UpCoupon'; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetFriendlyName
     * @ignore
     */
    public function GetFriendlyName() { return $this->Lang('friendlyname'); }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetVersion
     * @ignore
     */
    public function GetVersion() { return '0.0.8'; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#MinimumCMSVersion
     * @ignore
     */
    public function MinimumCMSVersion() { return '1.9.1'; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#MaximumCMSVersion
     * @ignore
     */
    public function MaximumCMSVersion() { return "1.10.9"; }

    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetHelp
     * @ignore
     */
     public function GetHelp() { return $this->lang('help'); }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAuthor
     * @ignore
     */  
    public function GetAuthor() { return 'Arnoud van Susteren'; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAuthorEmail
     * @ignore
     */
    public function GetAuthorEmail() { return 'arnoud@upservice.nl'; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetChangeLog
     * @ignore
     */
    public function GetChangeLog() { return file_get_contents(dirname(__FILE__).'/changelog.inc'); }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetEventDescription
     * @ignore
     */
    public function GetEventDescription($eventname) { return $this->lang('eventdesc-' . $eventname); }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetEventHelp
     * @ignore
     */
    public function GetEventHelp($eventname) { return $this->lang('eventhelp-' . $eventname);
    }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#IsPluginModule
     * @ignore
     */
    public function IsPluginModule() { return true; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#HasAdmin
     * @ignore
     */
    public function HasAdmin() { return true; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAdminSection
     * @ignore
     */
    public function GetAdminSection() { return 'content'; }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetAdminDescription
     * @ignore
     */
    public function GetAdminDescription() { return $this->Lang('admindescription'); }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#VisibleToAdminUser
     * @ignore
     */
    public function VisibleToAdminUser() { return $this->CheckAccess(); }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#methodCheckPermission
     * @ignore
     */
    public function CheckAccess() { return $this->CheckPermission('Modify UpCoupon'); }
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#GetDependecies
     * @ignore
     */
    public function GetDependencies() {
        return array('UpUtils'=>'0.0.4');
    }
    
    //public function Install() {}
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#InstallPostMessage
     * @ignore
     */
    public function InstallPostMessage() { return $this->Lang('postinstall'); }
    
    //public function Uninstall() {}

    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#UninstallPostMessage
     * @ignore
     */
    public function UninstallPostMessage() { return $this->Lang('postuninstall'); }
    
    //public function Upgrade($oldversion, $newversion) {}
        
    ###########################################################################
    # actions
    ###########################################################################
    
    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#DoAction
     * @ignore
     */
     
     /*
     
    public function DoAction($action, $id, $params, $returnid = '') {    
        $action = isset(self::$frontend_actions[$action]) ? $action : 'default';
        $params['action'] = $action;        
        parent::DoAction($action, $id, $params, $returnid);
    }
    
    */

    /**
     * @link http://www.cmsmadesimple.org/apidoc/CMS/CMSModule.html#SetParameters
     *
     * Called from within the constructor, Only for frontend module actions
     * This method should be overridden to create routes, and set handled params
     * and performs other initialization tasks that need to be setup for all
     * frontend actions
     *
     */
    public function SetParameters() {
        
        // no unknown parameters
        $this->RestrictUnknownParams();
        
        // register plugin to smarty with the name of the module
        $this->RegisterModulePlugin();
        
        // routes
        $this->SetupRoutes();
        
        // createparameters
        //$this->SetParameterType('origreturnid',CLEAN_INT);
        
        // common
        
        $this->SetParameterType('origreturnid',CLEAN_INT);
        
        $this->CreateParameter('action', '', $this->Lang('help_action'));
        $this->SetParameterType('action',CLEAN_STRING);
        
        $this->CreateParameter('template', '', $this->Lang('help_template'));
        $this->SetParameterType('template',CLEAN_STRING);
        
        // show_register
        $this->SetParameterType('first_name',CLEAN_STRING);
        $this->SetParameterType('last_name',CLEAN_STRING);
        $this->SetParameterType('email',CLEAN_STRING);
        $this->SetParameterType('phone',CLEAN_STRING);
        $this->SetParameterType('coupon_code_01',CLEAN_STRING);
        $this->SetParameterType('coupon_code_02',CLEAN_STRING);
        $this->SetParameterType('coupon_code_03',CLEAN_STRING);
        $this->SetParameterType('coupon_code_04',CLEAN_STRING);
        $this->SetParameterType('terms_and_conditions',CLEAN_INT);
        $this->SetParameterType('newsletter',CLEAN_INT);
        $this->SetParameterType('submit',CLEAN_STRING);
        
        // show_prize
        $this->CreateParameter('coupon_action_id', '', $this->Lang('help_coupon_action_id'));
        $this->SetParameterType('coupon_action_id',CLEAN_INT);

	}
	
	public function SetupRoutes() {
	   
	}
	
	###########################################################################
    # utils
    ###########################################################################
    
    public function _get_template_type() {
        
        $template_type = array (
            'register'  => $this->Lang('template_register'),
            'winner'    => $this->Lang('template_winner'),
            'prize'     => $this->Lang('template_prize')  
        );
        
        return $template_type;
        
    }
       

} // UpCoupon

?>
