From 9043dc31c807e75576a3fa57e8091d208873898f Mon Sep 17 00:00:00 2001 From: Tim Schumacher Date: Sat, 11 Apr 2015 13:18:55 +0200 Subject: [PATCH] Integrated selectize with the location chooser. --- .../Controller/LocationController.php | 48 +++++++++++++++++++ .../Resources/assets/js/events.js | 47 ++++++++++++++++++ 2 files changed, 95 insertions(+) diff --git a/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php b/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php index a9ec487..bbdb83b 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php +++ b/src/Hackspace/Bundle/CalciferBundle/Controller/LocationController.php @@ -20,6 +20,7 @@ use Jsvrcek\ICS\Model\Calendar; use Jsvrcek\ICS\Utility\Formatter; use Jsvrcek\ICS\CalendarStream; use Jsvrcek\ICS\CalendarExport; +use Symfony\Component\HttpFoundation\AcceptHeader; /** * Location controller. @@ -174,4 +175,51 @@ class LocationController extends Controller return $this->redirect($this->generateUrl('location_show', array('slug' => $location->slug))); } + + /** + * Finds and displays a Event entity. + * + * @Route("/") + * @Method("GET") + */ + public function indexAction() { + $accepts = AcceptHeader::fromString($this->getRequest()->headers->get('Accept')); + if ($accepts->has('application/json')) { + $em = $this->getDoctrine()->getManager(); + + /** @var QueryBuilder $qb */ + $qb = $em->createQueryBuilder(); + $qb->select(['l']) + ->from('CalciferBundle:Location', 'l') + ->where('lower(l.name) LIKE lower(:location)') + ->orderBy('l.name') + ->setParameter('location', sprintf('%%%s%%',$this->getRequest()->query->get('q'))); + + $entities = $qb->getQuery()->execute(); + + $locations = []; + foreach($entities as $location) { + /** @var Location $location */ + $locations[] = array( + 'id' => $location->id, + 'name' => $location->name, + 'description' => \Michelf\Markdown::defaultTransform($location->description), + 'streetaddress' => $location->streetaddress, + 'streetnumber' => $location->streetnumber, + 'zipcode' => $location->zipcode, + 'city' => $location->city, + 'lon' => $location->lon, + 'lat' => $location->lat, + ); + } + + + $response = new Response(json_encode($locations)); + $response->headers->set('Content-Type', 'application/json'); + + return $response; + } else { + return $this->redirect('/'); + } + } } diff --git a/src/Hackspace/Bundle/CalciferBundle/Resources/assets/js/events.js b/src/Hackspace/Bundle/CalciferBundle/Resources/assets/js/events.js index 30f739b..22e1c14 100755 --- a/src/Hackspace/Bundle/CalciferBundle/Resources/assets/js/events.js +++ b/src/Hackspace/Bundle/CalciferBundle/Resources/assets/js/events.js @@ -142,6 +142,53 @@ $(document).ready(function() { } }); + $('#event_location').selectize({ + create: true, + diacritics: true, + valueField: 'name', + labelField: 'name', + searchField: 'name', + maxItems: 1, + render: { + item: function(data,escape){ + console.log([data,escape]); + return '
' + escape(data.name) + '
'; + }, + option: function(item, escape) { + return '
' + + '
'+ + '
' + + '' + escape(item.name) + + '
' + + '
'+ + (item.lon && item.lat ? 'lon: '+ escape(item.lon)+' lat: ' + escape(item.lat) : '')+ + (item.streetaddress ? ' Anschrift: ' + item.streetaddress + ' ' + item.streetnumber + ' ' + item.zipcode + ' ' + item.city : '')+ + '
'+ + (item.description ? '
' + item.description + '
' : '') + + '
'+ + '
'; + } + }, + load: function(query, callback) { + if (!query.length) return callback(); + $.ajax({ + url: "/orte/", + type: "GET", + dataType: 'json', + data: { + q: query + }, + error: function() { + callback(); + }, + success: function(res) { + console.log(res); + callback(res); + } + }); + } + }); + if (view_map_selector.length == 1) { jQuery('.show_map').click(addGeoCoordinates); map = L.map('view-map');