src/Controller/EventController.php line 270

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\CMS\CmsContent;
  4. use App\Entity\Event\Characteristic;
  5. use App\Entity\Event\Event;
  6. use App\Entity\Event\EventCharacteristic;
  7. use App\Entity\Event\EventGroup;
  8. use App\Entity\Event\Workshop;
  9. use App\Entity\Parameters\Parameters;
  10. use App\Entity\User;
  11. use App\Entity\User\UserEventSubscribe;
  12. use App\Entity\User\UserEventGroupSubscribe;
  13. use App\Entity\User\UserWorkshopSubscribe;
  14. use App\Form\CMS\CmsOrganisationType;
  15. use App\Form\Company\EventFinalSubscribeType;
  16. use App\Form\Company\EventGroupFinalSubscribeType;
  17. use App\Form\Company\EventProfileType;
  18. use App\Service\EditService;
  19. use App\Service\EventService;
  20. use App\Service\ParametersService;
  21. use App\Service\PlanningService;
  22. use App\Service\UserSeasonService;
  23. use Doctrine\ORM\EntityManagerInterface;
  24. use FOS\RestBundle\Controller\Annotations as Rest;
  25. use FOS\RestBundle\Request\ParamFetcherInterface;
  26. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  27. use Symfony\Component\HttpFoundation\JsonResponse;
  28. use Symfony\Component\HttpFoundation\Request;
  29. use Symfony\Component\HttpFoundation\Response;
  30. use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
  31. use Symfony\Component\Routing\Annotation\Route;
  32. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
  33. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  34. use Symfony\Component\Serializer\Encoder\CsvEncoder;
  35. use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
  36. use Symfony\Component\Serializer\Serializer;
  37. class EventController extends Controller
  38. {
  39.     /**
  40.      * @Route("/type/evenement/modification/{slug}-{id}",
  41.      *   requirements={"slug"=".*","id"="\d*"},
  42.      *   name="event_characteristic_edit"
  43.      * )
  44.      */
  45.     public function editCharacteristic(
  46.         EditService $editService,
  47.         EntityManagerInterface $em,
  48.         Request $request,
  49.         string $slug,
  50.         int $id
  51.     ){
  52.         $roles = ['ROLE_ADMIN_EVENT'];
  53.         $type 'event_characteristic';
  54.         $formClass CmsOrganisationType::class;
  55.         $event $em->getRepository(EventCharacteristic::class)->find($id);
  56.         if ($return $editService->checkEventAuthorization($request$id$slug$roles$type$event)) {
  57.             return $return;
  58.         }
  59.         return $this->render('Event/edit_characteristic.html.twig'$editService->returnForm($request$formClass$type$event));
  60.     }
  61.     /**
  62.      * @Route(
  63.      *   "/type/evenement/{slug}-{id}",
  64.      *   requirements={"slug"=".*","id"="\d*"},
  65.      *   name="event_characteristic_view"
  66.      * )
  67.      */
  68.     public function viewCharacteristic(
  69.         EntityManagerInterface $em,
  70.         $id,
  71.         $slug
  72.     ){
  73.         /** @var $event EventCharacteristic */
  74.         $event $em->getRepository(EventCharacteristic::class)->find($id);
  75.         if (null === $event) {
  76.             throw new NotFoundHttpException('app.error.404');
  77.         }
  78.         if (
  79.             !empty($event->getBaseInformation()->getSlug()) &&
  80.             $event->getBaseInformation()->getSlug() !== $slug
  81.         ){
  82.             return $this->redirectToRoute('event_characteristic_view', [
  83.                 'slug' => $event->getBaseInformation()->getSlug(),
  84.                 'id' => $event->getId()
  85.             ], 301);
  86.         }
  87.         $canEdit false;
  88.         if (
  89.             $this->isGranted('ROLE_ADMIN_EVENT') &&
  90.             (empty($this->getUser()->getTestUserType()) || $this->getUser()->getTestUserType() === 'ADMIN')
  91.         ) {
  92.             $canEdit true;
  93.         }
  94.         return $this->render('Event/view_characteristic.html.twig', [
  95.             'event' => $event'type' => 'event_characteristic''canEdit' => $canEdit,
  96.         ]);
  97.     }
  98.     private function eventEdit(
  99.         ParametersService $parametersService,
  100.         Request $request,
  101.         EntityManagerInterface $em,
  102.         string $type,
  103.         ?int $id null
  104.     ){
  105.         /** @var User $user */
  106.         $user $this->getUser();
  107.         if (null === $id) {
  108.             $event = new Event();
  109.             $event->setBrand($user->getCurrentSeason()->getBrand());
  110.         } else {
  111.             $event $em->getRepository(Event::class)->find($id);
  112.         }
  113.         if (null === $event || !$parametersService->canEdit('event'$event)) {
  114.             throw new NotFoundHttpException('app.error.404');
  115.         }
  116.         if ($event->getCmsDescription() === null) {
  117.             $cms = new CmsContent();
  118.             $event->setCmsDescription($cms);
  119.         }
  120.         if ($event->getCmsOrganisation() === null) {
  121.             $cms = new CmsContent();
  122.             $event->setCmsOrganisation($cms);
  123.         }
  124.         $form $this->createForm(EventProfileType::class, $event, ['characteristicType' => ('special-event' === $type)]);
  125.         if ($request->isMethod('POST') && $eventInformation $request->request->get('event_profile')) {
  126.             if (!isset($eventInformation['cancel']) && $form->handleRequest($request)->isValid()) {
  127.                 $em->persist($event);
  128.                 $em->flush();
  129.                 $request->getSession()->getFlashBag()->add('success''app.user.account.success');
  130.             }
  131.         }
  132.         return $this->render('Event/view.html.twig', ['edit' => true'type' => 'event''event' => $event'form' => $form->createView()]);
  133.         /*
  134.         return $this->render('User/Profile/event_edit.html.twig', [
  135.             'user' => $user,
  136.             'event' => $event,
  137.             'form' => $form->createView(),
  138.         ]);
  139.         */
  140.     }
  141.     /**
  142.      * @Route(
  143.      *   "/evenement/modification/{id}",
  144.      *   requirements={"id"="\d*"},
  145.      *   name="user_event_edit"
  146.      * )
  147.      * @Route(
  148.      *   "/evenement/modification/{id}",
  149.      *   requirements={"id"="\d*"},
  150.      *   name="event_edit"
  151.      * )
  152.      *
  153.      * @Route(
  154.      *   "/evenement/cree",
  155.      *   name="event_create"
  156.      * )
  157.      */
  158.     public function userEventEdit(
  159.         ParametersService $parametersService,
  160.         Request $request,
  161.         EntityManagerInterface $em,
  162.         $id null
  163.     ){
  164.         if ($id) {
  165.             $event $em->getRepository(Event::class)->find($id);
  166.             if (null !== $event && null !== $event->getCharacteristic() && $event->getCharacteristic()->getExtraType()) {
  167.                 return $this->redirectToRoute('extra_event_edit', ['id' => $id]);
  168.             }
  169.         }
  170.         return $this->eventEdit($parametersService$request$em'event'$id);
  171.     }
  172.     /**
  173.      * @Route(
  174.      *   "/special-evenement/modification/{id}",
  175.      *   requirements={"id"="\d*"},
  176.      *   name="extra_event_edit"
  177.      * )
  178.      *
  179.      * @Route(
  180.      *   "/special-evenement/cree",
  181.      *   name="extra_event_create"
  182.      * )
  183.      */
  184.     public function userExtraEventEdit(
  185.         ParametersService $parametersService,
  186.         Request $request,
  187.         EntityManagerInterface $em,
  188.         $id null
  189.     ){
  190.         return $this->eventEdit($parametersService$request$em'special-event'$id);
  191.     }
  192.     /**
  193.      * @Route(
  194.      *   "/evenement/single/export/{slug}-{id}",
  195.      *   requirements={"slug"=".*","id"="\d*"},
  196.      *   name="event_view_export"
  197.      * )
  198.      */
  199.     public function viewExport(
  200.         Request $request,
  201.         EventService $eventService,
  202.         EntityManagerInterface $em,
  203.         UserSeasonService $userSeasonService,
  204.         $id
  205.     ){
  206.         $user $this->getUser();
  207.         /** @var $event Event */
  208.         $event $em->getRepository(Event::class)->find($id);
  209.         $brand $userSeasonService->getBrand($user);
  210.         if (null === $event || $event->getBrand() !== $brand) {
  211.             throw new NotFoundHttpException('app.error.404');
  212.         }
  213.         $stats = [];
  214.         $extraType $event->getCharacteristic()->getExtraType()??false;
  215.         $eventView $eventService->getEventView($request$user'event'$eventnull false$extraType);
  216.         if (!isset($eventView['canEdit']) && !$eventView['canEdit']) {
  217.             $request->getSession()->getFlashBag()->add('warning''app.user.account.unauthorized');
  218.             $route $request->headers->get('referer');
  219.             return $this->redirect($route);
  220.         }
  221.         if (!empty($eventView['userList'])) {
  222.             foreach ($eventView['userList'] as $surname => $choices) {
  223.                 $userIntended '';
  224.                 if (!empty($choices['INTENDED'])&& !empty($choices['INTENDED']['allEvent'])) {
  225.                     foreach ($choices['INTENDED']['allEvent'] as $choiceType => $choiceArray) {
  226.                         $userIntended .= $choiceType ':' count($choiceArray) . ' | ';
  227.                     }
  228.                 }
  229.                 $userFinal '';
  230.                 if (!empty($choices['FINAL'])&& !empty($choices['FINAL']['allEvent'])) {
  231.                     foreach ($choices['FINAL']['allEvent'] as $choiceType => $choiceArray) {
  232.                         $userFinal .= $choiceType ':' count($choiceArray) . ' | ';
  233.                     }
  234.                 }
  235.                 $stats[] = [
  236.                     'surname' => $surname,
  237.                     'userIntended' => $userIntended,
  238.                     'userFinal' => $userFinal
  239.                 ];
  240.             }
  241.         }
  242.         $encoders = [new CsvEncoder()];
  243.         $normalizers = array(new ObjectNormalizer());
  244.         $serializer = new Serializer($normalizers$encoders);
  245.         $csvContent $serializer->serialize($stats'csv');
  246.         $response = new Response($csvContent);
  247.         $response->headers->set('Content-Encoding''UTF-8');
  248.         $response->headers->set('Content-Type''text/csv; charset=UTF-8');
  249.         $response->headers->set('Content-Disposition''attachment; filename=stats.csv');
  250.         return $response;
  251.     }
  252.     /**
  253.      * @Route(
  254.      *   "/evenement/{slug}-{id}",
  255.      *   requirements={"slug"=".*","id"="\d*"},
  256.      *   name="event_view"
  257.      * )
  258.      */
  259.     public function view(
  260.         Request $request,
  261.         EventService $eventService,
  262.         EntityManagerInterface $em,
  263.         UserSeasonService $userSeasonService,
  264.         $id,
  265.         $slug
  266.     ){
  267.         $user $this->getUser();
  268.         /** @var $event Event */
  269.         $event $em->getRepository(Event::class)->find($id);
  270.         $brand $userSeasonService->getBrand($user);
  271.         if (null === $event || $event->getBrand() !== $brand) {
  272.             throw new NotFoundHttpException('app.error.404');
  273.         }
  274.         if (!empty($event->getBaseInformation()->getSlug()) && $event->getBaseInformation()->getSlug() !== $slug) {
  275.             return $this->redirectToRoute('event_view', ['slug' => $event->getBaseInformation()->getSlug(), 'id' => $event->getId()], 301);
  276.         }
  277.         $extraType $event->getCharacteristic()&&$event->getCharacteristic()->getExtraType()??false;
  278.         return $this->render('Event/view.html.twig'$eventService->getEventView($request$user'event'$eventnullfalse$extraType));
  279.     }
  280.     /**
  281.      * @Route(
  282.      *   "/groupe-evenement/modification/{slug}-{id}",
  283.      *   requirements={"slug"=".*","id"="\d*"},
  284.      *   name="user_event_group_edit"
  285.      * )
  286.      * @Route(
  287.      *   "/groupe-evenement/modification/{slug}-{id}",
  288.      *   requirements={"slug"=".*","id"="\d*"},
  289.      *   name="event_group_edit"
  290.      * )
  291.      */
  292.     public function userEventGroupEdit(
  293.         ParametersService $parametersService,
  294.         EventService $eventService,
  295.         Request $request,
  296.         EntityManagerInterface $em,
  297.         string $slug,
  298.         int $id
  299.     ){
  300.         if (
  301.             !$this->isGranted('ROLE_ADMIN_EVENT')
  302.         ) {
  303.             $request->getSession()->getFlashBag()->add('warning''app.user.account.unauthorized');
  304.             return $this->redirectToRoute('event_group_view', ['id' => $id'slug' => $slug]);
  305.         }
  306.         $user $this->getUser();
  307.         $event $em->getRepository(EventGroup::class)->find($id);
  308.         if (null === $event || !$parametersService->canEdit('event'$event)) {
  309.             throw new NotFoundHttpException('app.error.404');
  310.         }
  311.         $form $this->createForm(EventProfileType::class, $event);
  312.         if ($request->isMethod('POST') && $eventInformation $request->request->get('event_profile')) {
  313.             if (!isset($eventInformation['cancel']) && $form->handleRequest($request)->isValid()) {
  314.                 $em->persist($event);
  315.                 $em->flush();
  316.                 $request->getSession()->getFlashBag()->add('success''app.user.account.success');
  317.             }
  318.         }
  319.         $extraType $event->getCharacteristic()->getExtraType()??false;
  320.         return $this->render('Event/view.html.twig',
  321.             array_merge(
  322.                 $eventService->getEventView($request$user'event_group'$eventnulltrue$extraType),
  323.                 ['form' => $form->createView()]
  324.             ));
  325.     }
  326.     /**
  327.      * @Route(
  328.      *   "/groupe-evenement/{slug}-{id}",
  329.      *   requirements={"slug"=".*","id"="\d*"},
  330.      *   name="event_group_view"
  331.      * )
  332.      */
  333.     public function viewGroup(
  334.         Request $request,
  335.         EventService $eventService,
  336.         EntityManagerInterface $em,
  337.         UserSeasonService $userSeasonService,
  338.         $id,
  339.         $slug
  340.     ){
  341.         $user $this->getUser();
  342.         /** @var $event EventGroup */
  343.         $event $em->getRepository(EventGroup::class)->find($id);
  344.         $brand $userSeasonService->getBrand($user);
  345.         if (null === $event || $event->getBrand() !== $brand) {
  346.             throw new NotFoundHttpException('app.error.404');
  347.         }
  348.         if (!empty($event->getBaseInformation()->getSlug()) && $event->getBaseInformation()->getSlug() !== $slug) {
  349.             return $this->redirectToRoute('event_group_view', ['slug' => $event->getBaseInformation()->getSlug(), 'id' => $event->getId()], 301);
  350.         }
  351.         $eventList = [];
  352.         /** @var Event $subEvent */
  353.         foreach ($event->getEvents() as $subEvent) {
  354.             $key = ($subEvent->getEventDate()?$subEvent->getEventDate()->getTimestamp():$subEvent->getId());
  355.             $extraType $subEvent->getCharacteristic()->getExtraType()??false;
  356.             $eventList[$key] = $eventService->getEventView($request$user'event'$subEventnullfalse$extraType);
  357.         }
  358.         $extraType $event->getCharacteristic()->getExtraType()??false;
  359.         $viewArray $eventService->getEventView($request$user'event_group'$eventnullfalse$extraType);
  360.         $viewArray['eventList'] = $eventList;
  361.         return $this->render('Event/view.html.twig'$viewArray);
  362.     }
  363.     /**
  364.      * @Route(
  365.      *   "/evenement/filtre/{id}",
  366.      *   requirements={"id"="\d*"},
  367.      *   name="event_filter"
  368.      * )
  369.      */
  370.     public function eventFilter(Request $requestEntityManagerInterface $emEventService $eventService$id)
  371.     {
  372.         /** @var $event Event */
  373.         $event $em->getRepository(Event::class)->find($id);
  374.         if (null === $event) {
  375.             throw new NotFoundHttpException('app.error.404');
  376.         }
  377.         $filter = !empty($request->request->get('event_filter'))?$request->request->get('event_filter'):[];
  378.         $extraType $event->getCharacteristic()->getExtraType()??false;
  379.         return $this->render(
  380.             'Event/Partials/filter_result.html.twig',
  381.             $eventService->getEventView($request$this->getUser(), 'event'$event$filterfalse$extraType)
  382.         );
  383.     }
  384.     /**
  385.      * @Route(
  386.      *   "/groupe-evenement/filtre/{id}",
  387.      *   requirements={"id"="\d*"},
  388.      *   name="event_group_filter"
  389.      * )
  390.      */
  391.     public function eventGroupFilter(Request $requestEntityManagerInterface $emEventService $eventService$id)
  392.     {
  393.         /** @var $event EventGroup */
  394.         $event $em->getRepository(EventGroup::class)->find($id);
  395.         if (null === $event) {
  396.             throw new NotFoundHttpException('app.error.404');
  397.         }
  398.         $filter = !empty($request->request->get('event_filter'))?$request->request->get('event_filter'):[];
  399.         $extraType $event->getCharacteristic()->getExtraType()??false;
  400.         return $this->render(
  401.             'Event/Partials/filter_result.html.twig',
  402.             $eventService->getEventView($request$this->getUser(), 'event_group'$event$filterfalse$extraType)
  403.         );
  404.     }
  405.     /**
  406.      * @Route(
  407.      *   "/evenement/liste/{slug}",
  408.      *   defaults={"slug"=null},
  409.      *   name="event_list"
  410.      * )
  411.      */
  412.     public function list_user(
  413.         Request $request,
  414.         PlanningService $planningService,
  415.         AuthorizationCheckerInterface $authorizationChecker,
  416.         ?string $slug
  417.     ){
  418.         if (!$authorizationChecker->isGranted('ROLE_USER')) {
  419.             $request->getSession()->getFlashBag()->add('warning''app.event.list.anonymous');
  420.             throw $this->createAccessDeniedException();
  421.         }
  422.         $user $this->getUser();
  423.         if ($slug) {
  424.             $extraType true;
  425.         } else {
  426.             $extraType false;
  427.         }
  428.         return $this->render(
  429.             'Event/list.html.twig',
  430.             $planningService->getUserEvent(
  431.                 ['event''event_group'],
  432.                 false,
  433.                 true,
  434.                 $user,
  435.                 $slug,
  436.                 $extraType
  437.             ));
  438.     }
  439.     /**
  440.      * @Route(
  441.      *   "/evenement/saison/resume",
  442.      *   name="event_all_list"
  443.      * )
  444.      */
  445.     public function list(PlanningService $planningServiceEditService $editServiceUserSeasonService $userSeasonService)
  446.     {
  447.         $brand $userSeasonService->getBrand($this->getUser());
  448.         $roles = ['ROLE_ADMIN_EVENT'];
  449.         return $this->render('Event/list_troupe.html.twig'array_merge(
  450.             ['type' => 'event''checkAuthorization' => $editService->checkAuthorization($roles,$brand),],
  451.             $planningService->getUserEvent(['event'], falsefalse$this->getUser(), nullfalse),
  452.         ));
  453.     }
  454.     /**
  455.      * @Route(
  456.      *   "/evenement/saison/resume/ajax",
  457.      *   name="event_all_list_ajax"
  458.      * )
  459.      */
  460.     public function list_ajax(Request $requestPlanningService $planningServiceEditService $editServiceUserSeasonService $userSeasonService)
  461.     {
  462.         $brand $userSeasonService->getBrand($this->getUser());
  463.         $roles = ['ROLE_ADMIN_EVENT'];
  464.         return $this->render('Event/Partials/list_troupe_ajax.html.twig'array_merge(
  465.             ['type' => 'event''checkAuthorization' => $editService->checkAuthorization($roles,$brand),],
  466.             $planningService->getUserEvent(['event'], falsefalse$this->getUser(), nullfalse$request),
  467.         ));
  468.     }
  469.     /** FIXME: To refacto */
  470.     private function retrieveChoices (
  471.         EntityManagerInterface $em,
  472.         User $user,
  473.         $brand,
  474.         $type,
  475.         $id,
  476.         $column
  477.     ){
  478.         if ($type === 'event') {
  479.             $eventRepository $em->getRepository(Event::class);
  480.             $subscribeRepository $em->getRepository(UserEventSubscribe::class);
  481.         } elseif ($type === 'event_group') {
  482.             $eventRepository $em->getRepository(EventGroup::class);
  483.             $subscribeRepository $em->getRepository(UserEventGroupSubscribe::class);
  484.         } else {
  485.             $eventRepository $em->getRepository(Workshop::class);
  486.             $subscribeRepository $em->getRepository(UserWorkshopSubscribe::class);
  487.         }
  488.         $event $eventRepository->find($id);
  489.         if (null === $id || null === $user || null == $event || empty($brand) || $brand !== $event->getBrand()) {
  490.             return 'error';
  491.         }
  492.         $nonPresentType Parameters::NONPRESENTTYPE;
  493.         $canAddEvent null;
  494.         if ($user->getCurrentSeason()->getSeminar()) {
  495.             $otherEventInRange $subscribeRepository->otherEventInRange($user$event$column);
  496.             if ($otherEventInRange) {
  497.                 foreach ($otherEventInRange as $choices) {
  498.                     $choice $choices->getChoice();
  499.                     foreach ($choice->getParameterTypes() as $parameterType) {
  500.                         if (in_array($parameterType$nonPresentType)) {
  501.                             $canAddEvent true;
  502.                         } else {
  503.                             $canAddEvent false;
  504.                             break;
  505.                         }
  506.                     }
  507.                     if (false === $canAddEvent) {
  508.                         break;
  509.                     }
  510.                 }
  511.             } else {
  512.                 $canAddEvent true;
  513.             }
  514.         } else {
  515.             $canAddEvent true;
  516.         }
  517.         $participationsCount = [];
  518.         $participationsUser = [];
  519.         $postIds = [];
  520.         /** @var UserEventSubscribe $choices */
  521.         foreach ($subscribeRepository->findBy(['event' => $event'type' => $column]) as $choices) {
  522.             $choice $choices->getChoice();
  523.             foreach ($choice->getParameterTypes() as $parameterType) {
  524.                 if (!in_array($parameterType,$nonPresentType)) {
  525.                     if ($user === $choices->getUser()) {
  526.                         $participationsUser[$choices->getType()] = true;
  527.                         $postIds[$choice->getValue()]= ['value' => $choice->getValue(), 'id' => $choice->getId()];
  528.                     }
  529.                     if (null !== $choices->getUser()) {
  530.                         if (empty($participationsCount[$choices->getType()])) {
  531.                             $participationsCount[$choices->getType()][] = $choices->getUser()->getId();
  532.                         } elseif (!in_array($choices->getUser()->getId(), $participationsCount[$choices->getType()])) {
  533.                             $participationsCount[$choices->getType()][] = $choices->getUser()->getId();
  534.                         }
  535.                     }
  536.                 }
  537.             }
  538.         }
  539.         ksort($postIds);
  540.         $limit $event->getAuditorium() && $event->getAuditorium()->getGauge() && $event->getGaugeLimitation() ?
  541.             $event->getAuditorium()->getGauge() : 0;
  542.         $limitedChoices = (bool)(
  543.             $limit &&
  544.             empty($participationsUser[$column]) &&
  545.             !empty($participationsCount[$column]) &&
  546.             $limit <= count($participationsCount[$column])
  547.         );
  548.         $toComplete true;
  549.         if ($limitedChoices) {
  550.             $postIds[] = ['value' => EventService::LIMITED_CHOICES'id'=> -1];
  551.             $toComplete false;
  552.         }
  553.         if (!$canAddEvent) {
  554.             $postIds[] = ['value' => EventService::CANT_ADD_CHOICE'id'=> -1];
  555.             $toComplete false;
  556.         }
  557.         $completeArrays = [];
  558.         if ($toComplete) {
  559.             if ($column === 'INTENDED') {
  560.                 $postChoices $event->getPostIntendeds();
  561.             } else {
  562.                 $postChoices $event->getPostPrevisions();
  563.             }
  564.             foreach ($postChoices as $postChoice) {
  565.                 $completeArrays[$postChoice->getPost()->getValue()]= ['value' => $postChoice->getPost()->getValue(), 'id' => $postChoice->getPost()->getId()];
  566.             }
  567.         }
  568.         ksort($completeArrays);
  569.         $returnPost = [];
  570.         foreach (array_merge($postIds$completeArrays) as $postId) {
  571.             $returnPost[] = $postId;
  572.         }
  573.         return $returnPost;
  574.     }
  575.     /** FIXME: To refacto */
  576.     private function changeStatus (
  577.         EntityManagerInterface $em,
  578.         User $user,
  579.         $brand,
  580.         $type,
  581.         $id,
  582.         $column,
  583.         $parameterChoices
  584.     ){
  585.         if ($type === 'event') {
  586.             $eventRepository $em->getRepository(Event::class);
  587.             $subscribeRepository $em->getRepository(UserEventSubscribe::class);
  588.             $newSubscribe UserEventSubscribe::class;
  589.         } elseif ($type === 'event_group') {
  590.             $eventRepository $em->getRepository(EventGroup::class);
  591.             $subscribeRepository $em->getRepository(UserEventGroupSubscribe::class);
  592.             $newSubscribe UserEventGroupSubscribe::class;
  593.         } else {
  594.             $eventRepository $em->getRepository(Workshop::class);
  595.             $subscribeRepository $em->getRepository(UserWorkshopSubscribe::class);
  596.             $newSubscribe UserWorkshopSubscribe::class;
  597.         }
  598.         $event $eventRepository->find($id);
  599.         if (null === $id || null === $user || null == $event || empty($brand) || $brand !== $event->getBrand()) {
  600.             return 'error';
  601.         }
  602.         $nonPresentType Parameters::NONPRESENTTYPE;
  603.         $canAddEvent null;
  604.         if ($user->getCurrentSeason()->getSeminar()) {
  605.             $otherEventInRange $subscribeRepository->otherEventInRange($user$event$column);
  606.             if ($otherEventInRange) {
  607.                 foreach ($otherEventInRange as $choices) {
  608.                     $choice $choices->getChoice();
  609.                     foreach ($choice->getParameterTypes() as $parameterType) {
  610.                         if (in_array($parameterType$nonPresentType)) {
  611.                             $canAddEvent true;
  612.                         } else {
  613.                             $canAddEvent false;
  614.                             break;
  615.                         }
  616.                     }
  617.                     if (false === $canAddEvent) {
  618.                         break;
  619.                     }
  620.                 }
  621.             } else {
  622.                 $canAddEvent true;
  623.             }
  624.         } else {
  625.             $canAddEvent true;
  626.         }
  627.         $participationsCount = [];
  628.         $participationsUser = [];
  629.         /** @var UserEventSubscribe $choices */
  630.         foreach ($subscribeRepository->findBy(['event' => $event'type' => $column]) as $choices) {
  631.             $choice $choices->getChoice();
  632.             foreach ($choice->getParameterTypes() as $parameterType) {
  633.                 if (!in_array($parameterType,$nonPresentType)) {
  634.                     if ($user === $choices->getUser()) {
  635.                         $participationsUser[$choices->getType()] = true;
  636.                     }
  637.                     if (null !== $choices->getUser()) {
  638.                         if (empty($participationsCount[$choices->getType()])) {
  639.                             $participationsCount[$choices->getType()][] = $choices->getUser()->getId();
  640.                         } elseif (!in_array($choices->getUser()->getId(), $participationsCount[$choices->getType()])) {
  641.                             $participationsCount[$choices->getType()][] = $choices->getUser()->getId();
  642.                         }
  643.                     }
  644.                 }
  645.             }
  646.             if ($user === $choices->getUser()) {
  647.                 $choose false;
  648.                 foreach ($parameterChoices as $choiceKey => $parameter) {
  649.                     if ($choice->getId() === intval($parameter['id'])) {
  650.                         if ($choices->getValidate() !== $parameter['validate']) {
  651.                             $choices->setValidate($parameter['validate']);
  652.                             $em->persist($choices);
  653.                         }
  654.                         $choose true;
  655.                         unset($parameterChoices[$choiceKey]);
  656.                     }
  657.                 }
  658.                 if (!$choose) {
  659.                     $em->remove($choices);
  660.                 }
  661.             }
  662.         }
  663.         $limit $event->getAuditorium() && $event->getAuditorium()->getGauge() && $event->getGaugeLimitation() ?
  664.             $event->getAuditorium()->getGauge() : 0;
  665.         $limitedChoices = (bool)(
  666.             $limit &&
  667.             empty($participationsUser[$column]) &&
  668.             !empty($participationsCount[$column]) &&
  669.             $limit <= count($participationsCount[$column])
  670.         );
  671.         if ($column === 'INTENDED') {
  672.             $postChoices $event->getPostIntendeds();
  673.         } else {
  674.             $postChoices $event->getPostPrevisions();
  675.         }
  676.         $postIds = [];
  677.         foreach ($postChoices as $postChoice) {
  678.             $postIds[$postChoice->getPost()->getId()] = $postChoice->getPost();
  679.         }
  680.         foreach ($parameterChoices as $parameter) {
  681.             $invalideChoice false;
  682.             if (isset($postIds[intval($parameter['id'])])) {
  683.                 $post $postIds[intval($parameter['id'])];
  684.                 if (!$canAddEvent || $limitedChoices) {
  685.                     foreach ($post->getParameterTypes() as $parameterType) {
  686.                         if (!in_array($parameterType$nonPresentType)) {
  687.                             if ($limitedChoices) {
  688.                                 $invalideChoice EventService::LIMITED_CHOICES;
  689.                             } elseif (!$canAddEvent) {
  690.                                 $invalideChoice EventService::CANT_ADD_CHOICE;
  691.                             } else {
  692.                                 $invalideChoice EventService::CHANGE_ERROR;
  693.                             }
  694.                             break;
  695.                         }
  696.                     }
  697.                 }
  698.             } else {
  699.                 $invalideChoice EventService::CHANGE_ERROR;
  700.             }
  701.             if (!$invalideChoice) {
  702.                 $userSubscribe = new $newSubscribe();
  703.                 $userSubscribe->setEvent($event);
  704.                 $userSubscribe->setUser($user);
  705.                 $userSubscribe->setChoice($post);
  706.                 $userSubscribe->setType($column);
  707.                 $userSubscribe->setValidate($parameter['validate']);
  708.                 $em->persist($userSubscribe);
  709.             }
  710.             if ($invalideChoice) {
  711.                 return $invalideChoice;
  712.             }
  713.         }
  714.         $em->flush();
  715.         return 'success';
  716.     }
  717.     /**
  718.      * @Route(
  719.      *   "/evenement/changement-statut",
  720.      *   name="event_change_status"
  721.      * )
  722.      */
  723.     public function eventChangeStatus(Request $requestEntityManagerInterface $emUserSeasonService $userSeasonService)
  724.     {
  725.         $user $this->getUser();
  726.         $brand $userSeasonService->getBrand($user);
  727.         $type $request->request->get('type');
  728.         $id $request->request->get('id');
  729.         $column $request->request->get('column');
  730.         $choices = [];
  731.         $parameterChoices $request->request->get('value');
  732.         if (!empty($parameterChoices)) {
  733.             foreach ($parameterChoices as $parameterChoice) {
  734.                 $choices[] = ['id' => $parameterChoice'validate' => true];
  735.             }
  736.         }
  737.         $error $this->changeStatus($em$user$brand$type$id$column$choices);
  738.         return $this->render('Components/ReturnXHR/success.html.twig', ['error' => $error]);
  739.     }
  740.     /**
  741.      * @Route(
  742.      *   "/evenement/changement-statut-final",
  743.      *   name="event_change_final_status"
  744.      * )
  745.      */
  746.     public function eventChangeFinalStatus(Request $requestEntityManagerInterface $emUserSeasonService $userSeasonService)
  747.     {
  748.         $userChoice $request->request->get('value');
  749.         $user $em->getRepository(User::class)->find($userChoice);
  750.         $brand $userSeasonService->getBrand($user);
  751.         $type $request->request->get('type');
  752.         $id $request->request->get('id');
  753.         $column $request->request->get('column');
  754.         $choices = [];
  755.         $parameterChoices[] = $request->request->get('post_id');
  756.         if (!empty($parameterChoices)) {
  757.             foreach ($parameterChoices as $parameterChoice) {
  758.                 $choices[] = ['id' => $parameterChoice'validate' => true];
  759.             }
  760.         }
  761.         if (null === $user) {
  762.             throw new NotFoundHttpException('app.error.404');
  763.         }
  764.         $error $this->changeStatus($em$user$brand$type$id$column$choices);
  765.         if ($error) {
  766.             throw new NotFoundHttpException('app.error.404');
  767.         }
  768.         return $this->render('Components/ReturnXHR/success.html.twig');
  769.     }
  770.     /**
  771.      * @Route(
  772.      *   "/evenement/validation-statut/{id}",
  773.      *   requirements={"id"="\d*"},
  774.      *   name="event_validation_status"
  775.      * )
  776.      */
  777.     public function eventValidationStatus(
  778.         Request $request,
  779.         EntityManagerInterface $em,
  780.         EventService $eventService,
  781.         $id
  782.     ){
  783.         $event $em->getRepository(Event::class)->find($id);
  784.         if (null === $event) {
  785.             throw new NotFoundHttpException('app.error.404');
  786.         }
  787.         $usersEvents $eventService->getUsersEvents('event'$event);
  788.         $form $this->createForm(EventFinalSubscribeType::class, $event, ['users' => $usersEvents['users']]);
  789.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  790.             $em->persist($event);
  791.             $em->flush();
  792.         }
  793.         return $this->render('Components/ReturnXHR/success.html.twig');
  794.     }
  795.     /**
  796.      * @Route(
  797.      *   "/groupe-evenement/changement-statut",
  798.      *   name="event_group_change_status"
  799.      * )
  800.      */
  801.     public function eventGroupChangeStatus(Request $requestEntityManagerInterface $emUserSeasonService $userSeasonService)
  802.     {
  803.         $user $this->getUser();
  804.         $brand $userSeasonService->getBrand($user);
  805.         $type $request->request->get('type');
  806.         $id $request->request->get('id');
  807.         $column $request->request->get('column');
  808.         $choices = [];
  809.         $parameterChoices $request->request->get('value');
  810.         if (!empty($parameterChoices)) {
  811.             foreach ($parameterChoices as $parameterChoice) {
  812.                 $choices[] = ['id' => $parameterChoice'validate' => true];
  813.             }
  814.         }
  815.         $error $this->changeStatus($em$user$brand$type$id$column$choices);
  816.         if ($error) {
  817.             throw new NotFoundHttpException('app.error.404');
  818.         }
  819.         return $this->render('Components/ReturnXHR/success.html.twig');
  820.     }
  821.     /**
  822.      * @Route(
  823.      *   "/groupe-evenement/changement-statut-final",
  824.      *   name="event_group_change_final_status"
  825.      * )
  826.      */
  827.     public function eventGroupChangeFinalStatus(Request $requestEntityManagerInterface $emUserSeasonService $userSeasonService)
  828.     {
  829.         $userChoice $request->request->get('value');
  830.         $user $em->getRepository(User::class)->find($userChoice);
  831.         $brand $userSeasonService->getBrand($user);
  832.         $type $request->request->get('type');
  833.         $id $request->request->get('id');
  834.         $column $request->request->get('column');
  835.         $choices = [];
  836.         $parameterChoices $request->request->get('post_id');
  837.         if (!empty($parameterChoices)) {
  838.             foreach ($parameterChoices as $parameterChoice) {
  839.                 $choices[] = ['id' => $parameterChoice'validate' => true];
  840.             }
  841.         }
  842.         if (null === $user) {
  843.             throw new NotFoundHttpException('app.error.404');
  844.         }
  845.         $error $this->changeStatus($em$user$brand$type$id$column$choices);
  846.         if ($error) {
  847.             throw new NotFoundHttpException('app.error.404');
  848.         }
  849.         return $this->render('Components/ReturnXHR/success.html.twig');
  850.     }
  851.     /**
  852.      * @Route(
  853.      *   "/groupe-evenement/validation-statut/{id}",
  854.      *   requirements={"id"="\d*"},
  855.      *   name="event_group_validation_status"
  856.      * )
  857.      */
  858.     public function eventGroupValidationStatus(
  859.         Request $request,
  860.         EntityManagerInterface $em,
  861.         EventService $eventService,
  862.         $id
  863.     ){
  864.         $event $em->getRepository(Event::class)->find($id);
  865.         if (null === $event) {
  866.             throw new NotFoundHttpException('app.error.404');
  867.         }
  868.         $usersEvents $eventService->getUsersEvents('event_group'$event);
  869.         $form $this->createForm(EventGroupFinalSubscribeType::class, $event, ['users' => $usersEvents['users']]);
  870.         if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
  871.             $em->persist($event);
  872.             $em->flush();
  873.         }
  874.         return $this->render('Components/ReturnXHR/success.html.twig');
  875.     }
  876.     /**
  877.      * @Rest\View()
  878.      * @Rest\Post("/recherche/autocomplete")
  879.      * @Rest\RequestParam(name="type", requirements=".*", default="", description="Type d'évènement")
  880.      * @Rest\RequestParam(name="id", requirements=".*", default="", description="ID de l'évènement")
  881.      * @Rest\RequestParam(name="column", requirements=".*", default="", description="Colonne de choix")
  882.      */
  883.     public function searchEventChoice(
  884.         ParamFetcherInterface $paramFetcher,
  885.         EntityManagerInterface $em
  886.     ){
  887.         /** @var User $user */
  888.         $user $this->getUser();
  889.         $brand $user->getCurrentSeason()->getBrand();
  890.         return new JsonResponse(
  891.             ['message' => $this->retrieveChoices(
  892.                 $em,
  893.                 $user,
  894.                 $brand,
  895.                 $paramFetcher->get('type'),
  896.                 $paramFetcher->get('id'),
  897.                 $paramFetcher->get('column')
  898.             )],
  899.             Response::HTTP_OK
  900.         );
  901.     }
  902.     /**
  903.      * @Route(
  904.      *   "/evenement/export/csv",
  905.      *   name="export_event_csv"
  906.      * )
  907.      */
  908.     public function exportEventCsv(
  909.         Request $request,
  910.         EditService $editService,
  911.         UserSeasonService $userSeasonService,
  912.         PlanningService $planningService
  913.     ){
  914.         $brand $userSeasonService->getBrand($this->getUser());
  915.         $roles = ['ROLE_ADMIN_EVENT'];
  916.         $checkAuthorization $editService->checkAuthorization($roles,$brand);
  917.         if (!$checkAuthorization) {
  918.             $request->getSession()->getFlashBag()->add('warning''app.user.account.unauthorized');
  919.             $route $request->headers->get('referer');
  920.             return $this->redirect($route);
  921.         }
  922.         $userEvents $planningService->getUserEvent(['event'], falsefalse$this->getUser(), nullfalse);
  923.         $events = [];
  924.         foreach ($userEvents['events'] as $eventArray) {
  925.             $eventType $eventArray['type'];
  926.             /** @var Event $event */
  927.             $event $eventArray['event'];
  928.             $currentEventArray = [
  929.                 'eventId'   => $event->getId(),
  930.                 'eventName' => $event->getBaseInformation()->getName(),
  931.                 'eventType' => $event->getCharacteristic()->getBaseInformation()->getName(),
  932.                 'eventStart' => $event->getEventDate()->format('Y-m-d H:i:s'),
  933.                 'eventEnd' => $event->getEventEndDate()->format('Y-m-d H:i:s'),
  934.             ];
  935.             /** @var User $user */
  936.             foreach ($userEvents['users'] as $user) {
  937.                 $currentEventArray['userName'] = $user->getSurname();
  938.                 $userChoices $userEvents['userChoices'];
  939.                 if (
  940.                     $userChoices[$eventType] and
  941.                     !empty($userChoices[$eventType][$user->getId()]) and
  942.                     !empty($userChoices[$eventType][$user->getId()][$event->getId()])
  943.                 ) {
  944.                     $choices $userChoices[$eventType][$user->getId()][$event->getId()];
  945.                     $currentEventArray['userIntended'] = empty($choices['INTENDED'])?'':implode(' | ',$choices['INTENDED']);
  946.                     $currentEventArray['userFinal'] = empty($choices['FINAL'])?'':implode(' | ',$choices['FINAL']);
  947.                 } else {
  948.                     $currentEventArray['userIntended'] = '';
  949.                     $currentEventArray['userFinal'] = '';
  950.                 }
  951.                 $events[] = $currentEventArray;
  952.             }
  953.         }
  954.         $encoders = [new CsvEncoder()];
  955.         $normalizers = array(new ObjectNormalizer());
  956.         $serializer = new Serializer($normalizers$encoders);
  957.         $csvContent $serializer->serialize($events'csv');
  958.         $response = new Response($csvContent);
  959.         $response->headers->set('Content-Encoding''UTF-8');
  960.         $response->headers->set('Content-Type''text/csv; charset=UTF-8');
  961.         $response->headers->set('Content-Disposition''attachment; filename=season_resume.csv');
  962.         return $response;
  963.     }
  964. }