src/EventSubscriber/TwigEventSubscriber.php line 25

Open in your IDE?
  1. <?php
  2. namespace App\EventSubscriber;
  3. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  4. use Symfony\Component\HttpKernel\Event\ControllerEvent;
  5. use App\Repository\ChatRepository;
  6. use App\Repository\UserRepository;
  7. use App\Repository\SubjectRepository;
  8. use App\Repository\TheClassRepository;
  9. use Symfony\Component\Security\Core\Security;
  10. use Twig\Environment;
  11. class TwigEventSubscriber implements EventSubscriberInterface
  12. {
  13.     public function __construct(
  14.         private ChatRepository $chatRepo,
  15.         private Security $security,
  16.         private Environment $twig,
  17.         private UserRepository $userRepo,
  18.         private SubjectRepository $subjectRepo,
  19.         private TheClassRepository $classRepo
  20.     ){}
  21.     
  22.     public function onControllerEvent(ControllerEvent $event): void
  23.     {
  24.         $user $this->security->getUser();
  25.         
  26.         if ($user) {
  27.             $school $user->getSchool();
  28.             $profClasses $this->subjectRepo->findProfClasses($user->getId());
  29.             $this->twig->addGlobal('profClasses'$profClasses);
  30.             $this->twig->addGlobal('allChat'$this->chatRepo->findByUser($user->getId()));
  31.             $this->twig->addGlobal('chatList'$this->ShowListForAllowedChat());
  32.             $this->twig->addGlobal('allClasses'$this->classRepo->getClassByLevel());
  33.             // TriĆ©es par nom (et non par id) pour les menus de la sidebar qui listent les classes.
  34.             $this->twig->addGlobal('all_classes'$this->classRepo->findBy(['school' => $school], ['name' => 'ASC']));
  35.             $this->twig->addGlobal('myClasses'$this->classRepo->findByTeacherSorted($user->getId()));
  36.         }
  37.        // dd($this->chatRepo->findByUser($user->getId()));
  38.     }
  39.     public function ShowListForAllowedChat()
  40.     {
  41.         $user $this->security->getUser();
  42.         if ($user) {
  43.             $type $user->getUserType();
  44.             if ($type == 'prof') {
  45.                 return $list $this->userRepo->findByUserTypes('parent''admin'$user->getId());
  46.             } elseif ($type == 'admin') {
  47.             return $list $this->userRepo->findByNameAsc($user->getId());
  48.             }
  49.         }
  50.     }
  51.     public static function getSubscribedEvents(): array
  52.     {
  53.         return [
  54.             ControllerEvent::class => 'onControllerEvent',
  55.         ];
  56.     }
  57. }