<?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: ProductCategoryController.php, v 1.0 2011/06/14 20:52:11
//
class ProductCategoryController extends Controller {
    public function indexAction() {
        $ObjProductCategory = new ProductCategoryModel();
        $languageIDs     = $this->ObjView->languageIDs;
        if (isset($_POST['submitBulkStatus']) == "Update") {
            if ($_POST['productCategoryBulkStatus']) {
                $respBulkUpdate = $ObjProductCategory->updateProductCategoryBulkStatus($_POST);
                if ($respBulkUpdate) {
                    $this->setMessage("language.msgProductCategoryStatusUpdate", 3);
                }
                $this->setRedirect();
            }
        }
		
		$brandID       			= $this->getRequestValue('brandID');
		$ObjBrand       		= new BrandModel();	
		$brandParams         	= array(
			'languageIDs' 		=> $languageIDs
		);       
		$listBrand      		= $ObjBrand->listBrand($brandParams);  
		$brandID = (!empty($brandID )) ? $brandID : $listBrand[0]['brandID'];
		$this->ObjView->assign('listBrand', $listBrand);
		$this->ObjView->assign('brandID', $brandID);
		
        $languageIDs['Current'] = $languageIDs['Content'];
        $resProductCategories      = $ObjProductCategory->listProductCategories($languageIDs, -1,'','',0,0,'','',$brandID);
        $productCategoryCount   = count($resProductCategories);
        foreach ($resProductCategories as $key => $value) {
            $categoryTree[$resProductCategories[$key]['productCategoryParent']][] = $resProductCategories[$key];
        }
        $resProductCategories = $this->createCategoryTree($categoryTree, $categoryTree[0]); //echo '<pre>'; print_r($resProductCategories); exit;
        $this->ObjView->assign("resProductCategories", $resProductCategories);
        $this->ObjView->assign("productCategoryCount", $productCategoryCount);
        $this->ObjView->assign('statusprivilege', $this->ObjCommon->hasPrivilege('productcategory/edit/'));
        $this->ObjView->assign('deleteprivilege', $this->ObjCommon->hasPrivilege('productcategory/destroy/'));
        $this->ObjView->display('ProductCategory.tpl');
    }
    private function createCategoryTree(&$list, $parent) {
        $tree = array();
        if (is_array($parent)) {
            foreach ($parent as $k => $l) {
                if (isset($list[$l['productCategoryParent']]) && !empty($list[$l['productCategoryID']])) {
                    $l['children'] = $this->createCategoryTree($list, $list[$l['productCategoryID']]);
                }
                $tree[] = $l;
            }
        }
        return $tree;
    }
    public function ajaxSavePriorityAction() {
        $ObjProductCategory = new ProductCategoryModel();
        $ObjProductCategory->saveCategoryPriority();
    }
    //FUNCTION to change ProductCategory Status
    public function ajaxchangeStatusAction() {
        $ObjProductCategory                    = new ProductCategoryModel();
        $_POST['productCategoryBulkStatus'] = array(
            $_POST['categoryid']
        );
        $_POST['bulkStatus']                = $_POST['categorystatus'];
        $resUpdate                          = $ObjProductCategory->updateProductCategoryBulkStatus($_POST);
        if (!$resUpdate) {
            echo "0";
        } else
            $this->setMessage("language.msgProductCategoryStatusUpdate", 3);
    }
    //TO ADD NEW DEAL CATEGORY
    public function newAction() {
        $ObjImage                   = new ImageModel();
        $ObjProductCategory            = new ProductCategoryModel();
        $ObjWidget                  = new WidgetModel();
		$ObjBrand       = new BrandModel();	
        $languageIDs                = $this->ObjView->languageIDs;
        $languageIDs['Current']     = $languageIDs['Content'];
        $settingsDateFormat         = $this->getSettingValue('site_settings', 'date_format');
        //URL Rewrite settings        
        $settingsProductThumbWidth  = $this->getSettingValue('product_cart_settings', 'thumb_width');
        $settingsProductThumbHeight = $this->getSettingValue('product_cart_settings', 'thumb_height');
        $suffix                     = $this->getSettingValue('seo_settings', 'category_url_suffix');
        $resListProductCategory        = $ObjProductCategory->listProductCategoryItems($languageIDs, 0, 0, 0, '', false, $this->ObjView->_languageXML, 'Yes');
        $resCategoryTree            = $ObjProductCategory->listProductCategoryItems($languageIDs, 0, 0);
        $resCategoryTree            = array_slice($resCategoryTree, 1);
        $this->ObjView->assign('resCategoryTree', $resCategoryTree);
        if ($this->ObjForm->isValid('formProductCategorySubmit', 'submitProductCategory')) {
            $urlKey                           = ($_POST['productCategoryKey']) ? $_POST['productCategoryKey'] : $this->ObjCommon->generateKey("catalog", $_POST['productCategoryName'], 0, "unique");
            $_POST['layoutActiveFrom']        = (isset($_POST['layoutActiveFrom']) && $_POST['layoutActiveFrom'] != '') ? $this->convertDateForSql($_POST['layoutActiveFrom'], $settingsDateFormat) : '';
            $_POST['layoutActiveTo']          = (isset($_POST['layoutActiveTo']) && $_POST['layoutActiveTo'] != '') ? $this->convertDateForSql($_POST['layoutActiveTo'], $settingsDateFormat) : '';
            $_POST['pageDefaultView']         = isset($_POST['pageDefaultView']) ? $_POST['pageDefaultView'] : '';
            $_POST['productGridListLayoutID'] = isset($_POST['productGridListLayoutID']) ? $_POST['productGridListLayoutID'] : '';
            $_POST['productDetailsLayoutID']  = isset($_POST['productDetailsLayoutID']) ? $_POST['productDetailsLayoutID'] : '';
            if ($_POST['pageDefaultView'] == '') {
                $_POST['pageDefaultView'] = $_POST['hiddenPageDefaultView'];
            }
            if ($_POST['productGridListLayoutID'] == '') {
                $_POST['productGridListLayoutID'] = $_POST['hiddenProductGridListLayoutID'];
            }
            if ($_POST['productDetailsLayoutID'] == '') {
                $_POST['productDetailsLayoutID'] = $_POST['hiddenProductDetailsLayoutID'];
            }
            if (!in_array($_POST['pageDefaultView'],array('category_with_products','widget_and_category_with_products'))) {
                $_POST['numberOfProductCount'] = 'NULL';
            }
            $rsProductCategory = $ObjProductCategory->submitProductCategory($languageIDs, '', $urlKey);
            if ($rsProductCategory == "ae") {
                $this->setMessage("language.msgDuplicateProductCategoryKey", "error");
            } else {
                $saveWidgetToCategory = $ObjProductCategory->saveWidgetToCategory($rsProductCategory);
                //URL Rewite
                $ObjSef               = new SefModel();
                $ObjSef->_refreshCategoryRewrites($languageIDs['AllList'], $rsProductCategory, $suffix, '', false, $_POST['productCategoryParent'], "insert");
                //END
                if (!empty($_POST['productCategoryImage_temp'])) {
                    $category_image_width  = $this->getSettingValue('product_cart_settings', 'category_image_width');
                    $category_image_height = $this->getSettingValue('product_cart_settings', 'category_image_height');
                    //$productImage =  $_POST['productCategoryParent'].'_'.str_replace(' ','_',$urlKey).substr($_POST['productCategoryImage'], strpos($_POST['productCategoryImage'], "."));
                    $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["productCategoryImage_temp"], '', $category_image_width, $category_image_height);
                    $ObjImage->copy_file($_POST["productCategoryImage_temp"], '/' . __UPLOADS_FOLDER . 'product-category/' . $_POST['productCategoryImage']);
                }
                if (!empty($_POST['productCategoryIcon_temp'])) {
                    $category_icon_width  = $this->getSettingValue('product_cart_settings', 'category_icon_width');
                    $category_icon_height = $this->getSettingValue('product_cart_settings', 'category_icon_height');
                    //$productImage =  $_POST['productCategoryParent'].'_'.str_replace(' ','_',$urlKey).substr($_POST['productCategoryIcon'], strpos($_POST['productCategoryIcon'], "."));
                    $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["productCategoryIcon_temp"], '', $category_icon_width, $category_icon_height);
                    $ObjImage->copy_file($_POST["productCategoryIcon_temp"], '/' . __UPLOADS_FOLDER . 'product-category/icon/' . $_POST['productCategoryIcon']);
                }
                $this->setMessage("language.msgProductCategoryAdd", 3);
                $this->setRedirect('productcategory');
            }
        }
        $gridLayout                  = $ObjProductCategory->listLayoutDetails('grid');
        $detailsLayout               = $ObjProductCategory->listLayoutDetails('details');
        $params['languageIDs']       = $languageIDs;
        $params['widgetDisplayedOn'] = 'category';
        $resWidgetList               = $ObjWidget->listWidget($params);
		$this->ObjView->assign("resWidgetList", $resWidgetList);
		$brandParams         = array(
            'languageIDs' => $languageIDs
        );       
        $listBrand      = $ObjBrand->listBrand($brandParams);  
        $this->ObjView->assign('listBrand', $listBrand);

        $this->ObjView->assign("resGridLayout", $gridLayout);
        $this->ObjView->assign("resDetailsLayout", $detailsLayout);
        $this->ObjView->assign("resListParentProductCategory", $resListProductCategory);
        $this->ObjView->assign('settingsDateFormat', $settingsDateFormat);
        $this->ObjView->assign("MAX_PRODUCT_CATEGORY_LEVEL", MAX_PRODUCT_CATEGORY_LEVEL);
        $this->ObjView->assign('sysKeys', SYSTEM_DIR_KEYS);
        $resGoogleCategoryTree = $ObjProductCategory->listGoogleCategories();
        $this->ObjView->assign('resGoogleCategoryTree', $resGoogleCategoryTree);
        $this->ObjView->assign('globalCommisionPercentage', $this->getSettingValue('multi_vendor_settings', 'commission_percentage'));
        $this->ObjView->assign('settingsProductThumbWidth', $settingsProductThumbWidth);
        $this->ObjView->assign('settingsProductThumbHeight', $settingsProductThumbHeight);
        $this->ObjView->display('ProductCategoryAdd.tpl');
    }
    //FUNCTION TO EDIT DEAL CATEGORY
    public function editAction() {
        $ObjImage               = new ImageModel();
        $ObjProductCategory        = new ProductCategoryModel();
        $ObjWidget              = new WidgetModel();
		$ObjBrand       = new BrandModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $settingsDateFormat     = $this->getSettingValue('site_settings', 'date_format');
        //URL Rewrite settings
        $redirectforurl         = $this->getSettingValue('seo_settings', 'redirect_for_url');
        $suffix                 = $this->getSettingValue('seo_settings', 'category_url_suffix');
        $product_sef_format     = $this->getSettingValue('seo_settings', 'product_sef_format');
        $product_sef_format		= isset($product_sef_format)  ? $product_sef_format   : false;
		//URL Rewrite Settings
        $settingsRedirectUrl = ($redirectforurl == "Yes") ? "Yes" : 1;
        $this->ObjView->assign('settingsRedirectforurl', $settingsRedirectUrl);
		
        $productCategoryID      = $this->getRequestValue("id");
        if ($productCategoryID) {
            $productParentCategory     = $ObjProductCategory->getParentBySubID($languageIDs, $productCategoryID);
            $resListParentProductCategory = $ObjProductCategory->listProductCategoryItems($languageIDs, 0, 0, $productParentCategory['productCategoryID'], '', false, $this->ObjView->_languageXML, 'Yes');
            $rsProductCategoryDetails     = $ObjProductCategory->getProductCategoryByID($languageIDs, $productCategoryID);
        }
        if (!$rsProductCategoryDetails) {
            $this->setredirect('/new/');
        }
		
        $resCategoryTree = $ObjProductCategory->listProductCategoryItems($languageIDs, 0, 0);
        $resCategoryTree = array_slice($resCategoryTree, 1);
        $this->ObjView->assign('resCategoryTree', $resCategoryTree);
        $this->ObjView->assign("productCategoryID", $productCategoryID);
        $this->ObjView->assign("productParentCategoryId", $productParentCategory['productCategoryID']);
		
		foreach ($resListParentProductCategory as $key => $productCategory) {
			if(($key>0) && ($productCategory['brandID'] != $rsProductCategoryDetails['brandID'])) {
				unset($resListParentProductCategory[$key]);
			}
		}
		
        $this->ObjView->assign("resListParentProductCategory", $resListParentProductCategory);
        $this->ObjView->assign("MAX_PRODUCT_CATEGORY_LEVEL", MAX_PRODUCT_CATEGORY_LEVEL);
        $resGoogleCategoryTree = $ObjProductCategory->listGoogleCategories();
        $this->ObjView->assign('resGoogleCategoryTree', $resGoogleCategoryTree);
        $this->ObjView->assignValues($rsProductCategoryDetails);
        if ($this->ObjForm->isValid('formProductCategorySubmit', 'submitProductCategory')) {
            $urlKey                    = ($_POST['productCategoryKey']) ? $_POST['productCategoryKey'] : $this->ObjCommon->generateKey("catalog", $_POST['productCategoryName'], $productCategoryID, "unique");
            $_POST['layoutActiveFrom'] = (isset($_POST['layoutActiveFrom']) && $_POST['layoutActiveFrom'] != '') ? $this->convertDateForSql($_POST['layoutActiveFrom'], $settingsDateFormat) : '';
            $_POST['layoutActiveTo']   = (isset($_POST['layoutActiveTo']) && $_POST['layoutActiveTo'] != '') ? $this->convertDateForSql($_POST['layoutActiveTo'], $settingsDateFormat) : '';
            if ($_POST['pageDefaultView'] == '') {
                $_POST['pageDefaultView'] = $_POST['hiddenPageDefaultView'];
            }
            if (!(isset($_POST['productGridListLayoutID'])) && $_POST['productGridListLayoutID'] == '') {
                $_POST['productGridListLayoutID'] = $_POST['hiddenProductGridListLayoutID'];
            }
            if (!(isset($_POST['productDetailsLayoutID'])) && $_POST['productDetailsLayoutID'] == '') {
                $_POST['productDetailsLayoutID'] = $_POST['hiddenProductDetailsLayoutID'];
            }
            if (!in_array($_POST['pageDefaultView'],array('category_with_products','widget_and_category_with_products')))  {
                $_POST['numberOfProductCount'] = 'NULL';
            }
            if($_POST['numberOfProductCount']=="")
                $_POST['numberOfProductCount'] = 'NULL';
            $rsProductCategory = $ObjProductCategory->submitProductCategory($languageIDs, $productCategoryID, $urlKey);
            if ($rsProductCategory == "ae") {
                $this->setMessage("language.msgDuplicateProductCategoryKey", "error");
            } else {
                $saveWidgetToCategory = $ObjProductCategory->saveWidgetToCategory($productCategoryID);
                if ( $_POST['productCategoryKey'] != $_POST['oldCatagoryKey']) {  //$productParentCategoryId != $_POST['productCategoryParent'] ||
                    //URL Rewite
                    $_POST['redirectOption'] = isset($_POST['redirectOption']) ? $_POST['redirectOption'] : '';
                    $ObjSef                  = new SefModel();
                    $ObjSef->_refreshCategoryRewrites($languageIDs['AllList'], $productCategoryID, $suffix, $_POST['redirectOption'],true,$_POST['productCategoryParent'],$product_sef_format);
                    //END
                }
                if (!empty($_POST['productCategoryImage_temp'])) {
                    /*unlink old image*/
                    $imagePath = __UPLOADS_FOLDER . 'product-category/' . trim($rsProductCategoryDetails['productCategoryImage']);
                    $ObjImage->unlinkImage($imagePath);
                    /*unlink old image*/
                    $category_image_width  = $this->getSettingValue('product_cart_settings', 'category_image_width');
                    $category_image_height = $this->getSettingValue('product_cart_settings', 'category_image_height');
                    $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["productCategoryImage_temp"], '', $category_image_width, $category_image_height);
                    $ObjImage->copy_file($_POST["productCategoryImage_temp"], '/' . __UPLOADS_FOLDER . 'product-category/' . $_POST['productCategoryImage']);
                    @unlink($_POST["productCategoryImage_temp"]);
                }
                if (!empty($_POST['productCategoryIcon_temp'])) {
                    /*unlink old image*/
                    $imagePath = __UPLOADS_FOLDER . 'product-category/' . trim($rsProductCategoryDetails['productCategoryIcon']);
                    $ObjImage->unlinkImage($imagePath);
                    /*unlink old image*/
                    $category_icon_width  = $this->getSettingValue('product_cart_settings', 'category_icon_width');
                    $category_icon_height = $this->getSettingValue('product_cart_settings', 'category_icon_height');
                    $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["productCategoryIcon_temp"], '', $category_icon_width, $category_icon_height);
                    $ObjImage->copy_file($_POST["productCategoryIcon_temp"], '/' . __UPLOADS_FOLDER . 'product-category/icon/' . $_POST['productCategoryIcon']);
                    @unlink($_POST["productCategoryIcon_temp"]);
                }
                $this->setMessage("language.msgProductCategoryUpdate", 3);
                $this->setRedirect('productcategory');
            }
            if (!in_array($_POST['pageDefaultView'],array('category_with_products','widget_and_category_with_products')))  {
                $_POST['numberOfProductCount'] = 'NULL';
            }
            $rsProductCategory = $ObjProductCategory->submitProductCategory($languageIDs, '', $urlKey);
            if ($rsProductCategory == "ae") {
                $this->setMessage("language.msgDuplicateProductCategoryKey", "error");
            } else {
                $saveWidgetToCategory = $ObjProductCategory->saveWidgetToCategory($rsProductCategory);
                //URL Rewite
                $ObjSef               = new SefModel();
                $ObjSef->_refreshCategoryRewrites($languageIDs['AllList'], $rsProductCategory, $suffix, '', false, $_POST['productCategoryParent'], "insert");
                //END
                if (!empty($_POST['productCategoryImage_temp'])) {
                    $category_image_width  = $this->getSettingValue('product_cart_settings', 'category_image_width');
                    $category_image_height = $this->getSettingValue('product_cart_settings', 'category_image_height');
                    //$productImage =  $_POST['productCategoryParent'].'_'.str_replace(' ','_',$urlKey).substr($_POST['productCategoryImage'], strpos($_POST['productCategoryImage'], "."));
                    $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["productCategoryImage_temp"], '', $category_image_width, $category_image_height);
                    $ObjImage->copy_file($_POST["productCategoryImage_temp"], '/' . __UPLOADS_FOLDER . 'product-category/' . $_POST['productCategoryImage']);
                }
                if (!empty($_POST['productCategoryIcon_temp'])) {
                    $category_icon_width  = $this->getSettingValue('product_cart_settings', 'category_icon_width');
                    $category_icon_height = $this->getSettingValue('product_cart_settings', 'category_icon_height');
                    //$productImage =  $_POST['productCategoryParent'].'_'.str_replace(' ','_',$urlKey).substr($_POST['productCategoryIcon'], strpos($_POST['productCategoryIcon'], "."));
                    $ObjImage->resizeImage(__ROOT_PATH . '/' . $_POST["productCategoryIcon_temp"], '', $category_icon_width, $category_icon_height);
                    $ObjImage->copy_file($_POST["productCategoryIcon_temp"], '/' . __UPLOADS_FOLDER . 'product-category/icon/' . $_POST['productCategoryIcon']);
                }
                $this->setMessage("language.msgProductCategoryAdd", 3);
                $this->setRedirect('productcategory');
            }
        }
        $gridLayout                  = $ObjProductCategory->listLayoutDetails('grid');
        $detailsLayout               = $ObjProductCategory->listLayoutDetails('details');
        $params['languageIDs']       = $languageIDs;
        $params['widgetDisplayedOn'] = 'category';
        $resWidgetList               = $ObjWidget->listWidget($params);
		$params=array(
				'languageIDs'=>$languageIDs, 				
				'productcategoryID'=>$productCategoryID
				);		
        $resGetCategoryWidgetIDs               = $ObjProductCategory->getCategoryToWidgetIDs($params);
        $this->ObjView->assign("resGetCategoryWidgetIDs", $resGetCategoryWidgetIDs);
        $this->ObjView->assign("resWidgetList", $resWidgetList);
		
		$brandParams         = array(
            'languageIDs' => $languageIDs
        );       
        $listBrand      = $ObjBrand->listBrand($brandParams);  
        $this->ObjView->assign('listBrand', $listBrand);
        $this->ObjView->assign('brandID', $rsProductCategoryDetails['brandID']);
		
        $this->ObjView->assign("resGridLayout", $gridLayout);
        $this->ObjView->assign("resDetailsLayout", $detailsLayout);
      //  $this->ObjView->assign("resListParentProductCategory", isset($resListProductCategory)?$resListProductCategory:'');
        $this->ObjView->assign('settingsDateFormat', $settingsDateFormat);
        $this->ObjView->assign("MAX_PRODUCT_CATEGORY_LEVEL", MAX_PRODUCT_CATEGORY_LEVEL);
        $this->ObjView->assign('sysKeys', SYSTEM_DIR_KEYS);
        $settingsProductThumbWidth  = $this->getSettingValue('product_cart_settings', 'thumb_width');
        $settingsProductThumbHeight = $this->getSettingValue('product_cart_settings', 'thumb_height');
        $this->ObjView->assign('settingsProductThumbWidth', $settingsProductThumbWidth);
        $this->ObjView->assign('settingsProductThumbHeight', $settingsProductThumbHeight);
		$this->ObjView->assign('globalCommisionPercentage', $this->getSettingValue('multi_vendor_settings', 'commission_percentage'));
        $this->ObjView->display('ProductCategoryAdd.tpl');
    }
    
    //TO DELETE CATEGORY
    public function destroyAction() {
        $ObjProductCategory = new ProductCategoryModel();
        $productCategoryID  = $this->getRequestValue("id");
        $rsProductCategory  = $ObjProductCategory->destroyProductCategory($productCategoryID);
        $ObjUrl             = new UrlrewriteModel();
        // DELETE FROM URL TABLE
        if ($rsProductCategory == "ok") {
            $childrenArray = $ObjProductCategory->frontendListAllSubCategories($productCategoryID);
            $ids           = implode(',', $childrenArray);
            $ids .= (($ids) ? ',' : '') . $productCategoryID;
            if ($ids) {
                $ObjUrl->deleteUrlRewrites($ids, 'product', 0);
                $ObjUrl->deleteUrlRewrites('', 'catalog', $productCategoryID);
            }
        }
        if ($rsProductCategory == "error") {
            $this->setMessage("language.msgProductCategoryCannotDelete", "error");
        } else {
            $this->setMessage("language.msgProductCategoryDelete", "success");
        }
        $this->setRedirect('productcategory');
    }
    //TO CHECK DEAL CATEGORY EXISTS OR NOT
    public function productCategoryExitsAction() {
        $ObjProductCategory     = new ProductCategoryModel();
        $languageIDs            = $this->ObjView->languageIDs;
        $languageIDs['Current'] = $languageIDs['Content'];
        $productCategoryName    = urldecode($_POST['productCategoryName']);
        $productCategoryParent  = $_POST['productCategoryParent'];
        $productCategoryID      = $_POST['productCategoryID'];
        $check                  = $ObjProductCategory->isProductCategoryNameExists($languageIDs, $productCategoryParent, $productCategoryName, $productCategoryID);
        return $check;
    }
    //TO CHECK DEAL CATEGORY KEY EXISTS OR NOT => Callback
    function productCatKeyExistsAction() {
        $ObjProductCategory = new ProductCategoryModel();
        $productcategoryKey = isset($_POST['productCategoryKey']) ? trim($_POST['productCategoryKey']) : '';
        $productCategoryID  = $this->getRequestValue("id");
        if ($ObjProductCategory->isProductCatKeyExists($productcategoryKey, $productCategoryID)) {
            return false;
        }
        return true;
    }
    public function ajaxgetwidgetAction() {
        $ObjWidget                   = new WidgetModel();
        $ObjProductCategory          = new ProductCategoryModel();
        $languageIDs                 = $this->ObjView->languageIDs;
        $languageIDs['current']      = $languageIDs['Content'];
        $params['languageIDs']       = $languageIDs;
        $params['widgetTitle']       = isset($_POST['widgetName']) ? $_POST['widgetName'] : '';
        $params['widgetDisplayedOn'] = 'category';
        $str                         = '';
        if (isset($_POST['selectedCategoryID']) && $_POST['selectedCategoryID'] != '') {
            $selectedWidgetIDs = explode('~*', $_POST['selectedCategoryID']);
        }
        $resWidgetList = $ObjWidget->listWidget($params);
        $str           = '';
        if (!empty($resWidgetList)) {
            for ($i = 0; $i < count($resWidgetList); $i++) {
                $displayText = (in_array($resWidgetList[$i]['widgetID'], (isset($selectedWidgetIDs)) ? $selectedWidgetIDs : array())) ? 'none' : 'block';
                $str .= '		<li id=' . $resWidgetList[$i]['widgetID'] . ' class="sort' . $resWidgetList[$i]['widgetID'] . '" ad_id="' . $resWidgetList[$i]['widgetID'] . '" style="display:' . $displayText . '">
								<a class="remove-btn product_drop_delete hide"></a>
								<div class="title" >' . $resWidgetList[$i]['widgetTitle'] . '</div>
							</li>
							';
            }
        } else
            $str .= '<li><div class="message_info">' . $this->getLanguageValue("msgNoRecords") . '</div></li>';
        echo $str;
    }
    public function ajaxgetcategorydetailsAction() {
        $ObjProductCategory                           = new ProductCategoryModel();
        $languageIDs                                  = $this->ObjView->languageIDs;
        $languageIDs['current']                       = $languageIDs['Content'];
        $productCategoryID                            = $_POST['productCategoryID'];
        $settingDateFormat                            = $this->getSettingValue('site_settings', 'date_format');
        $rsProductCategoryDetails                     = $ObjProductCategory->getProductCategoryByID($languageIDs, $productCategoryID);
        $rsProductCategoryDetails['layoutActiveFrom'] = (isset($rsProductCategoryDetails['layoutActiveFrom']) && ($rsProductCategoryDetails['layoutActiveFrom'] != '0000-00-00')) ? date($settingDateFormat, strtotime($rsProductCategoryDetails['layoutActiveFrom'])) : '';
        $rsProductCategoryDetails['layoutActiveTo']   = (isset($rsProductCategoryDetails['layoutActiveTo']) && ($rsProductCategoryDetails['layoutActiveTo'] != '0000-00-00')) ? date($settingDateFormat, strtotime($rsProductCategoryDetails['layoutActiveTo'])) : '';
        echo json_encode($rsProductCategoryDetails);
    }
    public function ajaxparentadmincommissionAction() {
        $ObjProductCategory                           = new ProductCategoryModel();
		$productCategoryID                            = $_POST['productCategoryParent'];
		echo  $productCommissionRate                     = $ObjProductCategory->getparentadmincommission($productCategoryID);
		exit;
    }	
	public function ajaxGetParentsAction() {
        $ObjProductCategory     = new ProductCategoryModel();
		$languageIDs            = $this->ObjView->languageIDs;
		$brandID       			= isset($_POST['brandID']) ? $_POST['brandID'] : 0;
		if($brandID >0 ) {
		$resListProductCategory = $ObjProductCategory->listProductCategoryItems($languageIDs, 0, 0, 0, '', false, $this->ObjView->_languageXML, 'Yes');
		$array = array();
		foreach ($resListProductCategory as $key => $productCategory) {
			if($productCategory['brandID'] != $brandID) {
				unset($resListProductCategory[$key]);
			}
		}
		$this->ObjView->assign("resListParentProductCategory", $resListProductCategory);
		$this->ObjView->display('AjaxProductCategoryParent.tpl');
		exit;
		} else {
			echo 0;exit;
		}
    }	
}
