<?php

namespace OfficeBrain\Bundle\EmailBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * EmailUnsubscribedEventRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EmailUnsubscribedEventRepository extends EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	
	public function setProjectSetting($projetSetting)
	{
		$this->projectSetting = $projetSetting;
		$this->prepareProjectSettingData();
	}
	
	public function getProjectSetting()
	{
		return $this->projetSetting;
	}
	
	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'];
	}
	
	/**
     * @author Employee Id: 4795
     *
     * Description : Find unsubscibe event object of specific user
     *
     */
    public function findUnsubscribedEvent($eventId,$userId)
    {
    	return $this->createQueryBuilder('eue')
                ->select('eue')
                ->where('eue.userId = :userId')
                ->andWhere('eue.eventId = :eventId')
                ->andWhere('eue.instanceId = :instanceId')
                ->andWhere('eue.instanceType = :instanceType')
                ->setParameter(':userId', $userId)
                ->setParameter(':eventId', $eventId)
                ->setParameter(':instanceId', $this->instanceId)
                ->setParameter(':instanceType', $this->instanceType)
                
                ->getQuery()
                ->getOneOrNullResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : get unsubscibe events of specific user
     *
     */
    public function getUnsubscribedEvents($userId)
    {
    	return $this->createQueryBuilder('eue')
    	->select('eue.eventId')
    	->where('eue.userId = :userId')
    	->andWhere('eue.instanceId = :instanceId')
    	->andWhere('eue.instanceType = :instanceType')
    	->setParameter(':userId', $userId)
    	->setParameter(':instanceId', $this->instanceId)
    	->setParameter(':instanceType', $this->instanceType)
    	->getQuery()
    	->getResult();
    }
}
