<?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.
 *
 */
/**
 * EmailTemplateHeaderMasterRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EmailTemplateHeaderMasterRepository 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 headers
     */
    public function findAll()
    {
        return $this->createQueryBuilder('eh')
                ->select('eh')
                ->where('eh.deletedAt IS NULL')
                ->andWhere('eh.instanceId = :instanceId')
                ->andWhere('eh.instanceType = :instanceType')
                ->setParameter(':instanceId', $this->instanceId)
                ->setParameter(':instanceType', $this->instanceType)
                ->orderBy('eh.id', 'DESC')
                ->getQuery()
                ->getResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find single header based on ID
     *
     * @param $id
     */
    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        return $this->createQueryBuilder('eh')
                ->select('eh')
                ->where('eh.id = :id')
                ->andWhere('eh.instanceId = :instanceId')
                ->andWhere('eh.instanceType = :instanceType')
                ->setParameter(':instanceId', $this->instanceId)
                ->setParameter(':instanceType', $this->instanceType)
                ->setParameter(':id', $id)
                ->getQuery()
                ->getSingleResult();
    }
    
    public function findHeaderByShortKey($short_key)
    {
        try {
            return $this->createQueryBuilder('eh')
            ->select('eh.id')
            ->where('eh.shortKey = :short_key')
            ->andWhere('eh.instanceId = :instanceId')
            ->andWhere('eh.instanceType = :instanceType')
            ->andWhere('eh.deletedAt IS NULL')
            ->setParameter(':instanceId', $this->instanceId)
            ->setParameter(':instanceType', $this->instanceType)
            ->setParameter(':short_key', $short_key)
            ->getQuery()
            ->getOneOrNullResult();
        } catch (Exception $e) {
            return array();
        }
    }
    
    public function findDuplicateHeader($name,$id){
        
        $queryBulder = $this->createQueryBuilder('eh');
        $queryBulder ->select('count(eh.id)')
            ->where('eh.instanceId = :instanceId')
            ->andWhere('eh.instanceType = :instanceType')
            ->setParameter(':instanceId', $this->instanceId)
            ->setParameter(':instanceType', $this->instanceType);
        if($name != ''){
             $queryBulder->andWhere('eh.name = :name')
                         ->setParameter('name', $name);
        }
        if($id != ''){
            $queryBulder->andWhere('eh.id != :id')
                         ->setParameter('id', $id);
        } 
        return $queryBulder->getQuery()->getSingleScalarResult();
        
    }
    
    public function findsearchHeaders($language,$dataArr)
    {
        $queryBulder = $this->createQueryBuilder('eh'); 
        $queryBulder ->select('eh')
            ->where('eh.instanceId = :instanceId')
            ->andWhere('eh.instanceType = :instanceType')
            ->andWhere('eh.deletedAt IS NULL')
            ->setParameter(':instanceId', $this->instanceId)
            ->setParameter(':instanceType', $this->instanceType);
        if(isset($dataArr['keyword']) && $dataArr['keyword'] != '' ){
             $queryBulder->andWhere('eh.name LIKE :name')
                         ->setParameter('name', '%'.$dataArr['keyword'].'%');
        }
        
        return $queryBulder->orderBy('eh.id', 'DESC')->getQuery()->getResult();
    }
}
