<?php
namespace OfficeBrain\CustomBundle\FlyerBundle\Manager\Extended;
use OfficeBrain\Bundle\FlyerBundle\Manager\Extended\FlyerExtended as BaseFlyerExtended;
class FlyerExtended extends BaseFlyerExtended
{
    public function __construct($entityManager, $request, $translator, $knpPaginator, $country, $language)
    {
        parent::__construct($entityManager, $request, $translator, $knpPaginator, $country, $language);
        $entityManager->getRepository('OfficeBrainCustomBundleFlyerBundle:FlyerCategory')->setProjectSetting($this->projectSetting);
        $this->flyerRepository  = $this->entityManager->getRepository('OfficeBrainCustomBundleFlyerBundle:FlyerCategory');
        $this->knpPaginator = $knpPaginator;
        $this->flyerRepository->setPaginatorService($this->knpPaginator);
    }
    public function __destruct()
    {
        parent::__destruct();
    }
    
    /**
     *
     * @author Employee Id: 5429
     *
     * Description : flyer category list 
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function getFlyerCategoryList($page='',$recordPerPage='')
    {
    	$flyerArray = array();
    	$flyerResults = array();
    	$adminUser = false;
    	$searchWord=trim($this->request->get('flyer_search_query'));
    	$this->getCultureValueAjax();
    	if ($this->countryCode)
    		$this->countryId = $this->getCountryIdByCode($this->countryCode);
    		$isAdmin = $this->flyerRepository->findAdminUser();
    	if($isAdmin!="" && sizeof($isAdmin)>0)
    		$adminUser = true;
    		$flyerArray = $this->flyerRepository->findByActiveFlyerCategoryList($this->countryId,$searchWord, $adminUser,$page,$recordPerPage);
    		$this->responseArray['countryCode'] = $this->countryCode;
    		$this->responseArray['recordSet']= $flyerArray;
    		
    	return $this->responseArray;
    }
    public function findById($id)
    {
    	return $this->flyerRepository->find($id);
    }
    public function findallcategories($CountryId,$id)
    {
    	return $this->flyerRepository->allcategories($CountryId,$id);
    }
    
    /**
     *
     * @author Employee Id: 5429
     *
     * Description : flyer category Active/Inactive
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function publishFlyerCategory($flyer)
    {
    	$status = $this->request->get('value');
    	$var_jason = array();
    
    	if($status == 0){
    		$flyer->setStatus(0);
    		$var_jason['msg'] = "deactivated";
    	}else if($status == 1){
    		$flyer->setStatus(1);
    		$var_jason['msg'] = "activated";
    	}
    
    	$flyer->setStatus($status);
    
    	$this->entityManager->persist($flyer);
    	$this->entityManager->flush();
    	return $var_jason;
    }
    /**
     *
     * @author Employee Id: 5429
     *
     * Description : save flyer html
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function saveAddFlyerCategory($flyer, $form,$mod)
    {
    	//echo"<pre>";print_r($form->getData());die();
    	$i=1;
    	$slug=$form->getData()->getTitle();
    	$slug=preg_replace('/[^A-Za-z0-9-]+/', '-',$form->getData()->getTitle());
    	$baseSlug=$slug;
    	while($this->flyerRepository->slug_exist($slug))
    	{
    		$slug = $baseSlug . "-" . $i++;
    	}
    	$flyer->setTitle($form->getData()->getTitle());
    	$flyer->setSlug(strtolower($slug));
    	if ($this->countryCode)
    	{
    		$this->countryId = $this->getCountryIdByCode($this->countryCode);
    	}
    	$flyer->setCountryId($this->countryId);
    	if($mod=='add')
    	{
    		if($form->getData()->getParentId()==null)
    		{
    			$flyer->setParentId('0');
    		}
    		else
    		{
    			$flyer->setParentId(null);
    		}
    		$flyer->setStatus('1');
    		$flyer->setCreatedAt(new \DateTime(date('Y-m-d')));
    		$flyer->setCreatedUid($this->projectSetting['user_id']);
    	}
    	else
    	{
    		if($form->getData()->getParentId()==null)
    			$setparntid=0;
    		else
    			$setparntid=$form->getData()->getParentId();
    		$flyer->setParentId($setparntid);
    		$flyer->setUpdatedAt(new \DateTime(date('Y-m-d')));
    		$flyer->setUpdatedUid($this->projectSetting['user_id']);
    	}
    	$flyer->setInstanceId($this->projectSetting['instance_id']);
    	$flyer->setInstanceType($this->projectSetting['instance_type']);
    	$this->entityManager->persist($flyer);
    	$this->entityManager->flush();
    }
    public function deleteFlyerCategoryRecords()
    {
    	if($this->request->get('action')=="delete")
    	{
    		$flyercategory = $this->flyerRepository->deleteFlyerCategoryrecord($this->request->get('id'));
    		if($flyercategory>0)
    		{
    			$this->flyerRepository->updateparentrecord($this->request->get('id'));
    			return $returnMessage =  'SUCCESS';
    			
    		}
    		else
    		{
    			return $returnMessage = 'FAIL';
    		}
    		
    	}
    }
	public function getFlyerCategoryMenuList()
	{
		if ($this->countryCode)
		$this->countryId = $this->getCountryIdByCode($this->countryCode);
		
		$flyerMenuArray = $this->flyerRepository->FlyerCategorymenuList($this->countryId);
		$this->responseArray1['recordSet']= $flyerMenuArray;
// 		echo "<pre>";print_r($flyerMenuArray);echo "</pre>";;
		return $this->responseArray1;
	}
}
