<?php

namespace OfficeBrain\Bundle\ServiceGroupBundle\Entity;

use Doctrine\ORM\EntityRepository;

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

    public function setProjectSetting($projetSetting)
    {
        $this->projectSetting = $projetSetting;
        $this->prepareProjectSettingData();

        return $this;
    }

    public function getProjectSetting()
    {
        return $this->projetSetting;
    }

    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'];
    }

    /*
     * New structured functions
     */

    public function findByRuleType($ruleType)
    {
        $qb = $this->_em->createQueryBuilder()
                ->select('sro')
                ->from('OfficeBrainServiceGroupBundle:ServiceRuleOption', 'sro')
                ->where('sro.deletedAt IS NULL')
                ->andWhere('sro.ruleType = :ruleType')
                ->setParameter('ruleType', $ruleType)
                ->andWhere('sro.status = :status')
                ->setParameter('status', 1)
                ->andWhere('sro.instanceId = :instanceId')
                ->setParameter('instanceId', $this->instanceId)
                ->andWhere('sro.instanceType = :instanceType')
                ->setParameter('instanceType', $this->instanceType);

        $qb->addOrderBy('sro.ruleOptionsTitle', 'ASC');

        return  $qb->getQuery()->getResult();
    }
}
