]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/LocationController.php
Remove return hidden field
[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 use Rapsys\AirBundle\Entity\Session;
8 use Rapsys\AirBundle\Entity\Location;
9
10 class LocationController extends DefaultController {
11 /**
12 * List all locations
13 *
14 * @desc Display all sessions by location with an application or login form
15 *
16 * @param Request $request The request instance
17 *
18 * @return Response The rendered view
19 */
20 public function index(Request $request = null) {
21 //Set section
22 $section = $this->translator->trans('Locations');
23
24 //Set title
25 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
26
27 //Init context
28 $context = [];
29
30 //Create application form for role_guest
31 if ($this->isGranted('ROLE_GUEST')) {
32 //Create ApplicationType form
33 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
34 //Set the action
35 'action' => $this->generateUrl('rapsys_air_application_add'),
36 //Set the form attribute
37 'attr' => [ 'class' => 'col' ],
38 //Set admin
39 'admin' => $this->isGranted('ROLE_ADMIN'),
40 //Set default user to current
41 'user' => $this->getUser()->getId(),
42 //Set default slot to evening
43 //XXX: default to Evening (3)
44 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
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 //Fetch doctrine
64 $doctrine = $this->getDoctrine();
65
66 //Compute period
67 $period = new \DatePeriod(
68 //Start from first monday of week
69 new \DateTime('Monday this week'),
70 //Iterate on each day
71 new \DateInterval('P1D'),
72 //End with next sunday and 4 weeks
73 new \DateTime('Monday this week + 5 week')
74 );
75
76 //Fetch sessions
77 $sessions = $doctrine->getRepository(Session::class)->findAllByDatePeriod($period);
78
79 //Init calendar
80 $calendar = [];
81
82 //Init month
83 $month = null;
84
85 //Iterate on each day
86 foreach($period as $date) {
87 //Init day in calendar
88 $calendar[$Ymd = $date->format('Ymd')] = [
89 'title' => $date->format('d'),
90 'class' => [],
91 'sessions' => []
92 ];
93 //Append month for first day of month
94 if ($month != $date->format('m')) {
95 $month = $date->format('m');
96 $calendar[$Ymd]['title'] .= '/'.$month;
97 }
98 //Deal with today
99 if ($date->format('U') == ($today = strtotime('today'))) {
100 $calendar[$Ymd]['title'] .= '/'.$month;
101 $calendar[$Ymd]['current'] = true;
102 $calendar[$Ymd]['class'][] = 'current';
103 }
104 //Disable passed days
105 if ($date->format('U') < $today) {
106 $calendar[$Ymd]['disabled'] = true;
107 $calendar[$Ymd]['class'][] = 'disabled';
108 }
109 //Set next month days
110 if ($date->format('m') > date('m')) {
111 $calendar[$Ymd]['next'] = true;
112 $calendar[$Ymd]['class'][] = 'next';
113 }
114 //Iterate on each session to find the one of the day
115 foreach($sessions as $session) {
116 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) {
117 //Count number of application
118 $count = count($session->getApplications());
119
120 //Compute classes
121 $class = [];
122 if ($session->getApplication()) {
123 $class[] = 'granted';
124 } elseif ($count == 0) {
125 $class[] = 'orphaned';
126 } elseif ($count > 1) {
127 $class[] = 'disputed';
128 } else {
129 $class[] = 'pending';
130 }
131
132 //Add the session
133 $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [
134 'id' => $session->getId(),
135 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(),
136 'class' => $class
137 ];
138 }
139 }
140
141 //Sort sessions
142 ksort($calendar[$Ymd]['sessions']);
143 }
144
145 //Render the view
146 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+$context+$this->context);
147 }
148
149 public function view(Request $request, $id) {
150 //Fetch doctrine
151 $doctrine = $this->getDoctrine();
152
153 //Fetch location
154 $location = $doctrine->getRepository(Location::class)->findOneById($id);
155
156 //Set section
157 $section = $this->translator->trans($location);
158
159 //Set title
160 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
161
162 //Init context
163 $context = [];
164
165 //Create application form for role_guest
166 if ($this->isGranted('ROLE_GUEST')) {
167 //Create ApplicationType form
168 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
169 //Set the action
170 'action' => $this->generateUrl('rapsys_air_application_add'),
171 //Set the form attribute
172 'attr' => [ 'class' => 'col' ],
173 //Set admin
174 'admin' => $this->isGranted('ROLE_ADMIN'),
175 //Set default user to current
176 'user' => $this->getUser()->getId(),
177 //Set default slot to evening
178 //XXX: default to Evening (3)
179 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
180 ]);
181
182 //Add form to context
183 $context['application'] = $application->createView();
184 //Create login form for anonymous
185 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
186 //Create ApplicationType form
187 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
188 //Set the action
189 'action' => $this->generateUrl('rapsys_user_login'),
190 //Set the form attribute
191 'attr' => [ 'class' => 'col' ]
192 ]);
193
194 //Add form to context
195 $context['login'] = $login->createView();
196 }
197
198 //Compute period
199 $period = new \DatePeriod(
200 //Start from first monday of week
201 new \DateTime('Monday this week'),
202 //Iterate on each day
203 new \DateInterval('P1D'),
204 //End with next sunday and 4 weeks
205 new \DateTime('Monday this week + 5 week')
206 );
207
208 //Fetch sessions
209 $sessions = $doctrine->getRepository(Session::class)->findAllByLocationDatePeriod($location, $period);
210
211 //Init calendar
212 $calendar = [];
213
214 //Init month
215 $month = null;
216
217 //Iterate on each day
218 foreach($period as $date) {
219 //Init day in calendar
220 $calendar[$Ymd = $date->format('Ymd')] = [
221 'title' => $date->format('d'),
222 'class' => [],
223 'sessions' => []
224 ];
225 //Append month for first day of month
226 if ($month != $date->format('m')) {
227 $month = $date->format('m');
228 $calendar[$Ymd]['title'] .= '/'.$month;
229 }
230 //Deal with today
231 if ($date->format('U') == ($today = strtotime('today'))) {
232 $calendar[$Ymd]['title'] .= '/'.$month;
233 $calendar[$Ymd]['current'] = true;
234 $calendar[$Ymd]['class'][] = 'current';
235 }
236 //Disable passed days
237 if ($date->format('U') < $today) {
238 $calendar[$Ymd]['disabled'] = true;
239 $calendar[$Ymd]['class'][] = 'disabled';
240 }
241 //Set next month days
242 if ($date->format('m') > date('m')) {
243 $calendar[$Ymd]['next'] = true;
244 $calendar[$Ymd]['class'][] = 'next';
245 }
246 //Iterate on each session to find the one of the day
247 foreach($sessions as $session) {
248 if (($sessionYmd = $session->getDate()->format('Ymd')) == $Ymd) {
249 //Count number of application
250 $count = count($session->getApplications());
251
252 //Compute classes
253 $class = [];
254 if ($session->getApplication()) {
255 $class[] = 'granted';
256 } elseif ($count == 0) {
257 $class[] = 'orphaned';
258 } elseif ($count > 1) {
259 $class[] = 'disputed';
260 } else {
261 $class[] = 'pending';
262 }
263
264 //Add the session
265 $calendar[$Ymd]['sessions'][$session->getSlot()->getId().$session->getLocation()->getId()] = [
266 'id' => $session->getId(),
267 'title' => ($count > 1?'['.$count.'] ':'').$session->getSlot()->getTitle().' '.$session->getLocation()->getTitle(),
268 'class' => $class
269 ];
270 }
271 }
272
273 //Sort sessions
274 ksort($calendar[$Ymd]['sessions']);
275 }
276
277 //Render the view
278 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+$context+$this->context);
279 }
280 }