<?php

namespace OfficeBrain\CustomBundle\CmsBundle\Entity;

/**
 * ContactUsRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ContactUsRepository extends \Doctrine\ORM\EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	protected $userId;
	protected $projectId;
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : This function is used to set project settings
	 *
	 * @param 1: $projectSetting
	 *
	 * @return: NULL
	 *
	 * @throws Exception: Null
	 */
	public function setProjectSetting($projetSetting)
	{
		$this->projectSetting = $projetSetting;
		$this->prepareProjectSettingData();
	}
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : This function is used to get project settings
	 *
	 * @param : None
	 *
	 * @return: project setting array
	 *
	 * @throws Exception: Null
	 */
	public function getProjectSetting()
	{
		return $this->projetSetting;
	}
		
	public function getPaginatorService()
	{
		return $this->paginatorService;
	}
	
	public function setPaginatorService($paginatorService)
	{
		$this->paginatorService = $paginatorService;
	}
	

	/**
	 * @author Employee Id: 4571
	 *
	 * Description : This function is used to set member variables default value
	 *
	 * @param : None
	 *
	 * @return: NULL
	 *
	 * @throws Exception: Null
	 */
	public function prepareProjectSettingData()
	{
		$this->projectId = $this->projectSetting['project_id'];
		$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'];
		$this->userId = $this->projectSetting['user_id'];
	}
	
	/**
	 * @author Employee Id: 4571
	 *
	 * Description : This function is used to save contact us page details in db
	 *
	 * @param : None
	 *
	 * @return: NULL
	 *
	 * @throws Exception: Null
	 */

	public function addContacts($contactsObj)
	{		
		$contactsObj->setCreatedUid($this->userId);
        $contactsObj->setInstanceId($this->instanceId);
        $contactsObj->setInstanceType($this->instanceType);
        $contactsObj->setCreatedAt(new \DateTime());
       	$this->_em->persist($contactsObj);
        $this->_em->flush();
		
		return $contactsObj;
	}

	public function getContactDetailById($contactId)
	{		

		$queryBuilder = $this->createQueryBuilder('cm'); // alias defined here
	    $queryBuilder->where('cm.id = :contactId')
	                 ->setParameter('contactId', $contactId);

	    return $queryBuilder->getQuery()->getOneOrNullResult();
	}

	public function getContactAdminList($page, $recordsPerPage, $searchParameters, $searchKeyWordsArray)
	{
	    $queryBuilder = $this->createQueryBuilder('cm')
	       
	        ->select(
		    'cm.id',
		    'cm.fullName',
		    'cm.lastName',
		    'cm.companyName',
		    'cm.phone',
		    'cm.email',
		    'cm.countryId',
		    'cm.stateId',
		    'cm.cityName',
		    'cm.street1',
		    'cm.street2',
		    'cm.zipcode',
		    'cm.subject',
		    'cm.message',
		    'cm.createdAt',
		    'c.countryName',
		    's.stateName',
		    'cm.distributorPurchasInfo'
			)	
	        ->leftJoin('OfficeBrainCoreBundle:Country', 'c', 'WITH', 'c.id = cm.countryId')
	        ->leftJoin('OfficeBrainCoreBundle:State', 's', 'WITH', 's.id = cm.stateId');

	   
	    if (!empty($searchKeyWordsArray)) {

	        foreach ($searchKeyWordsArray as $key => $val) {

	            if ($key == 'email') {

	                $queryBuilder->andWhere('cm.email LIKE :email')
	                             ->setParameter('email', '%'.$val.'%');

	            } elseif ($key == 'fullName') {

	                $name = explode(' ', $val);

	                if (count($name) > 1) {

	                    $queryBuilder->andWhere('cm.fullName LIKE :fname OR cm.lastName LIKE :lname')
	                                 ->setParameter('fname', '%'.$name[0].'%')
	                                 ->setParameter('lname', '%'.$name[1].'%');

	                } else {

	                    $queryBuilder->andWhere('cm.fullName LIKE :name OR cm.lastName LIKE :name')
	                                 ->setParameter('name', '%'.$name[0].'%');
	                }
	            }else{
	            	  if($key!=""){
	            		$param = 'contact'.$key;
						$queryBuilder->andWhere('cm.'.$key.' LIKE :'.$param)
						->setParameter($param, '%'. $val .'%');
					}
	            }
	        }
	    }

	    $queryBuilder->orderBy('cm.id', 'DESC');
	    //return $queryBuilder->getQuery()->getArrayResult();
	    return $this->paginatorService->paginate($queryBuilder, $page, $recordsPerPage);
	}
}
