<?php
namespace App\Form\Filter\Subscriber;
use App\Entity\Company;
use App\Entity\CompanyGroup;
use App\Entity\User;
use App\Repository\CompanyRepository;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class InvoiceAutomationFilterSubscriber implements EventSubscriberInterface
{
/** @var AuthorizationCheckerInterface */
protected $_authorizationChecker;
/** @var TokenStorageInterface */
protected $_tokenStorage;
/**
* @param AuthorizationCheckerInterface $_authorizationChecker
* @param TokenStorageInterface $_tokenStorage
*/
public function __construct(AuthorizationCheckerInterface $_authorizationChecker, TokenStorageInterface $_tokenStorage)
{
$this->_authorizationChecker = $_authorizationChecker;
$this->_tokenStorage = $_tokenStorage;
}
/**
* Returns an array of event names this subscriber wants to listen to.
*
* The array keys are event names and the value can be:
*
* * The method name to call (priority defaults to 0)
* * An array composed of the method name to call and the priority
* * An array of arrays composed of the method names to call and respective
* priorities, or 0 if unset
*
* For instance:
*
* * ['eventName' => 'methodName']
* * ['eventName' => ['methodName', $priority]]
* * ['eventName' => [['methodName1', $priority], ['methodName2']]]
*
* The code must not depend on runtime state as it will only be called at compile time.
* All logic depending on runtime state must be put into the individual methods handling the events.
*
* @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
*/
public static function getSubscribedEvents()
{
return [
FormEvents::PRE_SET_DATA => 'dependsOn',
FormEvents::PRE_SUBMIT => 'dependsOn'
];
}
public function dependsOn(FormEvent $event){
/** @var FormInterface $form */
$form = $event->getForm();
$data = $event->getData();
// Only proceed if category is set in submitted data
if (isset($data['companyGroup'])) {
$companyGroupSelected = $data['companyGroup'];
$authorizationChecker = $this->_authorizationChecker;
$tokenStorage = $this->_tokenStorage;
$form->add('company', EntityType::class, [
'class' => Company::class,
'query_builder' => function(CompanyRepository $repository) use ($authorizationChecker, $tokenStorage, $companyGroupSelected){
$qb = $repository->createQueryBuilder('cg');
$qb->andWhere($qb->expr()->isInstanceOf('cg', Company::class));
$qb->andWhere($qb->expr()->not($qb->expr()->isInstanceOf('cg', CompanyGroup::class)));
// pokud je
if(false == $authorizationChecker->isGranted('ROLE_ADMIN')){
/** @var User $loggedUser */
$loggedUser = $tokenStorage->getToken()->getUser();
$loggedUserCompany = $loggedUser->getCompany();
$qb->andWhere($qb->expr()->eq('cg.companyGroup', $loggedUserCompany->getId()));
} else {
$qb->andWhere($qb->expr()->eq('cg.companyGroup', $companyGroupSelected));
}
return $qb;
},
'label' => 'Společnost',
'required' => false,
'placeholder' => '- Vyberte -',
]);
}
}
}