]> Raphaƫl G. Git Repositories - airbundle/blob - Controller/SessionController.php
Add start, stop, premium, rainfall, rainrisk, realfeel, realfeelmin, realfeelmax...
[airbundle] / Controller / SessionController.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 SessionController extends DefaultController {
11 /**
12 * List all sessions
13 *
14 * @desc Display all sessions 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 //Fetch doctrine
22 $doctrine = $this->getDoctrine();
23
24 //Set section
25 $section = $this->translator->trans('Sessions');
26
27 //Set title
28 $title = $section.' - '.$this->translator->trans($this->config['site']['title']);
29
30 //Init context
31 $context = [];
32
33 //Create application form for role_guest
34 if ($this->isGranted('ROLE_GUEST')) {
35 //Create ApplicationType form
36 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
37 //Set the action
38 'action' => $this->generateUrl('rapsys_air_application_add'),
39 //Set the form attribute
40 'attr' => [ 'class' => 'col' ],
41 //Set admin
42 'admin' => $this->isGranted('ROLE_ADMIN'),
43 //Set default user to current
44 'user' => $this->getUser()->getId(),
45 //Set default slot to evening
46 //XXX: default to Evening (3)
47 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3)
48 ]);
49
50 //Add form to context
51 $context['application'] = $application->createView();
52 //Create login form for anonymous
53 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
54 //Create ApplicationType form
55 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
56 //Set the action
57 'action' => $this->generateUrl('rapsys_user_login'),
58 //Set the form attribute
59 'attr' => [ 'class' => 'col' ]
60 ]);
61
62 //Add form to context
63 $context['login'] = $login->createView();
64 }
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 calendar
77 //TODO: highlight with current session route parameter
78 $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
79
80 //Fetch locations
81 //XXX: we want to display all active locations anyway
82 $locations = $doctrine->getRepository(Location::class)->fetchTranslatedLocationByDatePeriod($this->translator, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/);
83
84 //Render the view
85 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context);
86 }
87
88 /**
89 * Display session
90 *
91 * @desc Display session by id with an application or login form
92 *
93 * @param Request $request The request instance
94 * @param int $id The session id
95 *
96 * @return Response The rendered view
97 */
98 public function view(Request $request, $id) {
99 //Fetch doctrine
100 $doctrine = $this->getDoctrine();
101
102 //Fetch session
103 //TODO: genereate a custom request to fetch everything in a single request ???
104 $session = $doctrine->getRepository(Session::class)->findOneById($id);
105
106 //Fetch session
107 //TODO: genereate a custom request to fetch everything in a single request ???
108 $location = $session->getLocation(); #$doctrine->getRepository(Location::class)->findOneBySession($session);
109
110 //Set section
111 //TODO: replace with $session['location']['title']
112 $section = $this->translator->trans($location);
113
114 //Set title
115 $title = $this->translator->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator->trans($this->config['site']['title']);
116
117 //Init context
118 $context = [];
119
120 //Create application form for role_guest
121 if ($this->isGranted('ROLE_GUEST')) {
122 //Create ApplicationType form
123 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
124 //Set the action
125 'action' => $this->generateUrl('rapsys_air_application_add'),
126 //Set the form attribute
127 'attr' => [ 'class' => 'col' ],
128 //Set admin
129 'admin' => $this->isGranted('ROLE_ADMIN'),
130 //Set default user to current
131 'user' => $this->getUser()->getId(),
132 //Set default slot to evening
133 //XXX: default to Evening (3)
134 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById(3)
135 ]);
136
137 //Add form to context
138 $context['application'] = $application->createView();
139 //Create login form for anonymous
140 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
141 //Create ApplicationType form
142 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
143 //Set the action
144 'action' => $this->generateUrl('rapsys_user_login'),
145 //Set the form attribute
146 'attr' => [ 'class' => 'col' ]
147 ]);
148
149 //Add form to context
150 $context['login'] = $login->createView();
151 }
152
153 //Extract session date
154 $sessionDate = $session->getDate();
155
156 //Init session begin and end
157 $sessionBegin = $sessionEnd = null;
158
159 //Check session begin and end availability
160 if (($sessionBegin = $session->getBegin()) && ($sessionLength = $session->getLength())) {
161 $sessionBegin = new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP'));
162 #$sessionEnd = (new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP')))->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
163 $sessionEnd = (clone $sessionBegin)->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
164 }
165
166 //Add session in context
167 $context['session'] = [
168 'id' => ($sessionId = $session->getId()),
169 'date' => $sessionDate,
170 'begin' => $sessionBegin,
171 'end' => $sessionEnd,
172 #'begin' => new \DateTime($session->getDate()->format('Y-m-d')."\t".$session->getBegin()->format('H:i:sP')),
173 #'end' => (new \DateTime($session->getDate()->format('Y-m-d')."\t".$session->getBegin()->format('H:i:sP')))->add(new \DateInterval($session->getLength()->format('\P\TH\Hi\Ms\S'))),
174 #'length' => $session->getLength(),
175 'created' => $session->getCreated(),
176 'updated' => $session->getUpdated(),
177 'title' => $this->translator->trans('Session %id%', ['%id%' => $sessionId]),
178 'application' => null,
179 'location' => [
180 'id' => ($location = $session->getLocation())->getId(),
181 'title' => $this->translator->trans($location),
182 ],
183 'slot' => [
184 'id' => ($slot = $session->getSlot())->getId(),
185 'title' => $this->translator->trans($slot),
186 ],
187 'applications' => null,
188 ];
189
190 if ($application = $session->getApplication()) {
191 $context['session']['application'] = [
192 'user' => [
193 'id' => ($user = $application->getUser())->getId(),
194 'title' => (string) $user->getPseudonym(),
195 ],
196 'id' => ($applicationId = $application->getId()),
197 'title' => $this->translator->trans('Application %id%', [ '%id%' => $applicationId ]),
198 ];
199 }
200
201 if ($applications = $session->getApplications()) {
202 $context['session']['applications'] = [];
203 foreach($applications as $application) {
204 $context['session']['applications'][$applicationId = $application->getId()] = [
205 'user' => null,
206 'created' => $application->getCreated(),
207 'updated' => $application->getUpdated(),
208 ];
209 if ($user = $application->getUser()) {
210 $context['session']['applications'][$applicationId]['user'] = [
211 'id' => $user->getId(),
212 'title' => (string) $user->getPseudonym(),
213 ];
214 }
215 }
216 }
217
218 //Add location in context
219 #$context['location'] = [
220 # 'id' => $location->getId(),
221 # 'title' => $location->getTitle(),
222 #];
223
224 //Render the view
225 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+$context+$this->context);
226 }
227 }