X-Git-Url: https://git.rapsys.eu/airbundle/blobdiff_plain/f8d95cac25f72715d415a739e7a9938f5df89768..11f4f833ffd015afd0b06e4d5f542a1f81b0a1c9:/Controller/SessionController.php?ds=sidebyside diff --git a/Controller/SessionController.php b/Controller/SessionController.php index c3104a3..aca7c29 100644 --- a/Controller/SessionController.php +++ b/Controller/SessionController.php @@ -1,18 +1,34 @@ - + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ namespace Rapsys\AirBundle\Controller; +use Symfony\Component\Asset\Packages; +use Symfony\Component\Filesystem\Exception\IOExceptionInterface; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\Request; -use Symfony\Component\Routing\RequestContext; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\Routing\Exception\MethodNotAllowedException; use Symfony\Component\Routing\Exception\ResourceNotFoundException; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Routing\RequestContext; + use Rapsys\AirBundle\Entity\Application; use Rapsys\AirBundle\Entity\User; use Rapsys\AirBundle\Entity\Slot; use Rapsys\AirBundle\Entity\Session; use Rapsys\AirBundle\Entity\Location; -class SessionController extends DefaultController { +class SessionController extends AbstractController { /** * Edit session * @@ -24,7 +40,7 @@ class SessionController extends DefaultController { * * @throws \RuntimeException When user has not at least guest role */ - public function edit(Request $request, $id) { + public function edit(Request $request, $id): Response { //Prevent non-guest to access here $this->denyAccessUnlessGranted('ROLE_GUEST', null, $this->translator->trans('Unable to access this page without role %role%!', ['%role%' => $this->translator->trans('Guest')])); @@ -36,8 +52,11 @@ class SessionController extends DefaultController { //Get doctrine $doctrine = $this->getDoctrine(); + //Set locale + $locale = $request->getLocale(); + //Fetch session - $session = $doctrine->getRepository(Session::class)->fetchOneById($id, $request->getLocale()); + $session = $doctrine->getRepository(Session::class)->fetchOneById($id, $locale); //Check if if ( @@ -55,8 +74,8 @@ class SessionController extends DefaultController { //Set now $now = new \DateTime('now'); - //Create SessionEditType form - $form = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [ + //Create SessionType form + $form = $this->createForm('Rapsys\AirBundle\Form\SessionType', null, [ //Set the action 'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]), //Set the form attribute @@ -65,6 +84,8 @@ class SessionController extends DefaultController { 'admin' => $this->isGranted('ROLE_ADMIN'), //Set default user to current 'user' => $this->getUser()->getId(), + //Set date + 'date' => $session['date'], //Set begin 'begin' => $session['begin'], //Set length @@ -87,17 +108,49 @@ class SessionController extends DefaultController { $form->handleRequest($request); //Handle invalid data - #if (true) { $form->isValid(); - //TODO: mettre une contrainte sur un des boutons submit, je sais pas encore comment - if (!$form->isValid()) { - //Set section - $section = $this->translator->trans('Session %id%', ['%id%' => $id]); + if (!$form->isSubmitted() || !$form->isValid()) { + //Set page + $this->context['title'] = $this->translator->trans(!empty($session['au_id'])?'Session %id% by %pseudonym%':'Session %id%', ['%id%' => $id, '%pseudonym%' => $session['au_pseudonym']]); - //Set title - $title = $section.' - '.$this->translator->trans($this->config['site']['title']); + //Set facebook title + $this->context['facebook']['metas']['og:title'] = $this->context['title'].' '.$this->translator->trans('at '.$session['l_title']); + + //Set section + $this->context['section'] = $this->translator->trans($session['l_title']); + + //Set localization date formater + $intl = new \IntlDateFormatter($locale, \IntlDateFormatter::GREGORIAN, \IntlDateFormatter::SHORT); + + //Set description + $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango session the %date%', [ '%date%' => $intl->format($session['start']) ]); + + //Set localization date formater + $intlDate = new \IntlDateFormatter($locale, \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::NONE); + + //Set localization time formater + $intlTime = new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT); + + //Set facebook image + $this->context['facebook'] += [ + 'texts' => [ + $session['au_pseudonym'] => [ + 'font' => 'irishgrover', + 'size' => 110 + ], + ucfirst($intlDate->format($session['start']))."\n".$this->translator->trans('From %start% to %stop%', ['%start%' => $intlTime->format($session['start']), '%stop%' => $intlTime->format($session['stop'])]) => [ + 'align' => 'left' + ], + $this->translator->trans('at '.$session['l_title']) => [ + 'align' => 'right', + 'font' => 'labelleaurore', + 'size' => 75 + ] + ], + 'updated' => $session['updated']->format('U') + ]; //Add session in context - $context['session'] = [ + $this->context['session'] = [ 'id' => $id, 'title' => $this->translator->trans('Session %id%', ['%id%' => $id]), 'location' => [ @@ -105,8 +158,9 @@ class SessionController extends DefaultController { 'at' => $this->translator->trans('at '.$session['l_title']) ] ]; + //Render the view - return $this->render('@RapsysAir/session/edit.html.twig', ['title' => $title, 'section' => $section, 'form' => $form->createView()]+$context+$this->context); + return $this->render('@RapsysAir/session/edit.html.twig', ['form' => $form->createView()]+$this->context); } //Get manager @@ -176,6 +230,12 @@ class SessionController extends DefaultController { $this->addFlash('notice', $this->translator->trans('Session %id% updated', ['%id%' => $id])); //With modify } elseif ($action['modify']) { + //With admin + if ($this->isGranted('ROLE_ADMIN')) { + //Set date + $session->setDate($data['date']); + } + //Set begin $session->setBegin($data['begin']); @@ -336,27 +396,6 @@ class SessionController extends DefaultController { $session->setLocked($datetime); } -# //Get applications -# $applications = $doctrine->getRepository(Application::class)->findBySession($session); -# -# //Not empty -# if (!empty($applications)) { -# //Iterate on each applications -# foreach($applications as $application) { -# //Cancel application -# $application->setCanceled($canceled); -# -# //Update time -# $application->setUpdated($datetime); -# -# //Queue application save -# $manager->persist($application); -# -# //Add notice in flash message -# $this->addFlash('notice', $this->translator->trans('Application %id% updated', ['%id%' => $application->getId()])); -# } -# } - //Update time $session->setUpdated($datetime); @@ -434,51 +473,26 @@ class SessionController extends DefaultController { * * @return Response The rendered view */ - public function index(Request $request) { + public function index(Request $request): Response { //Fetch doctrine $doctrine = $this->getDoctrine(); //Set section $section = $this->translator->trans('Sessions'); - //Set title - $title = $this->translator->trans($this->config['site']['title']).' - '.$section; - - //Init context - $context = []; - - //Create application form for role_guest - if ($this->isGranted('ROLE_GUEST')) { - //Create ApplicationType form - $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ - //Set the action - 'action' => $this->generateUrl('rapsys_air_application_add'), - //Set the form attribute - 'attr' => [ 'class' => 'col' ], - //Set admin - 'admin' => $this->isGranted('ROLE_ADMIN'), - //Set default user to current - 'user' => $this->getUser()->getId(), - //Set default slot to evening - //XXX: default to Evening (3) - 'slot' => $doctrine->getRepository(Slot::class)->findOneById(3) - ]); + //Set description + $this->context['description'] = $this->translator->trans('Libre Air session list'); - //Add form to context - $context['application'] = $application->createView(); - //Create login form for anonymous - } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { - //Create ApplicationType form - $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ - //Set the action - 'action' => $this->generateUrl('rapsys_user_login'), - //Set the form attribute - 'attr' => [ 'class' => 'col' ] - ]); + //Set keywords + $this->context['keywords'] = [ + $this->translator->trans('sessions'), + $this->translator->trans('session list'), + $this->translator->trans('listing'), + $this->translator->trans('Libre Air') + ]; - //Add form to context - $context['login'] = $login->createView(); - } + //Set title + $title = $this->translator->trans($this->config['site']['title']).' - '.$section; //Compute period $period = new \DatePeriod( @@ -487,24 +501,121 @@ class SessionController extends DefaultController { //Iterate on each day new \DateInterval('P1D'), //End with next sunday and 4 weeks - new \DateTime('Monday this week + 5 week') + new \DateTime( + $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week' + ) ); //Fetch calendar //TODO: highlight with current session route parameter - $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')); + $calendar = $doctrine->getRepository(Session::class)->fetchCalendarByDatePeriod($this->translator, $period, null, $request->get('session'), !$this->isGranted('IS_AUTHENTICATED_REMEMBERED'), $request->getLocale()); //Fetch locations //XXX: we want to display all active locations anyway - $locations = $doctrine->getRepository(Location::class)->fetchTranslatedLocationByDatePeriod($this->translator, $period/*, !$this->isGranted('IS_AUTHENTICATED_REMEMBERED')*/); + $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period); //Render the view - return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$context+$this->context); + return $this->render('@RapsysAir/session/index.html.twig', ['title' => $title, 'section' => $section, 'calendar' => $calendar, 'locations' => $locations]+$this->context); + } + + /** + * List all sessions for tango argentin + * + * @desc Display all sessions in tango argentin json format + * + * @todo Drop it if unused by tangoargentin ??? + * + * @param Request $request The request instance + * + * @return Response The rendered view or redirection + */ + public function tangoargentin(Request $request): Response { + //Fetch doctrine + $doctrine = $this->getDoctrine(); + + //Compute period + $period = new \DatePeriod( + //Start from first monday of week + new \DateTime('today'), + //Iterate on each day + new \DateInterval('P1D'), + //End with next sunday and 4 weeks + new \DateTime('+2 week') + ); + + //Retrieve events to update + $sessions = $doctrine->getRepository(Session::class)->fetchAllByDatePeriod($period, $request->getLocale()); + + //Init return array + $ret = []; + + //Iterate on sessions + foreach($sessions as $sessionId => $session) { + //Set title + $title = $session['au_pseudonym'].' '.$this->translator->trans('at '.$session['l_title']); + + //Use Transliterator if available + if (class_exists('Transliterator')) { + $trans = \Transliterator::create('Any-Latin; Latin-ASCII; Upper()'); + $title = $trans->transliterate($title); + } else { + $title = strtoupper($title); + } + + //Set rate + $rate = 'Au chapeau'; + + //Without hat + if ($session['p_hat'] === null) { + //Set rate + $rate = 'Gratuit'; + + //With rate + if ($session['p_rate'] !== null) { + //Set rate + $rate = $session['p_rate'].' €'; + } + //With hat + } else { + //With rate + if ($session['p_rate'] !== null) { + //Set rate + $rate .= ', idéalement '.$session['p_rate'].' €'; + } + } + + //Store session data + $ret[$sessionId] = [ + 'start' => $session['start']->format(\DateTime::ISO8601), + 'stop' => $session['start']->format(\DateTime::ISO8601), + 'title' => $title, + 'short' => $session['p_short'], + 'rate' => $rate, + 'location' => implode(' ', [$session['l_address'], $session['l_zipcode'], $session['l_city']]), + 'status' => (empty($session['a_canceled']) && empty($session['locked']))?'confirmed':'cancelled', + 'updated' => $session['updated']->format(\DateTime::ISO8601), + 'organizer' => $session['au_forename'], + 'source' => $this->router->generate('rapsys_air_session_view', ['id' => $sessionId], UrlGeneratorInterface::ABSOLUTE_URL) + ]; + } + + //Set response + $response = new Response(json_encode($ret)); + + //Set header + $response->headers->set('Content-Type', 'application/json'); + + //Send response + return $response; } /** * Display session * + * @todo XXX: TODO: add + * @todo XXX: TODO: like described in: https://www.alsacreations.com/article/lire/1400-attribut-rel-relations.html#xnf-rel-attribute + * @todo XXX: TODO: or here: http://microformats.org/wiki/existing-rel-values#HTML5_link_type_extensions + * * @desc Display session by id with an application or login form * * @param Request $request The request instance @@ -512,50 +623,110 @@ class SessionController extends DefaultController { * * @return Response The rendered view */ - public function view(Request $request, $id) { + public function view(Request $request, $id): Response { //Fetch doctrine $doctrine = $this->getDoctrine(); + //Set locale + $locale = $request->getLocale(); + //Fetch session - if (empty($session = $doctrine->getRepository(Session::class)->fetchOneById($id, $request->getLocale()))) { + if (empty($session = $doctrine->getRepository(Session::class)->fetchOneById($id, $locale))) { throw $this->createNotFoundException($this->translator->trans('Unable to find session: %id%', ['%id%' => $id])); } + //Create response + $response = new Response(); + + //Set etag + $response->setEtag(md5(serialize($session))); + + //With logged user + if ($this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { + //Set last modified + $response->setLastModified(new \DateTime('-1 year')); + + //Set as private + $response->setPrivate(); + //Without logged user + } else { + //Extract applications updated + $session['sa_updated'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_updated'])); + + //Get last modified + $lastModified = max(array_merge([$session['updated'], $session['l_updated'], $session['t_updated'], $session['p_updated']], $session['sa_updated'])); + + //Set last modified + $response->setLastModified($lastModified); + + //Set as public + $response->setPublic(); + + //Without role and modification + if ($response->isNotModified($request)) { + //Return 304 response + return $response; + } + } + + //Set localization date formater + $intl = new \IntlDateFormatter($locale, \IntlDateFormatter::GREGORIAN, \IntlDateFormatter::SHORT); + //Set section - $section = $this->translator->trans($session['l_title']); + $this->context['section'] = $this->translator->trans($session['l_title']); - //Set title - $title = $this->translator->trans($this->config['site']['title']).' - '.$section.' - '.$this->translator->trans('Session %id%', ['%id%' => $id]); + //Set description + $this->context['description'] = $this->translator->trans('Outdoor Argentine Tango session the %date%', [ '%date%' => $intl->format($session['start']) ]); - //Init context - $context = []; + //Set keywords + $this->context['keywords'] = [ + $this->translator->trans('outdoor'), + $this->translator->trans('Argentine Tango'), + ]; - //Create application form for role_guest - if ($this->isGranted('ROLE_GUEST')) { - //Create ApplicationType form - $application = $this->createForm('Rapsys\AirBundle\Form\ApplicationType', null, [ - //Set the action - 'action' => $this->generateUrl('rapsys_air_application_add'), - //Set the form attribute - 'attr' => [ 'class' => 'col' ], - //Set admin - 'admin' => $this->isGranted('ROLE_ADMIN'), - //Set default user to current - 'user' => $this->getUser()->getId(), - //Set default slot to current - 'slot' => $this->getDoctrine()->getRepository(Slot::class)->findOneById($session['t_id']), - //Set default location to current - 'location' => $this->getDoctrine()->getRepository(Location::class)->findOneById($session['l_id']), - ]); + //Set localization date formater + $intlDate = new \IntlDateFormatter($locale, \IntlDateFormatter::TRADITIONAL, \IntlDateFormatter::NONE); - //Add form to context - $context['application'] = $application->createView(); + //Set localization time formater + $intlTime = new \IntlDateFormatter($locale, \IntlDateFormatter::NONE, \IntlDateFormatter::SHORT); + //Set facebook image + $this->context['facebook'] = [ + 'texts' => [ + $session['au_pseudonym'] => [ + 'font' => 'irishgrover', + 'size' => 110 + ], + ucfirst($intlDate->format($session['start']))."\n".$this->translator->trans('From %start% to %stop%', ['%start%' => $intlTime->format($session['start']), '%stop%' => $intlTime->format($session['stop'])]) => [ + 'align' => 'left' + ], + $this->translator->trans('at '.$session['l_title']) => [ + 'align' => 'right', + 'font' => 'labelleaurore', + 'size' => 75 + ] + ], + 'updated' => $session['updated']->format('U') + ]+$this->context['facebook']; + + //With granted session + if (!empty($session['au_id'])) { + array_unshift($this->context['keywords'], $session['au_pseudonym']); + } + + //Set page + $this->context['title'] = $this->translator->trans(!empty($session['au_id'])?'Session %id% by %pseudonym%':'Session %id%', ['%id%' => $id, '%pseudonym%' => $session['au_pseudonym']]); + + //Set facebook title + $this->context['facebook']['metas']['og:title'] = $this->context['title'].' '.$this->translator->trans('at '.$session['l_title']); + + //Create application form for role_guest + if ($this->isGranted('ROLE_GUEST')) { //Set now $now = new \DateTime('now'); - //Create SessionEditType form - $session_edit = $this->createForm('Rapsys\AirBundle\Form\SessionEditType', null, [ + //Create SessionType form + $sessionForm = $this->createForm('Rapsys\AirBundle\Form\SessionType', null, [ //Set the action 'action' => $this->generateUrl('rapsys_air_session_edit', [ 'id' => $id ]), //Set the form attribute @@ -564,6 +735,8 @@ class SessionController extends DefaultController { 'admin' => $this->isGranted('ROLE_ADMIN'), //Set default user to current 'user' => $this->getUser()->getId(), + //Set date + 'date' => $session['date'], //Set begin 'begin' => $session['begin'], //Set length @@ -583,23 +756,11 @@ class SessionController extends DefaultController { ]); //Add form to context - $context['session_edit'] = $session_edit->createView(); - //Create login form for anonymous - } elseif (!$this->isGranted('IS_AUTHENTICATED_REMEMBERED')) { - //Create ApplicationType form - $login = $this->createForm('Rapsys\UserBundle\Form\LoginType', null, [ - //Set the action - 'action' => $this->generateUrl('rapsys_user_login'), - //Set the form attribute - 'attr' => [ 'class' => 'col' ] - ]); - - //Add form to context - $context['login'] = $login->createView(); + $this->context['forms']['session'] = $sessionForm->createView(); } //Add session in context - $context['session'] = [ + $this->context['session'] = [ 'id' => $id, 'date' => $session['date'], 'begin' => $session['begin'], @@ -622,7 +783,6 @@ class SessionController extends DefaultController { 'location' => [ 'id' => $session['l_id'], 'at' => $this->translator->trans('at '.$session['l_title']), - 'short' => $this->translator->trans($session['l_short']), 'title' => $this->translator->trans($session['l_title']), 'address' => $session['l_address'], 'zipcode' => $session['l_zipcode'], @@ -636,21 +796,28 @@ class SessionController extends DefaultController { ], 'snippet' => [ 'id' => $session['p_id'], - 'description' => $session['p_description'] + 'description' => $session['p_description'], + 'class' => $session['p_class'], + 'contact' => $session['p_contact'], + 'donate' => $session['p_donate'], + 'link' => $session['p_link'], + 'profile' => $session['p_profile'], + 'rate' => $session['p_rate'], + 'hat' => $session['p_hat'] ], 'applications' => null ]; //With application if (!empty($session['a_id'])) { - $context['session']['application'] = [ + $this->context['session']['application'] = [ 'user' => [ 'id' => $session['au_id'], - 'title' => $session['au_pseudonym'], - 'donation' => $session['au_donation'], - 'site' => $session['au_site'] + 'by' => $this->translator->trans('by %pseudonym%', [ '%pseudonym%' => $session['au_pseudonym'] ]), + 'title' => $session['au_pseudonym'] ], 'id' => $session['a_id'], + 'canceled' => $session['a_canceled'], 'title' => $this->translator->trans('Application %id%', [ '%id%' => $session['a_id'] ]), ]; } @@ -665,7 +832,8 @@ class SessionController extends DefaultController { //Extract applications created $session['sa_created'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_created'])); //Extract applications updated - $session['sa_updated'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_updated'])); + //XXX: done earlied when computing last modified + #$session['sa_updated'] = array_map(function($v){return new \DateTime($v);}, explode("\n", $session['sa_updated'])); //Extract applications canceled //XXX: canceled is null before cancelation, replace NULL with 'NULL' to avoid silent drop in mysql $session['sa_canceled'] = array_map(function($v){return $v==='NULL'?null:new \DateTime($v);}, explode("\n", $session['sa_canceled'])); @@ -676,9 +844,9 @@ class SessionController extends DefaultController { $session['sau_pseudonym'] = explode("\n", $session['sau_pseudonym']); //Init applications - $context['session']['applications'] = []; + $this->context['session']['applications'] = []; foreach($session['sa_id'] as $i => $sa_id) { - $context['session']['applications'][$sa_id] = [ + $this->context['session']['applications'][$sa_id] = [ 'user' => null, 'score' => $session['sa_score'][$i], 'created' => $session['sa_created'][$i], @@ -686,7 +854,7 @@ class SessionController extends DefaultController { 'canceled' => $session['sa_canceled'][$i] ]; if (!empty($session['sau_id'][$i])) { - $context['session']['applications'][$sa_id]['user'] = [ + $this->context['session']['applications'][$sa_id]['user'] = [ 'id' => $session['sau_id'][$i], 'title' => $session['sau_pseudonym'][$i] ]; @@ -701,7 +869,9 @@ class SessionController extends DefaultController { //Iterate on each day new \DateInterval('P1D'), //End with next sunday and 4 weeks - new \DateTime('Monday this week + 5 week') + new \DateTime( + $this->isGranted('IS_AUTHENTICATED_REMEMBERED')?'Monday this week + 3 week':'Monday this week + 2 week' + ) ); //Fetch locations @@ -709,6 +879,6 @@ class SessionController extends DefaultController { $locations = $doctrine->getRepository(Location::class)->findTranslatedSortedByPeriod($this->translator, $period, $session['au_id']); //Render the view - return $this->render('@RapsysAir/session/view.html.twig', ['title' => $title, 'section' => $section, 'locations' => $locations]+$context+$this->context); + return $this->render('@RapsysAir/session/view.html.twig', ['locations' => $locations]+$this->context, $response); } }