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'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'));
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')*/);
85 return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+
$context+
$this->context
);
91 * @desc Display session by id with an application or login form
93 * @param Request $request The request instance
94 * @param int $id The session id
96 * @return Response The rendered view
98 public function view(Request
$request, $id) {
100 $doctrine = $this->getDoctrine();
103 //TODO: genereate a custom request to fetch everything in a single request ???
104 $session = $doctrine->getRepository(Session
::class)->findOneById($id);
107 //TODO: genereate a custom request to fetch everything in a single request ???
108 $location = $session->getLocation(); #$doctrine->getRepository(Location::class)->findOneBySession($session);
111 //TODO: replace with $session['location']['title']
112 $section = $this->translator
->trans($location);
115 $title = $this->translator
->trans('Session %id%', ['%id%' => $id]).' - '.$section.' - '.$this->translator
->trans($this->config
['site']['title']);
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, [
125 'action' => $this->generateUrl('rapsys_air_application_add'),
126 //Set the form attribute
127 'attr' => [ 'class' => 'col' ],
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)
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, [
144 'action' => $this->generateUrl('rapsys_user_login'),
145 //Set the form attribute
146 'attr' => [ 'class' => 'col' ]
149 //Add form to context
150 $context['login'] = $login->createView();
153 //Extract session date
154 $sessionDate = $session->getDate();
156 //Init session begin and end
157 $sessionBegin = $sessionEnd = null;
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')));
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,
180 'id' => ($location = $session->getLocation())->getId(),
181 'title' => $this->translator
->trans($location),
184 'id' => ($slot = $session->getSlot())->getId(),
185 'title' => $this->translator
->trans($slot),
187 'applications' => null,
190 if ($application = $session->getApplication()) {
191 $context['session']['application'] = [
193 'id' => ($user = $application->getUser())->getId(),
194 'title' => (string) $user->getPseudonym(),
196 'id' => ($applicationId = $application->getId()),
197 'title' => $this->translator
->trans('Application %id%', [ '%id%' => $applicationId ]),
201 if ($applications = $session->getApplications()) {
202 $context['session']['applications'] = [];
203 foreach($applications as $application) {
204 $context['session']['applications'][$applicationId = $application->getId()] = [
206 'created' => $application->getCreated(),
207 'updated' => $application->getUpdated(),
209 if ($user = $application->getUser()) {
210 $context['session']['applications'][$applicationId]['user'] = [
211 'id' => $user->getId(),
212 'title' => (string) $user->getPseudonym(),
218 //Add location in context
219 #$context['location'] = [
220 # 'id' => $location->getId(),
221 # 'title' => $location->getTitle(),
225 return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section]+
$context+
$this->context
);