<?php
namespace App\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use App\Repository\ChatRepository;
use App\Repository\UserRepository;
use App\Repository\SubjectRepository;
use App\Repository\TheClassRepository;
use Symfony\Component\Security\Core\Security;
use Twig\Environment;
class TwigEventSubscriber implements EventSubscriberInterface
{
public function __construct(
private ChatRepository $chatRepo,
private Security $security,
private Environment $twig,
private UserRepository $userRepo,
private SubjectRepository $subjectRepo,
private TheClassRepository $classRepo
){}
public function onControllerEvent(ControllerEvent $event): void
{
$user = $this->security->getUser();
if ($user) {
$school = $user->getSchool();
$profClasses = $this->subjectRepo->findProfClasses($user->getId());
$this->twig->addGlobal('profClasses', $profClasses);
$this->twig->addGlobal('allChat', $this->chatRepo->findByUser($user->getId()));
$this->twig->addGlobal('chatList', $this->ShowListForAllowedChat());
$this->twig->addGlobal('allClasses', $this->classRepo->getClassByLevel());
$this->twig->addGlobal('all_classes', $this->classRepo->findBy(['school' => $school]));
}
// dd($this->chatRepo->findByUser($user->getId()));
}
public function ShowListForAllowedChat()
{
$user = $this->security->getUser();
if ($user) {
$type = $user->getUserType();
if ($type == 'prof') {
return $list = $this->userRepo->findByUserTypes('parent', 'admin', $user->getId());
} elseif ($type == 'admin') {
return $list = $this->userRepo->findByNameAsc($user->getId());
}
}
}
public static function getSubscribedEvents(): array
{
return [
ControllerEvent::class => 'onControllerEvent',
];
}
}