<?php

# $Id: action.winner_draw_coupon.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;

###############################################################################
# CheckPermission
###############################################################################

if (!$this->CheckPermission('Modify UpCoupon')) {
    echo $this->ShowErrors($this->Lang('accessdenied', array('prize')));
    return;
}

###############################################################################
# params
###############################################################################

$coupon_prize_id = '';

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

$coupon_period_id = '';

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

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

$codedb = new coupon_codeDAO();
$mixeddb = new coupon_mixedDAO();
$perioddb = new coupon_periodDAO();

// figure out the start_ts and end_ts via the coupon_period_id
$period_row = $perioddb->fetch($coupon_period_id);
$start_ts = $perioddb->unix_time_stamp($period_row[0]['start_date']);
$end_ts = $perioddb->unix_time_stamp($period_row[0]['end_date']);

// draw a coupon 
$coupon_row = $codedb->draw_coupon($start_ts, $end_ts);

if (empty($coupon_row)) {
    $params = array('winner_message'=> 'need_registered_customer,', 'active_tab' => 'winner');
    $this->Redirect($id, 'defaultadmin', $returnid, $params);
    return;
}

// assign prize to user
$db_array = array('coupon_prize_id' => $coupon_prize_id, 'coupon_code_id' => $coupon_row['coupon_code_id']);
$codedb->update($db_array);

// fetch information of the winner to be used in the winner notification e-mail
$winner_array = $mixeddb->fetch_winners($coupon_row['coupon_code_id'], 't5.coupon_code_id');

if ($this->GetPreference('email_registered_true_false',0) == 1) {
    $mail = $this->GetModuleInstance('CMSMailer');
    if ($mail !== FALSE) {
        $mail->reset();
        $mail->SetSubject($this->GetPreference('email_registered_subject'));
        
        $email_array = array(
            'action_title'          => $winner_array[0]['action_title'], 
            'coupon_action_id'      => $winner_array[0]['coupon_action_id'],
            'period_title'          => $winner_array[0]['period_title'],
            'coupon_period_id'      => $winner_array[0]['coupon_period_id'],
            'prize_title'           => $winner_array[0]['prize_title'],
            'prize_code'            => $winner_array[0]['prize_code'],
            'number'                => $winner_array[0]['number'],
            'coupon_prize_id'       => $winner_array[0]['coupon_prize_id'],
            'coupon_customer_id'    => $winner_array[0]['coupon_customer_id'],
            'first_name'            => $winner_array[0]['first_name'],
            'last_name'             => $winner_array[0]['last_name'],
            'email'                 => $winner_array[0]['email'],
            'dashed_phone'          => implode('-', str_split($winner_array[0]['phone'], 3)),
            'phone'                 => $winner_array[0]['phone'],
            'coupon_code_id'        => $winner_array[0]['coupon_code_id'],
            'db_timestamp'          => $winner_array[0]['timestamp'],
            'dashed_coupon_code'    => implode('-', str_split($winner_array[0]['coupon_code'], 4)),
            'coupon_code'           => $winner_array[0]['coupon_code']
        );
                                    
        $this->smarty->assign('email', $email_array);
        
        $body = $this->ProcessTemplateFromData($this->GetTemplate('email_registered_message_winner'));
                
        $mail->SetBody($body);
        $mail->SetFrom($this->GetPreference('email_registered_address_email'));
        $mail->SetFromName($this->GetPreference('email_registered_address_name'));
                            
        $full_name = $winner_array[0]['first_name'] . ' '. $winner_array[0]['last_name']; 
        
        $mail->AddAddress($winner_array[0]['email'], $full_name);
        
        $res = $mail->Send();
    }
}

@$this->SendEvent('UpCouponWinnerAdded', $db_array);
$params = array('winner_message'=> 's_added,winner_s', 'active_tab' => 'winner');
$this->Redirect($id, 'defaultadmin', $returnid, $params);

#
# EOF
#
?>
