<?php

namespace OfficeBrain\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * CmsSearchCriteriaRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CmsSearchCriteriaRepository extends EntityRepository
{
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $cultureId;
    protected $cultureCode;

    /**
     * @author Employee Id: 4589
     * Description : Set project settings
     */
    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }

    /**
     * @author Employee Id: 4589
     * Description : Get project settings
     */
    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    /**
     * @author Employee Id: 4589
     * 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 findByCountryId($countryId, $status=false)
    {
        $qb = $this->createQueryBuilder('csc')
                    ->where('csc.countryId = :countryId AND csc.deletedAt IS NULL AND csc.instanceId = :instanceId')
                    ->setParameter(':countryId', $countryId)
                    ->setParameter(':instanceId', $this->instanceId)
                    ->orderBy('csc.id', 'DESC');
        if ($status) {
            $qb->andWhere("csc.status = 1");
        }
        
        return $qb->getQuery()->getResult();
    }

    /**
     * @author Employee Id: 4589
     * Description : Find search criteria from Id
     */
    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        return $this->createQueryBuilder('csc')
                    ->where('csc.id = :id AND csc.deletedAt IS NULL AND csc.instanceId = :instanceId AND csc.instanceType = :instanceType')
                    ->setParameter(':instanceId', $this->instanceId)
                    ->setParameter(':instanceType', $this->instanceType)
                    ->setParameter(':id', $id)
                    ->getQuery()
                    ->getOneOrNullResult();
    }

    public function findByOption($countryId, $option, $keyword)
    {
        $return = $this->createQueryBuilder('csc')
                        ->select(['csc']);

        $return->where('csc.deletedAt IS NULL AND csc.instanceId  = :instanceId AND csc.countryId = :countryId');

        if ($option == 'title') {
            $return->leftJoin('csc.languageMapping', 'cscl');
            $return->andWhere('cscl.title LIKE :keyword');
        }

        $return->setParameter(':keyword', '%'.$keyword.'%')
               ->setParameter(':instanceId', $this->instanceId)
               ->setParameter(':countryId', $countryId)
               ->orderBy('csc.id', 'DESC')
               ->getQuery()
               ->getResult();

        return $return;
    }

    public function set($entity)
    {
        $this->_em->persist($entity);
        $this->commit();

        return $entity;
    }

    public function commit()
    {
        $this->_em->flush();
    }
    
    /**
     * @author Employee Id: 5190
     *
     * Description : Change Status of Search Criteria Listing Page
     *
     * @param $id
     *
     * @return : boolean
     *
     * @throws Exception: Null
     */
    public function changeStatus($id)
    {
    	$searchCriteria = $this->findOneBy(array('id' => $id));
    	if ($searchCriteria) {
    		if ($searchCriteria->getStatus() == 1) {
    			$searchCriteria->setStatus(0);
    		} else {
    			$searchCriteria->setStatus(1);
    		}
    		$this->commit($searchCriteria);
    		return true;
    	}
    	return false;
    }
}
