|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace AppBundle\Controller\Admin\Talk; |
| 6 | + |
| 7 | +use AppBundle\Event\AdminEventSelection; |
| 8 | +use AppBundle\Event\Model\Repository\TalkRepository; |
| 9 | +use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
| 10 | +use Symfony\Component\Form\Extension\Core\Type\CheckboxType; |
| 11 | +use Symfony\Component\Form\Extension\Core\Type\FormType; |
| 12 | +use Symfony\Component\Form\Extension\Core\Type\HiddenType; |
| 13 | +use Symfony\Component\Form\Extension\Core\Type\SubmitType; |
| 14 | +use Symfony\Component\Form\Extension\Core\Type\TextType; |
| 15 | +use Symfony\Component\Form\FormFactoryInterface; |
| 16 | +use Symfony\Component\Form\FormInterface; |
| 17 | +use Symfony\Component\HttpFoundation\Request; |
| 18 | +use Symfony\Component\HttpFoundation\Response; |
| 19 | + |
| 20 | +final class IndexAction extends AbstractController |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + private readonly FormFactoryInterface $formFactory, |
| 24 | + private readonly TalkRepository $talkRepository, |
| 25 | + ) {} |
| 26 | + |
| 27 | + public function __invoke(Request $request, AdminEventSelection $eventSelection): Response |
| 28 | + { |
| 29 | + //TODO : à supprimer quand les actions via le formulaire auront été migrées |
| 30 | + if (isset($_SESSION['flash']['message'])) { |
| 31 | + $this->addFlash('notice', $_SESSION['flash']['message']); |
| 32 | + } |
| 33 | + if (isset($_SESSION['flash']['erreur'])) { |
| 34 | + $this->addFlash('error', $_SESSION['flash']['erreur']); |
| 35 | + } |
| 36 | + unset($_SESSION['flash']); |
| 37 | + |
| 38 | + $event = $eventSelection->event; |
| 39 | + |
| 40 | + $data = [ |
| 41 | + 'id' => $event->getId(), |
| 42 | + ]; |
| 43 | + $filterForm = $this->filterForm($data); |
| 44 | + $filterForm->handleRequest($request); |
| 45 | + |
| 46 | + if ($filterForm->isSubmitted() && $filterForm->isValid()) { |
| 47 | + $data = $filterForm->getData(); |
| 48 | + $data = array_filter($data); |
| 49 | + } |
| 50 | + |
| 51 | + $data['sort_key'] ??= 'talk.date_soumission'; |
| 52 | + $data['sort_direction'] ??= 'asc'; |
| 53 | + |
| 54 | + $sessions = $this->talkRepository->getByEventWithSpeakersAndVotes( |
| 55 | + event: $event, |
| 56 | + search: $data['q'] ?? '', |
| 57 | + orderBy: $data['sort_key'] . ' ' . $data['sort_direction'], |
| 58 | + planned: $data['planned'] ?? false, |
| 59 | + needMentoring: $data['needs_mentoring'] ?? false, |
| 60 | + ); |
| 61 | + |
| 62 | + return $this->render('admin/talk/index.html.twig', [ |
| 63 | + 'event' => $event, |
| 64 | + 'filter_form' => $filterForm, |
| 65 | + 'filter' => $data, |
| 66 | + 'event_select_form' => $eventSelection->selectForm(), |
| 67 | + 'sessions' => $sessions, |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + private function filterForm(array $data): FormInterface |
| 72 | + { |
| 73 | + return $this->formFactory->createNamedBuilder('', FormType::class, $data, [ |
| 74 | + 'csrf_protection' => false, |
| 75 | + ]) |
| 76 | + ->setMethod('GET') |
| 77 | + ->add('q', TextType::class, ['required' => false]) |
| 78 | + ->add('needs_mentoring', CheckboxType::class, ['required' => false]) |
| 79 | + ->add('planned', CheckboxType::class, ['required' => false]) |
| 80 | + ->add('id', HiddenType::class, ['required' => false]) |
| 81 | + ->add('sort_key', HiddenType::class, ['required' => false]) |
| 82 | + ->add('sort_direction', HiddenType::class, ['required' => false]) |
| 83 | + ->add('submit', SubmitType::class) |
| 84 | + ->getForm(); |
| 85 | + } |
| 86 | +} |
0 commit comments