From 8f66f5896597e9f848c5d4745284219c3bdb25a1 Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 19 Jul 2014 20:07:07 +0200 Subject: [PATCH] Finaly got the routing right. --- .../Controller/LocationController.php | 76 ++++++++---------- .../Controller/TagController.php | 78 ++++++++----------- 2 files changed, 67 insertions(+), 87 deletions(-) diff --git a/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php b/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php index 32efe75..5f614de 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php +++ b/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php @@ -2,6 +2,7 @@ namespace Hackspace\Bundle\CalciferBundle\Controller; +use Doctrine\Common\Annotations\Annotation\Required; use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityRepository; use Doctrine\ORM\QueryBuilder; @@ -35,11 +36,11 @@ class LocationController extends Controller /** * Finds and displays a Event entity. * - * @Route("/{slug}(?!\.ics)", name="location_show") + * @Route("/{slug}.{format}", name="location_show", defaults={"format" = "html"}) * @Method("GET") * @Template("CalciferBundle:Event:index.html.twig") */ - public function showAction($slug) + public function showAction($slug,$format) { /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); @@ -67,52 +68,41 @@ class LocationController extends Controller ->andWhere('e.locations_id = :location') ->orderBy('e.startdate') ->setParameter('startdate',$now) - ->setParameter('location',$location->getId()); + ->setParameter('location',$location->id); $entities = $qb->getQuery()->execute(); - return array( - 'entities' => $entities, - 'location' => $location, - ); - } + if ($format == 'ics') { + $calendar = new Calendar(); + $calendar->setProdId('-//My Company//Cool Calendar App//EN'); - /** - * Finds and displays a Event entity. - * - * @Route("/{slug}\.ics", name="location_show_ics") - * @Method("GET") - */ - public function showActionICS($slug) - { - $results = $this->showAction(str_replace('.ics','',$slug)); - $entities = $results['entities']; + foreach ($entities as $entity) { + /** @var Event $entity */ + $event = new CalendarEvent(); + $event->setStart($entity->startdate); + $event->setEnd($entity->enddate); + $event->setSummary($entity->summary); + $event->setDescription($entity->description); + $location = new \Jsvrcek\ICS\Model\Description\Location(); + $location->setName($entity->getLocation()->name); + $event->setLocations([$location]); + $calendar->addEvent($event); + } - $calendar = new Calendar(); - $calendar->setProdId('-//My Company//Cool Calendar App//EN'); + $calendarExport = new CalendarExport(new CalendarStream, new Formatter()); + $calendarExport->addCalendar($calendar); - foreach($entities as $entity) { - /** @var Event $entity */ - $event = new CalendarEvent(); - $event->setStart($entity->getStartdate()); - if ($entity->getEnddate() instanceof DateTime) - $event->setEnd($entity->getEnddate()); - $event->setSummary($entity->getSummary()); - $event->setDescription($entity->getDescription()); - $location = new \Jsvrcek\ICS\Model\Description\Location(); - $location->setName($entity->getLocation()->getName()); - $event->setLocations([$location]); - $calendar->addEvent($event); + //output .ics formatted text + $result = $calendarExport->getStream(); + + $response = new Response($result); + $response->headers->set('Content-Type', 'text/calendar'); + + return $response; + } else { + return array( + 'entities' => $entities, + 'location' => $location, + ); } - - $calendarExport = new CalendarExport(new CalendarStream, new Formatter()); - $calendarExport->addCalendar($calendar); - - //output .ics formatted text - $result = $calendarExport->getStream(); - - $response = new Response($result); - $response->headers->set('Content-Type', 'text/calendar'); - - return $response; } } diff --git a/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php b/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php index b6128d9..ce4e9a7 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php +++ b/src/Hackspace/Bundle/CalciferBundle/Controller/TagController.php @@ -35,11 +35,11 @@ class TagController extends Controller /** * Finds and displays a Event entity. * - * @Route("/{slug}(?!\.ics)", name="tag_show") + * @Route("/{slug}.{format}", defaults={"format" = "html"}, name="tag_show") * @Method("GET") * @Template("CalciferBundle:Event:index.html.twig") */ - public function showAction($slug) + public function showAction($slug, $format) { /** @var EntityManager $em */ $em = $this->getDoctrine()->getManager(); @@ -55,60 +55,50 @@ class TagController extends Controller } $now = new \DateTime(); - $now->setTime(0,0,0); + $now->setTime(0, 0, 0); /** @var QueryBuilder $qb */ $qb = $em->createQueryBuilder(); - $qb ->select(array('e')) + $qb->select(array('e')) ->from('CalciferBundle:Event', 'e') ->join('e.tags', 't', 'WITH', $qb->expr()->in('t.id', $tag->getId())) ->where('e.startdate >= :startdate') ->orderBy('e.startdate') - ->setParameter('startdate',$now); + ->setParameter('startdate', $now); $entities = $qb->getQuery()->execute(); - return array( - 'entities' => $entities, - 'tag' => $tag, - ); - } + if ($format == 'ics') { + $calendar = new Calendar(); + $calendar->setProdId('-//My Company//Cool Calendar App//EN'); - /** - * Finds and displays a Event entity. - * - * @Route("/{slug}.ics", name="tag_show_ics") - * @Method("GET") - */ - public function showActionICS($slug) - { - $results = $this->showAction(str_replace('.ics','',$slug)); - $entities = $results['entities']; + foreach ($entities as $entity) { + /** @var Event $entity */ + $event = new CalendarEvent(); + $event->setStart($entity->startdate); + $event->setEnd($entity->enddate); + $event->setSummary($entity->summary); + $event->setDescription($entity->description); + $location = new \Jsvrcek\ICS\Model\Description\Location(); + $location->setName($entity->getLocation()->name); + $event->setLocations([$location]); + $calendar->addEvent($event); + } - $calendar = new Calendar(); - $calendar->setProdId('-//My Company//Cool Calendar App//EN'); + $calendarExport = new CalendarExport(new CalendarStream, new Formatter()); + $calendarExport->addCalendar($calendar); - foreach($entities as $entity) { - /** @var Event $entity */ - $event = new CalendarEvent(); - $event->setStart($entity->getStartdate()); - $event->setEnd($entity->getEnddate()); - $event->setSummary($entity->getSummary()); - $event->setDescription($entity->getDescription()); - $location = new \Jsvrcek\ICS\Model\Description\Location(); - $location->setName($entity->getLocation()->getName()); - $event->setLocations([$location]); - $calendar->addEvent($event); + //output .ics formatted text + $result = $calendarExport->getStream(); + + $response = new Response($result); + $response->headers->set('Content-Type', 'text/calendar'); + + return $response; + } else { + return array( + 'entities' => $entities, + 'tag' => $tag, + ); } - - $calendarExport = new CalendarExport(new CalendarStream, new Formatter()); - $calendarExport->addCalendar($calendar); - - //output .ics formatted text - $result = $calendarExport->getStream(); - - $response = new Response($result); - $response->headers->set('Content-Type', 'text/calendar'); - - return $response; } }