]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/LocationController.php
Fix title length to 32
[airbundle] / Controller / LocationController.php
1 <?php
2
3 namespace Rapsys\AirBundle\Controller;
4
5 use Symfony\Component\HttpFoundation\Request;
6 use Rapsys\AirBundle\Entity\Slot;
7
8 class LocationController extends DefaultController {
9 /**
10 * List all locations
11 *
12 * @desc Display all sessions by location with an application or login form
13 *
14 * @param Request $request The request instance
15 *
16 * @return Response The rendered view
17 */
18 public function index(Request $request = null) {
19 //Set section
20 $section = $this->translator->trans('Locations');
21
22 //Set title
23 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
24
25 //Init context
26 $context = [];
27
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, [
32 //Set the action
33 'action' => $this->generateUrl('rapsys_air_application_add'),
34 //Set the form attribute
35 'attr' => [ 'class' => 'col' ],
36 //Set admin
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')]))
45 ]);
46
47 //Add form to context
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, [
53 //Set the action
54 'action' => $this->generateUrl('rapsys_user_login'),
55 //Set the form attribute
56 'attr' => [ 'class' => 'col' ]
57 ]);
58
59 //Add form to context
60 $context['login'] = $login->createView();
61 }
62
63 $calendar = 'toto';
64
65 //Render the view
66 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+$context+$this->context);
67 }
68
69 public function show(Request $request, $id) {
70 }
71
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!');
75
76 //Set section
77 $section = $this->translator->trans('Admin');
78
79 //Set title
80 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
81
82 header('Content-Type: text/plain');
83 var_dump('TODO');
84 exit;
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'),
90 'method' => 'POST',
91 'attr' => [ 'class' => 'form_col' ]
92 ]);
93
94 //Get doctrine
95 $doctrine = $this->getDoctrine();
96
97 //Handle request
98 if ($request->isMethod('POST')) {
99 // Refill the fields in case the form is not valid.
100 $form->handleRequest($request);
101
102 if ($form->isValid()) {
103 //Get data
104 $data = $form->getData();
105
106 //Get manager
107 $manager = $doctrine->getManager();
108
109 //Protect session fetching
110 try {
111 $session = $doctrine->getRepository(Session::class)->findOneByLocationSlotDate($data['location'], $data['slot'], $data['date']);
112 //Catch no session case
113 } catch (\Doctrine\ORM\NoResultException $e) {
114 //Create the session
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
123 #$manager->flush();
124 }
125
126 //Init application
127 $application = false;
128
129 //Protect application fetching
130 try {
131 //TODO: handle admin case where we provide a user in extra
132 $application = $doctrine->getRepository(Application::class)->findOneBySessionUser($session, $this->getUser());
133
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) {
141 }
142
143 //Create new application if none found
144 if (!$application) {
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);
153
154 //Flush to get the ids
155 $manager->flush();
156
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')]));
159
160 //Redirect to cleanup the form
161 return $this->redirectToRoute('rapsys_air_admin');
162 }
163 }
164 }
165
166 //Compute period
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')
174 );
175
176 //Fetch sessions
177 $sessions = $doctrine->getRepository(Session::class)->findAllByDatePeriod($period);
178
179 //Init calendar
180 $calendar = [];
181
182 //Init month
183 $month = null;
184
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'),
190 'class' => [],
191 'sessions' => []
192 ];
193 //Append month for first day of month
194 if ($month != $date->format('m')) {
195 $month = $date->format('m');
196 $calendar[$Ymd]['title'] .= '/'.$month;
197 }
198 //Deal with today
199 if ($date->format('U') == ($today = strtotime('today'))) {
200 $calendar[$Ymd]['title'] .= '/'.$month;
201 $calendar[$Ymd]['current'] = true;
202 $calendar[$Ymd]['class'][] = 'current';
203 }
204 //Disable passed days
205 if ($date->format('U') < $today) {
206 $calendar[$Ymd]['disabled'] = true;
207 $calendar[$Ymd]['class'][] = 'disabled';
208 }
209 //Set next month days
210 if ($date->format('m') > date('m')) {
211 $calendar[$Ymd]['next'] = true;
212 $calendar[$Ymd]['class'][] = 'next';
213 }
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());
219
220 //Compute classes
221 $class = [];
222 if ($session->getApplication()) {
223 $class[] = 'granted';
224 } elseif ($count == 0) {
225 $class[] = 'orphaned';
226 } elseif ($count > 1) {
227 $class[] = 'disputed';
228 } else {
229 $class[] = 'pending';
230 }
231
232 //Add the session
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(),
236 'class' => $class
237 ];
238 }
239 }
240
241 //Sort sessions
242 ksort($calendar[$Ymd]['sessions']);
243 }
244
245 return $this->render('@RapsysAir/admin/index.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView(), 'calendar' => $calendar]);
246 }
247
248 public function tata(Request $request, $id) {
249 /*header('Content-Type: text/plain');
250 var_dump($calendar);
251 exit;*/
252
253 //Set section
254 $section = $this->translator->trans('Session %id%', ['%id%' => $id]);
255
256 //Set title
257 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
258
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'),
264 'method' => 'POST',
265 'attr' => [ 'class' => 'form_col' ]
266 ]);*/
267
268 //Get doctrine
269 $doctrine = $this->getDoctrine();
270
271 //Fetch session
272 $session = $doctrine->getRepository(Session::class)->findOneById($id);
273
274 return $this->render('@RapsysAir/admin/session.html.twig', ['title' => $title, 'section' => $section, /*'form' => $form->createView(),*/ 'session' => $session]);
275 }
276 }