<?php

# $Id: action.show_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
/*

- collect winner data
- shmlz: implement

*/


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 = 'winner_'. $template;
    
    $tmp = '0';
    
    foreach ($all_templates as $k => $v) {
        if ($template == $v) {
            $tmp = '1';
        }
    }
    
    $template = ($tmp == '1') ? $template : '0';
    
}

$coupon_action_id = '';

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

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

$mixeddb = new coupon_mixedDAO();

$winners_data = array();

if ($coupon_action_id == '') {
    $winners_data = $mixeddb->fetch_winners();    
} else {
    $winners_data = $mixeddb->fetch_winners($coupon_action_id, 't1.coupon_action_id');
}    
    
$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));
        
    $winnerarray[] = $onerow;
    
}

$smarty->assign('winner_p_text', $this->Lang('winner_p'));
$smarty->assign('prize_code_text', $this->Lang('prize_code'));
$smarty->assign('title_text', $this->Lang('title'));
$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'));
	
$this->smarty->assign_by_ref('winneritems', $winnerarray);
$this->smarty->assign('winneritemcount', count($winnerarray));

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

if ($template != '0') {
    echo $this->ProcessTemplateFromDatabase($template); 
} else {
    $template_type_key = 'default_winner_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_winner.tpl');
    } else {
        echo $this->ProcessTemplateFromDatabase($template); 
    }
     
}

#
# EOF
#
?>