<?php

namespace OfficeBrain\Bundle\EcatalogueBundle\Entity;

/**
 * PrioritizeCatalogueRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class PrioritizeCatalogueRepository extends \Doctrine\ORM\EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	protected $projectUserSetting;
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : Set project setting
	 *
	 * @param : null
	 *
	 * @return null
	 *
	 * @throws : null
	 *
	 **/
	public function setProjectSetting($projetSetting)
	{
		$this->projectSetting = $projetSetting;
		$this->prepareProjectSettingData();
	}
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : get proj setting
	 *
	 * @param : null
	 *
	 * @return project setting array
	 *
	 * @throws : null
	 *
	 **/
	public function getProjectSetting()
	{
		return $this->projetSetting;
	}
	
	/**
	 * @author Employee Id: 4488
	 * OB eCommerce Product - getProjectUserSetting
	 * Function created to set Project user Setting
	 */
	
	public function getProjectUserSetting() {
		return $this->projectUserSetting;
	}
	/**
	 * @author Employee Id: 4488
	 * OB eCommerce Product - setProjectUserSetting
	 * Function created to set Project User Setting
	 */
	public function setProjectUserSetting($projectUserSetting) {
		$this->projectUserSetting = $projectUserSetting;
		$this->prepareProjectUserSettingData();
	
	}
	public function prepareProjectUserSettingData()
	{
		$this->user = $this->projectUserSetting['user_data'];
		if(!empty($this->user))
		{
			$this->parentId = null;
			$this->parentObj=null;
			$this->userType = $this->user->getUserType();
			if(is_object($this->user->getParent())){
				$this->parentId = $this->user->getParent()->getId();
				$this->parentObj=$this->user->getParent();
			}
		}
	}
	
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : Prepare Project Setting
	 *
	 * @param : null
	 *
	 * @return null
	 *
	 * @throws : null
	 *
	 **/
	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: 4571
	 *
	 * Description : find Flyer by Unique Id
	 *
	 * @param 1 : int $customUniqueId
	 *
	 * @return record set
	 *
	 * @throws : null
	 *
	 **/
	public function findByUniqueID($id, $userId)
	{
		return $this->createQueryBuilder('prcat')
		->select('prcat')
		->where(' prcat.catalogue = :catalog AND prcat.createdUid =:createdUid AND prcat.instanceId = :instanceId AND prcat.instanceType = :instanceType')
		->setParameter('catalog', $id)
		->setParameter('createdUid', $userId)
		->setParameter('instanceId', $this->instanceId)
		->setParameter('instanceType', $this->instanceType)
		->getQuery()
		->getResult(); // OneOrNull
	}
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : find Flyer by User Id
	 *
	 * @param 1 : int $userId
	 *
	 * @return record set
	 *
	 * @throws : null
	 *
	 **/
	public function findByUserId($userId)
	{
		return $this->createQueryBuilder('prcat')
		->select('prcat')
		->where('prcat.createdUid =:createdUid AND prcat.instanceId = :instanceId AND prcat.instanceType = :instanceType')
		//->setParameter('catalog', $id)
		->setParameter('createdUid', $userId)
		->setParameter('instanceId', $this->instanceId)
		->setParameter('instanceType', $this->instanceType)
		->getQuery()
		->getResult(); // OneOrNull
	}
	
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : Find Flyer by id
	 *
	 * @param 1 : int $id
	 *
	 * @return record set
	 *
	 * @throws : null
	 *
	 **/
	public function find($id, $lockMode = NULL, $lockVersion = NULL)
	{
		$sql =  $this->createQueryBuilder('cm')
		->select('cm')
		->where('cm.id =:id AND cm.instanceId = :instanceId AND cm.instanceType = :instanceType ')
		->setParameter('instanceId', $this->instanceId)
		->setParameter('instanceType', $this->instanceType)
		->setParameter('id', $id)
		->getQuery()
		->getResult();//getOneOrNullResult();
		return $sql[0];
	}
	
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : Set Values
	 *
	 * @param 1 : $id
	 * @param 2 : $order
	 * @param 3 : $userId
	 *
	 * @return record set
	 *
	 * @throws : null
	 *
	 **/
	public function setValues($id, $order, $userId)
	{
		$ifExists =  $this->findByUniqueID($id, $userId);  
					
		if($ifExists) 
		{
			$pid = $ifExists[0]->getId();				
			$ecatalogue =  $this->find($pid); 				
			
		}else
		{
			$ecatalogue = new PrioritizeCatalogue();
		}
		
		$ecatalogue->setCatalogueOrder($order);
		$ecatalogue->setCatalogue($this->_em->getReference('OfficeBrainEcatalogueBundle:Ecatalogue',$id));
		
		
		if($this->userType == "distributor")
		{
			$ecatalogue->setUser($this->user);
		}
		else 
		{
			if(is_object($this->parentObj))
			{
				if($this->parentObj->getUserType()=="distributor")
				{
					$ecatalogue->setUser($this->parentObj);
				}
				else
				{
					$ecatalogue->setUser($this->user);
				} 
			}
			else 
			{
				$ecatalogue->setUser($this->user);
			}
		}
		
		$ecatalogue->setCreatedUid($userId);
		$ecatalogue->setInstanceId($this->instanceId); //$this->projectSetting['instance_id']
		$ecatalogue->setInstanceType($this->instanceType);
		$this->_em->persist($ecatalogue);
		$this->_em->flush();
			
	}
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : Remove priority
	 *
	 * @param null
	 *
	 * @return result
	 *
	 * @throws null
	 *
	 **/
	public function resetPriority($userId)
	{		
		$isDeleted = $this->createQueryBuilder("prior")
		->delete()
		->where('prior.createdUid =:id AND prior.instanceId = :instanceId AND prior.instanceType = :instanceType')
		->setParameter("id", $userId)
		->setParameter('instanceId', $this->instanceId)
		->setParameter('instanceType', $this->instanceType)
		->getQuery()->execute();				
		
		return $isDeleted;
		 
	}
	
}
