3 namespace Rapsys\AirBundle\Controller
;
5 use Symfony\Component\HttpFoundation\Request
;
6 use Rapsys\AirBundle\Entity\Slot
;
7 use Rapsys\AirBundle\Entity\Session
;
8 use Rapsys\AirBundle\Entity\Location
;
10 class SessionController
extends DefaultController
{
14 * @desc Display all sessions with an application or login form
16 * @param Request $request The request instance
18 * @return Response The rendered view
20 public function index(Request
$request = null) {
22 $doctrine = $this->getDoctrine();
25 $section = $this->translator
->trans('Sessions');
28 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
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, [
38 'action' => $this->generateUrl('rapsys_air_application_add'),
39 //Set the form attribute
40 'attr' => [ 'class' => 'col' ],
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)
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, [
57 'action' => $this->generateUrl('rapsys_user_login'),
58 //Set the form attribute
59 'attr' => [ 'class' => 'col' ]
63 $context['login'] = $login->createView();
67 $period = new \
DatePeriod(
68 //Start from first monday of week
69 new \
DateTime('Monday this week'),
71 new \
DateInterval('P1D'),
72 //End with next sunday and 4 weeks
73 new \
DateTime('Monday this week + 5 week')
77 //TODO: highlight with current session route parameter
78 $calendar = $doctrine->getRepository(Session
::class)->fetchCalendarByDatePeriod($this->translator
, $period, null, $request->get('session'));
81 $locations = $doctrine->getRepository(Location
::class)->fetchTranslatedLocationByDatePeriod($this->translator
, $period);
84 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$context+
$this->context
);
90 * @desc Display session by id with an application or login form
92 * @param Request $request The request instance
93 * @param int $id The session id
95 * @return Response The rendered view
97 public function view(Request
$request, $id) {
99 $doctrine = $this->getDoctrine();
102 //TODO: genereate a custom request to fetch everything in a single request ???
103 $session = $doctrine->getRepository(Session
::class)->findOneById($id);
106 //TODO: genereate a custom request to fetch everything in a single request ???
107 $location = $session->getLocation(); #$doctrine->getRepository(Location::class)->findOneBySession($session);
110 //TODO: replace with $session['location']['title']
111 $section = $this->translator
->trans($location);
114 $title = $this->translator
->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator
->trans($this->config
['site']['title']);
119 //Create application form for role_guest
120 if ($this->isGranted('ROLE_GUEST')) {
121 //Create ApplicationType form
122 $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [
124 'action' => $this->generateUrl('rapsys_air_application_add'),
125 //Set the form attribute
126 'attr' => [ 'class' => 'col' ],
128 'admin' => $this->isGranted('ROLE_ADMIN'),
129 //Set default user to current
130 'user' => $this->getUser()->getId(),
131 //Set default slot to evening
132 //XXX: default to Evening (3)
133 'slot' => $this->getDoctrine()->getRepository(Slot
::class)->findOneById(3)
136 //Add form to context
137 $context['application'] = $application->createView();
138 //Create login form for anonymous
139 } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
140 //Create ApplicationType form
141 $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [
143 'action' => $this->generateUrl('rapsys_user_login'),
144 //Set the form attribute
145 'attr' => [ 'class' => 'col' ]
148 //Add form to context
149 $context['login'] = $login->createView();
152 //Extract session date
153 $sessionDate = $session->getDate();
155 //Init session begin and end
156 $sessionBegin = $sessionEnd = null;
158 //Check session begin and end availability
159 if (($sessionBegin = $session->getBegin()) && ($sessionLength = $session->getLength())) {
160 $sessionBegin = new \
DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP'));
161 #$sessionEnd = (new \DateTime($sessionDate->format('Y-m-d')."\t".$sessionBegin->format('H:i:sP')))->add(new \DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
162 $sessionEnd = (clone $sessionBegin)->add(new \
DateInterval($sessionLength->format('\P\TH\Hi\Ms\S')));
165 //Add session in context
166 $context['session'] = [
167 'id' => ($sessionId = $session->getId()),
168 'date' => $sessionDate,
169 'begin' => $sessionBegin,
170 'end' => $sessionEnd,
171 #'begin' => new \DateTime($session->getDate()->format('Y-m-d')."\t".$session->getBegin()->format('H:i:sP')),
172 #'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'))),
173 #'length' => $session->getLength(),
174 'created' => $session->getCreated(),
175 'updated' => $session->getUpdated(),
176 'title' => $this->translator
->trans('Session %id%', ['%id%' => $sessionId]),
177 'application' => null,
179 'id' => ($location = $session->getLocation())->getId(),
180 'title' => $this->translator
->trans($location),
183 'id' => ($slot = $session->getSlot())->getId(),
184 'title' => $this->translator
->trans($slot),
186 'applications' => null,
189 if ($application = $session->getApplication()) {
190 $context['session']['application'] = [
192 'id' => ($user = $application->getUser())->getId(),
193 'title' => (string) $user->getPseudonym(),
195 'id' => ($applicationId = $application->getId()),
196 'title' => $this->translator
->trans('Application %id%', [ '%id%' => $applicationId ]),
200 if ($applications = $session->getApplications()) {
201 $context['session']['applications'] = [];
202 foreach($applications as $application) {
203 $context['session']['applications'][$applicationId = $application->getId()] = [
205 'created' => $application->getCreated(),
206 'updated' => $application->getUpdated(),
208 if ($user = $application->getUser()) {
209 $context['session']['applications'][$applicationId]['user'] = [
210 'id' => $user->getId(),
211 'title' => (string) $user->getPseudonym(),
217 //Add location in context
218 #$context['location'] = [
219 # 'id' => $location->getId(),
220 # 'title' => $location->getTitle(),
224 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+
$context+
$this->context
);