<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Entity;

/**
 * CmsFlashMessageMasterRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CmsFlashMessageMasterRepository extends \Doctrine\ORM\EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	
	/**
	 * @author Employee Id: 5051
	 * Description : Set project settings
	 */
	public function setProjectSetting($projetSetting)
	{
		$this->projectSetting = $projetSetting;
		$this->prepareProjectSettingData();
	}
	
	/**
	 * @author Employee Id: 5051
	 * Description : Get project settings
	 */
	public function getProjectSetting()
	{
		return $this->projetSetting;
	}
	
	/**
	 * @author Employee Id: 5051
	 * Description : Prepare data from project setting
	 */
	public function prepareProjectSettingData()
	{
		$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'];
	}
	
	public function findByCountryId($countryId)
	{
		$result = $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.countryId = :countryId AND cp.deletedAt IS NULL AND cp.instanceId = :instanceId')
		->setParameter(':countryId', $countryId)
		->setParameter(':instanceId', $this->instanceId)
		->orderBy('cp.id', 'DESC')
		->getQuery()
		->getResult();
		return $result;
	}
	
	public function findActiveByCountryId($countryId)
	{
		return $this->createQueryBuilder('cp')
		->select('cp')
		->where('cp.countryId = :countryId AND cp.status = 1 AND cp.deletedAt IS NULL AND cp.instanceId = :instanceId')
		->setParameter(':countryId', $countryId)
		->setParameter(':instanceId', $this->instanceId)
		->orderBy('cp.id', 'DESC')
		->getQuery()
		->getResult();
	}
	
	public function setCmsFlashMessage($entity)
	{
		$this->_em->persist($entity);
		$this->_em->flush();
		return $entity;
	}
	
	/**
	 * @author Employee Id: 5051
	 *
	 * Description : Change Status of CMS Flash Message
	 *
	 * @param $id
	 *
	 * @return : boolean
	 *
	 * @throws Exception: Null
	 */
	public function changeStatus($id)
	{
		$cmsFlashMessageMaster = $this->findOneBy(array('id' => $id));
		if ($cmsFlashMessageMaster) {
			if ($cmsFlashMessageMaster->getStatus() == 1) {
				$cmsFlashMessageMaster->setStatus(0);
			} else {
				$cmsFlashMessageMaster->setStatus(1);
			}
			$this->_em->persist($cmsFlashMessageMaster);
			$this->_em->flush();
			return true;
		}
		return false;
	}
	
	public function findByOption($countryId, $option, $keyword)
	{
		$query = $this->createQueryBuilder('cp')
		->select(['cp']);
		if ($option == 'content') {
			$query->where('cp.flashMessageContent LIKE :keyword ANd cp.deletedAt IS NULL AND cp.instanceId  = :instanceId AND cp.countryId = :countryId');
		}
		$query->setParameter(':keyword', '%'.$keyword.'%')
		->setParameter(':instanceId', $this->instanceId)
		->setParameter(':countryId', $countryId)
		->orderBy('cp.id', 'DESC')
		->getQuery()
		->getResult();
		return $query;
	}
	
	/**
	 * @author Employee Id: 5051
	 * Description : Get Flash Message by Id
	 */
	public function getCmsFlashMessage($countryId, $languageId = null)
	{
		$query = $this->createQueryBuilder('p')
				->select('p.flashMessageContent')
				->where('p.status = :statusId and p.deletedAt IS NULL AND p.countryId = :countryId AND p.languageId = :languageId AND p.instanceId = :instanceId')
				->setParameter(':statusId', 1)
				->setParameter('countryId', $countryId)
				->setParameter('languageId', $languageId)
				->setParameter(':instanceId', $this->instanceId)
				->getQuery()
				->getResult();
		return $query;
	}
}
