<?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: StaticBlockController.php, v 1.0 2013/03/21 20:52:11
//
class StaticblockController extends controller {
    public function indexAction() {
        $ObjCustommenu          = new CustommenuModel();
        $ObjBrand               = new BrandModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        /*privileges*/
        $this->ObjView->assign('editprivilege', $this->ObjCommon->hasPrivilege('staticblock/edit/'));
        $deleteprivilege = $this->ObjCommon->hasPrivilege('staticblock/destroy/');
        $this->ObjView->assign('deleteprivilege', $deleteprivilege);
        /*privileges*/
        $sortByOption = $this->getRequestValue("sortby");
        $sortByOrder  = $this->getRequestValue("orderby");
        if (isset($_POST['bulkStatus'])) {
            if ($_POST['blockBulkIDs']) {
                $respBulkUpdate = $ObjCustommenu->updateStaticBlockStatus($_POST, $deleteprivilege);
                if ($respBulkUpdate) {
                    if ($_POST['bulkStatus'] == 'Deleted')
                        $this->setMessage("language.msgStaticBlockDeletedSuccessfully", 3);
                    else
                        $this->setMessage("language.msgStaticBlockStatusUpdate", 3);
                }
                $this->setRedirect();
            }
        }
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page');
        $resBlocks     = $ObjCustommenu->listStaticBlocks($languageIDs, $limitFrom, $limitTo, '', $sortByOption, $sortByOrder, 'list');
        $resPagesCount = $this->setPagesCount($key = 'page');
        // sort by
        if ($sortByOption == 'stitle' && $sortByOrder == "asc") {
            $sortByTitle = "desc";
        } else {
            $sortByTitle = "asc";
        }
        if (isset($blockList['dateFrom']))
            $dateFrom = substr($blockList['dateFrom'], 0, -3);
        if (isset($blockList['dateTo']))
            $dateTo = substr($blockList['dateTo'], 0, -3);
        $this->ObjView->assign('resBlocks', $resBlocks);
        $this->ObjView->assign("sortByTitle", $sortByTitle);
        //$this->ObjView->assign("sortByPosition", $sortByPosition);
        $this->ObjView->assign('sortByOrder', $sortByOrder);
        $this->ObjView->assign('sortByOption', $sortByOption);
        if (!$sortByOption) {
            $this->ObjView->assign('sortByOption', 'stitle');
            $this->ObjView->assign('sortByOrder', 'asc');
            $this->ObjView->assign('sortByTitle', 'desc');
        }
        $brandID = '';
        $this->ObjView->assign('brandID', $brandID);
        $params    = array(
            'languageIDs' => $languageIDs
        );
        $listBrand = $ObjBrand->listBrand($params);
        $this->ObjView->assign('listBrand', $listBrand);
        $this->ObjView->display('StaticblockList.tpl');
    }
    public function newAction() {
        $ObjCustommenu      = new CustommenuModel();
        $ObjBrand           = new BrandModel();
        $settingsDateFormat = $this->getSettingValue("site_settings", "date_format");
        $languageIDs        = $this->ObjView->languageIDs;
        if ($this->ObjForm->isValid('frmBlock', 'submitBlock')) {
            if ($_POST['dateFrom'] || $_POST['dateTo']) {
                $_POST['dateFrom'] = $this->convertDateForSQL($_POST['dateFrom'], $settingsDateFormat);
                $_POST['dateTo']   = $this->convertDateForSQL($_POST['dateTo'], $settingsDateFormat);
            }
            $blockID = $ObjCustommenu->saveBlockDetails($languageIDs, $_POST);
            $this->setMessage("language.msgBlockUpdate", 3);
            $this->setRedirect("/staticblock/index/");
        }
        $params    = array(
            'languageIDs' => $languageIDs
        );
        $listBrand = $ObjBrand->listBrand($params);
        $this->ObjView->assign('listBrand', $listBrand);
        $this->ObjView->display('StaticblockAdd.tpl');
    }
    //FUNCTION TO EDIT MENUS
    public function editAction() {
        $ObjCustommenu          = new CustommenuModel();
        $ObjBrand               = new BrandModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $blockID                = $this->getRequestValue("id");
        $settingsDateFormat     = $this->getSettingValue("site_settings", "date_format");
        if ($blockID) {
            $blockList = $ObjCustommenu->listStaticBlocks($languageIDs, '', '', $blockID, '', '', 'list');
            if (!$blockList) {
                $this->setRedirect("/staticblock/index/");
            }
            $blockList = $blockList[0];
            if ($blockList['dateFrom'])
                $dateFrom = substr($blockList['dateFrom'], 0, -3);
            if ($blockList['dateTo'])
                $dateTo = substr($blockList['dateTo'], 0, -3);
            $this->ObjView->assign('dateFrom', ($dateFrom > 0) ? $dateFrom : '');
            $this->ObjView->assign('dateTo', ($dateTo > 0) ? $dateTo : '');
        }
        if ($this->ObjForm->isValid('frmBlock', 'submitBlock')) {
            $blockID = ($blockID) ? $blockID : '';
            if ($_POST['dateFrom'] || $_POST['dateTo']) {
                $_POST['dateFrom'] = $this->convertDateForSQL($_POST['dateFrom'], $settingsDateFormat);
                $_POST['dateTo']   = $this->convertDateForSQL($_POST['dateTo'], $settingsDateFormat);
            }
            $result = $ObjCustommenu->saveBlockDetails($languageIDs, $_POST, $blockID);
            $this->setMessage("language.msgBlockUpdate", 3);
            $this->setRedirect("/staticblock/index/");
        }
        $this->ObjView->assign('blockList', $blockList);
        $this->ObjView->assign('brandID', $blockList['brandID']);
        $this->ObjView->assign('blockID', $blockID);
        $params    = array(
            'languageIDs' => $languageIDs
        );
        $listBrand = $ObjBrand->listBrand($params);
        $this->ObjView->assign('listBrand', $listBrand);
        $this->ObjView->display('StaticblockAdd.tpl');
    }
    //TO CHANGE STATIC BLOCK STATUS
    public function ajaxchangeStatusAction() {
        $ObjCustommenu        = new CustommenuModel();
        $POST                 = array();
        $POST['blockBulkIDs'] = $_POST['blockId'];
        $POST['bulkStatus']   = $_POST['newstatus'];
        $ObjCustommenu->updateStaticBlockStatus($POST, $this->ObjCommon->hasPrivilege('staticblock/destroy/'));
        $this->setMessage("language.msgStaticBlockStatusUpdate", 3);
    }
    //FUNCTION TO DELETE MENU
    public function destroyAction() {
        $ObjCustommenu        = new CustommenuModel();
        $POST                 = array();
        $POST['blockBulkIDs'] = $this->getRequestValue('id');
        $POST['bulkStatus']   = 'Deleted';
        if ($POST['blockBulkIDs']) {
            $respBulkUpdate = $ObjCustommenu->updateStaticBlockStatus($POST, $this->ObjCommon->hasPrivilege('staticblock/destroy/'));
            $this->setMessage("language.msgStaticBlockDelete", "success");
            $this->setRedirect('/staticblock/index/');
        }
    }
}