3 namespace Rapsys\AirBundle\Controller
;
5 use Symfony\Component\HttpFoundation\Request
;
6 use Rapsys\AirBundle\Entity\Slot
;
8 class LocationController
extends DefaultController
{
12 * @desc Display all sessions by location with an application or login form
14 * @param Request $request The request instance
16 * @return Response The rendered view
18 public function index(Request
$request = null) {
20 $section = $this->translator
->trans('Locations');
23 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
28 //Create application form for role_guest
29 if ($this->isGranted('ROLE_GUEST')) {
30 //Create ApplicationType form
31 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
33 'action' => $this->generateUrl('rapsys_air_application_add'),
34 //Set the form attribute
35 'attr' => [ 'class' => 'col' ],
37 'admin' => $this->isGranted('ROLE_ADMIN'),
38 //Set default user to current
39 'user' => $this->getUser()->getId(),
40 //Set default slot to evening
41 //XXX: default to Evening (3)
42 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3),
43 //Set shorted return url
44 'return' => $this->slugger
->short(json_encode(['_route' => $request->get('_route'), '_route_params' => $request->get('_route_params')]))
48 $context['application'] = $application->createView();
49 //Create login form for anonymous
50 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
51 //Create ApplicationType form
52 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
54 'action' => $this->generateUrl('rapsys_user_login'),
55 //Set the form attribute
56 'attr' => [ 'class' => 'col' ]
60 $context['login'] = $login->createView();
66 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+
$context+
$this->context
);
69 public function show(Request
$request, $id) {
72 public function toto(Request
$request) {
73 //Prevent non-admin to access here
74 $this->denyAccessUnlessGranted('ROLE_GUEST', null, 'Unable to access this page without ROLE_GUEST!');
77 $section = $this->translator
->trans('Admin');
80 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
82 header('Content-Type: text/plain');
85 //Create the form according to the FormType created previously.
86 //And give the proper parameters
87 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
88 // To set the action use $this->generateUrl('route_identifier')
89 'action' => $this->generateUrl('rapsys_air_admin'),
91 'attr' => [ 'class' => 'form_col' ]
95 $doctrine = $this->getDoctrine();
98 if ($request->isMethod('POST')) {
99 // Refill the fields in case the form is not valid.
100 $form->handleRequest($request);
102 if ($form->isValid()) {
104 $data = $form->getData();
107 $manager = $doctrine->getManager();
109 //Protect session fetching
111 $session = $doctrine->getRepository(Session
::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
112 //Catch no session case
113 } catch (\Doctrine\ORM\NoResultException
$e) {
115 $session = new Session();
116 $session->setLocation($data['location']);
117 $session->setSlot($data['slot']);
118 $session->setDate($data['date']);
119 $session->setCreated(new \
DateTime('now'));
120 $session->setUpdated(new \
DateTime('now'));
121 $manager->persist($session);
122 //Flush to get the ids
127 $application = false;
129 //Protect application fetching
131 //TODO: handle admin case where we provide a user in extra
132 $application = $doctrine->getRepository(Application
::class)->findOneBySessionUser($session, $this->getUser());
134 //Add error message to mail field
135 $form->get('slot')->addError(new FormError($this->translator
->trans('Application already exists')));
136 //Catch no application cases
137 //XXX: combine these catch when php 7.1 is available
138 } catch (\Doctrine\ORM\NoResultException
$e) {
139 //Catch invalid argument because session is not already persisted
140 } catch(\Doctrine\ORM\ORMInvalidArgumentException
$e) {
143 //Create new application if none found
145 //Create the application
146 $application = new Application();
147 $application->setSession($session);
148 //TODO: handle admin case where we provide a user in extra
149 $application->setUser($this->getUser());
150 $application->setCreated(new \
DateTime('now'));
151 $application->setUpdated(new \
DateTime('now'));
152 $manager->persist($application);
154 //Flush to get the ids
157 //Add notice in flash message
158 $this->addFlash('notice', $this->translator
->trans('Application request the %date% for %location% on the slot %slot% saved', ['%location%' => $data['location']->getTitle(), '%slot%' => $data['slot']->getTitle(), '%date%' => $data['date']->format('Y-m-d')]));
160 //Redirect to cleanup the form
161 return $this->redirectToRoute('rapsys_air_admin');
167 $period = new \
DatePeriod(
168 //Start from first monday of week
169 new \
DateTime('Monday this week'),
170 //Iterate on each day
171 new \
DateInterval('P1D'),
172 //End with next sunday and 4 weeks
173 new \
DateTime('Monday this week + 5 week')
177 $sessions = $doctrine->getRepository(Session
::class)->findAllByDatePeriod($period);
185 //Iterate on each day
186 foreach($period as $date) {
187 //Init day in calendar
188 $calendar[$Ymd = $date->format('Ymd')] = [
189 'title' => $date->format('d'),
193 //Append month for first day of month
194 if ($month != $date->format('m')) {
195 $month = $date->format('m');
196 $calendar[$Ymd]['title'] .= '/'.$month;
199 if ($date->format('U') == ($today = strtotime('today'))) {
200 $calendar[$Ymd]['title'] .= '/'.$month;
201 $calendar[$Ymd]['current'] = true;
202 $calendar[$Ymd]['class'][] = 'current';
204 //Disable passed days
205 if ($date->format('U') < $today) {
206 $calendar[$Ymd]['disabled'] = true;
207 $calendar[$Ymd]['class'][] = 'disabled';
209 //Set next month days
210 if ($date->format('m') > date('m')) {
211 $calendar[$Ymd]['next'] = true;
212 $calendar[$Ymd]['class'][] = 'next';
214 //Iterate on each session to find the one of the day
215 foreach($sessions as $session) {
216 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) {
217 //Count number of application
218 $count = count($session->getApplications());
222 if ($session->getApplication()) {
223 $class[] = 'granted';
224 } elseif ($count == 0) {
225 $class[] = 'orphaned';
226 } elseif ($count > 1) {
227 $class[] = 'disputed';
229 $class[] = 'pending';
233 $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [
234 'id' => $session->getId(),
235 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(),
242 ksort($calendar[$Ymd]['sessions']);
245 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]);
248 public function tata(Request
$request, $id) {
249 /*header('Content-Type: text/plain');
254 $section = $this->translator
->trans('Session %id%', ['%id%' => $id]);
257 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
259 //Create the form according to the FormType created previously.
260 //And give the proper parameters
261 /*$form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
262 // To set the action use $this->generateUrl('route_identifier')
263 'action' => $this->generateUrl('rapsys_air_admin'),
265 'attr' => [ 'class' => 'form_col' ]
269 $doctrine = $this->getDoctrine();
272 $session = $doctrine->getRepository(Session
::class)->findOneById($id);
274 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);