<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;
use OfficeBrain\CustomBundle\CmsBundle\Entity\OrderCatalog;
use OfficeBrain\CustomBundle\CmsBundle\Form\OrderCatalogType;

/**
 * OrderCatalogRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */

class OrderCatalogRepository extends \Doctrine\ORM\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'];
	}
	
	public function set($orderCatalog)
	{
		return $this->commit($orderCatalog);
	}
	
	/**
	 * Commit entity.
	 */
	private function commit($orderCatalog)
	{
		$this->_em->persist($orderCatalog);
		$this->_em->flush();
	
		return $orderCatalog;
	}
	
	public function getOrderList()
	{
		return $this->createQueryBuilder('ord')
		->select('ord.id, ord.firstName , ord.lastName, ord.companyName , ord.phone, ord.email , ord.instanceId , ord.instanceType')
		->where("ord.deletedAt is NULL")
		->orderBy('ord.id', 'DESC')
		->getQuery()
		->getResult();
	}
	
	/**
	 * @author Employee Id: 4242
	 *
	 * Description : Find all active events
	 */
	public function findAll($instanceId = '', $instanceType = '')
	{
		$sql =  $this->createQueryBuilder('ord')
		->select('ord')
		->where('ord.id =:id AND ord.instanceId = :instanceId AND ord.instanceType = :instanceType AND ord.deletedAt is NULL')
		->setParameter('instanceId', $this->instanceId)
		->setParameter('instanceType', $this->instanceType)
		->setParameter('id', $id)
		->getQuery()
		->getResult();//getOneOrNullResult();
		return $sql[0];
	}
	
	
	public function findByOption($option, $keyword)
	{
		$return = $this->createQueryBuilder('ord')
		->select(['ord']);
		$return->where('ord.'.$option.' LIKE :keyword AND ord.deletedAt is NULL');
		$return->setParameter(':keyword', '%'.$keyword.'%')
		->orderBy('ord.id', 'DESC')
		->getQuery()
		->getResult();
	
		return $return;
	}
}
 