Major overhaul:

* Switched to semantic ui.
* Add searching by location or tag
* Reworked the forms
This commit is contained in:
Tim Schumacher 2014-07-08 00:11:48 +02:00
parent 741e1c403a
commit 1f6c3c5fd4
51 changed files with 43286 additions and 771 deletions

View file

@ -2,6 +2,9 @@
namespace Hackspace\Bundle\CalciferBundle\Controller;
use Doctrine\ORM\EntityManager;
use Hackspace\Bundle\CalciferBundle\Entity\Location;
use Hackspace\Bundle\CalciferBundle\Entity\Tag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
@ -29,7 +32,7 @@ class EventController extends Controller
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('CalciferBundle:Event')->findAll();
$entities = $em->getRepository('CalciferBundle:Event')->findBy([],['startdate' => 'ASC']);
return array(
'entities' => $entities,
@ -49,6 +52,52 @@ class EventController extends Controller
$entity->setSummary($request->get('summary'));
$entity->setUrl($request->get('url'));
$startdate = $request->get('startdate');
$startdate = new \DateTime($startdate);
$entity->setStartdate($startdate);
$enddate = $request->get('enddate');
if (strlen($enddate) > 0) {
$enddate = new \DateTime($enddate);
$entity->setenddate($enddate);
}
$location = $request->get('location');
if (strlen($location) > 0) {
// check if the location already exists
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('CalciferBundle:Location');
$results = $repo->findBy(['name' => $location]);
if (count($results) > 0) {
$entity->setLocation($results[0]);
} else {
$location_obj = new Location();
$location_obj->setName($location);
$em->persist($location_obj);
$em->flush();
$entity->setLocation($location_obj);
}
}
$tags = $request->get('tags');
if (strlen($tags) > 0) {
$tags = explode(',',$tags);
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('CalciferBundle:Tag');
foreach ($tags as $tag) {
$tag = trim($tag);
$results = $repo->findBy(['name' => $tag]);
if (count($results) > 0) {
$entity->addTag($results[0]);
} else {
$tag_obj = new Tag();
$tag_obj->setName($tag);
$em->persist($tag_obj);
$em->flush();
$entity->addTag($tag_obj);
}
}
}
if ($entity->isValid()) {
@ -64,25 +113,6 @@ class EventController extends Controller
);
}
/**
* Creates a form to create a Event entity.
*
* @param Event $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createCreateForm(Event $entity)
{
$form = $this->createForm(new EventType(), $entity, array(
'action' => $this->generateUrl('_create'),
'method' => 'POST',
));
$form->add('submit', 'submit', array('label' => 'Create'));
return $form;
}
/**
* Displays a form to create a new Event entity.
*
@ -93,11 +123,9 @@ class EventController extends Controller
public function newAction()
{
$entity = new Event();
$form = $this->createCreateForm($entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
@ -118,11 +146,8 @@ class EventController extends Controller
throw $this->createNotFoundException('Unable to find Event entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
'entity' => $entity
);
}
@ -143,107 +168,92 @@ class EventController extends Controller
throw $this->createNotFoundException('Unable to find Event entity.');
}
$editForm = $this->createEditForm($entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Creates a form to edit a Event entity.
*
* @param Event $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Event $entity)
{
$form = $this->createForm(new EventType(), $entity, array(
'action' => $this->generateUrl('_update', array('id' => $entity->getId())),
'method' => 'PUT',
));
$form->add('submit', 'submit', array('label' => 'Update'));
return $form;
}
/**
* Edits an existing Event entity.
*
* @Route("/{id}", name="_update")
* @Method("PUT")
* @Method("POST")
* @Template("CalciferBundle:Event:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
/** @var Event $entity */
$entity = $em->getRepository('CalciferBundle:Event')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createEditForm($entity);
$editForm->handleRequest($request);
$entity->setDescription($request->get('description'));
$entity->setSummary($request->get('summary'));
$entity->setUrl($request->get('url'));
$startdate = $request->get('startdate');
$startdate = new \DateTime($startdate);
$entity->setStartdate($startdate);
if ($editForm->isValid()) {
$enddate = $request->get('enddate');
if (strlen($enddate) > 0) {
$enddate = new \DateTime($enddate);
$entity->setenddate($enddate);
}
$location = $request->get('location');
if (strlen($location) > 0) {
// check if the location already exists
/** @var EntityManager $em */
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('CalciferBundle:Location');
$results = $repo->findBy(['name' => $location]);
if (count($results) > 0) {
$entity->setLocation($results[0]);
} else {
$location_obj = new Location();
$location_obj->setName($location);
$em->persist($location_obj);
$em->flush();
$entity->setLocation($location_obj);
}
}
$tags = $request->get('tags');
if (strlen($tags) > 0) {
$tags = explode(',',$tags);
$em = $this->getDoctrine()->getManager();
$repo = $em->getRepository('CalciferBundle:Tag');
foreach ($tags as $tag) {
$tag = trim($tag);
$results = $repo->findBy(['name' => $tag]);
if (count($results) > 0) {
$entity->addTag($results[0]);
} else {
$tag_obj = new Tag();
$tag_obj->setName($tag);
$em->persist($tag_obj);
$em->flush();
$entity->addTag($tag_obj);
}
}
}
if ($entity->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('_edit', array('id' => $id)));
return $this->redirect($this->generateUrl('_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Event entity.
*
* @Route("/{id}", name="_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->handleRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('CalciferBundle:Event')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Event entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl(''));
}
/**
* Creates a form to delete a Event entity by id.
*
* @param mixed $id The entity id
*
* @return \Symfony\Component\Form\Form The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder()
->setAction($this->generateUrl('_delete', array('id' => $id)))
->setMethod('DELETE')
->add('submit', 'submit', array('label' => 'Delete'))
->getForm()
;
}
}

View file

@ -0,0 +1,100 @@
<?php
namespace Hackspace\Bundle\CalciferBundle\Controller;
use Doctrine\ORM\EntityManager;
use Hackspace\Bundle\CalciferBundle\Entity\Location;
use Hackspace\Bundle\CalciferBundle\Entity\Tag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
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 Symfony\Component\Validator\Constraints\DateTime;
/**
* Location controller.
*
* @Route("/locations")
*/
class LocationController extends Controller
{
/**
* Finds and displays a Event entity.
*
* @Route("/{id}", requirements={"id" = "\d+"}, name="location_show")
* @Method("GET")
* @Template("CalciferBundle:Event:index.html.twig")
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
/** @var Location $location */
$location = $em->getRepository('CalciferBundle:Location')->find($id);
if (!$location) {
throw $this->createNotFoundException('Unable to find Location entity.');
}
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('CalciferBundle:Event')->findBy(['locations_id' => $location->getId()],['startdate' => 'ASC']);
return array(
'entities' => $entities,
'location' => $location,
);
}
/**
* Finds and displays a Event entity.
*
* @Route("/{id}.ics", requirements={"id" = "\d+"}, name="location_show_ics")
* @Method("GET")
*/
public function showActionICS($id)
{
$results = $this->showAction(str_replace('.ics','',$id));
$entities = $results['entities'];
$calendar = new Calendar();
$calendar->setProdId('-//My Company//Cool Calendar App//EN');
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);
}
$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

@ -0,0 +1,103 @@
<?php
namespace Hackspace\Bundle\CalciferBundle\Controller;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\QueryBuilder;
use Hackspace\Bundle\CalciferBundle\Entity\Location;
use Hackspace\Bundle\CalciferBundle\Entity\Tag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
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;
/**
* Tag controller.
*
* @Route("/tags")
*/
class TagController extends Controller
{
/**
* Finds and displays a Event entity.
*
* @Route("/{id}", requirements={"id" = "\d+"}, name="tag_show")
* @Method("GET")
* @Template("CalciferBundle:Event:index.html.twig")
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
/** @var Tag $tag */
$tag = $em->getRepository('CalciferBundle:Tag')->find($id);
if (!$tag) {
throw $this->createNotFoundException('Unable to find tag entity.');
}
/** @var QueryBuilder $qb */
$qb = $em->createQueryBuilder();
$qb ->select(array('e'))
->from('CalciferBundle:Event', 'e')
->join('e.tags', 't', 'WITH', $qb->expr()->in('t.id', $tag->getId()))
->orderBy('e.startdate');
$entities = $qb->getQuery()->execute();
return array(
'entities' => $entities,
'tag' => $tag,
);
}
/**
* Finds and displays a Event entity.
*
* @Route("/{id}.ics", requirements={"id" = "\d+"}, name="tag_show_ics")
* @Method("GET")
*/
public function showActionICS($id)
{
$results = $this->showAction(str_replace('.ics','',$id));
$entities = $results['entities'];
$calendar = new Calendar();
$calendar->setProdId('-//My Company//Cool Calendar App//EN');
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);
}
$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;
}
}