<?php
/***************************************************************************
 *                                                                          *
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.   *
 *                                                                          *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.                                                    *
 *                                                                          *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.            *
 ****************************************************************************/ //
//$Id: AdBannerModel.php, v 1.0 2013/03/14 14:14:14
class AdBannerModel extends Model {
    // Function To List Promotional banner types	
    function listAdBannerType($limitFrom = 0, $limitTo = "", $status = "", $bannerCount = "NO") {
        $sql = "SELECT ";
        //IF PAGINATION REQUIRED						 
        if ($limitTo)
            $sql .= " SQL_CALC_FOUND_ROWS ";
        $sql .= " proBanTyp.*";
        if (isset($interface) && $interface != '')
            $sql .= " ,proBan.*";
        if ($bannerCount == "Yes")
            $sql .= ",COUNT( proBan.bannerID) AS bannerCount ";
        $sql .= " 
		FROM  
			" . DB_PREFIX . "promotional_banners_type proBanTyp";
        if ($bannerCount == "Yes") {
            $sql .= " 
				LEFT JOIN 
					" . DB_PREFIX . "promotional_banners proBan 
						ON  (
								proBan.bannerStatus !='Deleted' 
						 		AND proBan.bannerTypeID = proBanTyp.bannerTypeID
							)";
        }
        if ($status)
            $sql .= " WHERE proBanTyp.bannerTypeStatus='" . $status . "'";
        $sql .= " GROUP BY proBanTyp.bannerTypeID ";
        //IF PAGINATION REQUIRED	
        if ($limitTo)
            $sql .= " LIMIT  " . $limitFrom . "," . $limitTo . "";
        $result = $this->ObjDatabase->readValues($sql);
        return $result;
    }
    // End of function
    //Function to Update banner Type status	
    function changePromoBannerTypeStatus($bannerTypeStatus, $promoBannerBulkStatusArray) {
        $sql = $this->ObjDatabase->setQuery("
			UPDATE 
				" . DB_PREFIX . "promotional_banners_type  
			SET 
				bannerTypeStatus ='" . $bannerTypeStatus . "' 
			WHERE 
				bannerTypeID IN(" . implode(',', $promoBannerBulkStatusArray) . ")
		");
        return TRUE;
    }
    // End of function		
    //Function to Update banner status	
    function changePromoBannerStatus($bannerStatus, $promoBannerDetailsBulkStatusArray) {
        $sql = $this->ObjDatabase->setQuery("
		UPDATE 
			" . DB_PREFIX . "promotional_banners 
		SET 
			bannerStatus ='" . $bannerStatus . "' 
		WHERE 
			bannerID IN(" . implode(',', $promoBannerDetailsBulkStatusArray) . ")
		");
        return TRUE;
    }
    // End of function			
    //Function to Insert or Update Banner Details
    function saveAdBannerDetails($POST, $file, $bannerID = '') {
        if ($POST['bannerDisplayType'] == 'Image') {
            $bannerImage  = $this->escape($POST['bannerImage']);
            $bannerScript = '';
            $bannerSwf    = '';
        } elseif ($POST['bannerDisplayType'] == 'Script') {
            $bannerScript = $this->escape($POST['bannerScript1'], false);
            $bannerImage  = '';
            $bannerSwf    = '';
        } elseif ($POST['bannerDisplayType'] == 'Swf') {
            $bannerSwf    = $this->escape($file);
            $bannerImage  = '';
            $bannerScript = '';
        }
        if ($bannerID) {
            $sql = "
			UPDATE 
				" . DB_PREFIX . "promotional_banners 
			SET 
				bannerCaption='" . $this->escape($POST['bannerCaption']) . "',
				bannerTypeID='" . $POST['bannerPosition'] . "',
				bannerDisplayType='" . $POST['bannerDisplayType'] . "',
				bannerScript='" . $bannerScript . "',
				bannerImage='" . $bannerImage . "',";
            if ($bannerSwf && $POST['bannerDisplayType'] == 'Swf') {
                $sql .= " bannerSwf='" . $bannerSwf . "',";
            } else if ($POST['previousSwf'] && $POST['bannerDisplayType'] == 'Swf') {
                $sql .= "bannerSwf='" . $POST['previousSwf'] . "',";
            } else {
                $sql .= "bannerSwf='',";
            }
            $sql .= "bannerURL='" . $this->escape($POST['bannerURL']) . "',
						bannerStatus='" . $POST['bannerStatus'] . "'  
			WHERE
						bannerID='" . $bannerID . "'";
            $result = $this->ObjDatabase->setQuery($sql);
            return $result;
        } else {
            $sql    = "
			INSERT INTO 
				" . DB_PREFIX . "promotional_banners(
					bannerCaption,
					bannerTypeID,
					bannerDisplayType,
					bannerScript,
					bannerImage,
					bannerSwf,
					bannerURL,
					bannerStatus,
					bannerCreatedDate
				)VALUES(
					'" . $this->escape($POST['bannerCaption']) . "',
					'" . $POST['bannerPosition'] . "',
					'" . $POST['bannerDisplayType'] . "',
					'" . $bannerScript . "',
					'" . $bannerImage . "',
					'" . $bannerSwf . "',
					'" . $this->escape($POST['bannerURL']) . "',
					'" . $POST['bannerStatus'] . "',
					NOW()
				)";
            $result = $this->ObjDatabase->setQuery($sql);
            return $result;
        }
    }
    // End of function	
    //Function to List Promotional banners			
    function listAdBanner($bannerTypeID, $limitFrom = 0, $limitTo = "", $status = "") {
        $sql = "SELECT ";
        //IF PAGINATION REQUIRED						 
        if ($limitTo)
            $sql .= "SQL_CALC_FOUND_ROWS ";
        $sql .= "proBan.*,
								proBanTyp.bannerTypeWidth,
								proBanTyp.bannerTypeHeight 
		FROM  
			" . DB_PREFIX . "promotional_banners proBan,
			" . DB_PREFIX . "promotional_banners_type proBanTyp 
		WHERE 
			proBan.bannerTypeID=" . (int) $bannerTypeID . "
			AND proBanTyp.bannerTypeID=" . (int) $bannerTypeID . "
			AND proBan.bannerStatus <>'Deleted'
		GROUP BY proBan.bannerID  ";
        //IF PAGINATION REQUIRED	
        if ($limitTo) {
            $sql .= " LIMIT  " . $limitFrom . "," . $limitTo . "";
        }
        return $this->ObjDatabase->readValues($sql, MYSQLI_ASSOC);
    }
    // End of function	
    //Function to Get Banner type	
    function getBannerType($bannerTypeID, $bannerTypePosition = '') {
        if ($bannerTypeID) {
            $sql = "
			SELECT 
				* 
			FROM 
				" . DB_PREFIX . "promotional_banners_type 
			WHERE 
				bannerTypeID='" . $bannerTypeID . "'";
        }
        if ($bannerTypePosition) {
            $sql = "
			SELECT 
				* 
			FROM 
				" . DB_PREFIX . "promotional_banners_type 
			WHERE 
				bannerTypePosition='" . $bannerTypePosition . "'";
        }
        $result = $this->ObjDatabase->readValue($sql);
        return $result;
    }
    // End of function
    //Function to Get Banner By ID
    function getBanner($bannerID) {
        $sql    = "
		SELECT 
			* 
		FROM 
			" . DB_PREFIX . "promotional_banners 
		WHERE 
			bannerID='" . (int) $bannerID . "'";
        $result = $this->ObjDatabase->readValue($sql);
        return $result;
    }
    // End of function
    //Function to list published ads
    function listPublishedAd() {
        $sql = "
		SELECT  
			proBanTyp.bannerTypeID,
			proBanTyp.bannerTypePosition,
			proBanTyp.bannerTypeWidth,
			proBanTyp.bannerTypeHeight,
			proBan.bannerID,
			proBan.bannerCaption,
			proBan.bannerDisplayType,
			proBan.bannerScript,
			proBan.bannerImage,
			proBan.bannerSwf,
			proBan.bannerURL,
			(
				SELECT 
					COUNT(bannerID) 
				FROM 
					" . DB_PREFIX . "promotional_banners 
				WHERE 
					bannerTypeID=proBanTyp.bannerTypeID
			) AS bannerCount
		FROM 
			" . DB_PREFIX . "promotional_banners proBan
		LEFT JOIN 
			" . DB_PREFIX . "promotional_banners_type proBanTyp
			ON  ( 
					proBan.bannerStatus ='Active'  
					AND proBan.bannerTypeID = proBanTyp.bannerTypeID 
				) 				
		WHERE 
			proBanTyp.bannerTypeStatus='Active' ";
        return $this->ObjDatabase->readValuesSlave($sql);
    }
    // End of function
}