From 807749c8f56a16a87ec416089c23425b8ad2511c Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 7 Dec 2020 05:48:48 +0000 Subject: [PATCH 1/3] Add SAPBatteryContainerSize entity. #527 --- config/acl.yaml | 14 +++++++ config/menu.yaml | 4 ++ src/Entity/SAPBattery.php | 19 +++++++++ src/Entity/SAPBatteryContainerSize.php | 56 ++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/Entity/SAPBatteryContainerSize.php diff --git a/config/acl.yaml b/config/acl.yaml index cf6791d5..d39b6be3 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -472,3 +472,17 @@ access_keys: label: Update - id: sap_bsize.delete label: Delete + + - id: sap_csize + label: SAP Battery Container Size Access + acls: + - id: sap_csize.menu + label: Menu + - id: sap_csize.list + label: List + - id: sap_csize.add + label: Add + - id: sap_csize.update + label: Update + - id: sap_csize.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index 769c6d2d..d90ba3da 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -79,6 +79,10 @@ main_menu: acl: sap_bsize.list label: SAP Battery Sizes parent: sapbattery + - id: sapcsize_list + acl: sap_csize.list + label: SAP Container Sizes + parent: sapbattery - id: vehicle diff --git a/src/Entity/SAPBattery.php b/src/Entity/SAPBattery.php index 45c5c37e..284e2e32 100644 --- a/src/Entity/SAPBattery.php +++ b/src/Entity/SAPBattery.php @@ -51,6 +51,13 @@ class SAPBattery */ protected $flag_new; + // container size + /** + * @ORM\ManyToOne(targetEntity="SAPBatteryContainerSize", inversedBy="batteries") + * @ORM\JoinColumn(name="container_size_id", referencedColumnName="id") + */ + protected $container_size; + public function __construct() { $this->date_create = new DateTime(); @@ -110,4 +117,16 @@ class SAPBattery { return $this->flag_new; } + + public function setContainerSize($container_size) + { + $this->container_size = $container_size; + return $this; + } + + public function getContainerSize() + { + return $this->container_size; + } + } diff --git a/src/Entity/SAPBatteryContainerSize.php b/src/Entity/SAPBatteryContainerSize.php new file mode 100644 index 00000000..7a97df59 --- /dev/null +++ b/src/Entity/SAPBatteryContainerSize.php @@ -0,0 +1,56 @@ +batteries = new ArrayCollection(); + } + + public function getID() + { + return $this->id; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } +} From a9d631cbc2210fdab5574a3701327d3e0249ae9b Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 7 Dec 2020 06:46:43 +0000 Subject: [PATCH 2/3] Add list, create, update, and delete for container size. #527 --- config/routes/sap_battery.yaml | 35 +++ .../SAPBatteryContainerSizeController.php | 271 ++++++++++++++++++ .../sap-battery-container-size/form.html.twig | 134 +++++++++ .../sap-battery-container-size/list.html.twig | 146 ++++++++++ 4 files changed, 586 insertions(+) create mode 100644 src/Controller/SAPBatteryContainerSizeController.php create mode 100644 templates/sap-battery-container-size/form.html.twig create mode 100644 templates/sap-battery-container-size/list.html.twig diff --git a/config/routes/sap_battery.yaml b/config/routes/sap_battery.yaml index 99c4ba9b..960df237 100644 --- a/config/routes/sap_battery.yaml +++ b/config/routes/sap_battery.yaml @@ -111,3 +111,38 @@ sapbsize_delete: controller: App\Controller\SAPBatterySizeController::destroy methods: [DELETE] +# sap battery container sizes +sapcsize_list: + path: /sap-battery-container-sizes + controller: App\Controller\SAPBatteryContainerSizeController::index + +sapcsize_rows: + path: /sap-battery-container-sizes/rows + controller: App\Controller\SAPBatteryContainerSizeController::rows + methods: [POST] + +sapcsize_create: + path: /sap-battery-container-sizes/create + controller: App\Controller\SAPBatteryContainerSizeController::addForm + methods: [GET] + +sapcsize_create_submit: + path: /sap-battery-container-sizes/create + controller: App\Controller\SAPBatteryContainerSizeController::addSubmit + methods: [POST] + +sapcsize_update: + path: /sap-battery-container-sizes/{id} + controller: App\Controller\SAPBatteryContainerSizeController::updateForm + methods: [GET] + +sapcsize_update_submit: + path: /sap-battery-container-sizes/{id} + controller: App\Controller\SAPBatteryContainerSizeController::updateSubmit + methods: [POST] + +sapcsize_delete: + path: /sap-battery-container-sizes/{id} + controller: App\Controller\SAPBatteryContainerSizeController::destroy + methods: [DELETE] + diff --git a/src/Controller/SAPBatteryContainerSizeController.php b/src/Controller/SAPBatteryContainerSizeController.php new file mode 100644 index 00000000..bd95ae97 --- /dev/null +++ b/src/Controller/SAPBatteryContainerSizeController.php @@ -0,0 +1,271 @@ +denyAccessUnlessGranted('sap_csize.list', null, 'No access.'); + + return $this->render('sap-battery-container-size/list.html.twig'); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('sap_csize.list', null, 'No access.'); + + // build query + $qb = $this->getDoctrine() + ->getRepository(SAPBatteryContainerSize::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->request->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)'); + + // 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'); + + // 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.name 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'])) { + $order = $datatable['sort']['sort'] ?? 'asc'; + $query->orderBy('q.' . $datatable['sort']['field'], $order); + } else { + $query->orderBy('q.name', '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('sap_csize.update')) + $row['meta']['update_url'] = $this->generateUrl('sapcsize_update', ['id' => $row['id']]); + if ($this->isGranted('sap_csize.delete')) + $row['meta']['delete_url'] = $this->generateUrl('sapcsize_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="sapcsize_list") + */ + public function addForm() + { + $this->denyAccessUnlessGranted('sap_csize.add', null, 'No access.'); + + $params['obj'] = new SAPBatteryContainerSize(); + $params['mode'] = 'create'; + + // response + return $this->render('sap-battery-container-size/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('sap_csize.add', null, 'No access.'); + + // create new row + $em = $this->getDoctrine()->getManager(); + $row = new SAPBatteryContainerSize(); + + // set and save values + $row->setName($req->request->get('name')); + + // 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="sapcsize_list") + */ + public function updateForm($id) + { + $this->denyAccessUnlessGranted('sap_csize.update', null, 'No access.'); + + $params['mode'] = 'update'; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(SAPBatteryContainerSize::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + $params['obj'] = $row; + + // response + return $this->render('sap-battery-container-size/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('sap_csize.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(SAPBatteryContainerSize::class)->find($id); + + // make sure this row exists + if (empty($row)) + throw $this->createNotFoundException('The item does not exist'); + + // set and save values + $row->setName($req->request->get('name')); + + // 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_csize.delete', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(SAPBatteryContainerSize::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.name LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } +} + diff --git a/templates/sap-battery-container-size/form.html.twig b/templates/sap-battery-container-size/form.html.twig new file mode 100644 index 00000000..82173aa5 --- /dev/null +++ b/templates/sap-battery-container-size/form.html.twig @@ -0,0 +1,134 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

SAP Battery Container Sizes

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

+ {% if mode == 'update' %} + Edit SAP Battery Container Size + {{ obj.getName() }} + {% else %} + New SAP Battery Container Size + {% endif %} +

+
+
+
+
+
+
+ +
+ + + Display name for this container size +
+
+
+
+
+
+
+ + Back +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/templates/sap-battery-container-size/list.html.twig b/templates/sap-battery-container-size/list.html.twig new file mode 100644 index 00000000..00f83e1f --- /dev/null +++ b/templates/sap-battery-container-size/list.html.twig @@ -0,0 +1,146 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ SAP Battery Container Sizes +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} From b17a95e35a2f628014dbff0c70c7bd98f9d2c100 Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Mon, 7 Dec 2020 09:05:00 +0000 Subject: [PATCH 3/3] Add container size to SAP battery form. #527 --- src/Controller/SAPBatteryController.php | 31 ++++++++++++++++++++++--- templates/sap-battery/form.html.twig | 12 ++++++++++ templates/sap-battery/list.html.twig | 5 ++++ 3 files changed, 45 insertions(+), 3 deletions(-) diff --git a/src/Controller/SAPBatteryController.php b/src/Controller/SAPBatteryController.php index 389f3eb1..099c1998 100644 --- a/src/Controller/SAPBatteryController.php +++ b/src/Controller/SAPBatteryController.php @@ -5,6 +5,7 @@ namespace App\Controller; use App\Entity\SAPBattery; use App\Entity\SAPBatteryBrand; use App\Entity\SAPBatterySize; +use App\Entity\SAPBatteryContainerSize; use Doctrine\ORM\Query; use Symfony\Component\HttpFoundation\Request; @@ -42,6 +43,7 @@ class SAPBatteryController extends Controller $tquery = $qb->select('COUNT(q)') ->join('q.brand', 'brand') ->join('q.size', 'size'); + //->join('q.container_size', 'container_size'); // add filters to count query $this->setQueryFilters($datatable, $tquery); @@ -68,7 +70,8 @@ class SAPBatteryController extends Controller // build query $query = $qb->select('q') ->addSelect('brand.name as brand_name') - ->addSelect('size.name as size_name');; + ->addSelect('size.name as size_name'); + //->addSelect('container_size.name as container_size_name'); // add filters to query $this->setQueryFilters($datatable, $query); @@ -83,7 +86,8 @@ class SAPBatteryController extends Controller if (isset($datatable['sort']['field']) && !empty($datatable['sort']['field'])) { $prefix = ''; - if (!in_array($datatable['sort']['field'], ['brand_name', 'size_name'])) + //if (!in_array($datatable['sort']['field'], ['brand_name', 'size_name', 'container_size_name' ])) + if (!in_array($datatable['sort']['field'], ['brand_name', 'size_name' ])) $prefix = 'q.'; $order = $datatable['sort']['sort'] ?? 'asc'; @@ -105,6 +109,7 @@ class SAPBatteryController extends Controller $row['id'] = $orow[0]->getID(); $row['brand_name'] = $orow['brand_name']; $row['size_name'] = $orow['size_name']; + //$row['container_size_name'] = $orow['container_size_name']; $row['flag_new'] = $orow[0]->isNew(); // add row metadata @@ -144,6 +149,7 @@ class SAPBatteryController extends Controller // get parent associations $params['brands'] = $em->getRepository(SAPBatteryBrand::class)->findAll(); $params['sizes'] = $em->getRepository(SAPBatterySize::class)->findAll(); + $params['container_sizes'] = $em->getRepository(SAPBatteryContainerSize::class)->findAll(); // response return $this->render('sap-battery/form.html.twig', $params); @@ -187,6 +193,15 @@ class SAPBatteryController extends Controller else $row->setSize($size); + // custom validation for container size + $container_size = $em->getRepository(SAPBatteryContainerSize::class) + ->find($req->request->get('container_size')); + + if (empty($container_size)) + $error_array['container_size'] = 'Invalid container size selected.'; + else + $row->setContainerSize($container_size); + // validate $errors = $validator->validate($row); @@ -234,7 +249,7 @@ class SAPBatteryController extends Controller // get parent associations $params['brands'] = $em->getRepository(SAPBatteryBrand::class)->findAll(); $params['sizes'] = $em->getRepository(SAPBatterySize::class)->findAll(); - + $params['container_sizes'] = $em->getRepository(SAPBatteryContainerSize::class)->findAll(); $params['obj'] = $row; @@ -284,6 +299,15 @@ class SAPBatteryController extends Controller else $row->setSize($size); + // custom validation for container size + $container_size = $em->getRepository(SAPBatteryContainerSize::class) + ->find($req->request->get('container_size')); + + if (empty($container_size)) + $error_array['container_size'] = 'Invalid container size selected.'; + else + $row->setContainerSize($container_size); + // validate $errors = $validator->validate($row); @@ -338,6 +362,7 @@ class SAPBatteryController extends Controller $query->where('q.id LIKE :filter') ->orWhere('brand.name LIKE :filter') ->orWhere('size.name LIKE :filter') + //->orWhere('container_size.name LIKE :filter') ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); } } diff --git a/templates/sap-battery/form.html.twig b/templates/sap-battery/form.html.twig index 2cf2ac95..84e12d85 100644 --- a/templates/sap-battery/form.html.twig +++ b/templates/sap-battery/form.html.twig @@ -74,6 +74,18 @@
+
+ + + +