<?php

namespace OfficeBrain\Bundle\CategoryBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * CategoryMappingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CategoryMappingRepository extends EntityRepository
{
	public function addCategoryMapping($masterID,$parentId,$image)
	{
		$em = $this->getEntityManager();
		$categoryMappingObj= new CategoryMapping();
		$categoryMappingObj->setPmsId($masterID);
		$categoryMappingObj->setParentId($parentId);
		$categoryMappingObj->setImage($image);
		$em->persist($categoryMappingObj);
		$em->flush();
		return $categoryMappingObj;
		
	}
	public function updateCategoryParent($mappingObj,$parentID,$image)
	{
		$em = $this->getEntityManager();
		$categoryMappingObj= new CategoryMapping();
		$mappingObj->setParentId($parentID);
		$mappingObj->setImage($image);
		$em->flush();
		return $mappingObj;
	}
	public function getCategoryMapping()
	{
		$query=$this->createQueryBuilder('cm')
		->select('cm.id as local_id, cm.pmsId as pms_id','cm.parentId as parent_id','cm.image','cm.categoryFlag','cm.sortPosition','cm.categoryLink,cm.status')
		->addOrderBy('cm.parentId','ASC')
		->where("cm.deletedAt is NULL");
		return $query->getQuery()->getResult();
	}
	public function getCategoryImageById($categoryId)
	{	
		$query=$this->createQueryBuilder('cm')
		->select('cm.pmsId as pms_id','cm.image')
		->where("cm.pmsId IN(:id)")
		->Andwhere("cm.deletedAt is NULL")
		->setParameter('id', array_values($categoryId));
		return $query->getQuery()->getResult();
	}

	public function updateStatus($pmsId,$status){
		$catObj = $this->getCategoryByPmsId($pmsId);
		if($catObj!=NULL){
			$em = $this->getEntityManager();
			$catObj->setStatus($status);
			$em->flush();
			return $catObj;
		}
	}

	public function getCategoryByPmsId($pmsId){
		$catObj = $this->findOneBy(array('pmsId' => $pmsId,'deletedAt' => NULL));		
		return $catObj;
	}
	public function delete($pmsId){
		$obj = $this->findOneBy(array('pmsId' => $pmsId,'deletedAt' => NULL));
		//$id = $obj->getId();
		$em = $this->getEntityManager();
		$em->remove($obj);
		$em->flush();
		return $obj;
	}
	
	/**
	 * 
	 * @param unknown $masterID
	 * @param unknown $parentId
	 * @param unknown $image
	 * @return unknown|\OfficeBrain\Bundle\CategoryBundle\Entity\CategoryMapping
	 */
	public function insertCategoryMapping($masterID, $parentId, $image) {
		
		$em = $this->getEntityManager();
		$categoryMappingObj = $this->findOneBy(array('pmsId' => $masterID,'deletedAt' => NULL));
		$categoryMappingObj = ($categoryMappingObj) ? $categoryMappingObj: new CategoryMapping();
		$categoryMappingObj->setPmsId($masterID);
		$categoryMappingObj->setParentId($parentId);
		$categoryMappingObj->setImage($image);
		$em->persist($categoryMappingObj);
		$em->flush();
		
		return $categoryMappingObj;
	}
	public function updateProductCategoryMapping($productMappedObj,$new_master_id)
	{
		$em = $this->getEntityManager();
		$productMappedObj->setCategoryId($new_master_id);
		$em->persist($productMappedObj);
		$em->flush();
	}
	
	public function updateCategoryMapping($mappingObj,$new_master_id,$parent_id,$categoryImage)
	{
		if($categoryImage==''){
			$categoryImage=NULL;
		}
		$em = $this->getEntityManager();
		$categoryMappingObj= new CategoryMapping();
		$mappingObj->setPmsId($new_master_id);
		$mappingObj->setParentId($parent_id);
		$mappingObj->setImage($categoryImage);
		$em->persist($mappingObj);
		$em->flush();
		return $mappingObj;
	
	}

	public function setCategoryMappingById($catMapId,$pmsId,$catFlag)
	{
		$em = $this->getEntityManager();
		$categoryMappingObj = $this->findOneBy(array('id' => $catMapId,'deletedAt' => NULL));
		$categoryMappingObj->setCategoryFlag($catFlag);
		$em->persist($categoryMappingObj);
		$em->flush();
		return $categoryMappingObj;
	}

	 /**
     * @author Krunal Jethi
     *
     * Description : Persist Object
     *
     * @param : null
     *
     * @return Object
     *
     * @throws : null
     *
     **/
    public function set($categoryMapObj)
    {
		$em = $this->getEntityManager();
        $this->_em->persist($categoryMapObj);
        return $categoryMapObj;
    }
}
