<?php

namespace OfficeBrain\Bundle\CmsBundle\Entity;

use Doctrine\ORM\EntityRepository;
use Doctrine\Common\Collections\Criteria;

/**
 * CmsMenuMasterRepository.
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class CmsMenuMasterRepository extends EntityRepository
{
    protected $projectSetting;
    protected $instanceId;
    protected $instanceType;
    protected $cultureId;
    protected $cultureCode;
    protected $userType;

    /**
     * @author Employee Id: 4494
     * Description : Set project settings
     */
    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();
    }

    /**
     * @author Employee Id: 4494
     * Description : Get project settings
     */
    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    /**
     * @author Employee Id: 4494
     * Description : Prepare data from project setting
     */
    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'];
        $this->userType = ($this->projectSetting['menu_user_type']=="end_user") ? 'public' : $this->projectSetting['menu_user_type'];

    }

    public function find($id, $lockMode = NULL, $lockVersion = NULL)
    {
        return $this->createQueryBuilder('cm')
                    ->select('cm')
                    ->where('cm.id =:id AND cm.deletedAt IS NULL AND cm.instanceId = :instanceId AND cm.status = 1')
                    ->setParameter(':instanceId', $this->instanceId)
                    ->setParameter(':id', $id)
                    ->getQuery()
                    ->getOneOrNullResult();
    }

    public function findBySlug($slug, $countryId, $visibilityFilter = false)
    {    
    	$menu = $this->createQueryBuilder('cm')
                    ->select('cm')
                    ->where('cm.slug =:slug AND cm.countryId = :countryId AND cm.status = 1 AND cm.deletedAt IS NULL AND cm.instanceId = :instanceId')
                    ->setParameter(':slug', $slug)
                    ->setParameter(':countryId', $countryId)
                    ->setParameter(':instanceId', $this->instanceId)
                    ->getQuery()
                    ->getOneOrNullResult();

		if ($visibilityFilter == true && method_exists($menu, 'getMenuItemMapping')) {
			$menuRights = $this->createQueryBuilder('cm')
								->select('cmr')
			                    ->where('cm.slug =:slug AND cm.countryId = :countryId AND cm.status = 1 AND cm.deletedAt IS NULL AND cm.instanceId = :instanceId')
			                    ->innerJoin('OfficeBrainCmsBundle:CmsMenuRights', 'cmr', 'WITH', 'cmr.slug = cm.slug AND cmr.countryId = cm.countryId AND cmr.instanceId = cm.instanceId AND cmr.userType = :userType')
			                    ->setParameter(':slug', $slug)
			                    ->setParameter(':countryId', $countryId)
			                    ->setParameter(':instanceId', $this->instanceId)
			                    ->setParameter(':userType', $this->userType)
			                    ->getQuery()
			                    ->getOneOrNullResult();

			if (count($menuRights) && $menuRights->getMenuItems() != '') {

				$criteria = Criteria::create()
							->where(Criteria::expr()->in("id", json_decode($menuRights->getMenuItems(), 'Array')))
							->orderBy(array('sortOrder' => 'DESC'));
			
				$menu->setMenuItemMapping($menu->getMenuItemMapping()->matching($criteria));
			}

		}

		return $menu;
    }

    public function findBySlugWithInstanceId($slug, $instanceId)
    {
        return $this->createQueryBuilder('cm')
                    ->select('cm')
                    ->where('cm.slug =:slug AND cm.instanceId = :instanceId')
                    ->setParameter(':slug', $slug)
                    ->setParameter(':instanceId', $instanceId)
                    ->getQuery()
                    ->getResult();
    }

    public function findBySlugWithCountryIdWithInstanceId($slug, $countryId, $instanceId)
    {
    	return $this->createQueryBuilder('cm')
			    	->select('cm')
			    	->where('cm.slug =:slug AND cm.countryId = :countryId AND cm.instanceId = :instanceId')
			    	->setParameter(':slug', $slug)
			    	->setParameter(':countryId', $countryId)
			    	->setParameter(':instanceId', $instanceId)
			    	->getQuery()
			    	->getOneOrNullResult();
    }

    public function findByUnique($statusId = 1)
    {
        return $this->createQueryBuilder('cm')
                    ->select('cm.name, cm.slug')
                    ->where('cm.status =:statusId AND cm.deletedAt IS NULL AND cm.instanceId = :instanceId')
                    ->groupBy('cm.slug')
                    ->orderBy('cm.id')
                    ->setParameter(':statusId', $statusId)
                    ->setParameter(':instanceId', $this->instanceId)
                    ->getQuery()
                    ->getResult();
    }

    public function set($entity)
    {
        $this->_em->persist($entity);
        $this->_em->flush();

        return $entity;
    }
}
