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'] . '%'); } } }