From 8f196000a846e3e0f7b3f6816ae30490097df12a Mon Sep 17 00:00:00 2001 From: Korina Cordero Date: Tue, 22 Aug 2023 08:36:51 +0000 Subject: [PATCH] Add permisssions, menu, forms, entity, for ServiceOffering. #758 --- config/packages/catalyst_auth.yaml | 16 +- config/packages/catalyst_menu.yaml | 6 +- config/routes/service_offering.yaml | 34 +++ src/Controller/ServiceOfferingController.php | 252 +++++++++++++++++++ src/Entity/ServiceOffering.php | 81 ++++++ templates/service-offering/form.html.twig | 151 +++++++++++ templates/service-offering/list.html.twig | 150 +++++++++++ translations/messages.en.yaml | 1 + 8 files changed, 689 insertions(+), 2 deletions(-) create mode 100644 config/routes/service_offering.yaml create mode 100644 src/Controller/ServiceOfferingController.php create mode 100644 src/Entity/ServiceOffering.php create mode 100644 templates/service-offering/form.html.twig create mode 100644 templates/service-offering/list.html.twig diff --git a/config/packages/catalyst_auth.yaml b/config/packages/catalyst_auth.yaml index 4a13e9e4..f4e5461a 100644 --- a/config/packages/catalyst_auth.yaml +++ b/config/packages/catalyst_auth.yaml @@ -618,6 +618,20 @@ catalyst_auth: - id: ownership_type.delete label: Delete + - id: service_offering + label: Service Offering Access + acls: + - id: service_offering.menu + label: Menu + - id: service_offering.list + label: List + - id: service_offering.add + label: Add + - id: service_offering.update + label: Update + - id: service_offering.delete + label: Delete + api: user_entity: "App\\Entity\\ApiUser" acl_data: @@ -876,4 +890,4 @@ catalyst_auth: - id: cust_api_v2.warranty.check label: Check Status - id: cust_api_v2.warranty.register - label: Register \ No newline at end of file + label: Register diff --git a/config/packages/catalyst_menu.yaml b/config/packages/catalyst_menu.yaml index 48947331..98bce994 100644 --- a/config/packages/catalyst_menu.yaml +++ b/config/packages/catalyst_menu.yaml @@ -279,4 +279,8 @@ catalyst_menu: - id: ownership_type_list acl: ownership_type.menu label: '[menu.database.ownershiptypes]' - parent: database \ No newline at end of file + parent: database + - id: service_offering_list + acl: service_offering.menu + label: '[menu.database.serviceofferings]' + parent: database diff --git a/config/routes/service_offering.yaml b/config/routes/service_offering.yaml new file mode 100644 index 00000000..4f87ca89 --- /dev/null +++ b/config/routes/service_offering.yaml @@ -0,0 +1,34 @@ +service_offering_list: + path: /service-offerings + controller: App\Controller\ServiceOfferingController::index + methods: [GET] + +service_offering_rows: + path: /service-offerings/rowdata + controller: App\Controller\ServiceOfferingController::datatableRows + methods: [POST] + +service_offering_add_form: + path: /service-offerings/newform + controller: App\Controller\ServiceOfferingController::addForm + methods: [GET] + +service_offering_add_submit: + path: /service-offerings + controller: App\Controller\ServiceOfferingController::addSubmit + methods: [POST] + +service_offering_update_form: + path: /service-offerings/{id} + controller: App\Controller\ServiceOfferingController::updateForm + methods: [GET] + +service_offering_update_submit: + path: /service-offerings/{id} + controller: App\Controller\ServiceOfferingController::updateSubmit + methods: [POST] + +service_offering_delete: + path: /service-offerings/{id} + controller: App\Controller\ServiceOfferingController::deleteSubmit + methods: [DELETE] diff --git a/src/Controller/ServiceOfferingController.php b/src/Controller/ServiceOfferingController.php new file mode 100644 index 00000000..66a447a7 --- /dev/null +++ b/src/Controller/ServiceOfferingController.php @@ -0,0 +1,252 @@ +render('service-offering/list.html.twig'); + } + + /** + * @IsGranted("service_offering.list") + */ + public function datatableRows(Request $req) + { + // get query builder + $qb = $this->getDoctrine() + ->getRepository(ServiceOffering::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['fee'] = $orow->getFee(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('service_offering.update')) + $row['meta']['update_url'] = $this->generateUrl('service_offering_update_form', ['id' => $row['id']]); + if ($this->isGranted('service_offering.delete')) + $row['meta']['delete_url'] = $this->generateUrl('service_offering_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="service_offering.list") + * @IsGranted("service_offering.add") + */ + public function addForm() + { + $service_offering = new ServiceOffering(); + $params = [ + 'service_offering' => $service_offering, + 'mode' => 'create', + ]; + + // response + return $this->render('service-offering/form.html.twig', $params); + } + + /** + * @IsGranted("service_offering.add") + */ + public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator) + { + $service_offering = new ServiceOffering(); + + $this->setObject($service_offering, $req); + + // validate + $errors = $validator->validate($service_offering); + + // 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($service_offering); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + + /** + * @Menu(selected="service_offering_list") + * @ParamConverter("service_offering", class="App\Entity\ServiceOffering") + * @IsGranted("service_offering.update") + */ + public function updateForm($id, EntityManagerInterface $em, ServiceOffering $service_offering) + { + $params = []; + $params['service_offering'] = $service_offering; + $params['mode'] = 'update'; + + // response + return $this->render('service-offering/form.html.twig', $params); + } + + /** + * @ParamConverter("service_offering", class="App\Entity\ServiceOffering") + * @IsGranted("service_offering.update") + */ + public function updateSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator, ServiceOffering $service_offering) + { + $this->setObject($service_offering, $req); + + // validate + $errors = $validator->validate($service_offering); + + // 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!' + ]); + } + + /** + * @ParamConverter("service_offering", class="App\Entity\ServiceOffering") + * @IsGranted("service_offering.delete") + */ + public function deleteSubmit(EntityManagerInterface $em, ServiceOffering $service_offering) + { + // delete this object + $em->remove($service_offering); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + + protected function setObject(ServiceOffering $obj, Request $req) + { + // set and save values + $obj->setName($req->request->get('name')) + ->setCode($req->request->get('code')) + ->setFee($req->request->get('fee')); + } + + 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/src/Entity/ServiceOffering.php b/src/Entity/ServiceOffering.php new file mode 100644 index 00000000..3e8a935f --- /dev/null +++ b/src/Entity/ServiceOffering.php @@ -0,0 +1,81 @@ +id; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function setCode($code) + { + $this->code = $code; + return $this; + } + + public function getCode() + { + return $this->code; + } + + public function setFee($fee) + { + $this->fee = $fee; + return $this; + } + + public function getFee() + { + return $this->fee; + } +} + diff --git a/templates/service-offering/form.html.twig b/templates/service-offering/form.html.twig new file mode 100644 index 00000000..28fd33c8 --- /dev/null +++ b/templates/service-offering/form.html.twig @@ -0,0 +1,151 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Service Offerings

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

+ {% if mode == 'update' %} + Edit Service Offering + {{ service_offering.getName() }} + {% else %} + New Service Offering + {% endif %} +

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

+ Service Offerings +

+
+
+
+ +
+ +
+
+
+
+
+
+
+
+
+
+ + + + +
+
+
+
+ +
+
+ +
+ +
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/translations/messages.en.yaml b/translations/messages.en.yaml index d92fd4bb..78080119 100644 --- a/translations/messages.en.yaml +++ b/translations/messages.en.yaml @@ -157,6 +157,7 @@ menu.database.tickettypes: 'Ticket Types' menu.database.subtickettypes: 'Sub Ticket Types' menu.database.emergencytypes: 'Emergency Types' menu.database.ownershiptypes: 'Ownership Types' +menu.database.serviceofferings: 'Service Offerings' # fcm jo status updates jo_fcm_title_outlet_assign: Looking for riders