src/Form/Filter/Subscriber/InvoiceAutomationFilterSubscriber.php line 65

Open in your IDE?
  1. <?php
  2. namespace App\Form\Filter\Subscriber;
  3. use App\Entity\Company;
  4. use App\Entity\CompanyGroup;
  5. use App\Entity\User;
  6. use App\Repository\CompanyRepository;
  7. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\Form\FormEvent;
  10. use Symfony\Component\Form\FormEvents;
  11. use Symfony\Component\Form\FormInterface;
  12. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  13. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  14. class InvoiceAutomationFilterSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var AuthorizationCheckerInterface */
  17.     protected $_authorizationChecker;
  18.     /** @var TokenStorageInterface */
  19.     protected $_tokenStorage;
  20.     /**
  21.      * @param AuthorizationCheckerInterface $_authorizationChecker
  22.      * @param TokenStorageInterface $_tokenStorage
  23.      */
  24.     public function __construct(AuthorizationCheckerInterface $_authorizationCheckerTokenStorageInterface $_tokenStorage)
  25.     {
  26.         $this->_authorizationChecker $_authorizationChecker;
  27.         $this->_tokenStorage $_tokenStorage;
  28.     }
  29.     /**
  30.      * Returns an array of event names this subscriber wants to listen to.
  31.      *
  32.      * The array keys are event names and the value can be:
  33.      *
  34.      *  * The method name to call (priority defaults to 0)
  35.      *  * An array composed of the method name to call and the priority
  36.      *  * An array of arrays composed of the method names to call and respective
  37.      *    priorities, or 0 if unset
  38.      *
  39.      * For instance:
  40.      *
  41.      *  * ['eventName' => 'methodName']
  42.      *  * ['eventName' => ['methodName', $priority]]
  43.      *  * ['eventName' => [['methodName1', $priority], ['methodName2']]]
  44.      *
  45.      * The code must not depend on runtime state as it will only be called at compile time.
  46.      * All logic depending on runtime state must be put into the individual methods handling the events.
  47.      *
  48.      * @return array<string, string|array{0: string, 1: int}|list<array{0: string, 1?: int}>>
  49.      */
  50.     public static function getSubscribedEvents()
  51.     {
  52.         return [
  53.             FormEvents::PRE_SET_DATA => 'dependsOn',
  54.             FormEvents::PRE_SUBMIT => 'dependsOn'
  55.         ];
  56.     }
  57.     public function dependsOn(FormEvent $event){
  58.         /** @var FormInterface $form */
  59.         $form $event->getForm();
  60.         $data $event->getData();
  61.         // Only proceed if category is set in submitted data
  62.         if (isset($data['companyGroup'])) {
  63.             $companyGroupSelected $data['companyGroup'];
  64.             $authorizationChecker $this->_authorizationChecker;
  65.             $tokenStorage $this->_tokenStorage;
  66.             $form->add('company'EntityType::class, [
  67.                 'class' => Company::class,
  68.                 'query_builder' => function(CompanyRepository $repository) use ($authorizationChecker$tokenStorage$companyGroupSelected){
  69.                     $qb $repository->createQueryBuilder('cg');
  70.                     $qb->andWhere($qb->expr()->isInstanceOf('cg'Company::class));
  71.                     $qb->andWhere($qb->expr()->not($qb->expr()->isInstanceOf('cg'CompanyGroup::class)));
  72.                     // pokud je
  73.                     if(false == $authorizationChecker->isGranted('ROLE_ADMIN')){
  74.                         /** @var User $loggedUser */
  75.                         $loggedUser $tokenStorage->getToken()->getUser();
  76.                         $loggedUserCompany $loggedUser->getCompany();
  77.                         $qb->andWhere($qb->expr()->eq('cg.companyGroup'$loggedUserCompany->getId()));
  78.                     } else {
  79.                         $qb->andWhere($qb->expr()->eq('cg.companyGroup'$companyGroupSelected));
  80.                     }
  81.                     return $qb;
  82.                 },
  83.                 'label' => 'Společnost',
  84.                 'required' => false,
  85.                 'placeholder' => '- Vyberte -',
  86.             ]);
  87.         }
  88.     }
  89. }