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 LocationController
extends DefaultController
{
14 * @desc Display all sessions by location 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 $section = $this->translator
->trans('Locations');
25 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
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, [
35 'action' => $this->generateUrl('rapsys_air_application_add'),
36 //Set the form attribute
37 'attr' => [ 'class' => 'col' ],
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)
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, [
54 'action' => $this->generateUrl('rapsys_user_login'),
55 //Set the form attribute
56 'attr' => [ 'class' => 'col' ]
60 $context['login'] = $login->createView();
64 $doctrine = $this->getDoctrine();
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 $sessions = $doctrine->getRepository(Session
::class)->findAllByDatePeriod($period);
86 foreach($period as $date) {
87 //Init day in calendar
88 $calendar[$Ymd = $date->format('Ymd')] = [
89 'title' => $date->format('d'),
93 //Append month for first day of month
94 if ($month != $date->format('m')) {
95 $month = $date->format('m');
96 $calendar[$Ymd]['title'] .= '/'.$month;
99 if ($date->format('U') == ($today = strtotime('today'))) {
100 $calendar[$Ymd]['title'] .= '/'.$month;
101 $calendar[$Ymd]['current'] = true;
102 $calendar[$Ymd]['class'][] = 'current';
104 //Disable passed days
105 if ($date->format('U') < $today) {
106 $calendar[$Ymd]['disabled'] = true;
107 $calendar[$Ymd]['class'][] = 'disabled';
109 //Set next month days
110 if ($date->format('m') > date('m')) {
111 $calendar[$Ymd]['next'] = true;
112 $calendar[$Ymd]['class'][] = 'next';
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());
122 if ($session->getApplication()) {
123 $class[] = 'granted';
124 } elseif ($count == 0) {
125 $class[] = 'orphaned';
126 } elseif ($count > 1) {
127 $class[] = 'disputed';
129 $class[] = 'pending';
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(),
142 ksort($calendar[$Ymd]['sessions']);
146 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+
$context+
$this->context
);
149 public function view(Request
$request, $id) {
151 $doctrine = $this->getDoctrine();
154 $location = $doctrine->getRepository(Location
::class)->findOneById($id);
157 $section = $this->translator
->trans($location);
160 $title = $section.' - '.$this->translator
->trans($this->config
['site']['title']);
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, [
170 'action' => $this->generateUrl('rapsys_air_application_add'),
171 //Set the form attribute
172 'attr' => [ 'class' => 'col' ],
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)
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, [
189 'action' => $this->generateUrl('rapsys_user_login'),
190 //Set the form attribute
191 'attr' => [ 'class' => 'col' ]
194 //Add form to context
195 $context['login'] = $login->createView();
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')
209 $sessions = $doctrine->getRepository(Session
::class)->findAllByLocationDatePeriod($location, $period);
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'),
225 //Append month for first day of month
226 if ($month != $date->format('m')) {
227 $month = $date->format('m');
228 $calendar[$Ymd]['title'] .= '/'.$month;
231 if ($date->format('U') == ($today = strtotime('today'))) {
232 $calendar[$Ymd]['title'] .= '/'.$month;
233 $calendar[$Ymd]['current'] = true;
234 $calendar[$Ymd]['class'][] = 'current';
236 //Disable passed days
237 if ($date->format('U') < $today) {
238 $calendar[$Ymd]['disabled'] = true;
239 $calendar[$Ymd]['class'][] = 'disabled';
241 //Set next month days
242 if ($date->format('m') > date('m')) {
243 $calendar[$Ymd]['next'] = true;
244 $calendar[$Ymd]['class'][] = 'next';
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());
254 if ($session->getApplication()) {
255 $class[] = 'granted';
256 } elseif ($count == 0) {
257 $class[] = 'orphaned';
258 } elseif ($count > 1) {
259 $class[] = 'disputed';
261 $class[] = 'pending';
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(),
274 ksort($calendar[$Ymd]['sessions']);
278 return $this->render('@RapsysAir/location/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar]+
$context+
$this->context
);