<?php

# $Id: function.admin_tab_statistics.php 2 2011-06-12 17:04:12Z 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
###############################################################################

$query = "SELECT t1.title AS action_title,
                t1.coupon_action_id,
                t1.start_date AS action_start_date,
                t1.end_date AS action_end_date,
                UNIX_TIMESTAMP(t1.start_date) AS action_start_ts,
                UNIX_TIMESTAMP(t1.end_date) AS action_end_ts,
                t2.title AS period_title,
                t2.coupon_period_id,
                t2.start_date AS period_start_date,
                t2.end_date AS period_end_date,
                UNIX_TIMESTAMP(t2.start_date) AS period_start_ts,
                UNIX_TIMESTAMP(t2.end_date) AS period_end_ts
            FROM ".UPCOUPON_ACTION_TABLE." t1, 
                ".UPCOUPON_PERIOD_TABLE." t2
            WHERE t1.coupon_action_id = t2.coupon_action_id
            AND t1.status = '1'
            AND t2.status = '1'
            ORDER BY t2.end_date ASC";

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

$dbresult = $db->Execute($query);

$query_part = "SELECT count(1) AS count, 
    concat('{my_key}') AS my_key 
    FROM ".UPCOUPON_CODE_TABLE." t5,
        ".UPCOUPON_CUSTOMER_TABLE." t4 
    WHERE t5.coupon_customer_id = t4.coupon_customer_id
    AND t4.status = '1'
    AND t5.timestamp >= '{period_start_ts}' 
    AND t5.timestamp <= '{period_end_ts}'";

$query = '';
$db_result = array();

while ($dbresult && $row = $dbresult->FetchRow()) {
    
    $obj = new stdclass;
    
    foreach ($row as $k => $v) {
        $obj->$k = $row[$k];
    }
        
    $db_result[] = $obj;
    
    // build query
    
    $temp = $query_part;
    
    $temp = str_replace('{period_start_ts}', $row['period_start_ts'], $temp);
    $temp = str_replace('{period_end_ts}', $row['period_end_ts'], $temp);
    $temp = str_replace('{my_key}', ($dbresult->CurrentRow() -1), $temp);
    
    if ($dbresult->CurrentRow() != $dbresult->RecordCount()) {
        $temp .= "\nUNION ALL\n";
    }

    $query .= $temp;
    
}

// add coupon_period_count

$dbresult = $db->Execute($query);

$coupon_action_count = array();

while ($dbresult && $row = $dbresult->FetchRow()) {    
    $db_result[$row['my_key']]->period_count = $row['count'];
    
    $coupon_action_id = $db_result[$row['my_key']]->coupon_action_id;
        
    if (isset($coupon_action_count[$coupon_action_id])) {
        $coupon_action_count[$coupon_action_id] = ($coupon_action_count[$coupon_action_id] + $row['count']);     
    } else {
        $coupon_action_count[$coupon_action_id] = $row['count'];       
    }
        
}

// add coupon_action_count

$coupon_action_match = '';

foreach ($db_result as $k => $v) {
    $action_id = $db_result[$k]->coupon_action_id;
    $db_result[$k]->action_count = $coupon_action_count[$action_id];
    
    if ($coupon_action_match != $action_id) {
        $coupon_action_match = $action_id;
        $db_result[$k]->show_action_title = '1';
    } else {
        $db_result[$k]->show_action_title = '0';        
    }
    
}

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

$this->smarty->assign('items', $db_result);
$this->smarty->assign('itemcount', count($db_result));
$this->smarty->assign('title_text', $this->Lang('title'));
$this->smarty->assign('start_date_text', $this->Lang('start_date'));
$this->smarty->assign('end_date_text', $this->Lang('end_date'));
$this->smarty->assign('registrations_text', $this->Lang('registrations'));

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

echo $this->ProcessTemplate ('statistics_list.tpl');

#
# EOF
#
?>
