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 Hackspace\Bundle\CalciferBundle\Form\EventType;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Jsvrcek\ICS\Model\Calendar; 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\Utility\Formatter;
use Jsvrcek\ICS\CalendarStream; use Jsvrcek\ICS\CalendarStream;
use Jsvrcek\ICS\CalendarExport; use Jsvrcek\ICS\CalendarExport;
use Jsvrcek\ICS\Model\Description\Geo;
/** /**
* Location controller. * Location controller.
@ -77,27 +72,7 @@ class LocationController extends Controller
foreach ($entities as $entity) { foreach ($entities as $entity) {
/** @var Event $entity */ /** @var Event $entity */
$event = new CalendarEvent(); $event = $entity->ConvertToCalendarEvent();
$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]);
$calendar->addEvent($event); $calendar->addEvent($event);
} }
@ -152,7 +127,8 @@ class LocationController extends Controller
* @Route("/{slug}/bearbeiten", name="location_update") * @Route("/{slug}/bearbeiten", name="location_update")
* @Method("POST") * @Method("POST")
*/ */
public function updateAction(Request $request, $slug) { public function updateAction(Request $request, $slug)
{
/** @var EntityManager $em */ /** @var EntityManager $em */
$em = $this->getDoctrine()->getManager(); $em = $this->getDoctrine()->getManager();

View file

@ -148,25 +148,7 @@ EOF;
foreach ($entities as $entity) { foreach ($entities as $entity) {
/** @var Event $entity */ /** @var Event $entity */
$event = new CalendarEvent(); $event = $entity->ConvertToCalendarEvent();
$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);
}
}
$calendar->addEvent($event); $calendar->addEvent($event);
} }

View file

@ -2,8 +2,20 @@
namespace Hackspace\Bundle\CalciferBundle\Entity; namespace Hackspace\Bundle\CalciferBundle\Entity;
use Doctrine\ORM\Mapping as ORM; use Doctrine\ORM\Mapping as ORM;
use Jsvrcek\ICS\Model\Description\Location;
use Symfony\Component\Validator\Constraints\DateTime; 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 * Event
@ -129,4 +141,27 @@ class Event extends BaseEntity
} }
return $retval; 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;
}
} }