<?php

namespace OfficeBrain\Bundle\CmsBundle\Entity;

/**
 * CmsMenuRightsRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CmsMenuRightsRepository extends \Doctrine\ORM\EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	
	/**
	 * @author Employee Id: 4494
	 * Description : Set project settings
	 */
	public function setProjectSetting($projetSetting)
	{
		$this->projectSetting = $projetSetting;
		$this->prepareProjectSettingData();
	}
	
	/**
	 * @author Employee Id: 4494
	 * Description : Get project settings
	 */
	public function getProjectSetting()
	{
		return $this->projetSetting;
	}
	
	/**
	 * @author Employee Id: 4494
	 * 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 findBySlug($slug, $countryId)
	{
		return $this->createQueryBuilder('cmr')
					->select('cmr')
					->where('cmr.slug =:slug AND cmr.instanceId = :instanceId AND cmr.countryId = :countryId')
					->setParameter(':slug', $slug)
					->setParameter(':instanceId', $this->instanceId)
					->setParameter(':countryId', $countryId)
					->getQuery()
					->getResult();
	}
	
	public function findByType($slug, $countryId, $type)
	{
		return $this->createQueryBuilder('cmr')
				->select('cmr')
				->where('cmr.slug =:slug AND cmr.instanceId = :instanceId AND cmr.countryId = :countryId AND cmr.userType = :type')
				->setParameter(':type', $type)
				->setParameter(':slug', $slug)
				->setParameter(':instanceId', $this->instanceId)
				->setParameter(':countryId', $countryId)
				->getQuery()
				->getOneOrNullResult();
	}
	
	public function set($entity)
    {
        $this->_em->persist($entity);
        $this->_em->flush();

        return $entity;
    }
}
