3 namespace Rapsys\AirBundle\Controller
;
5 use Symfony\Component\HttpFoundation\Request
;
6 use Symfony\Component\Routing\RequestContext
;
7 use Symfony\Component\Form\FormError
;
8 use Rapsys\AirBundle\Entity\Slot
;
9 use Rapsys\AirBundle\Entity\User
;
10 use Rapsys\AirBundle\Entity\Session
;
11 use Rapsys\AirBundle\Entity\Application
;
13 class ApplicationController
extends DefaultController
{
17 * @desc Persist application and all required dependencies in database
19 * @param Request $request The request instance
21 * @return Response The rendered view or redirection
23 * @throws \RuntimeException When user has not at least guest role
25 public function add(Request
$request) {
26 //Prevent non-guest to access here
27 $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator
->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator
->trans('Guest')]));
29 //Create ApplicationType form
30 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
32 'action' => $this->generateUrl('rapsys_air_application_add'),
33 //Set the form attribute
34 #'attr' => [ 'class' => 'col' ],
36 'admin' => $this->isGranted('ROLE_ADMIN'),
37 //Set default user to current
38 'user' => $this->getUser()->getId(),
39 //Set default slot to evening
40 //XXX: default to Evening (3)
41 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3)
44 //Reject non post requests
45 if (!$request->isMethod('POST')) {
46 throw new \
RuntimeException('Request method MUST be POST');
49 //Refill the fields in case of invalid form
50 $form->handleRequest($request);
53 if (!$form->isValid()) {
55 $section = $this->translator
->trans('Application Add');
58 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
61 return $this->render('@RapsysAir/application/add.html.twig', ['title' => $title, 'section' => $section, 'form' => $form]+
$this->context
);
65 $doctrine = $this->getDoctrine();
68 $manager = $doctrine->getManager();
71 $data = $form->getData();
73 //Protect session fetching
76 $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
77 //Catch no session case
78 } catch (\Doctrine\ORM\NoResultException
$e) {
80 $session = new Session();
81 $session->setLocation($data['location']);
82 $session->setDate($data['date']);
83 $session->setSlot($data['slot']);
84 $session->setCreated(new \
DateTime('now'));
85 $session->setUpdated(new \
DateTime('now'));
88 $manager->persist($session);
90 //Flush to get the ids
93 $this->addFlash('notice', $this->translator
->trans('Session on %date% %location% %slot% created', ['%location%' => $this->translator
->trans('at '.$data['location']), '%slot%' => $this->translator
->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
97 $user = $this->getUser();
99 //Replace with requested user for admin
100 if ($this->isGranted('ROLE_ADMIN') && !empty($data['user'])) {
101 $user = $this->getDoctrine()->getRepository(User
::class)->findOneById($data['user']);
104 //Protect application fetching
106 //Retrieve application
107 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $user);
109 //Add notice in flash message
110 $this->addFlash('warning', $this->translator
->trans('Application on %date% %location% %slot% already exists', ['%location%' => $this->translator
->trans('at '.$data['location']), '%slot%' => $this->translator
->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
111 //Catch no application and session without identifier (not persisted&flushed) cases
112 } catch (\Doctrine\ORM\NoResultException
|\Doctrine\ORM\ORMInvalidArgumentException
$e) {
113 //Create the application
114 $application = new Application();
115 $application->setSession($session);
116 $application->setUser($user);
117 $application->setCreated(new \
DateTime('now'));
118 $application->setUpdated(new \
DateTime('now'));
120 //Refresh session updated field
121 $session->setUpdated(new \
DateTime('now'));
124 $manager->persist($session);
126 //Queue application save
127 $manager->persist($application);
129 //Flush to get the ids
132 //Add notice in flash message
133 $this->addFlash('notice', $this->translator
->trans('Application on %date% %location% %slot% created', ['%location%' => $this->translator
->trans('at '.$data['location']), '%slot%' => $this->translator
->trans('the '.strtolower($data['slot'])), '%date%' => $data['date']->format('Y-m-d')]));
136 //Extract and process referer
137 if ($referer = $request->headers
->get('referer')) {
138 //Create referer request instance
139 $req = Request
::create($referer);
142 $path = $req->getPathInfo();
144 //Get referer query string
145 $query = $req->getQueryString();
148 $path = str_replace($request->getScriptName(), '', $path);
150 //Try with referer path
153 $oldContext = $this->router
->getContext();
155 //Force clean context
156 //XXX: prevent MethodNotAllowedException because current context method is POST in onevendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php+42
157 $this->router
->setContext(new RequestContext());
159 //Retrieve route matching path
160 $route = $this->router
->match($path);
163 $this->router
->setContext($oldContext);
169 $name = $route['_route'];
171 //Remove route and controller from route defaults
172 unset($route['_route'], $route['_controller']);
175 return $this->redirectToRoute($name, ['session' => $session->getId()]+
$route);
177 } catch(ResourceNotFoundException
$e) {
178 //Unset referer to fallback to default route
183 //Redirect to cleanup the form
184 return $this->redirectToRoute('rapsys_air', ['session' => $session->getId()]);