<?php

# $Id: function.admin_tab_winner.php 1 2011-06-12 15:25:27Z 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;

###############################################################################
# program start 
###############################################################################

$prizedb = new coupon_prizeDAO();
$actiondb = new coupon_actionDAO();
$perioddb = new coupon_periodDAO();
$codedb = new coupon_codeDAO();
$mixeddb = new coupon_mixedDAO();

###############################################################################
# program start 
###############################################################################

###############################################################################
# prizes 
###############################################################################

$prize_data = $mixeddb->fetch_action_period_prizes();
$given_prize_count = $codedb->fetch_given_prize_count();

$prizearray = array();

foreach ($prize_data as $key => $value) {

    $onerow = new stdClass();
    
    foreach ($value as $sub_key => $sub_value) {
        $onerow->$sub_key = $value[$sub_key]; 
        
        if ($sub_key == 'coupon_prize_id') {
            if (isset($given_prize_count[$value[$sub_key]])) {
                
                $current_count = $given_prize_count[$value[$sub_key]];
                
                $onerow->free = ($value['number'] - $current_count);
                $onerow->given = $current_count;
            } else {
                $onerow->free = $value['number'];
                $onerow->given = '0';
            }
        }
        
    }
    
    if ($onerow->free) {
       
        $onerow->draw_link = $this->CreateLink(
            $id, 
            'winner_draw_coupon', 
            $returnid, 
            $admintheme->DisplayImage('icons/system/run.gif',
            $this->Lang('draw_coupon'),'','','systemicon'),
            array('coupon_prize_id'=>$value['coupon_prize_id'], 'coupon_period_id'=>$value['coupon_period_id']), 
            $this->Lang('areyousure')
        );
        
    } else {
        $onerow->draw_link = $admintheme->DisplayImage('icons/extra/yellow.gif',
            $this->Lang('given_number'),'','','systemicon');
    }
    
    $prizearray[] = $onerow;
    
}    

$this->smarty->assign_by_ref('prizeitems', $prizearray);
$this->smarty->assign('prizeitemcount', count($prizearray));

###############################################################################
# winners 
###############################################################################

$winners_data = $mixeddb->fetch_winners();
$winnerarray = array();

foreach ($winners_data as $key => $value) {

    $onerow = new stdClass();
    
    foreach ($value as $sub_key => $sub_value) {
        $onerow->$sub_key = $value[$sub_key];   
    }
    
    $onerow->dashed_coupon_code =  implode('-', str_split($onerow->coupon_code, 4));
    
    $onerow->dashed_phone =  implode(' ', str_split($onerow->phone, 3));
        
    $onerow->withdraw_link = $this->CreateLink(
        $id, 
        'winner_withdraw_coupon', 
        $returnid, 
        $admintheme->DisplayImage('icons/system/delete.gif',
        $this->Lang('withdraw_coupon'),'','','systemicon'),
        array('coupon_code_id'=>$value['coupon_code_id']), 
        $this->Lang('areyousure')
    );
    
    $winnerarray[] = $onerow;
    
}
	
$this->smarty->assign_by_ref('winneritems', $winnerarray);
$this->smarty->assign('winneritemcount', count($winnerarray));

###############################################################################
# define text for labels
###############################################################################	

$smarty->assign('prize_p_text', $this->Lang('prize_p'));
$smarty->assign('free_number_text', $this->Lang('free_number'));
$smarty->assign('given_number_text', $this->Lang('given_number'));
$smarty->assign('draw_coupon_text', $this->Lang('draw_coupon'));

$smarty->assign('winner_p_text', $this->Lang('winner_p'));
$smarty->assign('name_text', $this->Lang('name'));
$smarty->assign('email_text', $this->Lang('email'));
$smarty->assign('phone_text', $this->Lang('phone'));
$smarty->assign('coupon_code_text', $this->Lang('coupon_code'));
$smarty->assign('withdraw_coupon_text', $this->Lang('withdraw_coupon'));

###############################################################################
# setup form
###############################################################################

$form_params = array();
$form_params['active_tab'] = 'winner';

if (isset($params['winner_message'])) {
        $winner_message = explode(",", $params['winner_message']);
        $smarty->assign('winner_message',$this->ShowMessage($this->Lang($winner_message[0], array($this->Lang($winner_message[1])))));
}

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

echo $this->ProcessTemplate('winner_list.tpl');
	
#
# EOF
#
?>