Extracted some duplicate code into a common function.

This commit is contained in:
Tim Schumacher 2014-11-15 21:30:49 +01:00
parent 097469b376
commit dbb74b06dc
3 changed files with 53 additions and 60 deletions

View file

@ -17,14 +17,9 @@ use Hackspace\Bundle\CalciferBundle\Entity\Event;
use Hackspace\Bundle\CalciferBundle\Form\EventType;
use Symfony\Component\HttpFoundation\Response;
use Jsvrcek\ICS\Model\Calendar;
use Jsvrcek\ICS\Model\CalendarEvent;
use Jsvrcek\ICS\Model\Relationship\Attendee;
use Jsvrcek\ICS\Model\Relationship\Organizer;
use Jsvrcek\ICS\Utility\Formatter;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\CalendarExport;
use Jsvrcek\ICS\Model\Description\Geo;
/**
* Location controller.
@ -77,27 +72,7 @@ class LocationController extends Controller
foreach ($entities as $entity) {
/** @var Event $entity */
$event = new CalendarEvent();
$event->setStart($entity->startdate);
if ($entity->enddate instanceof \DateTime)
$event->setEnd($entity->enddate);
$event->setSummary($entity->summary);
$event->setUrl($entity->url);
if ($entity->location instanceof Location) {
$location = new \Jsvrcek\ICS\Model\Description\Location();
$location->setName($entity->location->name);
$event->setLocations([$location]);
if (\is_float($entity->location->lon) && \is_float($entity->location->lat)) {
$geo = new Geo();
$geo->setLatitude($entity->location->lat);
$geo->setLongitude($entity->location->lon);
$event->setGeo($geo);
}
}
$event->setDescription($entity->description);
$location = new \Jsvrcek\ICS\Model\Description\Location();
$location->setName($entity->getLocation()->name);
$event->setLocations([$location]);
$event = $entity->ConvertToCalendarEvent();
$calendar->addEvent($event);
}
@ -152,7 +127,8 @@ class LocationController extends Controller
* @Route("/{slug}/bearbeiten", name="location_update")
* @Method("POST")
*/
public function updateAction(Request $request, $slug) {
public function updateAction(Request $request, $slug)
{
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
@ -168,7 +144,7 @@ class LocationController extends Controller
if ($location->name != $request->get('name')) {
$location->name = $request->get('name');
$location->slug = $location->generateSlug($location->name,$em);
$location->slug = $location->generateSlug($location->name, $em);
}
$location->streetaddress = $request->get('streetaddress');
$location->streetnumber = $request->get('streetnumber');
@ -177,7 +153,7 @@ class LocationController extends Controller
$location->description = $request->get('description');
$latlon = $request->get('geocords');
$latlon = explode(',',$latlon);
$latlon = explode(',', $latlon);
if (count($latlon) == 2) {
$location->lat = $latlon[0];
$location->lon = $latlon[1];

View file

@ -50,9 +50,9 @@ class TagController extends Controller
$repo = $em->getRepository('CalciferBundle:Tag');
$tags = [];
$operator = 'or';
if (strpos($slug,'|') !== false) {
$slugs = explode('|',$slug);
foreach($slugs as $item) {
if (strpos($slug, '|') !== false) {
$slugs = explode('|', $slug);
foreach ($slugs as $item) {
/** @var Tag $tag */
$tag = $repo->findOneBy(['slug' => $item]);
@ -60,10 +60,10 @@ class TagController extends Controller
$tags[] = $tag;
}
}
} else if (strpos($slug,'&') !== false) {
$slugs = explode('&',$slug);
} else if (strpos($slug, '&') !== false) {
$slugs = explode('&', $slug);
$operator = 'and';
foreach($slugs as $item) {
foreach ($slugs as $item) {
/** @var Tag $tag */
$tag = $repo->findOneBy(['slug' => $item]);
@ -103,7 +103,7 @@ WHERE tags @> array[@tags@]
AND e.startdate >= :startdate
ORDER BY e.startdate
EOF;
$tag_ids = array_reduce($tags,function($carry,$item){
$tag_ids = array_reduce($tags, function ($carry, $item) {
if (strlen($carry) == 0) {
return $item->id;
} else {
@ -111,14 +111,14 @@ EOF;
}
});
$sql = str_replace('@tags@',$tag_ids,$sql);
$sql = str_replace('@tags@', $tag_ids, $sql);
$rsm = new ResultSetMappingBuilder($em);
$rsm->addRootEntityFromClassMetadata('CalciferBundle:Event','e');
$rsm->addRootEntityFromClassMetadata('CalciferBundle:Event', 'e');
$query = $em->createNativeQuery($sql,$rsm);
$query = $em->createNativeQuery($sql, $rsm);
$query->setParameter('startdate',$now);
$query->setParameter('startdate', $now);
$entities = $query->getResult();
@ -131,7 +131,7 @@ EOF;
->orderBy('e.startdate')
->setParameter('startdate', $now);
$qb->join('e.tags', 't', 'WITH', $qb->expr()->in('t.id', array_reduce($tags,function($carry,$item){
$qb->join('e.tags', 't', 'WITH', $qb->expr()->in('t.id', array_reduce($tags, function ($carry, $item) {
if (strlen($carry) == 0) {
return $item->id;
} else {
@ -148,25 +148,7 @@ EOF;
foreach ($entities as $entity) {
/** @var Event $entity */
$event = new CalendarEvent();
$event->setStart($entity->startdate);
if ($entity->enddate instanceof \DateTime)
$event->setEnd($entity->enddate);
$event->setSummary($entity->summary);
$event->setDescription($entity->description);
$event->setUrl($entity->url);
$event->setUid($entity->slug);
if ($entity->location instanceof Location) {
$location = new \Jsvrcek\ICS\Model\Description\Location();
$location->setName($entity->location->name);
$event->setLocations([$location]);
if (\is_float($entity->location->lon) && \is_float($entity->location->lat)) {
$geo = new Geo();
$geo->setLatitude($entity->location->lat);
$geo->setLongitude($entity->location->lon);
$event->setGeo($geo);
}
}
$event = $entity->ConvertToCalendarEvent();
$calendar->addEvent($event);
}

View file

@ -2,8 +2,20 @@
namespace Hackspace\Bundle\CalciferBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Jsvrcek\ICS\Model\Description\Location;
use Symfony\Component\Validator\Constraints\DateTime;
use Jsvrcek\ICS\Model\Calendar;
use Jsvrcek\ICS\Model\CalendarEvent;
use Jsvrcek\ICS\Model\Relationship\Attendee;
use Jsvrcek\ICS\Model\Relationship\Organizer;
use Jsvrcek\ICS\Utility\Formatter;
use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\CalendarExport;
use Jsvrcek\ICS\Model\Description\Geo;
/**
* Event
@ -129,4 +141,27 @@ class Event extends BaseEntity
}
return $retval;
}
public function ConvertToCalendarEvent() {
$event = new CalendarEvent();
$event->setStart($this->startdate);
if ($this->enddate instanceof \DateTime)
$event->setEnd($this->enddate);
$event->setSummary($this->summary);
$event->setUrl($this->url);
if ($this->location instanceof Location) {
$location = new Location();
$location->setName($this->location->name);
$event->setLocations([$location]);
if (\is_float($this->location->lon) && \is_float($this->location->lat)) {
$geo = new Geo();
$geo->setLatitude($this->location->lat);
$geo->setLongitude($this->location->lon);
$event->setGeo($geo);
}
}
$event->setDescription($this->description);
return $event;
}
}