<?php

namespace OfficeBrain\Bundle\EcatalogueBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use OfficeBrain\Bundle\EcatalogueBundle\Manager\Extended;
use OfficeBrain\Bundle\EcatalogueBundle\Entity\Ecatalogue;
use OfficeBrain\Bundle\EcatalogueBundle\Form\EcatalogueType;
use Symfony\Component\Form\FormError;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

/**
 *
 * @author 4488
 * @Route("admin/{_locale}/")
 */
class EcatalogueAdminController extends Controller
{
	/**
	 * @author Employee Id: 4488
	 * set Default Value
	 */
	
	protected $categoryId;
	protected $page;
	protected $ecatalogueRecordsPerPage;
	protected $option;
	protected $keywords;
	protected $ecatalogueCategoryId;
	protected $userManager;
	private $projectUserSetting;
	private $requestCopyStatus;
	//private $loginUserType;
	
	public function __construct()
	{		
		$this->entityManager=null;
		$this->ecatalogueManager=null;
		$this->coreManager=null;
		$this->request=null;
		$this->responseArray['status']=null;
		$this->projectSetting=null;
		$this->projectUserSetting = null;
		$this->ecatalogueAccessField=null;
		$this->ecatalogueConfigurationField=null;
		$this->ecatalogueImageParameter=null;
		$this->ecatalogueFileParameter=null;
		$this->per_page=null;
		$this->location=null;
		$this->userManager = null;
		$this->translator = null;
		$this->session= null;
		$this->activeStatus = null;
		$this->inactiveStatus = null;
		$this->countFlagForView = null;
		$this->countFlagForStatus = null;
		$this->searchParameterArray = array();
		$this->responseArray['total_active'] = 0;
		$this->responseArray['total_inactive'] = 0;
		$this->responseArray['total_record'] = 0;
		$this->keywords=null;
		$this->categoryId=null;
		$this->apiClient=null;
		$this->commonLibraryManager = null;
		$this->option=null;
		$this->keywords=null;
		$this->ecatalogueCategoryId=null;
		//$this->loginUserType = null;
	}
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description: To unset member variables
	 *
	 * @return NULL
	 *
	 */
	public function __destruct()
	{	
		unset($this->entityManager);
		unset($this->ecatalogueManager);
		unset($this->coreManager);
		unset($this->request);
		unset($this->responseArray['status']);
		unset($this->projectSetting);
		unset($this->projectUserSetting);
		unset($this->ecatalogueAccessField);
		unset($this->ecatalogueConfigurationField);
		unset($this->ecatalogueImageParameter);
		unset($this->ecatalogueFileParameter);
		unset($this->per_page);
		unset($this->location);
		unset($this->userManager);
		unset($this->translator);
		unset($this->session);
		unset($this->activeStatus);
		unset($this->inactiveStatus);
		unset($this->countFlagForView);
		unset($this->countFlagForStatus);
		unset($this->searchParameterArray);
		unset($this->responseArray['total_active']);
		unset($this->responseArray['total_inactive']);
		unset($this->responseArray['total_record']);
		unset($this->keywords);
		unset($this->categoryId);
		unset($this->apiClient);
		unset($this->commonLibraryManager);
		unset($this->option);
		unset($this->keywords);
		unset($this->ecatalogueCategoryId);
		//unset($this->loginUserType);
	
	}
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description: To initialize member variables and objects
	 *
	 * @return NULL
	 *
	 */
	
	public function initAction()
	{
		$this->request=$this->getRequest();
		$this->session= $this->request->getSession();
		$this->entityManager = $this->getDoctrine()->getEntityManager();
		$this->location=$this->get('kernel')->getRootDir().'/../web/images/uploads/';
		$this->translator = $this->get('translator');
		$this->projectSetting=$this->request->get('project_setting');
		$this->ecatalogueManager=$this->get("office_brain_ecatalogue_manager");
		$this->categoryService = $this->get('office_brain_search_manager');
		$this->commonLibraryManager = $this->get('office_brain_common_library');
		$this->ecatalogueImageParameter=$this->projectSetting['configuration_rule']['promo_discount']['option']['ecatalogue']['ecatalogue_image_upload_parameter'];
		$this->ecatalogueFileParameter=$this->projectSetting['configuration_rule']['promo_discount']['option']['ecatalogue']['ecatalogue_file_upload_parameter'];
		$this->ecatalogue_per_page=$this->projectSetting['configuration_rule']['promo_discount']['option']['ecatalogue']['no_of_record_per_page'];
		$this->ecatalogueAccessField= $this->projectSetting['access_rule']['promo_discount']['option']['ecatalogue'];
		$this->ecatalogueConfigurationField=$this->projectSetting['configuration_rule']['promo_discount']['option']['ecatalogue'];
		$this->searchFlag = $this->request->get('searchFlag',null);
		$this->page = $this->request->get('page', '1');
		$this->ecatalogueRecordsPerPage = $this->request->get('records_per_page', $this->ecatalogue_per_page);
		$this->responseArray['page']=$this->page;
		$this->responseArray['records_per_page'] = $this->ecatalogueRecordsPerPage;
		$this->ecataloguefileParameter=$this->projectSetting['configuration_rule']['promo_discount']['option']['ecatalogue']['ecatalogue_file_upload_parameter'];
		$this->responseArray['ecatalogueConfigrationField']=$this->ecatalogueConfigurationField;
		$this->responseArray['ecatalogueAccessField'] = $this->ecatalogueAccessField;
		$this->keywords = $this->request->get('keywords',null);
		$this->requestEcatalogueId = $this->request->get('ecatalogueId',null);
		$this->requestedStatus=$this->request->get('status',null);
		$this->requestEcataloguegroupId = $this->request->get('ecatalogue_group_id',null);
		$this->requestCopyCountry=$this->request->get('make_copy_country',null);
		$this->requestCopyStatus=$this->request->get('make_copy_status',null);
		$this->coreManager=$this->get("office_brain_core_manager");
		$this->userId = $this->projectSetting['user_id'];
		$this->requestUserId = $this->request->get('userId',null);
		$this->requestStatus = $this->request->get('status','');
		$this->activeStatus = '1';
		$this->inactiveStatus = '0';
		$this->countFlagForView = '0';
		$this->countFlagForStatus = '1';
		$this->option = $this->request->get('option',null);
		$this->responseArray['ecatalogueId'] = $this->requestEcatalogueId;
		$this->ecatalogueCategoryId=$this->request->get('categoryOption',null);
		$this->localeTab=$this->request->get("countryCode",null);
		$this->projectUserSetting = $this->request->get('project_user_setting');		
		$this->userManager = $this->get('office_brain_user_manager');
		$this->instanceId=$this->projectSetting['instance_id'];
		
	}
	
	/**
	 * @author Employee Id: 4488 -
	 * OB eCommerce Product -  Ecatalogue Listing
	 *
	 * @Route("ecatalog/list/{instance_id}",
	 * name="office_brain_ecataloguebundle_ecatalogue_list",	 
	 * requirements={"instance_id"="\d+"}, 
	 * defaults={"instance_id"="0"},	 
	 * options={"expose"=true}
	 * )
	 * @Template()
	 */
	public function listEcatalogueAction()
	{
		$this->initAction();
		
		$this->searchFlag = $this->request->get('searchFlag',null);
		$this->localeTab = $this->request->get('countryCode',null);
		$instanceId=$this->request->get('instance_id',null);
		$this->prepareSearchParameterArray();
		$supp = array();
		$supplierArr = $this->userManager->getAllSuppliers(); // call service from user bundle
			
		foreach ($supplierArr as $val){
			$supp[$val['id']]=  $val['companyName']; //$val['firstName']." ".$val['lastName'];
		}
		$this->getStatusCountAction();
		$this->responseArray['recordSet'] = $this->ecatalogueManager->getEcatalogueList($this->userId, $this->countFlagForView ,$this->searchParameterArray, $this->page, $this->ecatalogueRecordsPerPage, $this->requestStatus);
		$this->responseArray['categoryData']=$this->getAllCategory();
		$this->responseArray['instanceId']=$instanceId;
		$this->responseArray['supp']=$supp;
		
		return $this->responseArray;
	}
	/**
	 * @author Employee Id: 4488 -
	 * OB eCommerce Product -  Ecatalogue Search Listing
	 *
	 * @Route("ecatalog/search/list",
	 * name="office_brain_ecataloguebundle_ecatalogue_searchListAjax",
	 * options={"expose"=true}
	 * )
	 * @Template("OfficeBrainEcatalogueBundle:EcatalogueAdmin:tabEcatalogueListContents.html.twig")
	 */
	public function searchListAjaxAction()
	{
		
		$this->initAction();
		$this->searchFlag = $this->request->get('searchFlag',null);
		$this->localeTab = $this->request->get('countryCode',null);
		$this->responseArray['recordSet'] = $this->ecatalogueManager->getEcatalogueSearchListAjax($this->userId,$this->countFlagForView ,$this->searchParameterArray, $this->page, $this->ecatalogueRecordsPerPage,$this->option,$this->keywords,$this->localeTab,$this->ecatalogueCategoryId);
		
		$this->getStatusCountAction();
        return $this->responseArray;
	}
		
	/**
	 *
	 * @author Employee Id: 4488
	 * Description : List ecatalogue Ajax action
	 *
	 * @param  : null
	 *
	 * @return Array
	 *
	 * @throws null
	 *
	 * @Route("ecatalog/list-ajax/{culture}/{instance_id}", name="office_brain_ecataloguebundle_ecatalogue_listecatalogueajax",
	 * defaults={"culture"="en_us","instance_id"="0"},
	 * requirements={"instance_id"="\d+"}, 
	 * options={"expose"=true})
	 * @Template("OfficeBrainEcatalogueBundle:EcatalogueAdmin:tabEcatalogueListContents.html.twig")
	 *
	 **/
	public function listEcatalogueAjaxAction()
	{
		$this->initAction();
		$this->responseArray['recordSet'] = $this->ecatalogueManager->getEcatalogueListAjax($this->userId, $this->countFlagForView ,$this->searchParameterArray, $this->page, $this->ecatalogueRecordsPerPage, $this->requestStatus,$this->option,$this->keywords,$this->ecatalogueCategoryId);
		$this->getStatusCountAction();
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4488
	 * Description : Get the ecatalogue view details
	 *
	 * @param  : null
	 *
	 * @return Array
	 *
	 * @throws null
	 *
	 * @Route("ecatalogue/view-details/{ecatalogueId}", name="office_brain_ecataloguebundle_ecatalogueadmin_ecataloguedetailsajax")
	 * @Template()
	 *
	 **/
	public function ecatalogueDetailsAjaxAction()
	{
		$this->initAction();
		$this->responseArray['record'] = $this->ecatalogueManager->getEcatalogueListById($this->requestEcatalogueId);
		foreach ($this->responseArray['record'] as $key=>$value)
			$this->responseArray['recordSet']=$value;
		return $this->responseArray;
	}
	
	/**
	 *
	 * @author Employee Id: 4488
	 * Description : make Copy from list ecatalogue
	 *
	 * @Route("ecatalogue/make-copy/{ecatalogueId}", name="office_brain_ecataloguebundle_ecatalogueadmin_listmakecopy")
	 * @Template()
	 *
	 **/
	public function listMakeCopyAction()
	{
		$this->initAction();
		$this->responseArray['ecatalogId']=$this->requestEcatalogueId;
		$this->responseArray['instance_id']=$this->instanceId;
		return $this->responseArray;
	}
	
	
	
	/**
	 *
	 * @author Employee Id: 4488
	 * Description : Make a copy of ecatalogue
	 *
	 * @param 1 : type image
	 *
	 * @return thumbimage name
	 *
	 * @throws null
	 *
	 * @return $image
	 *
	 * @Route("ecatalog/copy-ecatlogue/{instance_id}", name="office_brain_ecataloguebundle_ecatalogueadmin_copyecatalogue")
	 * requirements={"instance_id"="\d+"}, 
	 * defaults={"instance_id"="0"},
	 * options={"expose"=true},
	 * @Method({"POST"})
	 *
	 **/
	public function copyEcatalogueAction()
	{
		$this->initAction();
		$param = $this->request->get('_route_params');
		$makecopy="list";
		$ecatalogueGroup = $this->ecatalogueManager->copyEcatalogueGroup($this->requestEcataloguegroupId,$this->requestCopyCountry,$this->requestCopyStatus,$makecopy);
		if ($this->request->isXmlHttpRequest()) {
			$this->responseArray['status'] = 'success';
			$this->responseArray['success_message'] = $this->translator->trans('admin.service_group_management.make_copy.copy_message');
			$this->responseArray['ecatalog_list_url'] = $this->generateUrl('office_brain_ecataloguebundle_ecatalogue_list',array('instance_id' =>  $param['instance_id']));
			return new JsonResponse($this->responseArray);
		}
		$this->session->getFlashBag()->add('success_message', $this->translator->trans('admin.ecatalogue.copy_message'));
		return $this->redirect($this->generateUrl('office_brain_ecataloguebundle_ecatalogue_list', array()));
	}
	/**
	 * @author Employee Id: 4488
	 * OB eCommerce Product - Add Ecatalogue Form View
	 * @Route("ecatalog/add/{instance_id}",name="office_brain_ecataloguebundle_ecatalogue_add",
	 * requirements={"instance_id"="\d+"}, 
	 * defaults={"instance_id"="0"},
	 * options={"expose"=true},
	 * )
	 * @Method({"GET"})
	 * @Template()
	 */
	public function addEcatalogueAction()
	{
		$this->initAction();
		
		$countryData=$this->ecatalogueManager->getCountry();
		$ecatalogue=new Ecatalogue();
		$ecatalogue=$this->ecatalogueManager->getLanguage($ecatalogue);
		
		$supp = array();
		$supplierArr = $this->userManager->getAllSuppliers(); // call service from user bundle
			
		foreach ($supplierArr as $val){			
		   $supp[$val['id']]=  $val['companyName']; //$val['firstName']." ".$val['lastName'];			
		} 		 
		
		if(sizeof($supp) < 1){
			$this->get('session')->getFlashBag()->add('error_message', $this->translator->trans('admin.no_supplier_err'));
		}		 
		$instanceId=$this->request->get('instance_id',null);
		$this->form = $this->createForm(new EcatalogueType($this->container, $countryData, $supp, '', array(), $this->projectSetting, $this->userManager, $this->projectUserSetting), $ecatalogue);
		$this->form->handleRequest($this->request);
		$this->responseArray['categoryData']=$this->getAllCategory();
		$this->responseArray['form_ecatalogue'] = $this->form->createView();
		
		return $this->responseArray;
	}
	/**
	 * @author (Created By : 4488)
	 * OB eCommerce Product -  Ecatalogue Save
	 *
	 * @Route(
	 *    "ecatalog/add/{instance_id}",
	 * 	  name="office_brain_ecataloguebundle_ecatalogue_addpost",
	 *    options={"expose"=true},
	 *    defaults={ "instance_id"="0"}
	 * )
	 * @Method({"POST"})
	 * @Template("OfficeBrainEcatalogueBundle:EcatalogueAdmin:addEcatalogue.html.twig")
	 */
	public function addEcataloguePostAction()
	{		
		$this->initAction();
		$param = $this->request->get('_route_params');
		$countryData=$this->ecatalogueManager->getCountry();
		$ecatalogue=new Ecatalogue();
		$ecatalogue=$this->ecatalogueManager->getLanguage($ecatalogue);
		$supp = array();
		$supplierArr = $this->userManager->getAllSuppliers(); // call service from user bundle
			
		foreach ($supplierArr as $val){
			$supp[$val['id']]=  $val['companyName']; //$val['firstName']." ".$val['lastName'];
		}
		
		$this->responseArray['categoryData']=$this->getAllCategory();
		$this->form = $this->createForm(new EcatalogueType($this->container,$countryData,$supp,'',array(), $this->projectSetting, $this->userManager, $this->projectUserSetting), $ecatalogue);
		$this->form->handleRequest($this->request);
		$this->form=$this->ecatalogueManager->filterFormData($this->form);

		if($this->form->isValid())
		{	 
				$ecatalogueData=$this->form->getData();
				$this->coreManager->setLocationPath($this->location);
				$this->responseArray['add']=$this->ecatalogueManager->addEcatalogue($ecatalogueData);
				$messageKey='';
				$messageValue='';
				switch ($this->responseArray['add']) {
					case 'SUCCESS':
						$this->responseArray['status'] = $this->get('translator')->trans('Success');
						$this->responseArray['page_url']= $this->generateUrl("office_brain_ecataloguebundle_ecatalogue_list");
						$messageKey = "success_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.add.success');
						if (!$this->request->isXmlHttpRequest()) {
							
							$this->get('session')->getFlashBag()->add($messageKey, $messageValue);
							return $this->redirect($this->generateUrl("office_brain_ecataloguebundle_ecatalogue_list"));
						}
						break;
					case 'FAIL':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.fail');
						break;
					case 'TYPE':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.type');
						break;
					case 'SIZE':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.size');
						break;
					case 'HEIGHT':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.height');
						break;
					case 'WIDTH':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.width');
						break;
					case 'INVALID_DATE':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.date_validate');
						break;
				}
				if ($this->request->isXmlHttpRequest()) {
					$this->responseArray['status']= $messageKey;
					$this->responseArray['page_url']= $this->generateUrl("office_brain_ecataloguebundle_ecatalogue_list");
					$this->responseArray[$messageKey] = $messageValue;
					if($messageKey=="error_message")
						$this->responseArray['ecatalog_list_url'] = $this->generateUrl('office_brain_ecataloguebundle_ecatalogue_addpost',array('instance_id' =>  $param['instance_id']));
					else
							$this->responseArray['ecatalog_list_url'] = $this->generateUrl('office_brain_ecataloguebundle_ecatalogue_list',array('instance_id' =>  $param['instance_id']));
					return new JsonResponse($this->responseArray);
				}
				else
					$this->get('session')->getFlashBag()->add($messageKey, $messageValue);
		} else {
			if ($this->request->isXmlHttpRequest())
			{
				$this->responseArray = array(
						'form_name' => $this->form->getName(),
						'form_status' => 'error_message',
						'form_errors' => $this->getErrorsAsArray($this->form)						
				);
// 				$this->responseArray['error_message'] = $this->translator->trans('admin.ecatalogue.add.fail');
				return new JsonResponse($this->responseArray);	
			}
 		} 
		
		$this->responseArray['form_ecatalogue']= $this->form->createView();
		return $this->responseArray;
	}
	
	public function getErrorsAsArray($form)
	{
		$errors = array();
		foreach ($form->getErrors() as $error) {
			$errors[] = $error->getMessage();
		}
	
		foreach ($form->all() as $key => $child) {
			if ($err = $this->getErrorsAsArray($child)) {
				$errors[$key] = $err;
			}
		}
	
		return $errors;
	}
	
	/**
	 * @author (Created By : 4488
	 * OB eCommerce Product - Ecatalogue Edit Form View
	 * @Route("ecatalog/edit/{ecatalogueId}/{instance_id}",
	 *  name="office_brain_ecataloguebundle_ecatalogue_editecatalogue",
	 *  options={"expose"=true},
	 *  defaults={ "instance_id"="0"}
	 * )
	 * @Method({"GET"})
	 * @Template()
	 */
	public function editEcatalogueAction()
	{
		$this->initAction();
		$countryData=$this->ecatalogueManager->getCountry();
		$param = $this->request->get('_route_params');
		$ecatalogue=$this->get('doctrine')->getManager()->getRepository('OfficeBrainEcatalogueBundle:Ecatalogue')->find($this->requestEcatalogueId);
		$ecatalogueObj=$this->ecatalogueManager->getEcatalogueEditListById($this->requestEcatalogueId);

		$imagename=$ecatalogueObj[0]['ecatalogueLanguage'];
		$filename=$this->getFile($ecatalogueObj);
		$supp = array();
		$supplierArr = $this->userManager->getAllSuppliers(); // call service from user bundle
			
		foreach ($supplierArr as $val){
			$supp[$val['id']]= $val['companyName']; // $val['firstName']." ".$val['lastName'];
		}
		$supp_id=$ecatalogue->getSupplier()->getId();
		$category='';
		if($ecatalogue->getCategory())
			$category=explode(',',$ecatalogue->getCategory());
		
		$this->form = $this->createForm(new EcatalogueType($this->container, $countryData, $supp, $supp_id, $category, $this->projectSetting, $this->userManager, $this->projectUserSetting), $ecatalogue);
		$this->responseArray['form_ecatalogue'] = $this->form->createView();
		$this->responseArray['imagename']=$imagename;
		$this->responseArray['categoryData']=$category;
		$this->responseArray['filename']=$filename;
		return $this->responseArray;
	}
	
	
	/**
	 * @author (Modifyed By : 4488)
	 * OB eCommerce Product -  Edit Data Save Ectalogue 
	 *
	 * @Route("ecatalog/edit/{ecatalogueId}/{instance_id}",
	 * name="office_brain_ecataloguebundle_ecatalogue_editecataloguepost",
	 * options={"expose"=true},
	 * defaults={ "instance_id"="0"}
	 * )
	 * @Method({"POST"})
	 * @Template("OfficeBrainEcatalogueBundle:EcatalogueAdmin:editEcatalogue.html.twig")
	 */
	
	public function editEcataloguePostAction()
	{
		
		$this->initAction();
		$param = $this->request->get('_route_params');
		$ecatalogueFind=$this->get('doctrine')->getManager()->getRepository('OfficeBrainEcatalogueBundle:Ecatalogue')->find($this->requestEcatalogueId);
		$ecatalogueObj=$this->ecatalogueManager->getEcatalogueEditListById($this->requestEcatalogueId);
		$ecatalogueLanguageData=$ecatalogueObj[0]['ecatalogueLanguage'];

		$filename=$this->getFile($ecatalogueObj);
		$countryData=$this->ecatalogueManager->getCountry();
		//$supp = $this->ecatalogueManager->getAllSupplier();
		$supp = array();
		$supplierArr = $this->userManager->getAllSuppliers(); // call service from user bundle
			
		foreach ($supplierArr as $val){
			$supp[$val['id']]= $val['companyName']; // $val['firstName']." ".$val['lastName'];
		}
		
		$supp_id=$ecatalogueFind->getSupplier()->getId();
		$category='';
		if($ecatalogueFind->getCategory())
			$category=explode(',',$ecatalogueFind->getCategory());
		$this->form = $this->createForm(new EcatalogueType($this->container, $countryData, $supp, $supp_id, $category, $this->projectSetting, $this->userManager, $this->projectUserSetting), $ecatalogueFind);
		$this->form->handleRequest($this->request);	
		
		$this->form=$this->ecatalogueManager->filterFormData($this->form,'edit');
		 
		if ($this->form->isValid()) {
			$ecatalogueData = $this->form->getData();
			$this->coreManager->setLocationPath($this->location);
			$this->responseArray['edit']=$this->ecatalogueManager->editEcatalogue($ecatalogueData,$ecatalogueLanguageData);
			$messageKey='';
			$messageValue='';
				switch ($this->responseArray['edit']) {					
					case 'SUCCESS':
						$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.ecatalogue.edit.success'));
						if ($this->request->isXmlHttpRequest()) {
							$messageKey = "success_message";
							$this->responseArray['status']='success_message';
							$this->responseArray['page_url']= $this->generateUrl('office_brain_ecataloguebundle_ecatalogue_list',array('instance_id' =>  $param['instance_id']));
							$this->responseArray['success_message'] = $this->translator->trans('admin.ecatalogue.edit.success');
							$this->responseArray['ecatalog_list_url'] = $this->generateUrl('office_brain_ecataloguebundle_ecatalogue_list',array('instance_id' =>  $param['instance_id']));
							return new JsonResponse($this->responseArray);
						}
						return $this->redirect($this->generateUrl('office_brain_ecataloguebundle_ecatalogue_list'));
						break;
					case 'FAIL':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.fail');
						$this->responseArray['edit']=array('status'=>'FAIL');
						break;
					case 'TYPE':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.type');
						$this->responseArray['edit']=array('status'=>'FAIL');
						break;
					case 'SIZE':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.size',array('%size%'=>$this->ecatalogueConfigurationField['ecatalogue_image_upload_parameter']['size']));
						$this->responseArray['edit']=array('status'=>'FAIL');
						break;
					case 'HEIGHT':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.height',array('%width%'=>$this->ecatalogueConfigurationField['ecatalogue_image_upload_parameter']['width'],'%height%'=>$this->ecatalogueConfigurationField['ecatalogue_image_upload_parameter']['height']));
						$this->responseArray['edit']=array('status'=>'FAIL');
						break;
					case 'WIDTH':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.upload.height',array('%width%'=>$this->ecatalogueConfigurationField['ecatalogue_image_upload_parameter']['width'],'%height%'=>$this->ecatalogueConfigurationField['ecatalogue_image_upload_parameter']['height']));
						$this->responseArray['edit']=array('status'=>'FAIL');
						break;
					case 'INVALID_DATE':
						$messageKey = "error_message";
						$messageValue= $this->translator->trans('admin.ecatalogue.date_validate');
						break;
				}
				if ($this->request->isXmlHttpRequest()) {
					$this->responseArray['status']=$messageKey;
					$this->responseArray['page_url']= $this->generateUrl("office_brain_ecataloguebundle_ecatalogue_list");
					$this->responseArray[$messageKey] = $messageValue;
					$this->responseArray['ecatalog_list_url'] = $this->generateUrl('office_brain_ecataloguebundle_ecatalogue_editecataloguepost',array('ecatalogueId'=>  $param['ecatalogueId'],'instance_id' =>  $param['instance_id']));
					return new JsonResponse($this->responseArray);
				}else
				{
					$this->get('session')->getFlashBag()->add($messageKey, $messageValue);
				}		
				/* if($this->responseArray['edit']['data']->getId() && ($this->requestCopyCountry !='' || $this->requestCopyStatus !='')){
					$this->ecatalogueManager->copyEcatalogueGroup($this->responseArray['add']['data']->getId(),$this->requestCopyCountry,$this->requestCopyStatus);
				} */				
		}
		else {
			if ($this->request->isXmlHttpRequest())
			{
				$this->responseArray = array(
						'form_name' => $this->form->getName(),
						'form_status' => 'error_message',
						'form_errors' => $this->getErrorsAsArray($this->form)						
				);
// 				$this->responseArray['error_message'] = $this->translator->trans('admin.ecatalogue.add.fail');
				return new JsonResponse($this->responseArray);	
			}
		}
		$ecatalogueObj=$this->ecatalogueManager->getEcatalogueEditListById($this->requestEcatalogueId);
		$imagename=$ecatalogueObj[0]['ecatalogueLanguage'];
		$this->responseArray['imagename']=$imagename;
		$this->responseArray['filename']=$filename;
		$this->responseArray['form_ecatalogue']= $this->form->createView();
		return $this->responseArray;
	}
	
	/**
	 * @author Employee Id: 4488
	 * OB eCommerce Product -  Ecatalogue Delete
	 *
	 * @Route("ecatalog/delete",
	 * 	name="office_brain_ecataloguebundle_ecatalogue_deleteecatalogue",
	 *  options={"expose"=true}
	 * )
	 * @Template()
	 */
	public function deleteAction()
	{
		$this->initAction();
        $recordDelete=$this->ecatalogueManager->deleteEcatalogue($this->requestEcatalogueId);
        $this->getStatusCountAction();
        switch ($recordDelete['status']) {
        	case  'SUCCESS' :
        		$this->responseArray['status'] = 'success';
        		$this->responseArray['success_message'] = $this->get('translator')->trans('admin.ecatalogue.delete.success');
        		break;
        	case  'FAIL':
        		$this->responseArray['error_message'] = $this->get('translator')->trans('admin.ecatalogue.delete.fail');
        		break;
        }
        return new JsonResponse($this->responseArray);
	}
	

	/**
	 * @author Employee Id: 4488
	 * OB eCommerce Product - preapre search parameter
	 */
	private function prepareSearchParameterArray()
	{
		$this->searchParameterArray[$this->option]=trim($this->keywords);
	}
	
	/** @author  4488
	 * OB eCommerce Product - For get count of active or inactive status
	 */
	public function getStatusCountAction()
	{
		$this->initAction();
		$this->responseArray['count_record_set'] = $this->ecatalogueManager->getEcatalogueList($this->userId, $this->countFlagForStatus ,$this->searchParameterArray, $this->page, $this->ecatalogueRecordsPerPage, $this->requestStatus, $this->option,$this->keywords,$this->ecatalogueCategoryId);
		$active=0;
		$inactive=0;
		if($this->responseArray['count_record_set']!='')
		{	
			foreach ($this->responseArray['count_record_set'] as $key=>$val)
			{
				 if($val['status']=='1'){
					$active=$active+1;
					$this->responseArray['total_active'] = $active;
				}else{
					$inactive=$inactive +1;
					$this->responseArray['total_inactive'] = $inactive; 
				}
				$this->responseArray['total_record']= $this->responseArray['total_active'] + $this->responseArray['total_inactive']; 
			}
		}
	}
	/**
	 * @author Employee Id: 4488
	 * OB eCommerce Product -  Ecatalogue status Update
	 *
	 * @Route("ecatalog/update-status/{ecatalogueId}",
	 * 	name="office_brain_ecataloguebundle_ecatalogue_updatestatus",
	 *  options={"expose"=true}
	 * )
	 * @Template()
	 */
	
	public function updateStatusEcatalogueAction()
	{
		$this->initAction();
		$ecatalogueObj=$this->ecatalogueManager->updateStatus($this->requestEcatalogueId);
		$this->getStatusCountAction();
		switch ($ecatalogueObj['status']) {
			case  'SUCCESS' :
				$this->responseArray['status'] = 'success';
				$this->responseArray['status_message'] = $this->get('translator')->trans('admin.ecatalogue.status.success');
				
				break;
			case  'FAIL':
				$this->responseArray['error_message'] = $this->get('translator')->trans('admin.ecatalogue.status.fail');
				break;
		}
		return new JsonResponse($this->responseArray);
	}
	public function getImage($ecatalogueObj)
	{
		foreach ($ecatalogueObj as $key=>$value)
		{
			foreach ($ecatalogueObj[$key]['ecatalogueLanguage'] as $keylang=>$valuelang)
			{
				if($ecatalogueObj[$key]['ecatalogueLanguage'][$keylang]['catalogueImage']!='')
				{
					$imagename=$ecatalogueObj[$key]['ecatalogueLanguage'][$keylang]['catalogueImage'];
					return $imagename;
				}
			}
				
		}
		
	}
	public function getFile($ecatalogueObj)
	{
		foreach ($ecatalogueObj as $key=>$value)
		{
			foreach ($ecatalogueObj[$key]['ecatalogueLanguage'] as $keylang=>$valuelang)
			{
				if($ecatalogueObj[$key]['ecatalogueLanguage'][$keylang]['catalogueFile']!='')
				{
					$filename=$ecatalogueObj[$key]['ecatalogueLanguage'][$keylang]['catalogueFile'];
					return $filename;
				}
			}
	
		}
		return null;
	
	}
	public  function getAllCategory()
	{
		$this->finalArr = array();
		$categoryListData=$this->categoryService->getCategorySubCategoryList();
		if(count($categoryListData))
		{
			$this->getAllCategoryInOneLevel($categoryListData);
		}
		return $this->finalArr;
	}
	/*
	 * Product
	*4488
	* */
	/* private function getAllCategoryInOneLevel($categoryListArray)
	{
		foreach ($categoryListArray as $key => $val) {
			if (isset($val['child'])) {
				$this->finalArr[$val['pms_id']] = $val['cat_name'];
				$this->getAllCategoryInOneLevel($val['child']);
			} else {
				$this->finalArr[$val['pms_id']] = $val['cat_name'];
			}
		}
		return true;
	}
	 */
	
	/*
	 * For Gosh skip 1 level Category (Display only child Category)
	 *4488 Move After Repo this is function in Custom Module 
	 * 19feb2016
	 *Star  
	 * */
	
	private function getAllCategoryInOneLevel($categoryListArray)
	{
		
		foreach ($categoryListArray as $key => $val) {
			if (isset($val['child'])) {
				foreach ($val['child'] as $key1 => $val1) {
					$this->finalArr[$val1['pms_id']] = $val1['cat_name'];
				}
	
// 				$this->getAllCategoryInOneLevel($val['child']); // (comment due to Only Second Level Subcategory display)
			} else {
				//$this->finalArr[$val['pms_id']] = $val['cat_name'];
			}
		}
	
		return true;
	}
	/*
	 * End Custom
	 * */
	
	
	/**
	 * @author Employee Id: 4571
	 * 
	 * Description: Prioritize Catalogue Action
	 *
	 * @param null
	 *
	 * @return data array
	 * 
	 * @Route("ecatalog/prioritize-ecatalog/{instance_id}", name="office_brain_ecataloguebundle_ecatalogueadmin_prioritizecatalogue",
	 * defaults={"instance_id"="0"},
	 * requirements={"instance_id"="\d+"},
	 * options={"expose"=true}) 
	 * 
	 * @Template()
	 * 
	 */
	public function prioritizeCatalogueAction()
	{
		$this->initAction();
		
		/* if($this->projectUserSetting['user_data']->getUserType() != "distributor")
		{	//$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.ecatalogue.success_msg'));
		 	return $this->redirect($this->generateUrl('office_brain_page_admin_dashboard'));
		} */
		
		//$supp = $this->ecatalogueManager->getAllSupplier();
		$supp = array();
		$supplierArr = $this->userManager->getAllSuppliers(); // call service from user bundle
			
		foreach ($supplierArr as $val){
			$supp[$val['id']]=  $val['companyName']; //$val['firstName']." ".$val['lastName'];
		}
	
		$this->responseArray['recordSet'] = $this->ecatalogueManager->prioritizeEcatalogueList();
		
		$this->responseArray['supp']=$supp;
	
		return $this->responseArray;
	}
	
	/**
	 * @author Employee Id: 4571
	 * 
	 * Description: Save Priority Catalogue Action
	 *
	 * @param null
	 *
	 * @return data array
	 * 
	 * @Route("ecatalog/save-prioritize-ecatalog", name="office_brain_ecataloguebundle_ecatalogueadmin_saveprioritizecatalogue")
	 * 
	 * @Template()
	 * 
	 */
	public function savePrioritizeCatalogueAction()
	{
		$this->initAction();
		
		$allPostCatalogues = $this->request->request->all(); 
		
	    $this->ecatalogueManager->setPriorityForEcatalog($allPostCatalogues);
	     			
		$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.ecatalogue.success_msg'));
		
		return $this->redirect($this->generateUrl('office_brain_ecataloguebundle_ecatalogueadmin_prioritizecatalogue'));		 
	}
	
	/**
	 *
	 * @author Employee Id: 4488
	 * 
	 * Description : List ecatalogue Ajax action
	 *
	 * @param  : null
	 *
	 * @return Array
	 *
	 * @throws null
	 *
	 * @Route("ecatalog/custom-list-ajax/{culture}/{instance_id}", name="office_brain_ecataloguebundle_ecatalogueadmin_customlistecatalogueajax",
	 * defaults={"culture"="en_us","instance_id"="0"},
	 * requirements={"instance_id"="\d+"},
	 * options={"expose"=true})
	 * @Template("OfficeBrainEcatalogueBundle:EcatalogueAdmin:tabContents.html.twig")
	 *
	 **/
	public function customListEcatalogueAjaxAction()
	{
		$this->initAction();
		
		$this->responseArray['recordSet'] = $this->ecatalogueManager->getCustomEcatalogueListAjax($this->userId);
		//$this->getStatusCountAction();
		
		return $this->responseArray;
	}
	
	/**
	 * @author Employee Id: 4488
	 *
	 * Description: To reset the priority
	 *
	 * @Route("ecatalog/reset-prioritize-ecatalog/{instance_id}", name="office_brain_ecataloguebundle_ecatalogueadmin_reset",
	 * defaults={"instance_id"="0"},
	 * requirements={"instance_id"="\d+"},
	 * options={"expose"=true})
	 *
	 * @Template()
	 */
	public function resetAction()
	{
		$this->initAction();
	
		if($this->ecatalogueManager->removePrioritiy())
			$this->get('session')->getFlashBag()->add('success_message', $this->translator->trans('admin.ecatalogue.prior_reset'));
		return $this->redirect($this->generateUrl('office_brain_ecataloguebundle_ecatalogueadmin_prioritizecatalogue'));
	}
	/**
	 * @author Employee Id: 4488 -
	 * OB eCommerce Product -  Ecatalogue Status Details
	 *
	 * @Route("ecatalog/status/list",
	 * name="office_brain_ecataloguebundle_ecatalogue_status_count_list",
	 * options={"expose"=true}
	 * )
	 * @Template("OfficeBrainEcatalogueBundle:EcatalogueAdmin:tabEcatalogueListContents.html.twig")
	 */
	public function statusCountListAction()
	{
		$this->initAction();
		$this->searchFlag = $this->request->get('searchFlag',null);
		$this->localeTab = $this->request->get('countryCode',null);
		$this->responseArray['recordSet'] = $this->ecatalogueManager->getStatusCountList($this->userId,$this->countFlagForView ,$this->searchParameterArray, $this->page, $this->ecatalogueRecordsPerPage,$this->option,$this->keywords,$this->localeTab,$this->ecatalogueCategoryId,$this->requestStatus);
		$this->getStatusCountAction();
		return $this->responseArray;
	}
	
}
	

