<?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: AdbannerController.php, v 1.0 2011/06/14 20:52:11
//
class AdbannerController extends Controller {
    public function __construct() {
        parent::__construct();
        $this->ObjCommon->askPrivilege('adbanner/index/');
    }
    //Adbanner index action
    public function indexAction() {
        $ObjAdbanner = new AdBannerModel();
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resPromoBannerTypeList = $ObjAdbanner->listAdBannerType($limitFrom, $limitTo, '', 'Yes');
        $resPagesCount          = $this->setPagesCount($key = 'page');
        $links                  = "adbanner/index/";
        if ($this->getRequestValue('page')) {
            $links .= "page/" . $this->getRequestValue('page') . "/";
        }
        if (isset($_POST['submitBulkStatus']) && $_POST['promoBannerBulkStatus'] != '' && $_POST['bannerTypeStatus'] != '') {
            $resChangePromoBannerTypeStatus = $ObjAdbanner->changePromoBannerTypeStatus($_POST['bannerTypeStatus'], $_POST['promoBannerBulkStatus']);
            if ($resChangePromoBannerTypeStatus) {
                $this->setMessage("language.msgStatusUpdatedSuccessfully", "message_success");
                $this->setRedirect();
            }
        }
        $this->ObjView->assign('resPromoBannerTypeList', $resPromoBannerTypeList);
        $this->ObjView->display('PromotionalBannerTypeList.tpl');
    }
    //Adbanner add
    public function newAction() {
        $ObjAdbanner  = new AdBannerModel();
        $ObjImage     = new ImageModel();
        $previousURL  = $this->getPreviousURL();
        $bannerTypeID = $this->getRequestValue("type");
        if (strstr($previousURL, 'adbanner')) {
            $this->ObjView->assign('previousURL', $previousURL);
        }
        $resPromoBannerTypeList = $ObjAdbanner->listAdBannerType();
        foreach ($resPromoBannerTypeList as $key => $resPromoBannerTypeList) {
            $promobannerPositon[$key][0] = $resPromoBannerTypeList['bannerTypeID'];
            $promobannerPositon[$key][1] = $resPromoBannerTypeList['bannerTypePosition'] . '(' . $resPromoBannerTypeList['bannerTypeWidth'] . 'x' . $resPromoBannerTypeList['bannerTypeHeight'] . ')';
        }
        $this->ObjView->assign('resPromoBannerTypeList', $promobannerPositon);
        if (isset($_POST['bannerDisplayType']))
            $this->ObjView->assign('bannerDisplayType', $_POST['bannerDisplayType']);
        if ($this->ObjForm->isValid('formPromoBannerAdd', 'submitBanner')) {
            $file                   = $_FILES['bannerSwf']['name'];
            $resSaveAdBannerDetails = $ObjAdbanner->saveAdBannerDetails($_POST, $file);
            if ($resSaveAdBannerDetails) {
                $resPromoBannerType = $ObjAdbanner->getBannerType($_POST['bannerPosition']);
                if ($_POST['bannerDisplayType'] == 'Image' && !empty($_POST['bannerImage_temp'])) {
                    $ObjImage->copy_file($_POST["bannerImage_temp"], '/' . __UPLOADS_FOLDER . 'adBanner/' . $_POST['bannerImage']);
                    $ObjImage->copy_file($_POST["bannerImage_temp"], '/' . __UPLOADS_FOLDER . 'adBanner/thumb/' . $_POST['bannerImage']);
                    @unlink($_POST["bannerImage_temp"]);
                }
                if ($_POST['bannerDisplayType'] == 'Swf' && !empty($_FILES['bannerSwf']['tmp_name'])) {
                    /*$bannerSwfpath=     __CORE_PUBLIC_PATH."/uploads/adBanner/Swf/".$file;
                    $bannerSwftemppath=   $_FILES['bannerSwf']['tmp_name'];
                    copy($bannerSwftemppath,$bannerSwfpath);*/
                    $ObjImage->copy_file($_FILES['bannerSwf']['tmp_name'], '/' . __UPLOADS_FOLDER . 'adBanner/Swf/' . $file);
                    @unlink($_FILES['bannerSwf']['tmp_name']);
                }
                $this->setMessage("language.msgAddNewBanner", "message_success");
                $this->setRedirect("adbanner/");
            }
        }
        $this->ObjView->assign('bannerTypeID', $bannerTypeID);
        $this->ObjView->display('PromotionalBannerAdd.tpl');
    }
    //Adbanner details
    public function detailsAction() {
        $bannerTypeID = $this->getRequestValue("id");
        $ObjAdbanner  = new AdBannerModel();
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resPromoBannerList = $ObjAdbanner->listAdBanner($bannerTypeID, $limitFrom, $limitTo);
        $resPagesCount      = $this->setPagesCount($key = 'page');
        $links              = "adbanner/details/id/" . $bannerTypeID . "/";
        if (isset($_POST['promoBannerDetailsBulkStatus']) && $_POST['promoBannerDetailsBulkStatus'] != '' && $_POST['bannerStatus'] != '') {
            $resChangePromoBannerStatus = $ObjAdbanner->changePromoBannerStatus($_POST['bannerStatus'], $_POST['promoBannerDetailsBulkStatus']);
            if ($resChangePromoBannerStatus) {
                if ($_POST['bannerStatus'] == 'Deleted') {
                    $this->setMessage("language.msgBannersDelete", 3);
                } else {
                    $this->setMessage("language.msgStatusUpdatedSuccessfully", "message_success");
                }
                $this->setRedirect();
            }
        }
        if ($this->getRequestValue('page')) {
            $links .= "page/" . $this->getRequestValue('page') . "/";
        }
        $config['date'] = $this->getSettingValue('site_settings', 'date_format');
        $config['time'] = $this->getSettingValue('site_settings', 'time_format');
        $this->ObjView->assign('links', $links);
        $this->ObjView->assign('config', $config);
        $this->ObjView->assign('type', $bannerTypeID);
        $this->ObjView->assign('resPromoBannerList', $resPromoBannerList);
        $this->ObjView->assign('publicPath', __CORE_PUBLIC_PATH);
        $this->ObjView->display('PromotionalBannerDetails.tpl');
    }
    //Adbanner destroy
    public function destroyAction() {
        $bannerID[]           = $this->getRequestValue("id");
        $ObjAdbanner          = new AdBannerModel();
        $resChangeAdminStatus = $ObjAdbanner->changePromoBannerStatus('Deleted', $bannerID);
        $this->setMessage("language.msgBannerDelete", "message_success");
        $previousURL = $this->getPreviousURL();
        $this->setRedirect($previousURL);
    }
    //Edit adbanner
    public function editAction() {
        $ObjAdbanner            = new AdBannerModel();
        $ObjImage               = new ImageModel();
        $bannerID               = $this->getRequestValue("id");
        $resPromoBannerTypeList = $ObjAdbanner->listAdBannerType();
        foreach ($resPromoBannerTypeList as $key => $resPromoBannerTypeList) {
            $promobannerPositon[$key][0] = $resPromoBannerTypeList['bannerTypeID'];
            $promobannerPositon[$key][1] = $resPromoBannerTypeList['bannerTypePosition'] . '(' . $resPromoBannerTypeList['bannerTypeWidth'] . 'x' . $resPromoBannerTypeList['bannerTypeHeight'] . ')';
        }
        $resPromoBanner = $ObjAdbanner->getBanner($bannerID);
        if (!$resPromoBanner) {
            $previousURL = $this->getPreviousURL();
            $this->setRedirect($previousURL);
        }
        if ($resPromoBanner['bannerDisplayType'] == 'Script') {
            $this->ObjView->assign('bannerScript1', $resPromoBanner['bannerScript']);
        } else if ($resPromoBanner['bannerDisplayType'] == 'Swf') {
            $this->ObjView->assign('previousSwf', $resPromoBanner['bannerSwf']);
        }
        $this->ObjView->assignValues($resPromoBanner);
        $this->ObjView->assign('resPromoBanner', $resPromoBanner);
        $this->ObjView->assign('resPromoBannerTypeList', $promobannerPositon);
        if ($this->ObjForm->isValid('formPromoBannerAdd', 'submitBanner')) {
            $file                   = $_FILES['bannerSwf']['name'];
            $resSaveAdBannerDetails = $ObjAdbanner->saveAdBannerDetails($_POST, $file, $bannerID);
            if ($resSaveAdBannerDetails) {
                if ($_POST['bannerDisplayType'] == 'Image') {
                    $resPromoBannerType = $ObjAdbanner->getBannerType($_POST['bannerPosition']);
                    if (!empty($_POST['bannerImage_temp'])) {
                        $imagePath = __UPLOADS_FOLDER . 'adBanner/' . trim($resPromoBanner['bannerImage']);
                        $ObjImage->unlinkImage($imagePath);
                        $imagePath = __UPLOADS_FOLDER . 'adBanner/thumb/' . trim($resPromoBanner['bannerImage']);
                        $ObjImage->unlinkImage($imagePath);
                        $ObjImage->copy_file($_POST["bannerImage_temp"], '/uploads/adBanner/' . $_POST['bannerImage']);
                        $ObjImage->copy_file($_POST["bannerImage_temp"], '/uploads/adBanner/thumb/' . $_POST['bannerImage']);
                    }
                }
                if ($_POST['bannerDisplayType'] == 'Swf') {
                    $bannerSwfpath     = __CORE_PUBLIC_PATH . "/uploads/adBanner/Swf/" . $file;
                    $bannerSwftemppath = $_FILES['bannerSwf']['tmp_name'];
                    if ($bannerSwftemppath)
                        copy($bannerSwftemppath, $bannerSwfpath);
                }
                $this->setMessage("language.msgBannerUpdate", "message_success");
                $this->setRedirect('adbanner/details/id/' . $resPromoBanner['bannerTypeID'] . '/');
            }
        }
        $this->ObjView->display('PromotionalBannerAdd.tpl');
    }
    public function ajaxtypeeditAction() {
        $ObjAdbanner = new AdBannerModel();
        $newstatus   = $_POST['newstatus'];
        $id          = $_POST['id'];
        $bannerID    = array(
            $id
        );
        if ($newstatus && $id) {
            $resChangePromoBannerTypeStatus = $ObjAdbanner->changePromoBannerTypeStatus($newstatus, $bannerID);
            $this->setMessage("language.msgStatusUpdatedSuccessfully", "message_success");
            if ($resChangePromoBannerTypeStatus)
                echo 1;
        }
    }
    // end of function
    public function ajaxstatusAction() {
        $ObjAdbanner    = new AdBannerModel();
        $newstatus      = $_POST['newstatus'];
        $bannerDetailID = $_POST['bannerid'];
        if ($newstatus && $bannerDetailID) {
            $bannerDetailID             = array(
                $bannerDetailID
            );
            $resChangePromoBannerStatus = $ObjAdbanner->changePromoBannerStatus($newstatus, $bannerDetailID);
            $this->setMessage("language.msgStatusUpdatedSuccessfully", "message_success");
            if ($resChangePromoBannerStatus)
                echo 1;
        }
    }
    // end of function
}