<?php
namespace App\Controller;
use App\Entity\CMS\CmsContent;
use App\Entity\Event\Characteristic;
use App\Entity\Event\Event;
use App\Entity\Event\EventCharacteristic;
use App\Entity\Event\EventGroup;
use App\Entity\Event\Workshop;
use App\Entity\Parameters\Parameters;
use App\Entity\User;
use App\Entity\User\UserEventSubscribe;
use App\Entity\User\UserEventGroupSubscribe;
use App\Entity\User\UserWorkshopSubscribe;
use App\Form\CMS\CmsOrganisationType;
use App\Form\Company\EventFinalSubscribeType;
use App\Form\Company\EventGroupFinalSubscribeType;
use App\Form\Company\EventProfileType;
use App\Service\EditService;
use App\Service\EventService;
use App\Service\ParametersService;
use App\Service\PlanningService;
use App\Service\UserSeasonService;
use Doctrine\ORM\EntityManagerInterface;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
use Symfony\Component\Serializer\Encoder\CsvEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;
class EventController extends Controller
{
/**
* @Route("/type/evenement/modification/{slug}-{id}",
* requirements={"slug"=".*","id"="\d*"},
* name="event_characteristic_edit"
* )
*/
public function editCharacteristic(
EditService $editService,
EntityManagerInterface $em,
Request $request,
string $slug,
int $id
){
$roles = ['ROLE_ADMIN_EVENT'];
$type = 'event_characteristic';
$formClass = CmsOrganisationType::class;
$event = $em->getRepository(EventCharacteristic::class)->find($id);
if ($return = $editService->checkEventAuthorization($request, $id, $slug, $roles, $type, $event)) {
return $return;
}
return $this->render('Event/edit_characteristic.html.twig', $editService->returnForm($request, $formClass, $type, $event));
}
/**
* @Route(
* "/type/evenement/{slug}-{id}",
* requirements={"slug"=".*","id"="\d*"},
* name="event_characteristic_view"
* )
*/
public function viewCharacteristic(
EntityManagerInterface $em,
$id,
$slug
){
/** @var $event EventCharacteristic */
$event = $em->getRepository(EventCharacteristic::class)->find($id);
if (null === $event) {
throw new NotFoundHttpException('app.error.404');
}
if (
!empty($event->getBaseInformation()->getSlug()) &&
$event->getBaseInformation()->getSlug() !== $slug
){
return $this->redirectToRoute('event_characteristic_view', [
'slug' => $event->getBaseInformation()->getSlug(),
'id' => $event->getId()
], 301);
}
$canEdit = false;
if (
$this->isGranted('ROLE_ADMIN_EVENT') &&
(empty($this->getUser()->getTestUserType()) || $this->getUser()->getTestUserType() === 'ADMIN')
) {
$canEdit = true;
}
return $this->render('Event/view_characteristic.html.twig', [
'event' => $event, 'type' => 'event_characteristic', 'canEdit' => $canEdit,
]);
}
private function eventEdit(
ParametersService $parametersService,
Request $request,
EntityManagerInterface $em,
string $type,
?int $id = null
){
/** @var User $user */
$user = $this->getUser();
if (null === $id) {
$event = new Event();
$event->setBrand($user->getCurrentSeason()->getBrand());
} else {
$event = $em->getRepository(Event::class)->find($id);
}
if (null === $event || !$parametersService->canEdit('event', $event)) {
throw new NotFoundHttpException('app.error.404');
}
if ($event->getCmsDescription() === null) {
$cms = new CmsContent();
$event->setCmsDescription($cms);
}
if ($event->getCmsOrganisation() === null) {
$cms = new CmsContent();
$event->setCmsOrganisation($cms);
}
$form = $this->createForm(EventProfileType::class, $event, ['characteristicType' => ('special-event' === $type)]);
if ($request->isMethod('POST') && $eventInformation = $request->request->get('event_profile')) {
if (!isset($eventInformation['cancel']) && $form->handleRequest($request)->isValid()) {
$em->persist($event);
$em->flush();
$request->getSession()->getFlashBag()->add('success', 'app.user.account.success');
}
}
return $this->render('Event/view.html.twig', ['edit' => true, 'type' => 'event', 'event' => $event, 'form' => $form->createView()]);
/*
return $this->render('User/Profile/event_edit.html.twig', [
'user' => $user,
'event' => $event,
'form' => $form->createView(),
]);
*/
}
/**
* @Route(
* "/evenement/modification/{id}",
* requirements={"id"="\d*"},
* name="user_event_edit"
* )
* @Route(
* "/evenement/modification/{id}",
* requirements={"id"="\d*"},
* name="event_edit"
* )
*
* @Route(
* "/evenement/cree",
* name="event_create"
* )
*/
public function userEventEdit(
ParametersService $parametersService,
Request $request,
EntityManagerInterface $em,
$id = null
){
if ($id) {
$event = $em->getRepository(Event::class)->find($id);
if (null !== $event && null !== $event->getCharacteristic() && $event->getCharacteristic()->getExtraType()) {
return $this->redirectToRoute('extra_event_edit', ['id' => $id]);
}
}
return $this->eventEdit($parametersService, $request, $em, 'event', $id);
}
/**
* @Route(
* "/special-evenement/modification/{id}",
* requirements={"id"="\d*"},
* name="extra_event_edit"
* )
*
* @Route(
* "/special-evenement/cree",
* name="extra_event_create"
* )
*/
public function userExtraEventEdit(
ParametersService $parametersService,
Request $request,
EntityManagerInterface $em,
$id = null
){
return $this->eventEdit($parametersService, $request, $em, 'special-event', $id);
}
/**
* @Route(
* "/evenement/single/export/{slug}-{id}",
* requirements={"slug"=".*","id"="\d*"},
* name="event_view_export"
* )
*/
public function viewExport(
Request $request,
EventService $eventService,
EntityManagerInterface $em,
UserSeasonService $userSeasonService,
$id
){
$user = $this->getUser();
/** @var $event Event */
$event = $em->getRepository(Event::class)->find($id);
$brand = $userSeasonService->getBrand($user);
if (null === $event || $event->getBrand() !== $brand) {
throw new NotFoundHttpException('app.error.404');
}
$stats = [];
$extraType = $event->getCharacteristic()->getExtraType()??false;
$eventView = $eventService->getEventView($request, $user, 'event', $event, null , false, $extraType);
if (!isset($eventView['canEdit']) && !$eventView['canEdit']) {
$request->getSession()->getFlashBag()->add('warning', 'app.user.account.unauthorized');
$route = $request->headers->get('referer');
return $this->redirect($route);
}
if (!empty($eventView['userList'])) {
foreach ($eventView['userList'] as $surname => $choices) {
$userIntended = '';
if (!empty($choices['INTENDED'])&& !empty($choices['INTENDED']['allEvent'])) {
foreach ($choices['INTENDED']['allEvent'] as $choiceType => $choiceArray) {
$userIntended .= $choiceType . ':' . count($choiceArray) . ' | ';
}
}
$userFinal = '';
if (!empty($choices['FINAL'])&& !empty($choices['FINAL']['allEvent'])) {
foreach ($choices['FINAL']['allEvent'] as $choiceType => $choiceArray) {
$userFinal .= $choiceType . ':' . count($choiceArray) . ' | ';
}
}
$stats[] = [
'surname' => $surname,
'userIntended' => $userIntended,
'userFinal' => $userFinal
];
}
}
$encoders = [new CsvEncoder()];
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$csvContent = $serializer->serialize($stats, 'csv');
$response = new Response($csvContent);
$response->headers->set('Content-Encoding', 'UTF-8');
$response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
$response->headers->set('Content-Disposition', 'attachment; filename=stats.csv');
return $response;
}
/**
* @Route(
* "/evenement/{slug}-{id}",
* requirements={"slug"=".*","id"="\d*"},
* name="event_view"
* )
*/
public function view(
Request $request,
EventService $eventService,
EntityManagerInterface $em,
UserSeasonService $userSeasonService,
$id,
$slug
){
$user = $this->getUser();
/** @var $event Event */
$event = $em->getRepository(Event::class)->find($id);
$brand = $userSeasonService->getBrand($user);
if (null === $event || $event->getBrand() !== $brand) {
throw new NotFoundHttpException('app.error.404');
}
if (!empty($event->getBaseInformation()->getSlug()) && $event->getBaseInformation()->getSlug() !== $slug) {
return $this->redirectToRoute('event_view', ['slug' => $event->getBaseInformation()->getSlug(), 'id' => $event->getId()], 301);
}
$extraType = $event->getCharacteristic()&&$event->getCharacteristic()->getExtraType()??false;
return $this->render('Event/view.html.twig', $eventService->getEventView($request, $user, 'event', $event, null, false, $extraType));
}
/**
* @Route(
* "/groupe-evenement/modification/{slug}-{id}",
* requirements={"slug"=".*","id"="\d*"},
* name="user_event_group_edit"
* )
* @Route(
* "/groupe-evenement/modification/{slug}-{id}",
* requirements={"slug"=".*","id"="\d*"},
* name="event_group_edit"
* )
*/
public function userEventGroupEdit(
ParametersService $parametersService,
EventService $eventService,
Request $request,
EntityManagerInterface $em,
string $slug,
int $id
){
if (
!$this->isGranted('ROLE_ADMIN_EVENT')
) {
$request->getSession()->getFlashBag()->add('warning', 'app.user.account.unauthorized');
return $this->redirectToRoute('event_group_view', ['id' => $id, 'slug' => $slug]);
}
$user = $this->getUser();
$event = $em->getRepository(EventGroup::class)->find($id);
if (null === $event || !$parametersService->canEdit('event', $event)) {
throw new NotFoundHttpException('app.error.404');
}
$form = $this->createForm(EventProfileType::class, $event);
if ($request->isMethod('POST') && $eventInformation = $request->request->get('event_profile')) {
if (!isset($eventInformation['cancel']) && $form->handleRequest($request)->isValid()) {
$em->persist($event);
$em->flush();
$request->getSession()->getFlashBag()->add('success', 'app.user.account.success');
}
}
$extraType = $event->getCharacteristic()->getExtraType()??false;
return $this->render('Event/view.html.twig',
array_merge(
$eventService->getEventView($request, $user, 'event_group', $event, null, true, $extraType),
['form' => $form->createView()]
));
}
/**
* @Route(
* "/groupe-evenement/{slug}-{id}",
* requirements={"slug"=".*","id"="\d*"},
* name="event_group_view"
* )
*/
public function viewGroup(
Request $request,
EventService $eventService,
EntityManagerInterface $em,
UserSeasonService $userSeasonService,
$id,
$slug
){
$user = $this->getUser();
/** @var $event EventGroup */
$event = $em->getRepository(EventGroup::class)->find($id);
$brand = $userSeasonService->getBrand($user);
if (null === $event || $event->getBrand() !== $brand) {
throw new NotFoundHttpException('app.error.404');
}
if (!empty($event->getBaseInformation()->getSlug()) && $event->getBaseInformation()->getSlug() !== $slug) {
return $this->redirectToRoute('event_group_view', ['slug' => $event->getBaseInformation()->getSlug(), 'id' => $event->getId()], 301);
}
$eventList = [];
/** @var Event $subEvent */
foreach ($event->getEvents() as $subEvent) {
$key = ($subEvent->getEventDate()?$subEvent->getEventDate()->getTimestamp():$subEvent->getId());
$extraType = $subEvent->getCharacteristic()->getExtraType()??false;
$eventList[$key] = $eventService->getEventView($request, $user, 'event', $subEvent, null, false, $extraType);
}
$extraType = $event->getCharacteristic()->getExtraType()??false;
$viewArray = $eventService->getEventView($request, $user, 'event_group', $event, null, false, $extraType);
$viewArray['eventList'] = $eventList;
return $this->render('Event/view.html.twig', $viewArray);
}
/**
* @Route(
* "/evenement/filtre/{id}",
* requirements={"id"="\d*"},
* name="event_filter"
* )
*/
public function eventFilter(Request $request, EntityManagerInterface $em, EventService $eventService, $id)
{
/** @var $event Event */
$event = $em->getRepository(Event::class)->find($id);
if (null === $event) {
throw new NotFoundHttpException('app.error.404');
}
$filter = !empty($request->request->get('event_filter'))?$request->request->get('event_filter'):[];
$extraType = $event->getCharacteristic()->getExtraType()??false;
return $this->render(
'Event/Partials/filter_result.html.twig',
$eventService->getEventView($request, $this->getUser(), 'event', $event, $filter, false, $extraType)
);
}
/**
* @Route(
* "/groupe-evenement/filtre/{id}",
* requirements={"id"="\d*"},
* name="event_group_filter"
* )
*/
public function eventGroupFilter(Request $request, EntityManagerInterface $em, EventService $eventService, $id)
{
/** @var $event EventGroup */
$event = $em->getRepository(EventGroup::class)->find($id);
if (null === $event) {
throw new NotFoundHttpException('app.error.404');
}
$filter = !empty($request->request->get('event_filter'))?$request->request->get('event_filter'):[];
$extraType = $event->getCharacteristic()->getExtraType()??false;
return $this->render(
'Event/Partials/filter_result.html.twig',
$eventService->getEventView($request, $this->getUser(), 'event_group', $event, $filter, false, $extraType)
);
}
/**
* @Route(
* "/evenement/liste/{slug}",
* defaults={"slug"=null},
* name="event_list"
* )
*/
public function list_user(
Request $request,
PlanningService $planningService,
AuthorizationCheckerInterface $authorizationChecker,
?string $slug
){
if (!$authorizationChecker->isGranted('ROLE_USER')) {
$request->getSession()->getFlashBag()->add('warning', 'app.event.list.anonymous');
throw $this->createAccessDeniedException();
}
$user = $this->getUser();
if ($slug) {
$extraType = true;
} else {
$extraType = false;
}
return $this->render(
'Event/list.html.twig',
$planningService->getUserEvent(
['event', 'event_group'],
false,
true,
$user,
$slug,
$extraType
));
}
/**
* @Route(
* "/evenement/saison/resume",
* name="event_all_list"
* )
*/
public function list(PlanningService $planningService, EditService $editService, UserSeasonService $userSeasonService)
{
$brand = $userSeasonService->getBrand($this->getUser());
$roles = ['ROLE_ADMIN_EVENT'];
return $this->render('Event/list_troupe.html.twig', array_merge(
['type' => 'event', 'checkAuthorization' => $editService->checkAuthorization($roles,$brand),],
$planningService->getUserEvent(['event'], false, false, $this->getUser(), null, false),
));
}
/**
* @Route(
* "/evenement/saison/resume/ajax",
* name="event_all_list_ajax"
* )
*/
public function list_ajax(Request $request, PlanningService $planningService, EditService $editService, UserSeasonService $userSeasonService)
{
$brand = $userSeasonService->getBrand($this->getUser());
$roles = ['ROLE_ADMIN_EVENT'];
return $this->render('Event/Partials/list_troupe_ajax.html.twig', array_merge(
['type' => 'event', 'checkAuthorization' => $editService->checkAuthorization($roles,$brand),],
$planningService->getUserEvent(['event'], false, false, $this->getUser(), null, false, $request),
));
}
/** FIXME: To refacto */
private function retrieveChoices (
EntityManagerInterface $em,
User $user,
$brand,
$type,
$id,
$column
){
if ($type === 'event') {
$eventRepository = $em->getRepository(Event::class);
$subscribeRepository = $em->getRepository(UserEventSubscribe::class);
} elseif ($type === 'event_group') {
$eventRepository = $em->getRepository(EventGroup::class);
$subscribeRepository = $em->getRepository(UserEventGroupSubscribe::class);
} else {
$eventRepository = $em->getRepository(Workshop::class);
$subscribeRepository = $em->getRepository(UserWorkshopSubscribe::class);
}
$event = $eventRepository->find($id);
if (null === $id || null === $user || null == $event || empty($brand) || $brand !== $event->getBrand()) {
return 'error';
}
$nonPresentType = Parameters::NONPRESENTTYPE;
$canAddEvent = null;
if ($user->getCurrentSeason()->getSeminar()) {
$otherEventInRange = $subscribeRepository->otherEventInRange($user, $event, $column);
if ($otherEventInRange) {
foreach ($otherEventInRange as $choices) {
$choice = $choices->getChoice();
foreach ($choice->getParameterTypes() as $parameterType) {
if (in_array($parameterType, $nonPresentType)) {
$canAddEvent = true;
} else {
$canAddEvent = false;
break;
}
}
if (false === $canAddEvent) {
break;
}
}
} else {
$canAddEvent = true;
}
} else {
$canAddEvent = true;
}
$participationsCount = [];
$participationsUser = [];
$postIds = [];
/** @var UserEventSubscribe $choices */
foreach ($subscribeRepository->findBy(['event' => $event, 'type' => $column]) as $choices) {
$choice = $choices->getChoice();
foreach ($choice->getParameterTypes() as $parameterType) {
if (!in_array($parameterType,$nonPresentType)) {
if ($user === $choices->getUser()) {
$participationsUser[$choices->getType()] = true;
$postIds[$choice->getValue()]= ['value' => $choice->getValue(), 'id' => $choice->getId()];
}
if (null !== $choices->getUser()) {
if (empty($participationsCount[$choices->getType()])) {
$participationsCount[$choices->getType()][] = $choices->getUser()->getId();
} elseif (!in_array($choices->getUser()->getId(), $participationsCount[$choices->getType()])) {
$participationsCount[$choices->getType()][] = $choices->getUser()->getId();
}
}
}
}
}
ksort($postIds);
$limit = $event->getAuditorium() && $event->getAuditorium()->getGauge() && $event->getGaugeLimitation() ?
$event->getAuditorium()->getGauge() : 0;
$limitedChoices = (bool)(
$limit > 0 &&
empty($participationsUser[$column]) &&
!empty($participationsCount[$column]) &&
$limit <= count($participationsCount[$column])
);
$toComplete = true;
if ($limitedChoices) {
$postIds[] = ['value' => EventService::LIMITED_CHOICES, 'id'=> -1];
$toComplete = false;
}
if (!$canAddEvent) {
$postIds[] = ['value' => EventService::CANT_ADD_CHOICE, 'id'=> -1];
$toComplete = false;
}
$completeArrays = [];
if ($toComplete) {
if ($column === 'INTENDED') {
$postChoices = $event->getPostIntendeds();
} else {
$postChoices = $event->getPostPrevisions();
}
foreach ($postChoices as $postChoice) {
$completeArrays[$postChoice->getPost()->getValue()]= ['value' => $postChoice->getPost()->getValue(), 'id' => $postChoice->getPost()->getId()];
}
}
ksort($completeArrays);
$returnPost = [];
foreach (array_merge($postIds, $completeArrays) as $postId) {
$returnPost[] = $postId;
}
return $returnPost;
}
/** FIXME: To refacto */
private function changeStatus (
EntityManagerInterface $em,
User $user,
$brand,
$type,
$id,
$column,
$parameterChoices
){
if ($type === 'event') {
$eventRepository = $em->getRepository(Event::class);
$subscribeRepository = $em->getRepository(UserEventSubscribe::class);
$newSubscribe = UserEventSubscribe::class;
} elseif ($type === 'event_group') {
$eventRepository = $em->getRepository(EventGroup::class);
$subscribeRepository = $em->getRepository(UserEventGroupSubscribe::class);
$newSubscribe = UserEventGroupSubscribe::class;
} else {
$eventRepository = $em->getRepository(Workshop::class);
$subscribeRepository = $em->getRepository(UserWorkshopSubscribe::class);
$newSubscribe = UserWorkshopSubscribe::class;
}
$event = $eventRepository->find($id);
if (null === $id || null === $user || null == $event || empty($brand) || $brand !== $event->getBrand()) {
return 'error';
}
$nonPresentType = Parameters::NONPRESENTTYPE;
$canAddEvent = null;
if ($user->getCurrentSeason()->getSeminar()) {
$otherEventInRange = $subscribeRepository->otherEventInRange($user, $event, $column);
if ($otherEventInRange) {
foreach ($otherEventInRange as $choices) {
$choice = $choices->getChoice();
foreach ($choice->getParameterTypes() as $parameterType) {
if (in_array($parameterType, $nonPresentType)) {
$canAddEvent = true;
} else {
$canAddEvent = false;
break;
}
}
if (false === $canAddEvent) {
break;
}
}
} else {
$canAddEvent = true;
}
} else {
$canAddEvent = true;
}
$participationsCount = [];
$participationsUser = [];
/** @var UserEventSubscribe $choices */
foreach ($subscribeRepository->findBy(['event' => $event, 'type' => $column]) as $choices) {
$choice = $choices->getChoice();
foreach ($choice->getParameterTypes() as $parameterType) {
if (!in_array($parameterType,$nonPresentType)) {
if ($user === $choices->getUser()) {
$participationsUser[$choices->getType()] = true;
}
if (null !== $choices->getUser()) {
if (empty($participationsCount[$choices->getType()])) {
$participationsCount[$choices->getType()][] = $choices->getUser()->getId();
} elseif (!in_array($choices->getUser()->getId(), $participationsCount[$choices->getType()])) {
$participationsCount[$choices->getType()][] = $choices->getUser()->getId();
}
}
}
}
if ($user === $choices->getUser()) {
$choose = false;
foreach ($parameterChoices as $choiceKey => $parameter) {
if ($choice->getId() === intval($parameter['id'])) {
if ($choices->getValidate() !== $parameter['validate']) {
$choices->setValidate($parameter['validate']);
$em->persist($choices);
}
$choose = true;
unset($parameterChoices[$choiceKey]);
}
}
if (!$choose) {
$em->remove($choices);
}
}
}
$limit = $event->getAuditorium() && $event->getAuditorium()->getGauge() && $event->getGaugeLimitation() ?
$event->getAuditorium()->getGauge() : 0;
$limitedChoices = (bool)(
$limit > 0 &&
empty($participationsUser[$column]) &&
!empty($participationsCount[$column]) &&
$limit <= count($participationsCount[$column])
);
if ($column === 'INTENDED') {
$postChoices = $event->getPostIntendeds();
} else {
$postChoices = $event->getPostPrevisions();
}
$postIds = [];
foreach ($postChoices as $postChoice) {
$postIds[$postChoice->getPost()->getId()] = $postChoice->getPost();
}
foreach ($parameterChoices as $parameter) {
$invalideChoice = false;
if (isset($postIds[intval($parameter['id'])])) {
$post = $postIds[intval($parameter['id'])];
if (!$canAddEvent || $limitedChoices) {
foreach ($post->getParameterTypes() as $parameterType) {
if (!in_array($parameterType, $nonPresentType)) {
if ($limitedChoices) {
$invalideChoice = EventService::LIMITED_CHOICES;
} elseif (!$canAddEvent) {
$invalideChoice = EventService::CANT_ADD_CHOICE;
} else {
$invalideChoice = EventService::CHANGE_ERROR;
}
break;
}
}
}
} else {
$invalideChoice = EventService::CHANGE_ERROR;
}
if (!$invalideChoice) {
$userSubscribe = new $newSubscribe();
$userSubscribe->setEvent($event);
$userSubscribe->setUser($user);
$userSubscribe->setChoice($post);
$userSubscribe->setType($column);
$userSubscribe->setValidate($parameter['validate']);
$em->persist($userSubscribe);
}
if ($invalideChoice) {
return $invalideChoice;
}
}
$em->flush();
return 'success';
}
/**
* @Route(
* "/evenement/changement-statut",
* name="event_change_status"
* )
*/
public function eventChangeStatus(Request $request, EntityManagerInterface $em, UserSeasonService $userSeasonService)
{
$user = $this->getUser();
$brand = $userSeasonService->getBrand($user);
$type = $request->request->get('type');
$id = $request->request->get('id');
$column = $request->request->get('column');
$choices = [];
$parameterChoices = $request->request->get('value');
if (!empty($parameterChoices)) {
foreach ($parameterChoices as $parameterChoice) {
$choices[] = ['id' => $parameterChoice, 'validate' => true];
}
}
$error = $this->changeStatus($em, $user, $brand, $type, $id, $column, $choices);
return $this->render('Components/ReturnXHR/success.html.twig', ['error' => $error]);
}
/**
* @Route(
* "/evenement/changement-statut-final",
* name="event_change_final_status"
* )
*/
public function eventChangeFinalStatus(Request $request, EntityManagerInterface $em, UserSeasonService $userSeasonService)
{
$userChoice = $request->request->get('value');
$user = $em->getRepository(User::class)->find($userChoice);
$brand = $userSeasonService->getBrand($user);
$type = $request->request->get('type');
$id = $request->request->get('id');
$column = $request->request->get('column');
$choices = [];
$parameterChoices[] = $request->request->get('post_id');
if (!empty($parameterChoices)) {
foreach ($parameterChoices as $parameterChoice) {
$choices[] = ['id' => $parameterChoice, 'validate' => true];
}
}
if (null === $user) {
throw new NotFoundHttpException('app.error.404');
}
$error = $this->changeStatus($em, $user, $brand, $type, $id, $column, $choices);
if ($error) {
throw new NotFoundHttpException('app.error.404');
}
return $this->render('Components/ReturnXHR/success.html.twig');
}
/**
* @Route(
* "/evenement/validation-statut/{id}",
* requirements={"id"="\d*"},
* name="event_validation_status"
* )
*/
public function eventValidationStatus(
Request $request,
EntityManagerInterface $em,
EventService $eventService,
$id
){
$event = $em->getRepository(Event::class)->find($id);
if (null === $event) {
throw new NotFoundHttpException('app.error.404');
}
$usersEvents = $eventService->getUsersEvents('event', $event);
$form = $this->createForm(EventFinalSubscribeType::class, $event, ['users' => $usersEvents['users']]);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$em->persist($event);
$em->flush();
}
return $this->render('Components/ReturnXHR/success.html.twig');
}
/**
* @Route(
* "/groupe-evenement/changement-statut",
* name="event_group_change_status"
* )
*/
public function eventGroupChangeStatus(Request $request, EntityManagerInterface $em, UserSeasonService $userSeasonService)
{
$user = $this->getUser();
$brand = $userSeasonService->getBrand($user);
$type = $request->request->get('type');
$id = $request->request->get('id');
$column = $request->request->get('column');
$choices = [];
$parameterChoices = $request->request->get('value');
if (!empty($parameterChoices)) {
foreach ($parameterChoices as $parameterChoice) {
$choices[] = ['id' => $parameterChoice, 'validate' => true];
}
}
$error = $this->changeStatus($em, $user, $brand, $type, $id, $column, $choices);
if ($error) {
throw new NotFoundHttpException('app.error.404');
}
return $this->render('Components/ReturnXHR/success.html.twig');
}
/**
* @Route(
* "/groupe-evenement/changement-statut-final",
* name="event_group_change_final_status"
* )
*/
public function eventGroupChangeFinalStatus(Request $request, EntityManagerInterface $em, UserSeasonService $userSeasonService)
{
$userChoice = $request->request->get('value');
$user = $em->getRepository(User::class)->find($userChoice);
$brand = $userSeasonService->getBrand($user);
$type = $request->request->get('type');
$id = $request->request->get('id');
$column = $request->request->get('column');
$choices = [];
$parameterChoices = $request->request->get('post_id');
if (!empty($parameterChoices)) {
foreach ($parameterChoices as $parameterChoice) {
$choices[] = ['id' => $parameterChoice, 'validate' => true];
}
}
if (null === $user) {
throw new NotFoundHttpException('app.error.404');
}
$error = $this->changeStatus($em, $user, $brand, $type, $id, $column, $choices);
if ($error) {
throw new NotFoundHttpException('app.error.404');
}
return $this->render('Components/ReturnXHR/success.html.twig');
}
/**
* @Route(
* "/groupe-evenement/validation-statut/{id}",
* requirements={"id"="\d*"},
* name="event_group_validation_status"
* )
*/
public function eventGroupValidationStatus(
Request $request,
EntityManagerInterface $em,
EventService $eventService,
$id
){
$event = $em->getRepository(Event::class)->find($id);
if (null === $event) {
throw new NotFoundHttpException('app.error.404');
}
$usersEvents = $eventService->getUsersEvents('event_group', $event);
$form = $this->createForm(EventGroupFinalSubscribeType::class, $event, ['users' => $usersEvents['users']]);
if ($request->isMethod('POST') && $form->handleRequest($request)->isValid()) {
$em->persist($event);
$em->flush();
}
return $this->render('Components/ReturnXHR/success.html.twig');
}
/**
* @Rest\View()
* @Rest\Post("/recherche/autocomplete")
* @Rest\RequestParam(name="type", requirements=".*", default="", description="Type d'évènement")
* @Rest\RequestParam(name="id", requirements=".*", default="", description="ID de l'évènement")
* @Rest\RequestParam(name="column", requirements=".*", default="", description="Colonne de choix")
*/
public function searchEventChoice(
ParamFetcherInterface $paramFetcher,
EntityManagerInterface $em
){
/** @var User $user */
$user = $this->getUser();
$brand = $user->getCurrentSeason()->getBrand();
return new JsonResponse(
['message' => $this->retrieveChoices(
$em,
$user,
$brand,
$paramFetcher->get('type'),
$paramFetcher->get('id'),
$paramFetcher->get('column')
)],
Response::HTTP_OK
);
}
/**
* @Route(
* "/evenement/export/csv",
* name="export_event_csv"
* )
*/
public function exportEventCsv(
Request $request,
EditService $editService,
UserSeasonService $userSeasonService,
PlanningService $planningService
){
$brand = $userSeasonService->getBrand($this->getUser());
$roles = ['ROLE_ADMIN_EVENT'];
$checkAuthorization = $editService->checkAuthorization($roles,$brand);
if (!$checkAuthorization) {
$request->getSession()->getFlashBag()->add('warning', 'app.user.account.unauthorized');
$route = $request->headers->get('referer');
return $this->redirect($route);
}
$userEvents = $planningService->getUserEvent(['event'], false, false, $this->getUser(), null, false);
$events = [];
foreach ($userEvents['events'] as $eventArray) {
$eventType = $eventArray['type'];
/** @var Event $event */
$event = $eventArray['event'];
$currentEventArray = [
'eventId' => $event->getId(),
'eventName' => $event->getBaseInformation()->getName(),
'eventType' => $event->getCharacteristic()->getBaseInformation()->getName(),
'eventStart' => $event->getEventDate()->format('Y-m-d H:i:s'),
'eventEnd' => $event->getEventEndDate()->format('Y-m-d H:i:s'),
];
/** @var User $user */
foreach ($userEvents['users'] as $user) {
$currentEventArray['userName'] = $user->getSurname();
$userChoices = $userEvents['userChoices'];
if (
$userChoices[$eventType] and
!empty($userChoices[$eventType][$user->getId()]) and
!empty($userChoices[$eventType][$user->getId()][$event->getId()])
) {
$choices = $userChoices[$eventType][$user->getId()][$event->getId()];
$currentEventArray['userIntended'] = empty($choices['INTENDED'])?'':implode(' | ',$choices['INTENDED']);
$currentEventArray['userFinal'] = empty($choices['FINAL'])?'':implode(' | ',$choices['FINAL']);
} else {
$currentEventArray['userIntended'] = '';
$currentEventArray['userFinal'] = '';
}
$events[] = $currentEventArray;
}
}
$encoders = [new CsvEncoder()];
$normalizers = array(new ObjectNormalizer());
$serializer = new Serializer($normalizers, $encoders);
$csvContent = $serializer->serialize($events, 'csv');
$response = new Response($csvContent);
$response->headers->set('Content-Encoding', 'UTF-8');
$response->headers->set('Content-Type', 'text/csv; charset=UTF-8');
$response->headers->set('Content-Disposition', 'attachment; filename=season_resume.csv');
return $response;
}
}