<?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.
 *
 */
/**
 * EmailTemplateMasterRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class EmailTemplateMasterRepository 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 templates
     *
     */
    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 all template by language
     */
    public function findAllByLanguage($language)
    {
	    return $this->createQueryBuilder('etm')
	    ->select('etm.id', 'etm.name', 'etm.status','etm.description', 'etm.createdAt','etl.templateContent')
	    ->leftJoin('OfficeBrainEmailBundle:EmailTemplateLanguage', 'etl', 'WITH', 'etm.id = etl.templateId')
	    ->where('etm.status = :statusId and etm.deletedAt IS NULL and etl.languageId = :languageId')
	    ->setParameter(':statusId', 1)
	    ->setParameter('languageId', $language)
	    ->orderBy('etm.id', 'DESC')
	    ->getQuery()
	    ->getResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find single template 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();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Find default template
     *
     */
    public function findDefault()
    {
        return $this->createQueryBuilder('eh')
                ->select('eh')
                ->where('eh.isDefault = :isDefault')
                ->andWhere('eh.instanceId = :instanceId')
                ->andWhere('eh.instanceType = :instanceType')
                ->setParameter(':instanceId', $this->instanceId)
                ->setParameter(':instanceType', $this->instanceType)
                ->setParameter(':isDefault', 1)
                ->getQuery()
                ->getSingleResult();
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Update default template
     *
     */
    public function updateDefaultTemplate()
    {
        $q=$this->createQueryBuilder('eh')
        ->update()
        ->set('eh.isDefault', '0')
        ->where('eh.instanceId = :instanceId')
        ->andWhere('eh.instanceType = :instanceType')
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->getQuery()
        ->execute()
        ;
    }
    
    /**
     * @author Employee Id: 4795
     *
     * Description : Serach template based on searched keyword
     *
     * @param $keyword
     */
    public function searchTemplate($language,$keyword)
    {
        return $this->createQueryBuilder('eh')
        ->select('eh')
        ->where('eh.deletedAt IS NULL')
        ->andWhere('eh.name like :name OR eh.description like :description')
        ->andWhere('eh.instanceId = :instanceId')
        ->andWhere('eh.instanceType = :instanceType')
        ->setParameter(':name', "%".$keyword."%")
        ->setParameter(':description', "%".$keyword."%")
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->orderBy('eh.id', 'DESC')
        ->getQuery()
        ->getResult();
    }
    
     /**
     * @author Employee Id: 4835
     *
     * Description : This function check if header attached with any templates
     *
     * @param $header_id
     */
    public function hasAssignedtoTemplate($header_id)
    {
        return $this->createQueryBuilder('etm')
        ->select('count(etm.id)')
        ->where('etm.deletedAt IS NULL and etm.header = :header')
        ->andWhere('etm.instanceId = :instanceId')
        ->andWhere('etm.instanceType = :instanceType')
        ->setParameter(':instanceId', $this->instanceId)
        ->setParameter(':instanceType', $this->instanceType)
        ->setParameter(':header', $header_id)
        ->orderBy('etm.id', 'DESC')
        ->getQuery()
        ->getSingleScalarResult();
    }
    
     /**
     * @author Employee Id: 4835
     *
     * Description : This function is used for chnage status of template
     *
     * @param $id
     */
    public function changeTemplateStatus($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;
    }
    
    /**
     * @author Employee Id: 4494
     * OB eCommerce Product - Clone Email Template Master data
     * return array
     * Function Clone Email Template Master data
     */
    public function cloneEmailTemplateMaster($emailTemplateMaster) {
    	$emailTemplateMaster = clone $emailTemplateMaster;
    	$this->_em->detach($emailTemplateMaster);
    	$this->_em->persist($emailTemplateMaster);
    	$this->_em->flush();

    	return $emailTemplateMaster;
    }
}
