<?php

# $Id: action.show_prize.php 7 2011-06-20 18:59:54Z 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

if(!is_object(cmsms())) exit;

###############################################################################
# fetch params
###############################################################################

$all_templates = $this->ListTemplates();

$template = '0';

if ( isset($params['template']) ) {      
    $template = UpHelper::alpha_numeric_lower($params['template']);
    $template = 'prize_'. $template;
    
    $tmp = '0';
    
    foreach ($all_templates as $k => $v) {
        if ($template == $v) {
            $tmp = '1';
        }
    }
    
    $template = ($tmp == '1') ? $template : '0';
    
}

if ( isset($params['coupon_action_id']) ) {    
    $coupon_action_id = trim(preg_replace('/[^0-9]/', '', $params['coupon_action_id']));    
}

###############################################################################
# program
###############################################################################

$query = "SELECT t1.title AS action_title,
            t1.alias AS action_alias,
            t1.description AS action_description,
            t1.start_date AS action_start_date,
            t1.end_date AS action_end_date,
            t2.title AS period_title,
            t2.start_date AS  period_start_date,
            t2.end_date AS period_end_date,
            t2.is_final AS period_final,
            t3.prize_code AS prize_code, 
            t3.title AS prize_title,
            t3.description AS prize_description,
            t3.number AS prize_number
        FROM ". UPCOUPON_ACTION_TABLE. " t1,
            ". UPCOUPON_PERIOD_TABLE. " t2,    
            ". UPCOUPON_PRIZE_TABLE. " t3
        WHERE t1.coupon_action_id = t2.coupon_action_id
        AND t2.coupon_period_id = t3.coupon_period_id
        AND t1.status = 1
        AND t2.status = 1
        AND t3.status = 1";

if (preg_match("/^[0-9]{1,5}$/",trim($coupon_action_id)) ) {
    $query .= " AND t1.coupon_action_id = ?";
}
        
$query .= " ORDER BY t2.end_date";

$db = cmsms()->GetDb();

if (preg_match("/^[0-9]{1,5}$/",trim($coupon_action_id)) ) {
    $dbresult = $db->Execute($query, array($coupon_action_id));
} else {
    $dbresult = $db->Execute($query);    
}

$db_result = array();

while ($dbresult && $row = $dbresult->FetchRow()) {
    
    $obj = new stdclass;
    
    foreach ($row as $k => $v) {
        $obj->$k = $row[$k];
    }
        
    $db_result[] = $obj;
    
}

###############################################################################
# assign smarty variables
###############################################################################

$this->smarty->assign('items', $db_result);
$this->smarty->assign('itemcount', count($db_result));

$this->smarty->assign('action_s_text', $this->Lang('action_s'));
$this->smarty->assign('action_p_text', $this->Lang('action_p'));

$this->smarty->assign('period_s_text', $this->Lang('period_s'));
$this->smarty->assign('period_p_text', $this->Lang('period_p'));

$this->smarty->assign('prize_s_text', $this->Lang('prize_s'));
$this->smarty->assign('prize_p_text', $this->Lang('prize_p'));

$this->smarty->assign('start_date_text', $this->Lang('start_date'));
$this->smarty->assign('end_date_text', $this->Lang('end_date'));

###############################################################################
# ProcessTemplate
###############################################################################

if ($template != '0') {
    echo $this->ProcessTemplateFromDatabase($template); 
} else {
    $template_type_key = 'default_prize_template';
    $template  = $this->GetPreference($template_type_key);
    
    $tmp = '0';
    
    foreach ($all_templates as $k => $v) {
        if ($template  == $v) {
            $tmp = '1';
        }
    }
    
    $template = ($tmp == '1') ? $template : '0';

    if ($template == '0') {
        echo $this->ProcessTemplate('factory_template_prize.tpl');
    } else {
        echo $this->ProcessTemplateFromDatabase($template); 
    }
     
}

#
# EOF
#
?>
