]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/LocationController.php
e692ade9623a93042973e1c0facf4ffe26e4ba13
[airbundle] / Controller / LocationController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6
7 class LocationController extends DefaultController {
8
9 public function index(Request $request = null) {
10 $title = 'title';
11 $section = 'section';
12 $calendar = 'toto';
13 //Create the form according to the FormType created previously.
14 //And give the proper parameters
15 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
16 // To set the action use $this->generateUrl('route_identifier')
17 'action' => $this->generateUrl('rapsys_air_location'),
18 'method' => 'POST',
19 'attr' => [ 'class' => 'form_col' ]
20 ]);
21 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]+$this->context);
22 }
23
24 public function show(Request $request, $id) {
25 }
26
27 public function toto(Request $request) {
28 //Prevent non-admin to access here
29 $this->denyAccessUnlessGranted('ROLE_GUEST', null, 'Unable to access this page without ROLE_GUEST!');
30
31 //Set section
32 $section = $this->translator->trans('Admin');
33
34 //Set title
35 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
36
37 header('Content-Type: text/plain');
38 var_dump('TODO');
39 exit;
40 //Create the form according to the FormType created previously.
41 //And give the proper parameters
42 $form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
43 // To set the action use $this->generateUrl('route_identifier')
44 'action' => $this->generateUrl('rapsys_air_admin'),
45 'method' => 'POST',
46 'attr' => [ 'class' => 'form_col' ]
47 ]);
48
49 //Get doctrine
50 $doctrine = $this->getDoctrine();
51
52 //Handle request
53 if ($request->isMethod('POST')) {
54 // Refill the fields in case the form is not valid.
55 $form->handleRequest($request);
56
57 if ($form->isValid()) {
58 //Get data
59 $data = $form->getData();
60
61 //Get manager
62 $manager = $doctrine->getManager();
63
64 //Protect session fetching
65 try {
66 $session = $doctrine->getRepository(Session::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
67 //Catch no session case
68 } catch (\Doctrine\ORM\NoResultException $e) {
69 //Create the session
70 $session = new Session();
71 $session->setLocation($data['location']);
72 $session->setSlot($data['slot']);
73 $session->setDate($data['date']);
74 $session->setCreated(new \DateTime('now'));
75 $session->setUpdated(new \DateTime('now'));
76 $manager->persist($session);
77 //Flush to get the ids
78 #$manager->flush();
79 }
80
81 //Init application
82 $application = false;
83
84 //Protect application fetching
85 try {
86 //TODO: handle admin case where we provide a user in extra
87 $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $this->getUser());
88
89 //Add error message to mail field
90 $form->get('slot')->addError(new FormError($this->translator->trans('Application already exists')));
91 //Catch no application cases
92 //XXX: combine these catch when php 7.1 is available
93 } catch (\Doctrine\ORM\NoResultException $e) {
94 //Catch invalid argument because session is not already persisted
95 } catch(\Doctrine\ORM\ORMInvalidArgumentException $e) {
96 }
97
98 //Create new application if none found
99 if (!$application) {
100 //Create the application
101 $application = new Application();
102 $application->setSession($session);
103 //TODO: handle admin case where we provide a user in extra
104 $application->setUser($this->getUser());
105 $application->setCreated(new \DateTime('now'));
106 $application->setUpdated(new \DateTime('now'));
107 $manager->persist($application);
108
109 //Flush to get the ids
110 $manager->flush();
111
112 //Add notice in flash message
113 $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')]));
114
115 //Redirect to cleanup the form
116 return $this->redirectToRoute('rapsys_air_admin');
117 }
118 }
119 }
120
121 //Compute period
122 $period = new \DatePeriod(
123 //Start from first monday of week
124 new \DateTime('Monday this week'),
125 //Iterate on each day
126 new \DateInterval('P1D'),
127 //End with next sunday and 4 weeks
128 new \DateTime('Monday this week + 5 week')
129 );
130
131 //Fetch sessions
132 $sessions = $doctrine->getRepository(Session::class)->findByDatePeriod($period);
133
134 //Init calendar
135 $calendar = [];
136
137 //Init month
138 $month = null;
139
140 //Iterate on each day
141 foreach($period as $date) {
142 //Init day in calendar
143 $calendar[$Ymd = $date->format('Ymd')] = [
144 'title' => $date->format('d'),
145 'class' => [],
146 'sessions' => []
147 ];
148 //Append month for first day of month
149 if ($month != $date->format('m')) {
150 $month = $date->format('m');
151 $calendar[$Ymd]['title'] .= '/'.$month;
152 }
153 //Deal with today
154 if ($date->format('U') == ($today = strtotime('today'))) {
155 $calendar[$Ymd]['title'] .= '/'.$month;
156 $calendar[$Ymd]['current'] = true;
157 $calendar[$Ymd]['class'][] = 'current';
158 }
159 //Disable passed days
160 if ($date->format('U') < $today) {
161 $calendar[$Ymd]['disabled'] = true;
162 $calendar[$Ymd]['class'][] = 'disabled';
163 }
164 //Set next month days
165 if ($date->format('m') > date('m')) {
166 $calendar[$Ymd]['next'] = true;
167 $calendar[$Ymd]['class'][] = 'next';
168 }
169 //Iterate on each session to find the one of the day
170 foreach($sessions as $session) {
171 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) {
172 //Count number of application
173 $count = count($session->getApplications());
174
175 //Compute classes
176 $class = [];
177 if ($session->getApplication()) {
178 $class[] = 'granted';
179 } elseif ($count == 0) {
180 $class[] = 'orphaned';
181 } elseif ($count > 1) {
182 $class[] = 'disputed';
183 } else {
184 $class[] = 'pending';
185 }
186
187 //Add the session
188 $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [
189 'id' => $session->getId(),
190 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(),
191 'class' => $class
192 ];
193 }
194 }
195
196 //Sort sessions
197 ksort($calendar[$Ymd]['sessions']);
198 }
199
200 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]);
201 }
202
203 public function tata(Request $request, $id) {
204 /*header('Content-Type: text/plain');
205 var_dump($calendar);
206 exit;*/
207
208 //Set section
209 $section = $this->translator->trans('Session %id%', ['%id%' => $id]);
210
211 //Set title
212 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
213
214 //Create the form according to the FormType created previously.
215 //And give the proper parameters
216 /*$form = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
217 // To set the action use $this->generateUrl('route_identifier')
218 'action' => $this->generateUrl('rapsys_air_admin'),
219 'method' => 'POST',
220 'attr' => [ 'class' => 'form_col' ]
221 ]);*/
222
223 //Get doctrine
224 $doctrine = $this->getDoctrine();
225
226 //Fetch session
227 $session = $doctrine->getRepository(Session::class)->findOneById($id);
228
229 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);
230 }
231 }