denyAccessUnlessGranted('hub.list', null, 'No access.'); return $this->render('hub/list.html.twig'); } public function rows(Request $req) { $this->denyAccessUnlessGranted('hub.list', null, 'No access.'); // get query builder $qb = $this->getDoctrine() ->getRepository(Hub::class) ->createQueryBuilder('q'); // get datatable params $datatable = $req->request->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(); $row['branch'] = $orow->getBranch(); $row['address'] = $orow->getAddress(); $row['contact_nums'] = $orow->getContactNumbers(); $row['time_open'] = $orow->getTimeOpen()->format('g:i A'); $row['time_close'] = $orow->getTimeClose()->format('g:i A'); // add row metadata $row['meta'] = [ 'update_url' => '', 'delete_url' => '' ]; // add crud urls if ($this->isGranted('hub.update')) $row['meta']['update_url'] = $this->generateUrl('hub_update', ['id' => $row['id']]); if ($this->isGranted('hub.delete')) $row['meta']['delete_url'] = $this->generateUrl('hub_delete', ['id' => $row['id']]); $rows[] = $row; } // response return $this->json([ 'meta' => $meta, 'data' => $rows ]); } /** * @Menu(selected="hub_list") */ public function addForm() { $this->denyAccessUnlessGranted('hub.add', null, 'No access.'); $params = []; $params['obj'] = new Hub(); $params['mode'] = 'create'; // response return $this->render('hub/form.html.twig', $params); } protected function setObject(Hub $obj, Request $req) { // coordinates $point = new Point($req->request->get('coord_lng'), $req->request->get('coord_lat')); // times $format = 'g:i A'; $time_open = DateTime::createFromFormat($format, $req->request->get('time_open')); $time_close = DateTime::createFromFormat($format, $req->request->get('time_close')); // set and save values $obj->setName($req->request->get('name')) ->setBranch($req->request->get('branch')) ->setAddress($req->request->get('address')) ->setContactNumbers($req->request->get('contact_nums')) ->setTimeOpen($time_open) ->setTimeClose($time_close) ->setCoordinates($point); } 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') ->orWhere('q.address LIKE :filter') ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); } } public function addSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator) { $this->denyAccessUnlessGranted('hub.add', null, 'No access.'); error_log($req->request->get('time_open')); // create new object $em = $this->getDoctrine()->getManager(); $obj = new Hub(); $this->setObject($obj, $req); // validate $errors = $validator->validate($obj); // initialize error list $error_array = []; // add errors to list foreach ($errors as $error) { $error_array[$error->getPropertyPath()] = $error->getMessage(); } // check if any errors were found if (!empty($error_array)) { // return validation failure response return $this->json([ 'success' => false, 'errors' => $error_array ], 422); } // validated! save the entity $em->persist($obj); $em->flush(); // return successful response return $this->json([ 'success' => 'Changes have been saved!' ]); } /** * @Menu(selected="hub_list") */ public function updateForm($id) { $this->denyAccessUnlessGranted('hub.update', null, 'No access.'); // get row data $em = $this->getDoctrine()->getManager(); $obj = $em->getRepository(Hub::class)->find($id); // make sure this row exists if (empty($obj)) throw $this->createNotFoundException('The item does not exist'); $params = []; $params['obj'] = $obj; $params['mode'] = 'update'; // response return $this->render('hub/form.html.twig', $params); } public function updateSubmit(Request $req, EncoderFactoryInterface $ef, ValidatorInterface $validator, $id) { $this->denyAccessUnlessGranted('hub.update', null, 'No access.'); // get object data $em = $this->getDoctrine()->getManager(); $obj = $em->getRepository(Hub::class)->find($id); // make sure this object exists if (empty($obj)) throw $this->createNotFoundException('The item does not exist'); $this->setObject($obj, $req); // validate $errors = $validator->validate($obj); // initialize error list $error_array = []; // add errors to list foreach ($errors as $error) { $error_array[$error->getPropertyPath()] = $error->getMessage(); } // check if any errors were found if (!empty($error_array)) { // return validation failure response return $this->json([ 'success' => false, 'errors' => $error_array ], 422); } // validated! save the entity $em->flush(); // return successful response return $this->json([ 'success' => 'Changes have been saved!' ]); } public function destroy($id) { $this->denyAccessUnlessGranted('hub.delete', null, 'No access.'); // get object data $em = $this->getDoctrine()->getManager(); $obj = $em->getRepository(Hub::class)->find($id); if (empty($obj)) throw $this->createNotFoundException('The item does not exist'); // delete this object $em->remove($obj); $em->flush(); // response $response = new Response(); $response->setStatusCode(Response::HTTP_OK); $response->send(); } public function nearest(MapTools $map_tools, Request $req) { // get lat / long $lat = $req->query->get('lat'); $long = $req->query->get('long'); // get nearest hubs according to position $point = new Point($long, $lat); $result = $map_tools->getClosestHubs($point, 10, date("H:i:s")); $hubs = []; foreach ($result as $hub_res) { $hub = $hub_res['hub']; $coords = $hub->getCoordinates(); $hubs[] = [ 'id' => $hub->getID(), 'long' => $coords->getLongitude(), 'lat' => $coords->getLatitude(), 'label' => $hub->getFullName(), 'name' => $hub->getName(), 'branch' => $hub->getBranch(), 'cnum' => $hub->getContactNumbers(), 'distance' => $hub_res['distance'], ]; } return $this->json([ 'hubs' => $hubs, ]); } public function getHubRiders(Request $req, RiderTracker $rider_tracker) { // get hub id $hub_id = $req->query->get('id'); // get hub $em = $this->getDoctrine()->getManager(); $hub = $em->getRepository(Hub::class)->find($hub_id); // make sure this row exists if (empty($hub)) throw $this->createNotFoundException('The item does not exist'); //TODO: get available riders sort by proximity, show 10 $available_riders = $hub->getAvailableRiders(); $riders = []; // TODO: remove this later when we don't get all available riders $riders_limit = 5; $num_riders = 0; foreach ($available_riders as $rider) { if ($num_riders > $riders_limit) break; // get location for each rider $rider_id = $rider->getID(); $coordinates = $rider_tracker->getRiderLocation($rider_id); $lat = $coordinates->getLatitude(); $long = $coordinates->getLongitude(); $riders[] = [ 'id' => $rider->getID(), 'first_name' => $rider->getFirstName(), 'last_name' => $rider->getLastName(), 'contact_num' => $rider->getContactNumber(), 'plate_num' => $rider->getPlateNumber(), 'location' => [$lat, $long], ]; $num_riders++; } return $this->json([ 'riders' => $riders, ]); } }