Finaly got the routing right.

This commit is contained in:
Tim Schumacher 2014-07-19 20:07:07 +02:00
parent 6f0b6eabef
commit 8f66f58965
2 changed files with 67 additions and 87 deletions

View file

@ -2,6 +2,7 @@
namespace Hackspace\Bundle\CalciferBundle\Controller; namespace Hackspace\Bundle\CalciferBundle\Controller;
use Doctrine\Common\Annotations\Annotation\Required;
use Doctrine\ORM\EntityManager; use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityRepository; use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\QueryBuilder; use Doctrine\ORM\QueryBuilder;
@ -35,11 +36,11 @@ class LocationController extends Controller
/** /**
* Finds and displays a Event entity. * Finds and displays a Event entity.
* *
* @Route("/{slug}(?!\.ics)", name="location_show") * @Route("/{slug}.{format}", name="location_show", defaults={"format" = "html"})
* @Method("GET") * @Method("GET")
* @Template("CalciferBundle:Event:index.html.twig") * @Template("CalciferBundle:Event:index.html.twig")
*/ */
public function showAction($slug) public function showAction($slug,$format)
{ {
/** @var EntityManager $em */ /** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
@ -67,52 +68,41 @@ class LocationController extends Controller
->andWhere('e.locations_id = :location') ->andWhere('e.locations_id = :location')
->orderBy('e.startdate') ->orderBy('e.startdate')
->setParameter('startdate',$now) ->setParameter('startdate',$now)
->setParameter('location',$location->getId()); ->setParameter('location',$location->id);
$entities = $qb->getQuery()->execute(); $entities = $qb->getQuery()->execute();
return array( if ($format == 'ics') {
'entities' => $entities, $calendar = new Calendar();
'location' => $location, $calendar->setProdId('-//My Company//Cool Calendar App//EN');
);
}
/** foreach ($entities as $entity) {
* Finds and displays a Event entity. /** @var Event $entity */
* $event = new CalendarEvent();
* @Route("/{slug}\.ics", name="location_show_ics") $event->setStart($entity->startdate);
* @Method("GET") $event->setEnd($entity->enddate);
*/ $event->setSummary($entity->summary);
public function showActionICS($slug) $event->setDescription($entity->description);
{ $location = new \Jsvrcek\ICS\Model\Description\Location();
$results = $this->showAction(str_replace('.ics','',$slug)); $location->setName($entity->getLocation()->name);
$entities = $results['entities']; $event->setLocations([$location]);
$calendar->addEvent($event);
}
$calendar = new Calendar(); $calendarExport = new CalendarExport(new CalendarStream, new Formatter());
$calendar->setProdId('-//My Company//Cool Calendar App//EN'); $calendarExport->addCalendar($calendar);
foreach($entities as $entity) { //output .ics formatted text
/** @var Event $entity */ $result = $calendarExport->getStream();
$event = new CalendarEvent();
$event->setStart($entity->getStartdate()); $response = new Response($result);
if ($entity->getEnddate() instanceof DateTime) $response->headers->set('Content-Type', 'text/calendar');
$event->setEnd($entity->getEnddate());
$event->setSummary($entity->getSummary()); return $response;
$event->setDescription($entity->getDescription()); } else {
$location = new \Jsvrcek\ICS\Model\Description\Location(); return array(
$location->setName($entity->getLocation()->getName()); 'entities' => $entities,
$event->setLocations([$location]); 'location' => $location,
$calendar->addEvent($event); );
} }
$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;
} }
} }

View file

@ -35,11 +35,11 @@ class TagController extends Controller
/** /**
* Finds and displays a Event entity. * Finds and displays a Event entity.
* *
* @Route("/{slug}(?!\.ics)", name="tag_show") * @Route("/{slug}.{format}", defaults={"format" = "html"}, name="tag_show")
* @Method("GET") * @Method("GET")
* @Template("CalciferBundle:Event:index.html.twig") * @Template("CalciferBundle:Event:index.html.twig")
*/ */
public function showAction($slug) public function showAction($slug, $format)
{ {
/** @var EntityManager $em */ /** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();
@ -55,60 +55,50 @@ class TagController extends Controller
} }
$now = new \DateTime(); $now = new \DateTime();
$now->setTime(0,0,0); $now->setTime(0, 0, 0);
/** @var QueryBuilder $qb */ /** @var QueryBuilder $qb */
$qb = $em->createQueryBuilder(); $qb = $em->createQueryBuilder();
$qb ->select(array('e')) $qb->select(array('e'))
->from('CalciferBundle:Event', 'e') ->from('CalciferBundle:Event', 'e')
->join('e.tags', 't', 'WITH', $qb->expr()->in('t.id', $tag->getId())) ->join('e.tags', 't', 'WITH', $qb->expr()->in('t.id', $tag->getId()))
->where('e.startdate >= :startdate') ->where('e.startdate >= :startdate')
->orderBy('e.startdate') ->orderBy('e.startdate')
->setParameter('startdate',$now); ->setParameter('startdate', $now);
$entities = $qb->getQuery()->execute(); $entities = $qb->getQuery()->execute();
return array( if ($format == 'ics') {
'entities' => $entities, $calendar = new Calendar();
'tag' => $tag, $calendar->setProdId('-//My Company//Cool Calendar App//EN');
);
}
/** foreach ($entities as $entity) {
* Finds and displays a Event entity. /** @var Event $entity */
* $event = new CalendarEvent();
* @Route("/{slug}.ics", name="tag_show_ics") $event->setStart($entity->startdate);
* @Method("GET") $event->setEnd($entity->enddate);
*/ $event->setSummary($entity->summary);
public function showActionICS($slug) $event->setDescription($entity->description);
{ $location = new \Jsvrcek\ICS\Model\Description\Location();
$results = $this->showAction(str_replace('.ics','',$slug)); $location->setName($entity->getLocation()->name);
$entities = $results['entities']; $event->setLocations([$location]);
$calendar->addEvent($event);
}
$calendar = new Calendar(); $calendarExport = new CalendarExport(new CalendarStream, new Formatter());
$calendar->setProdId('-//My Company//Cool Calendar App//EN'); $calendarExport->addCalendar($calendar);
foreach($entities as $entity) { //output .ics formatted text
/** @var Event $entity */ $result = $calendarExport->getStream();
$event = new CalendarEvent();
$event->setStart($entity->getStartdate()); $response = new Response($result);
$event->setEnd($entity->getEnddate()); $response->headers->set('Content-Type', 'text/calendar');
$event->setSummary($entity->getSummary());
$event->setDescription($entity->getDescription()); return $response;
$location = new \Jsvrcek\ICS\Model\Description\Location(); } else {
$location->setName($entity->getLocation()->getName()); return array(
$event->setLocations([$location]); 'entities' => $entities,
$calendar->addEvent($event); '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;
} }
} }