<?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: BrandController, v 1.0 2014/05/09 14:07:48
//
class BrandsController extends Controller {
    public function __construct() {
        parent::__construct();
        $brand_enable_in_frontend = $this->getSettingValue('product_cart_settings', 'brand_enable_in_frontend');
        if ($brand_enable_in_frontend != "Yes") {
            $this->ObjView->display('NoPage.tpl');
            exit;
        }
    }
    public function indexAction() {
        $ObjBrand      = new BrandModel();
        $ObjCategory   = new ProductCategoryModel();
        $languageIDs   = $this->ObjView->languageIDs;
        $alphabet      = $this->getRequestValue('alpha');
        
        $categoryKey   = $this->getRequestValue('cat');
        $selCategoryID = 0;
        $menuitemID = (int)$this->getRequestValue('itemID');
        if($menuitemID>0){
            // GET Meta Details
            $ObjCustomMenu = new CustommenuModel();
            $metaDetailsMenu = $ObjCustomMenu->getMenuItemMetaDetails($languageIDs,$menuitemID);
             if ($metaDetailsMenu['metaTitle'] != '') {
                $this->ObjView->assign('commonMetaTitle', $metaDetailsMenu['metaTitle']);
            }
            if ($metaDetailsMenu['metaKeywords'] != '') {
                $this->ObjView->assign('commonMetaKeywords', $metaDetailsMenu['metaKeywords']);
            }
            if ($metaDetailsMenu['metaDescription'] != '') {
                $this->ObjView->assign('commonMetaDescription', $metaDetailsMenu['metaDescription']);
            }
        }
        if ($categoryKey != '') {
            $selCategoryDet   = $ObjCategory->getCategoryInfo($languageIDs, '', $categoryKey, 'productCategoryID');
            $selCategoryID    = $selCategoryDet['productCategoryID'];
            $selCategoryLevel = $selCategoryDet['categoryLevel'];
            $params           = array(
                'productCategoryID' => $selCategoryID,
                'categoryLevel' => $selCategoryLevel,
                'currentLanguageID' => $languageIDs['Current'],
                'defaultLanguageID' => $languageIDs['Default']
            );
            $resBreadcrumbs   = $ObjCategory->getBreadcrumbsTree($params);
            $this->ObjView->assign('resBreadcrumbs', $resBreadcrumbs);
            $this->ObjView->assign('categoryKey', $categoryKey);
            $this->ObjView->assign('productCategoryID', $selCategoryID);
            $this->ObjView->assign('categoryLink', "cat/" . $categoryKey);
            $this->ObjView->assign('subcategories', $this->ObjCommon->getSubCategories($selCategoryID, 'Tree'));
            $catIDAndSubcategories = implode(",", array_merge(array(
                $selCategoryID
            ), $this->ObjCommon->getSubCategories($selCategoryID, 'ID')));
        }
        $activeAlpabets              = array();
        $list_brands_having_products = $this->getSettingValue('product_cart_settings', 'list_brands_having_products');
        $show_images_with_label      = $this->getSettingValue('product_cart_settings', 'show_images_with_label');
        $params                      = array(
            'languageIDs' => $languageIDs,
            'catIDAndSubcategories' => isset($catIDAndSubcategories) ? $catIDAndSubcategories : '',
            'list_brands_having_products' => $list_brands_having_products,
            'fields' => ''
        );
        if ($alphabet != '') {
            $params['readType'] = 'getFields';
            if (!$activeAlpabets = $this->ObjCommon->objCache->get('m_active_alpabets_' . $languageIDs['Current'] . "_" . $alphabet . "_" . $selCategoryID)) {
                $activeAlpabets = $ObjBrand->listBrandBasedOnAlpabets($params);
                $this->ObjCommon->objCache->set('m_active_alpabets_' . $languageIDs['Current'], $activeAlpabets, CACHE_TTL_LEVEL_3);
            }
        }
        $params['readType'] = 'readValues';
        $params['alphabet'] = $alphabet;
        $params['arLetters'] = '';
        $params['fields']   = ",brand.`brandID`,`brandName`,`brandKey`,`brandIcon`,`status`";
        //echo "<pre>";print_r($params);exit;
        if (!$resBrands = $this->ObjCommon->objCache->get('m_res_brands_' . $languageIDs['Current'])) {
            $resBrands = $ObjBrand->listBrandBasedOnAlpabets($params);
            $this->ObjCommon->objCache->set('m_res_brands_' . $languageIDs['Current'] . "_" . $alphabet . "_" . $selCategoryID, $resBrands, CACHE_TTL_LEVEL_3);
        }
        $listBrands = array();
        foreach ($resBrands as $key => $listBrand) {
            $listBrands[$listBrand['alphabet']][$key] = $listBrand;
        }
        $alphabets = range('A', 'Z');        
        $this->ObjView->assign('activeAlpabets', $activeAlpabets);
        $this->ObjView->assign('listBrands', $listBrands);
        $this->ObjView->assign('alphabet', $alphabet);
        $this->ObjView->assign('show_images_with_label', $show_images_with_label);
        $this->ObjView->assign("alphabets", $alphabets);
        $this->ObjView->assign('hasAlphabet', $alphabet != '' ? 0 : 1);
        $this->ObjView->display('BrandList.tpl');
    }
    public function ajaxLoadBrandsAction() {
        $ObjBrand                    = new BrandModel();        
        $languageIDs                 = $this->ObjView->languageIDs;
        $selCategoryID               = (int)$_POST['cid'];
        $alphabet                    = $_POST['key'];
        $activeAlpabets              = array();
        $list_brands_having_products = $this->getSettingValue('product_cart_settings', 'list_brands_having_products');
        $show_images_with_label      = $this->getSettingValue('product_cart_settings', 'show_images_with_label');
        $catIDAndSubcategories       = '';
        if($selCategoryID>0) {
            $catIDAndSubcategories       = implode(",", array_merge(array(
                    $selCategoryID
                ), $this->ObjCommon->getSubCategories($selCategoryID, 'ID')));
        }
        $params                      = array(
            'languageIDs' => $languageIDs,
            'catIDAndSubcategories' => isset($catIDAndSubcategories) ? $catIDAndSubcategories : '',
            'list_brands_having_products' => $list_brands_having_products,
            'fields' => ''
        );        
        $params['readType'] = 'readValues';
        $params['alphabet']  = '';
        $params['arLetters'] = $_POST['alpha'];
        $params['fields']   = ",brand.`brandID`,`brandName`,`brandKey`,`brandIcon`,`status`";
       
        if (!$resBrands = $this->ObjCommon->objCache->get('m_res_brands_' . $languageIDs['Current'])) {
            $resBrands = $ObjBrand->listBrandBasedOnAlpabets($params);            
            $this->ObjCommon->objCache->set('m_res_brands_' . $languageIDs['Current'] . "_" . $alphabet . "_" . $selCategoryID, $resBrands, CACHE_TTL_LEVEL_3);
        }
        $listBrands = array();
        foreach ($resBrands as $key => $listBrand) {
            $listBrands[$listBrand['alphabet']][$key] = $listBrand;
        }
        
        $this->ObjView->assign('alphabets', explode(",",$_POST['alpha']));
        $this->ObjView->assign('listBrands', $listBrands);
        $this->ObjView->assign('alphabet', '');
        $this->ObjView->assign('show_images_with_label', $show_images_with_label);
        $this->ObjView->assign('hasAlphabet', $alphabet != '' ? 0 : 1);
        echo json_encode($this->ObjView->fetch('AjaxLoadBrands.tpl'));
        exit;
    }
}
