From 0752fe3726f356f54f5d2a1fdcec4c21637dc0c9 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Fri, 27 Nov 2020 09:52:24 +0000 Subject: [PATCH] Add create, list, update, and delete for SAP Battery. #524 --- config/menu.yaml | 2 +- src/Controller/SAPBatteryBrandController.php | 6 +- src/Controller/SAPBatteryController.php | 332 +++++++++++++++++++ src/Controller/SAPBatterySizeController.php | 6 +- templates/sap-battery-size/form.html.twig | 2 +- templates/sap-battery/form.html.twig | 166 ++++++++++ templates/sap-battery/list.html.twig | 163 +++++++++ 7 files changed, 669 insertions(+), 8 deletions(-) create mode 100644 src/Controller/SAPBatteryController.php create mode 100644 templates/sap-battery/form.html.twig create mode 100644 templates/sap-battery/list.html.twig diff --git a/config/menu.yaml b/config/menu.yaml index 6afab689..769c6d2d 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -67,7 +67,7 @@ main_menu: acl: sap_battery.menu label: SAP Battery icon: fa fa-battery - - id: sapbattery.list + - id: sapbattery_list acl: sap_battery.list label: SAP Batteries parent: sapbattery diff --git a/src/Controller/SAPBatteryBrandController.php b/src/Controller/SAPBatteryBrandController.php index 5891bef5..700030f0 100644 --- a/src/Controller/SAPBatteryBrandController.php +++ b/src/Controller/SAPBatteryBrandController.php @@ -15,7 +15,7 @@ use Catalyst\MenuBundle\Annotation\Menu; class SAPBatteryBrandController extends Controller { /** - * @Menu(selected="sap_brand_list") + * @Menu(selected="sapbrand_list") */ public function index() { @@ -117,7 +117,7 @@ class SAPBatteryBrandController extends Controller } /** - * @Menu(selected="sap_brand_list") + * @Menu(selected="sapbrand_list") */ public function addForm() { @@ -172,7 +172,7 @@ class SAPBatteryBrandController extends Controller } /** - * @Menu(selected="sap_brand_list") + * @Menu(selected="sapbrand_list") */ public function updateForm($id) { diff --git a/src/Controller/SAPBatteryController.php b/src/Controller/SAPBatteryController.php new file mode 100644 index 00000000..19a547bc --- /dev/null +++ b/src/Controller/SAPBatteryController.php @@ -0,0 +1,332 @@ +denyAccessUnlessGranted('sap_battery.list', null, 'No access.'); + + return $this->render('sap-battery/list.html.twig'); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('sap_battery.list', null, 'No access.'); + + // build query + $qb = $this->getDoctrine() + ->getRepository(SAPBattery::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->request->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)') + ->join('q.brand', 'brand') + ->join('q.size', 'size'); + + // add filters to count query + $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') + ->addSelect('brand.name as brand_name') + ->addSelect('size.name as size_name');; + + // add filters to query + $this->setQueryFilters($datatable, $query); + + // check if filter is present + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where('q.id LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + + // check if sorting is present, otherwise use default + if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { + $prefix = ''; + + if (!in_array($datatable['sort']['field'], ['brand_name', 'size_name'])) + $prefix = 'q.'; + + $order = $datatable['sort']['sort'] ?? 'asc'; + $query->orderBy($prefix . $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[0]->getID(); + $row['brand_name'] = $orow['brand_name']; + $row['size_name'] = $orow['size_name']; + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('sap_battery.update')) + $row['meta']['update_url'] = $this->generateUrl('sapbattery_update', ['id' => $row['id']]); + if ($this->isGranted('sap_bsize.delete')) + $row['meta']['delete_url'] = $this->generateUrl('sapbattery_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="sapbattery_list") + */ + public function addForm() + { + $this->denyAccessUnlessGranted('sap_battery.add', null, 'No access.'); + + $params['obj'] = new SAPBattery(); + $params['mode'] = 'create'; + + $em = $this->getDoctrine()->getManager(); + + // get parent associations + $params['brands'] = $em->getRepository(SAPBatteryBrand::class)->findAll(); + $params['sizes'] = $em->getRepository(SAPBatterySize::class)->findAll(); + + // response + return $this->render('sap-battery/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('sap_battery.add', null, 'No access.'); + + // create new row + $em = $this->getDoctrine()->getManager(); + $row = new SAPBattery(); + + // set and save values + $row->setID($req->request->get('id')); + + // custom validation for battery brand + $brand = $em->getRepository(SAPBatteryBrand::class) + ->find($req->request->get('brand')); + + if (empty($brand)) + $error_array['brannd'] = 'Invalid brand selected.'; + else + $row->setBrand($brand); + + // custom validation for battery size + $size = $em->getRepository(SAPBatterySize::class) + ->find($req->request->get('size')); + + if (empty($size)) + $error_array['size'] = 'Invalid size selected.'; + else + $row->setSize($size); + + // validate + $errors = $validator->validate($row); + + // 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); + } else { + // validated! save the entity + $em->persist($row); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + /** + * @Menu(selected="sapbattery_list") + */ + public function updateForm($id) + { + $this->denyAccessUnlessGranted('sap_battery.update', null, 'No access.'); + + $params['mode'] = 'update'; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(SAPBattery::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // get parent associations + $params['brands'] = $em->getRepository(SAPBatteryBrand::class)->findAll(); + $params['sizes'] = $em->getRepository(SAPBatterySize::class)->findAll(); + + + $params['obj'] = $row; + + // response + return $this->render('sap-battery/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('sap_battery.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(SAPBattery::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // set and save values + $row->setID($req->request->get('id')); + + // custom validation for battery brand + $brand = $em->getRepository(SAPBatteryBrand::class) + ->find($req->request->get('brand')); + + if (empty($brand)) + $error_array['brand'] = 'Invalid brand selected.'; + else + $row->setBrand($brand); + + // custom validation for battery size + $size = $em->getRepository(SAPBatterySize::class) + ->find($req->request->get('size')); + + if (empty($size)) + $error_array['size'] = 'Invalid size selected.'; + else + $row->setSize($size); + + // validate + $errors = $validator->validate($row); + + // 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); + } else { + // validated! save the entity + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + public function destroy($id) + { + $this->denyAccessUnlessGranted('sap_battery.delete', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(SAPBattery::class)->find($id); + + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // delete this row + $em->remove($row); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + + // check if datatable filter is present and append to query + protected function setQueryFilters($datatable, &$query) + { + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where('q.id LIKE :filter') + ->orWhere('brand.name LIKE :filter') + ->orWhere('size.name LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } +} + diff --git a/src/Controller/SAPBatterySizeController.php b/src/Controller/SAPBatterySizeController.php index 8dff1c45..b2cd4138 100644 --- a/src/Controller/SAPBatterySizeController.php +++ b/src/Controller/SAPBatterySizeController.php @@ -15,7 +15,7 @@ use Catalyst\MenuBundle\Annotation\Menu; class SAPBatterySizeController extends Controller { /** - * @Menu(selected="sap_bsize_list") + * @Menu(selected="sapbsize_list") */ public function index() { @@ -117,7 +117,7 @@ class SAPBatterySizeController extends Controller } /** - * @Menu(selected="sap_bsize_list") + * @Menu(selected="sapbsize_list") */ public function addForm() { @@ -172,7 +172,7 @@ class SAPBatterySizeController extends Controller } /** - * @Menu(selected="sap_bsize_list") + * @Menu(selected="sapbsize_list") */ public function updateForm($id) { diff --git a/templates/sap-battery-size/form.html.twig b/templates/sap-battery-size/form.html.twig index a711de58..37671d9a 100644 --- a/templates/sap-battery-size/form.html.twig +++ b/templates/sap-battery-size/form.html.twig @@ -41,7 +41,7 @@
- Display name for this brand + Display name for this size
diff --git a/templates/sap-battery/form.html.twig b/templates/sap-battery/form.html.twig new file mode 100644 index 00000000..c2bf3fec --- /dev/null +++ b/templates/sap-battery/form.html.twig @@ -0,0 +1,166 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

SAP Batteries

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

+ {% if mode == 'update' %} + Edit SAP Battery + {{ obj.getID }} + {% else %} + New SAP Battery + {% endif %} +

+
+
+
+
+
+
+
+

+ Product Details +

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

+ SAP Batteries +

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