From e574ae966ae38d83a975e0f05c08a1f7f4e26d68 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Thu, 30 May 2019 09:21:23 +0000 Subject: [PATCH] Add geofence to menu under Location. Add list of saved coverage areas. #220 --- config/acl.yaml | 8 + config/menu.yaml | 2 +- config/routes/geofence.yaml | 35 ++++ src/Controller/GeofenceController.php | 179 ++++++++++++++++++++ templates/geofence/form.html.twig | 233 ++++++++++++++++++++++++++ templates/geofence/list.html.twig | 144 ++++++++++++++++ 6 files changed, 600 insertions(+), 1 deletion(-) create mode 100644 config/routes/geofence.yaml create mode 100644 src/Controller/GeofenceController.php create mode 100644 templates/geofence/form.html.twig create mode 100644 templates/geofence/list.html.twig diff --git a/config/acl.yaml b/config/acl.yaml index 3b0103bf..594a23f5 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -168,6 +168,14 @@ access_keys: acls: - id: geofence.menu label: Menu + - id: geofence.list + label: List + - id: geofence.add + label: Add + - id: geofence.update + label: Update + - id: geofence.delete + label: Delete - id: rider label: Rider Access diff --git a/config/menu.yaml b/config/menu.yaml index 202d81cd..ba9f0f2d 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -75,7 +75,7 @@ main_menu: acl: hub.menu label: Hub parent: location - - id: geofence + - id: geofence_list acl: geofence.menu label: Geofence parent: location diff --git a/config/routes/geofence.yaml b/config/routes/geofence.yaml new file mode 100644 index 00000000..b210e0db --- /dev/null +++ b/config/routes/geofence.yaml @@ -0,0 +1,35 @@ +#geofence + +geofence_list: + path: /geofence + controller: App\Controller\GeofenceController::index + +geofence_rows: + path: /geofence/rows + controller: App\Controller\GeofenceController::rows + methods: [POST] + +geofence_create: + path: /geofence/create + controller: App\Controller\GeofenceController::addForm + methods: [GET] + +geofence_create_submit: + path: /geofence/create + controller: App\Controller\GeofenceController::addSubmit + methods: [POST] + +geofence_update: + path: /geofence/{id} + controller: App\Controller\GeofenceController::updateForm + methods: [GET] + +geofence_update_submit: + path: /geofence/{id} + controller: App\Controller\GeofenceController::updateSubmit + methods: [POST] + +geofence_delete: + path: /geofence/{id} + controller: App\Controller\GeofenceController::destroy + methods: [DELETE] diff --git a/src/Controller/GeofenceController.php b/src/Controller/GeofenceController.php new file mode 100644 index 00000000..91585d91 --- /dev/null +++ b/src/Controller/GeofenceController.php @@ -0,0 +1,179 @@ +denyAccessUnlessGranted('geofence.list', null, 'No access.'); + + $params = $this->initParameters('geofence_list'); + + return $this->render('geofence/list.html.twig', $params); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('geofence.list', null, 'No access.'); + + // get query builder + $qb = $this->getDoctrine() + ->getRepository(SupportedArea::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)'); + $this->setQueryFilters($datatable, $tquery); + $total = $tquery->getQuery() + ->getSingleScalarResult(); + + // get current page number + $page = $datatable['pagination']['page'] ?? 1; + + $perpage = $datatable['pagination']['perpage']; + $offset = ($page - 1) * $perpage; + + // add metadata + $meta = [ + 'page' => $page, + 'perpage' => $perpage, + 'pages' => ceil($total / $perpage), + 'total' => $total, + 'sort' => 'asc', + 'field' => 'id' + ]; + + // build query + $query = $qb->select('q'); + $this->setQueryFilters($datatable, $query); + + // check if sorting is present, otherwise use default + if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { + $order = $datatable['sort']['sort'] ?? 'asc'; + $query->orderBy('q.' . $datatable['sort']['field'], $order); + } else { + $query->orderBy('q.id', 'asc'); + } + + // get rows for this page + $obj_rows = $query->setFirstResult($offset) + ->setMaxResults($perpage) + ->getQuery() + ->getResult(); + + // process rows + $rows = []; + foreach ($obj_rows as $orow) { + // add row data + $row['id'] = $orow->getID(); + $row['name'] = $orow->getName(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('geofence.update')) + $row['meta']['update_url'] = $this->generateUrl('geofence_update', ['id' => $row['id']]); + if ($this->isGranted('geofence.delete')) + $row['meta']['delete_url'] = $this->generateUrl('geofence_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + public function addForm() + { + $this->denyAccessUnlessGranted('geofence.add', null, 'No access.'); + + $params = $this->initParameters('geofence_list'); + $params['obj'] = new SupportedArea(); + $params['mode'] = 'create'; + + $this->fillFormTags($params); + + // response + return $this->render('geofence/form.html.twig', $params); + } + + public function updateForm($id) + { + $this->denyAccessUnlessGranted('geofence.update', null, 'No access.'); + + $params = $this->initParameters('geofence_list'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(SupportedArea::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $params['obj'] = $obj; + $params['mode'] = 'update'; + + $this->fillFormTags($params); + + // response + return $this->render('geofence/form.html.twig', $params); + } + + protected function initFormTags(&$params) + { + // default to editing, as we have more forms editing than creating + $params['ftags'] = [ + 'set_map_coordinate' => true, + ]; + } + + protected function fillFormTags(&$params) + { + $this->initFormTags($params); + + switch ($params['mode']) + { + case 'create': + $params['ftags']['set_map_coordinate'] = false; + break; + case 'update': + $params['ftags']['set_map_coordinate'] = true; + break; + } + } + + + protected function setQueryFilters($datatable, QueryBuilder $query) + { + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where('q.name LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } + +} diff --git a/templates/geofence/form.html.twig b/templates/geofence/form.html.twig new file mode 100644 index 00000000..c1f4a976 --- /dev/null +++ b/templates/geofence/form.html.twig @@ -0,0 +1,233 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Geofence

+
+
+
+ +
+ +
+
+
+
+
+
+ + + +

+ {% if mode == 'update' %} + Edit Geofence + {{ obj.getName }} + {% else %} + New Geofence + {% endif %} +

+
+
+
+
+
+
+
+ + + +
+
+
+
+ + + +
+ + + + +
+
+ +
+
+
+
+
+
+
+ + Back +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + + + + +{% endblock %} + + diff --git a/templates/geofence/list.html.twig b/templates/geofence/list.html.twig new file mode 100644 index 00000000..db6ad2cf --- /dev/null +++ b/templates/geofence/list.html.twig @@ -0,0 +1,144 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Geofence +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} + + +