<?php

# $Id: UpCouponDAO.class.php 1 2011-06-12 15:25:27Z arnoud $

#BEGIN_LICENSE
#-------------------------------------------------------------------------
# Class: childDAO (c) 2011 by Arnoud van Susteren (arnoud@upservice.nl)
#
# Implementation of some table methods
# 
#-------------------------------------------------------------------------
# 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

class coupon_actionDAO extends baseDAO {

    protected $_tablename = UPCOUPON_ACTION_TABLE;
    protected $_primarykey =  'coupon_action_id';
    
    /** fetch_actiondropdown
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_actiondropdown($value = NULL, $db_key = NULL) {  
    
        if (is_null($db_key)) {
            $query = "SELECT title, coupon_action_id FROM {$this->_tablename} WHERE status = ?";
        } else {
            $query = "SELECT title, coupon_action_id FROM {$this->_tablename} WHERE status = ? AND {$db_key} = ?";
        }            
        
        if (self::$debug) {
            print "$query \n";
        }
        
        if (is_null($db_key)) {        
            $dbresult = $this->__connection->Execute($query, array('1'));
        } else {
            $dbresult = $this->__connection->Execute($query, array('1', $value));
        }
      
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {
            $rows[$row['coupon_action_id']] = $row['title'];
        }
        
        return $rows;
        
    }
         
} // coupon_actionDAO

class coupon_periodDAO extends baseDAO {

    protected $_tablename = UPCOUPON_PERIOD_TABLE;
    protected $_primarykey =  'coupon_period_id';
    
    /** fetch_actiondropdown
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_perioddropdown($value = NULL, $db_key = NULL) {  
    
        if (is_null($db_key)) {
            $query = "SELECT title, coupon_period_id FROM {$this->_tablename} WHERE status = ?"; 
        } else {
            $query = "SELECT title, coupon_period_id FROM {$this->_tablename} WHERE status = ? AND {$db_key} = ?";          
        }
        
        if (self::$debug) {
            print "$query \n";
        }
        
        if (is_null($db_key)) {
            $dbresult = $this->__connection->Execute($query, array('1'));
        } else {
            $dbresult = $this->__connection->Execute($query, array('1', $value));          
        }
            
            
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {
           $rows[$row['coupon_period_id']] = $row['title'];
        }
        
        return $rows;
        
    }
            
} // coupon_periodDAO

class coupon_prizeDAO extends baseDAO {

    protected $_tablename = UPCOUPON_PRIZE_TABLE;
    protected $_primarykey =  'coupon_prize_id';
    
    /** fetch_prizedropdown
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_prizedropdown($value = NULL, $db_key = NULL) {  
    
        if (is_null($db_key)) {
            $query = "SELECT title, coupon_prize_id FROM {$this->_tablename} WHERE status = ?"; 
        } else {
            $query = "SELECT title, coupon_prize_id FROM {$this->_tablename} WHERE status = ? AND {$db_key} = ?";          
        }
        
        if (self::$debug) {
            print "$query \n";
        }
        
        if (is_null($db_key)) {
            $dbresult = $this->__connection->Execute($query, array('1'));
        } else {
            $dbresult = $this->__connection->Execute($query, array('1', $value));          
        }
            
            
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {
           $rows[$row['coupon_prize_id']] = $row['title'];
        }
        
        return $rows;
        
    }
    
} // coupon_prizeDAO

class coupon_customerDAO extends baseDAO {

    protected $_tablename = UPCOUPON_CUSTOMER_TABLE;
    protected $_primarykey =  'coupon_customer_id';

    public function fetch_by_ip_checksum($ip, $checksum) {

        $query = "SELECT * FROM {$this->_tablename} 
                WHERE ip = ? AND checksum = ?";

        if (self::$debug) {
            print "$query \n";
        }
        
        $dbresult = $this->__connection->Execute($query, array($ip, $checksum));
    
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {
            $rows[] = $row;
        }
    
        return $rows;
  
    }
  
} // coupon_customerDAO

class coupon_codeDAO extends baseDAO {

    protected $_tablename = UPCOUPON_CODE_TABLE;
    protected $_primarykey =  'coupon_code_id';
    
    function draw_coupon($start_ts, $end_ts) {
            
        $query = "SELECT * 
                    FROM {$this->_tablename} 
                    WHERE coupon_customer_id != 0 
                    AND coupon_prize_id = 0
                    AND timestamp >= ?
                    AND timestamp <= ?
                    ORDER BY RAND( ) LIMIT 1";
        
        if (self::$debug) {
            print "$query \n";
            print "start_ts: $start_ts \n";
            print "end_ts: $end_ts \n";
        }
        
        $row = $this->__connection->GetRow($query, array($start_ts, $end_ts));
                
        return $row;
        
    }
    
    /** fetch_given_prize_count, collect the (number) of given prizes
     * 
     * @return $rows array - result set 
     *
     */
     
     public function fetch_given_prize_count() {
         
         $query = "SELECT count(*) as count, coupon_prize_id 
                    FROM {$this->_tablename}
                    GROUP BY coupon_prize_id
                    HAVING coupon_prize_id > 0";
         
        if (self::$debug) {
            print "$query \n";
        }
        
        $dbresult = $this->__connection->Execute($query);
        
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {
            $rows[$row['coupon_prize_id']] = $row['count'];
        }
        
        return $rows;
         
     }
    
} // coupon_codeDAO

class coupon_mixedDAO extends baseDAO {
    
    protected $_action_table    = UPCOUPON_ACTION_TABLE;
    protected $_period_table    = UPCOUPON_PERIOD_TABLE;
    protected $_prize_table     = UPCOUPON_PRIZE_TABLE;
    protected $_customer_table  = UPCOUPON_CUSTOMER_TABLE;
    protected $_code_table      = UPCOUPON_CODE_TABLE;
    
    public function fetch_winners($value = NULL, $db_key = NULL) {

        $query = "SELECT t1.title AS action_title,
                    t1.coupon_action_id AS coupon_action_id,
                    t2.title AS period_title,
                    t2.coupon_period_id AS coupon_period_id,
                    t3.title AS prize_title,
                    t3.prize_code AS prize_code,
                    t3.number AS number,
                    t3.coupon_prize_id AS coupon_prize_id,
                    t4.coupon_customer_id AS coupon_customer_id,
                    t4.first_name AS first_name, 
                    t4.last_name AS last_name,
                    t4.email AS email,
                    t4.phone AS phone,
                    t5.coupon_code_id AS coupon_code_id,
                    t5.timestamp AS timestamp,
                    t5.coupon_code AS coupon_code
                    FROM {$this->_action_table} t1,
                        {$this->_period_table} t2,
                        {$this->_prize_table} t3,
                        {$this->_customer_table} t4,
                        {$this->_code_table} t5
                    WHERE t1.coupon_action_id = t2.coupon_action_id
                    AND t2.coupon_period_id = t3.coupon_period_id
                    AND t3.coupon_prize_id = t5.coupon_prize_id
                    AND t4.coupon_customer_id = t5.coupon_customer_id
                    AND t1.status = '1'
                    AND t2.status = '1'
                    AND t3.status = '1'
                    AND t4.status = '1'";
                    
        if (!is_null($db_key)) {
            $query .= " AND {$db_key} = ?";
        }
        
        $query .= " ORDER BY t1.end_date, t2.end_date"; 
                    
        if (self::$debug) {
            print "$query \n";
        }
        
        if (is_null($db_key)) {
            $dbresult = $this->__connection->Execute($query);
        } else {
            $dbresult = $this->__connection->Execute($query, array($value));          
        }
        
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) { 
            $rows[] = $row;
        }
        
        return $rows; 
                    
    }
    
    /** fetch_action_period_prizes
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_action_period_prizes($value = NULL, $db_key = NULL) { 
            
        $query = "SELECT t3.*,
                    t1.title AS action_title,
                    t1.coupon_action_id AS coupon_action_id,
                    t2.title AS period_title,
                    t2.coupon_period_id AS coupon_period_id
        FROM {$this->_action_table} t1, 
            {$this->_period_table} t2, 
            {$this->_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 (!is_null($db_key)) {
            $query .= " AND {$db_key} = ?";
        }
        
        $query .= " ORDER BY t2.end_date"; 
        
        if (self::$debug) {
            print "$query \n";
        }
                
        if (is_null($db_key)) {
            $dbresult = $this->__connection->Execute($query);
        } else {
            $dbresult = $this->__connection->Execute($query, array($value));          
        }

        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {
            $rows[] = $row;
        }
        
        return $rows;
    }
    
    /** fetch_prizes
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_prizes() {  
    
        $query = "SELECT t3.* 
        FROM {$this->_period_table} t2, {$this->_prize_table} t3
        WHERE t2.coupon_period_id = t3.coupon_period_id
        ORDER BY t2.end_date"; 
        
        if (self::$debug) {
            print "$query \n";
        }
        
        $dbresult = $this->__connection->Execute($query);
        
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {
            $rows[] = $row;
        }
        
        return $rows;
    }

    /** fetch_action_period_js
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_action_period_js() {  
   
        $query = "SELECT t1.coupon_action_id AS coupon_action_id, 
                    t2.coupon_period_id AS coupon_period_id, 
                    t2.title AS title 
                    FROM {$this->_action_table} t1, {$this->_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";
        
        if (self::$debug) {
            print "$query \n";
        }
        
        $dbresult = $this->__connection->Execute($query);
      
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {            
            $main_key = $row['coupon_action_id'];
            $sub_key = $row['coupon_period_id'];
            $rows[$main_key][$sub_key] = $row['title'];
        }
        
        return $rows;
        
    }
    
    /** fetch_action_period_prize_js
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_action_period_prize_js() {  
   
        $query = "SELECT t1.coupon_action_id AS coupon_action_id, 
                    t2.coupon_period_id AS coupon_period_id, 
                    t2.title AS title 
                    FROM {$this->_action_table} t1, 
                        {$this->_period_table} t2,
                        {$this->_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' 
                    ORDER BY t2.end_date ASC";
        
        if (self::$debug) {
            print "$query \n";
        }
        
        $dbresult = $this->__connection->Execute($query);
      
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {            
            $main_key = $row['coupon_action_id'];
            $sub_key = $row['coupon_period_id'];
            $rows[$main_key][$sub_key] = $row['title'];
        }
        
        return $rows;
        
    }
    
    /** fetch_period_prize_js
     *
     *  @return $rows array - result set
     *
     */
    
    public function fetch_period_prize_js() { 
        
        $query = "SELECT t2.coupon_period_id AS coupon_period_id, 
                    t3.coupon_prize_id AS coupon_prize_id, 
                    t3.title AS title 
                 FROM {$this->_period_table} t2, {$this->_prize_table} t3 
                 WHERE t2.coupon_period_id = t3.coupon_period_id 
                 AND t2.status = '1' 
                 AND t3.status = '1' ORDER BY t2.end_date ASC";
                 
        if (self::$debug) {
            print "$query \n";
        }
        
        $dbresult = $this->__connection->Execute($query);
      
        $rows = array();
        
        while ($dbresult && $row = $dbresult->FetchRow()) {            
            $main_key = $row['coupon_period_id'];
            $sub_key = $row['coupon_prize_id'];
            $rows[$main_key][$sub_key] = $row['title'];
        }
        
        return $rows;
        
    } 
    
     
} //coupon_mixedDAO

#
# EOF
#
?>
