<?php
namespace OfficeBrain\Bundle\BlogBundle\Manager\AbstractManager;

use Symfony\Component\HttpFoundation\Request;

/**
 *
 * @author OfficeBrain 4571 <info@officebrain.com>
 *
 * Description : Abstract Class
 *
 */
abstract class BlogAbstract
{
    protected $entityManager;
    protected $request;
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $cultureId;
    protected $cultureCode;
    protected $userId;
    protected $createdUid;
    protected $upatedUid;
    protected $translator;
    protected $knpPaginator;
    protected $languages;
    protected $countries;
    protected $flyerRepository;
    // protected $bannerMasterRepository;
    protected $culture;
    protected $cultureAjax;
    protected $languageCode;
    protected $countryCode;
    protected $responseArray;
    protected $session;
    protected $countryId;
    protected $languageId;
    
        
    /**
     *
     * @author Employee Id: 4571
     * Description : Initialize the required variable on class load
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __construct($entityManager, $request, $translator, $knpPaginator, $country, $language)
    {
        $this->entityManager = $entityManager;
        $this->request = $request;
        $this->projectSetting = $this->request->get('project_setting');
        $this->instanceId = $this->projectSetting['instance_id'];
        $this->instanceType = $this->projectSetting['instance_type'];
        $this->userId = $this->projectSetting['user_id'];
        $this->cultureId = $this->projectSetting['culture_id'];
        $this->cultureCode = $this->projectSetting['culture_code'];
        $this->createdUid = $this->userId;
        $this->upatedUid =$this->userId;
        $this->translator = $translator;
        $this->knpPaginator = $knpPaginator;
        $this->countries =  $country;
        $this->languages = $language;
        $this->culture = $this->request->get('_locale');
        $this->cultureAjax = $this->request->get('culture');
        $this->getCultureValue();        
        $this->responseArray = array();
        $this->session =  $this->request->getSession();
        $this->countryId = null;
        $this->languageId = null;
        $this->flyerRepository->setPaginatorService($this->knpPaginator);
    }
        
    /**
     *
     * @author Employee Id: 4571
     * Description : Unset the variables
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function __destruct()
    {
        unset($this->entityManager);
        unset($this->request);
        unset($this->projectSetting);
        unset($this->instanceId);
        unset($this->instanceType);
        unset($this->userId);
        unset($this->cultureId);
        unset($this->cultureCode);
        unset($this->createdUid);
        unset($this->upatedUid);
        unset($this->translator);
        unset($this->knpPaginator);        
        unset($this->countries);
        unset($this->languages);
        unset($this->culture);
        unset($this->cultureAjax);      
        unset($this->languageCode);
        unset($this->countryCode);
        unset($this->responseArray);
        unset($this->session);
        unset($this->countryId);
        unset($this->languageId);
    }
    
    /**
     *
     * @author Employee Id: 4571
     * Description : seprate culture with language and country.
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function getCultureValue()
    {
        $cultures = array();
        if (isset($this->culture) && $this->culture != "") {
            list($this->languageCode, $this->countryCode) = explode("_", $this->culture);
        }
    
        return $cultures;
    }
    
    /**
     *
     * @author Employee Id: 4571
     * Description : seprate culture with language and country.
     *
     * @param : null
     *
     * @return null
     *
     * @throws null
     *
     **/
    public function getCultureValueAjax()
    {
    	$cultures = array();
    
    	if (isset($this->cultureAjax) && $this->cultureAjax != "") {
    		list($this->languageCode, $this->countryCode) = explode("_", $this->cultureAjax);
    	}
    
    	return $cultures;
    }   
    
    /**
     * @author Employee Id: 4571
     * Description : Get Language id by Code
     *
     * @param 1 : string $languageCode
     *
     * @return lang id
     *
     * @throws null
     *
     **/
    public function getLanguageIdByCode($languageCode)
    {	
    	foreach ($this->languages as $key => $lan) {
    		//var_dump($languageCode ." >> ". $key);
    		if ($languageCode == $key) {
    			return $lan['id'];
    		}
    	}
    }
    
    /**
     * @author Employee Id: 4571
     * Description : get culture code
     *
     * @param 1 : string $cultureCode
     *
     * @return int $countryId
     *
     * @throws null
     *
     * */
    public function getCountryIdByCode($cultureCode)
    {
    	foreach ($this->countries as $key => $country) {
    		if ($cultureCode == $country['short_name']) {  // $country['short_name'] // $key
    			return $country['id'];
    		}
    	}
    }
     
}
