<?php

namespace OfficeBrain\Bundle\ProductBundle\Entity;

use Doctrine\ORM\EntityRepository;
use OfficeBrain\Bundle\CoreBundle\Constant\Constant;

/**
 * SearchKeywordRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SearchKeywordRepository extends EntityRepository
{
	public function removeProductKeywords($culture,$culture_product,$keywords)
	{
		
		$em = $this->getEntityManager();
		$queryBuilder = $em->createQueryBuilder();
		$result = $queryBuilder->select('sk')
		->from("OfficeBrainProductBundle:SearchKeyword", 'sk')
		->where('sk.culture_product_id = :cProd')
		->setParameter('cProd', $culture_product)
		->andWhere($queryBuilder->expr()->notIn('sk.keyword', $keywords))
		->andWhere('sk.deletedAt is NULL') //For soft delete - Brand
		//->getQuery()->getArrayResult();
		->getQuery()->getResult();
		
		foreach($result as $keyword){
			$em->remove($keyword);
		}
		$em->flush();
		
		return true;
	}
	
    public function addProductKeyword($culture,$culture_product,$keyword) {
        
        $em = $this->getEntityManager();
        $keywordInfo = $this->findOneBy(array('culture_product_id'=>$culture_product,'keyword'=>$keyword));
        if(!$keywordInfo)
        	$keywordInfo = new SearchKeyword();
        
        $keywordInfo->setProductMaster($culture_product->getProductId());
        $keywordInfo->setCultureProductId($culture_product);
        $keywordInfo->setCultureId($culture);
        $keywordInfo->setKeyword($keyword);
        $keywordInfo->setActive(null);
        $keywordInfo->setTag(true);
        
        $em->persist($keywordInfo);
        $em->flush();
        
        return $keywordInfo;
    }
    
    public function removeProductKeyword($culture,$culture_product,$keyword){
    	$keywordInfo = $this->findOneBy( array('culture'=>$culture,'culture_product_id'=>$culture_product,'keyword'=>$keyword));
    	
    	if(!is_object($keywordInfo)){
    		return false;
    	}
    	
    	$em = $this->getEntityManager();
    	$em->remove($keywordInfo);
    	$em->flush();
    	
    	return true;
    }
    
    public function getKeyword($cultureProductId){
    	$em = $this->getEntityManager();
    	
    	$query = $em->createQueryBuilder()
    	->from('OfficeBrainProductBundle:SearchKeyword','sk')
    	->select("STRING_AGG(sk.keyword,'".Constant::$DATA_SEP."') as keyword")
    	->where('sk.culture_product_id=:cultureProductId')
    	->setParameter('cultureProductId',$cultureProductId);
    	
    	return $query->getQuery()->getResult();
    }
    
    public function removeAllProductKeyword($cultureProductId){
    	$em = $this->getEntityManager();
    
    	$query = $em->createQueryBuilder()
    	->delete('OfficeBrainProductBundle:SearchKeyword','sk')
    	->where('sk.culture_product_id=:cultureProductId')
    	->setParameter('cultureProductId',$cultureProductId);
    	 
    	return $query->getQuery()->execute();
    }
    
    
    //Start Import Queries
    /**
     * 
     * @param unknown $culture_product
     * @param unknown $keyword
     * 
     * @return Ambigous <object, NULL>
     */
    public function getProductKeyword($culture_product,$keyword){
        return $this->findOneBy(array("productMaster" => $culture_product->getProductId(), "culture_product_id" => $culture_product, "keyword" => $keyword ));
    }
    
    /**
     * 
     * @param unknown $culture
     * @param unknown $culture_product
     * @param unknown $keyword
     * 
     * @return unknown
     */
    public function importProductKeyword($culture,$culture_product,$keyword){
         
        $em = $this->getEntityManager();
        
        $keywordInfo = $this->getProductKeyword($culture_product,$keyword);
        if($keywordInfo){
            return $keywordInfo;
        }
        
        $keywordInfo = $this->addProductKeyword($culture,$culture_product,$keyword);
        return $keywordInfo;
    }
    
    
    public function addProductSearchKeyword($culture,$culture_product,$keywordsArray) {
    
    	$em = $this->getEntityManager();
    	
    	foreach ($keywordsArray as $keyword ) 
    	{
    		
    		$keywordInfo = $this->findOneBy(array('culture_product_id'=>$culture_product,'keyword'=>trim($keyword), 'deletedAt' => NULL));
    		if(!$keywordInfo)
    			$keywordInfo = new SearchKeyword();
    		
    		$keywordInfo->setProductMaster($culture_product->getProductId());
    		$keywordInfo->setCultureProductId($culture_product);
    		$keywordInfo->setCulture($culture);
    		$keywordInfo->setKeyword(trim($keyword));
    		$keywordInfo->setActive(null);
    		$keywordInfo->setTag(true);
    		$em->persist($keywordInfo);
    	}
    	
    		$em->flush();
    
    		return $keywordInfo;
    }
}
