<?php

namespace OfficeBrain\CustomBundle\FlyerBundle\Entity;

use Symfony\Component\HttpFoundation\Session\Session;

/**
 * FlyerCategoryRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class FlyerCategoryRepository extends \Doctrine\ORM\EntityRepository
{
	protected $projectSetting;
	protected $instanceId;
	protected $instanceType;
	protected $cultureId;
	protected $cultureCode;
	protected $paginatorService;
	
	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 setProjectSetting($projetSetting)
	{
		$this->projectSetting = $projetSetting;
		$this->prepareProjectSettingData();
	}
	public function setPaginatorService($paginatorService)
	{
		$this->paginatorService = $paginatorService;
	}
	public function findByActiveFlyerCategoryList($countryId,$search=null, $adminUser,$page='',$recordPerPage='')
	{
		/* $query = $this->createQueryBuilder('fl');
		$query->select('fl');
		$query->where('fl.deletedAt IS NULL')
		->andWhere('fl.instanceId = :instanceId')
		->andwhere('fl.instanceType = :instanceType')
		->andwhere('fl.countryId = :countryId')
		->setParameter('countryId', $countryId)
		->setParameter('instanceId', $this->instanceId)
		->setParameter('instanceType', $this->instanceType);
		
		if(!$adminUser){
			$query->andwhere('fl.createdUid = :createdUid')
			->setParameter('createdUid', $this->userId);
		} */
		if ($search) {
			$query = $this->createQueryBuilder('fl');
			$query->select('fl');
			$query->where('fl.deletedAt IS NULL')
			->andWhere('fl.instanceId = :instanceId')
			->andwhere('fl.instanceType = :instanceType')
			->andwhere('fl.countryId = :countryId')
			->setParameter('countryId', $countryId)
			->setParameter('instanceId', $this->instanceId)
			->setParameter('instanceType', $this->instanceType);
			$query->andwhere('fl.title LIKE :searchByName');
			$query->setParameter('searchByName', '%'.$search.'%');
			//$result = $query->getQuery()->getResult();
			//return $result;
			return $this->paginatorService->paginate($query, $page, $recordPerPage);
		}
		else
		{
		 $em = $this->getEntityManager();
		 $this->dbConnection = $em->getConnection();
	 	 $query="SELECT * FROM (SELECT c3.id AS childid, c3.parent_id AS parentid,(SELECT COUNT(c4.`parent_id`) FROM tbl_flyers_category c4 WHERE c4.`parent_id`=c3.`id` and c4.deleted_at IS NULL) AS haschild, c3.title ,c3.status from tbl_flyers_category c3 WHERE c3.deleted_at IS NULL and (c3.parent_id IS NULL OR c3.parent_id=0) and  c3.country_id='".$countryId."' 
				UNION
				SELECT c1.id AS childid, c2.id AS childid, 0,c2.title ,c2.status from tbl_flyers_category c1 LEFT JOIN tbl_flyers_category c2 ON c1.id = c2.parent_id WHERE c2.parent_id IS NOT NULL and c2.deleted_at IS NULL and c2.country_id=".$countryId.") as temp ORDER BY temp.childid,temp.parentid";
		$statement = $this->dbConnection->prepare($query);
		$statement->execute();
		$result = $statement->fetchAll();
		//echo"<pre>";print_r($result);die();
		//$result = $query->getQuery()->getResult();
		return $result;
		//return $this->paginatorService->paginate($query, $page, $recordPerPage);
		}
	}
	
	public function findAdminUser()
	{
		$query=$this->getEntityManager()->getRepository("OfficeBrainUserBundle:User")->createQueryBuilder('u'); // required .. do not remove
		$query->select('u');
		$query->where("u.status='active'");
		
		if (isset($this->instanceId)) {
			$query->andWhere("u.instanceId=:instanceId")->setParameter('instanceId', $this->instanceId);
		}
		
		$query->andWhere("u.locked is NULL");
		$query->andWhere("u.expired is NULL");
		$query->andWhere("u.deletedAt is NULL");
		$query->andWhere('u.parent is NULL');
		$query->andWhere("u.id=:id")->setParameter('id', $this->userId);
		
		return $query->getQuery()->getScalarResult();
	}
	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];
	}
	public function allcategories($CountryId,$id)
	{
		$sql =  $this->createQueryBuilder('cm')
		->select('cm.id,cm.title')
		->where('cm.countryId =:CountryId AND cm.id !=:id AND cm.deletedAt IS NULL AND cm.parentId IS NULL')
		->setParameter('CountryId', $CountryId)
		->setParameter('id', $id)
		->getQuery()
		->getResult();//getOneOrNullResult();
		return $sql;
	}
	public function deleteFlyerCategoryrecord($id)
	{
		$em = $this->getEntityManager();
		$this->dbConnection = $em->getConnection();
		$query="update tbl_flyers_category SET deleted_at=now(),deleted_uid=".$this->userId." WHERE id =".$id." AND 1 > ( SELECT * FROM ( SELECT COUNT(id) FROM tbl_flyers_category where deleted_at IS NULL and parent_id =".$id.") AS p)";
		$statement = $this->dbConnection->prepare($query);
		$statement->execute();
		$result = $statement->rowCount();
		return $result;
		
	}
	public function updateparentrecord($id)
	{
		$em = $this->getEntityManager();
		$this->dbConnection = $em->getConnection();
		$query="UPDATE tbl_flyers_category SET parent_id=0 WHERE id =(SELECT * FROM (SELECT parent_id FROM tbl_flyers_category WHERE id=".$id.") AS p) AND 1 > ( SELECT * FROM (SELECT COUNT(id) FROM tbl_flyers_category WHERE parent_id=(SELECT parent_id FROM tbl_flyers_category WHERE id=".$id.") AND `deleted_at` IS NULL) AS p)";
		$statement = $this->dbConnection->prepare($query);
		$statement->execute();
		
	}
	public function FlyerCategorymenuList($countryId)
	{
		$em = $this->getEntityManager();
		 $this->dbConnection = $em->getConnection();
	 	 $query="SELECT * FROM (SELECT c3.slug,c3.id AS childid, c3.parent_id AS parentid,(SELECT COUNT(c4.`parent_id`) FROM tbl_flyers_category c4 WHERE c4.`parent_id`=c3.`id` and c4.deleted_at IS NULL and c4.status=1) AS haschild, c3.title ,c3.status from tbl_flyers_category c3 WHERE c3.deleted_at IS NULL AND c3.status =1 and (c3.parent_id IS NULL OR c3.parent_id=0) and  c3.country_id='".$countryId."' having haschild>0  
				UNION
				SELECT c2.slug,c1.id AS childid, c2.id AS childid, 0,c2.title ,c2.status from tbl_flyers_category c1 LEFT JOIN tbl_flyers_category c2 ON c1.id = c2.parent_id WHERE c2.parent_id IS NOT NULL and c2.status=1 and c1.status=1 and c2.deleted_at IS NULL and c2.country_id=".$countryId.") as temp ORDER BY temp.childid,temp.parentid";
		$statement = $this->dbConnection->prepare($query);
		$statement->execute();
		$result = $statement->fetchAll();
		//echo"<pre>";print_r($result);die();
		//$result = $query->getQuery()->getResult();
		return $result;
	}
	public function slug_exist($x)
	{
		$em = $this->getEntityManager();
		$this->dbConnection = $em->getConnection();
		$sql = "SELECT slug FROM tbl_flyers_category WHERE slug='".$x."'";
		$statement = $this->dbConnection->prepare($sql);
		$statement->execute();
		$result = $statement->rowCount();
		if($result > 0)
		{
            return true;
    	}
	}
}


