<?php

namespace OfficeBrain\Bundle\EmailBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 *
 * @author OfficeBrain 4795 <info@officebrain.com>
 *
 * Description: This class is auto generate by Doctrine ORM add your own custom function here.
 *
 */
/**
 * EmailEventMasterRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EmailEventMasterRepository 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 all active events
     */
    public function findAll($instanceId = '', $instanceType = '')
    {
    	if ($instanceId == '') {
    		$instanceId = $this->instanceId;
    	}
    	
    	if ($instanceType == '') {
    		$instanceType = $this->instanceType;
    	}

        return $this->createQueryBuilder('eem')
			        ->select('eem')
			        ->where('eem.deletedAt IS NULL')
			        ->andWhere('eem.instanceId = :instanceId')
			        ->andWhere('eem.instanceType = :instanceType')
			        ->setParameter(':instanceId', $instanceId)
			        ->setParameter(':instanceType', $instanceType)
			        ->orderBy('eem.id', 'DESC')
			        ->getQuery()
			        ->getResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find all active master
     */
    public function findAllMaster()
    {
    	return $this->createQueryBuilder('eem')
			        ->select('eem')
			        ->where('eem.deletedAt IS NULL and eem.parentId IS NULL')
			        ->andWhere('eem.instanceId = :instanceId')
			        ->andWhere('eem.instanceType = :instanceType')
			        ->setParameter(':instanceId', $this->instanceId)
			        ->setParameter(':instanceType', $this->instanceType)
			        ->orderBy('eem.id', 'DESC')
			        ->getQuery()
			        ->getResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find all active child
     */
    public function findAllChild()
    {
        return $this->createQueryBuilder('eem')
        ->select('eem')
        ->where('eem.deletedAt IS NULL and eem.parentId IS NOT NULL')
        ->orderBy('eem.id', 'DESC')
        ->andWhere('eem.instanceId = :instanceId')
        ->andWhere('eem.instanceType = :instanceType')
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->getQuery()
        ->getResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find all child of master
     */
    public function findChildOfMaster($masterId, $keyword=null)
    {
    	$qb=$this->createQueryBuilder('eem')
    	->select('eem')
    	->where('eem.deletedAt IS NULL')
    	->orderBy('eem.id', 'DESC')
    	->andWhere('eem.parentId = :parentId')
    	->andWhere('eem.instanceId = :instanceId')
    	->andWhere('eem.instanceType = :instanceType')
    	->setParameter(':parentId', $masterId)
    	->setParameter(':instanceId', $this->instanceId)
    	->setParameter(':instanceType', $this->instanceType);
        if($keyword != null){
            $qb->andWhere('eem.name like :name')  
               ->setParameter(':name', '%'.$keyword.'%');
        }
    	$result = $qb->getQuery()
    	->getResult();
        return $result;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find single event based on ID
     *
     * @param $id
     */
    public function find($id, $instanceId = '', $instanceType = '')
    {
    	if ($instanceId == '') {
    		$instanceId = $this->instanceId;
    	}
    	 
    	if ($instanceType == '') {
    		$instanceType = $this->instanceType;
    	}

        return $this->createQueryBuilder('eem')
                ->select('eem')
                ->where('eem.id = :id')
                ->setParameter(':id', $id)
                ->andWhere('eem.instanceId = :instanceId')
                ->andWhere('eem.instanceType = :instanceType')
                ->setParameter(':instanceId', $instanceId)
                ->setParameter(':instanceType', $instanceType)
                ->getQuery()
                ->getOneOrNullResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find single event based on event_key
     *
     * @param $eventKey
     */
    public function findByEventKey($eventKey)
    {
        return $this->createQueryBuilder('eem')
        ->select('eem')
        ->where('eem.deletedAt IS NULL and eem.eventKey = :eventKey')
        ->andWhere('eem.instanceId = :instanceId')
        ->andWhere('eem.instanceType = :instanceType')
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->setParameter(':eventKey', $eventKey)
        ->getQuery()
        ->getOneOrNullResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : This function checks that master event has any child or not
     *
     * @param $id
     */
    public function haveChild($id)
    {
        return $this->createQueryBuilder('eem')
        ->select('eem')
        ->where('eem.deletedAt IS NULL and eem.parentId = :parentid')
        ->andWhere('eem.instanceId = :instanceId')
        ->andWhere('eem.instanceType = :instanceType')
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->setParameter(':parentid', $id)
        ->orderBy('eem.id', 'DESC')
        ->getQuery()
        ->getResult();
    }
        
    /**
     * @author Employee Id: 4795
     *
     * Description : This function check if template has any child event or not
     *
     * @param $template_id
     */
    public function hasChildEvent($template_id)
    {
        return $this->createQueryBuilder('eem')
        ->select('eem')
        ->where('eem.deletedAt IS NULL and eem.parentId IS NOT NULL and eem.template = :template')
        ->andWhere('eem.instanceId = :instanceId')
        ->andWhere('eem.instanceType = :instanceType')
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->setParameter(':template', $template_id)
        ->orderBy('eem.id', 'DESC')
        ->getQuery()
        ->getResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Search event based on searched parameter
     *
     * @param $dataArr
     */
    public function searchEvent($dataArr)
    {
       $qb=$this->createQueryBuilder('eem')
    	->select('eem')
    	->where('eem.deletedAt IS NULL and eem.parentId IS NOT NULL')   
    	->orderBy('eem.id', 'DESC')
    	->andWhere('eem.instanceId = :instanceId')
    	->andWhere('eem.instanceType = :instanceType')
    	->setParameter(':instanceId', $this->instanceId)
    	->setParameter(':instanceType', $this->instanceType);
        if($dataArr['keyword'] != null){
            $qb->andWhere('eem.name like :name')  
               ->setParameter(':name', '%'.$dataArr['keyword'].'%');
        }
    	$result = $qb->getQuery()
    	->getResult();
        return $result; 
         
        
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : This function check if eventkey is exist or not
     *
     * @param $eventKey
     */
    public function checkEventKey($eventKey)
    {
    	$result = $this->createQueryBuilder('s')
		    	->select('COUNT(s)')
		    	->where('s.eventKey = :eventKey')
		    	->setParameter(':eventKey', $eventKey)
		    	->andWhere('s.instanceId = :instanceId') //4488 instance wise event exist or not and remove unique assert column unique true
		    	->setParameter(':instanceId', $this->instanceId)
		    	->getQuery()->getResult();
    	if ($result[0][1] > 0) {
    		return $result[0][1];
    	}
    	return;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find all optional events
     */
    public function findOptional()
    {
        return $this->createQueryBuilder('eem')
        ->select('eem')
        ->where('eem.deletedAt IS NULL')
        ->andWhere('eem.instanceId = :instanceId')
        ->andWhere('eem.instanceType = :instanceType')
        ->andWhere('eem.isOptional = :optional')
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->setParameter(':optional', 1)
       // ->groupBy('eem.parentId')
        ->orderBy('eem.id', 'DESC')
        ->getQuery()
        ->getResult();
    }
    
     /**
     * @author Employee Id: 4835
     *
     * Description : This function is used for chnage status of event
     *
     * @param $id
     */
    public function changeEventStatus($id)
    {
        
    	$emailMaster = $this->findOneBy(array('id' => $id));
       	if ($emailMaster) {
    		$em = $this->getEntityManager();
       		if ($emailMaster->getStatus() == 1) {
    			$emailMaster->setStatus(0);
    		} else {
    			$emailMaster->setStatus(1);
    		}
    		$em->persist($emailMaster);
    		$em->flush();
       		return true;
    	}
    
    	return false;
    }
}
