<?php

namespace OfficeBrain\Bundle\UserBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * SignUpFormRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class SignUpFormRepository extends EntityRepository
{
    protected $projectSetting;

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

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

    public function getSignUpForm($userType)
    {
    	
    	if($userType == 'branchEmployee'){ $userType = 'employee'; }
        $instanceTypeId = $this->projectSetting['instance_id'];
        $instanceType = $this->projectSetting['instance_type'];
        $query = $this->createQueryBuilder('s')->select('s');
        $query->where('s.userType = :userType')->setParameter('userType', $userType);
        $query->andWhere('s.instanceType = :instanceType')->setParameter('instanceType', $instanceType);
        $query->andWhere('s.instanceTypeId = :instanceTypeId')->setParameter('instanceTypeId', $instanceTypeId);
        $query->setMaxResults(1);        
        $query = $query->getQuery()->getArrayResult();        
        $result['fields'] =  (array) json_decode($query[0]['fields']);
        $result['validations'] = (array) explode(',', $query[0]['validations']);
        $result['unique_keys'] = (array) explode(',', $query[0]['uniqueKeys']);
        $result['approval_configuration'] = (array) json_decode($query[0]['approvalConfiguration']);
        return $result;
    }
    
    public function getSignUpFormArrayFormat($userType)
    {
    	 
    	if($userType == 'branchEmployee'){ $userType = 'employee'; }
    	$instanceTypeId = $this->projectSetting['instance_id'];
    	$instanceType = $this->projectSetting['instance_type'];
    	$query = $this->createQueryBuilder('s')->select('s');
    	$query->where('s.userType = :userType')->setParameter('userType', $userType);
    	$query->andWhere('s.instanceType = :instanceType')->setParameter('instanceType', $instanceType);
    	$query->andWhere('s.instanceTypeId = :instanceTypeId')->setParameter('instanceTypeId', $instanceTypeId);
    	$query->setMaxResults(1);
    	$query = $query->getQuery()->getArrayResult();
    	$result['fields'] =  (array) json_decode($query[0]['fields'],true);
    	$result['validations'] = (array) explode(',', $query[0]['validations']);
    	$result['unique_keys'] = (array) explode(',', $query[0]['uniqueKeys']);
    	$result['approval_configuration'] = (array) json_decode($query[0]['approvalConfiguration'],true);
    	return $result;
    }
    
    public function getUserApprovalConfiguration($instanceId, $instanceType)
    {   
    	$resultArray = array();
    	$query = $this->createQueryBuilder('s')->select('s.userType, s.approvalConfiguration');
    	$query->where('s.instanceType = :instanceTypeId')->setParameter('instanceType', $instanceId);
    	$query->andWhere('s.instanceTypeId = :instanceType')->setParameter('instanceTypeId', $instanceType);
    	$query->andWhere('s.deletedAt IS NULL');
    	$recordSet = $query->getQuery()->getArrayResult();
    
    	foreach($recordSet as $row) {
    		$resultArray[$row['userType']] = json_decode($row['approvalConfiguration'], true);
    	}
    
    	return $resultArray;
    }

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

    	return $signUpForm;
    }
}
