diff --git a/config/acl.yaml b/config/acl.yaml index 5695e217..e868c7ec 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -252,6 +252,7 @@ access_keys: label: Search - id: warranty.search label: Customer Battery Search + - id: ticket label: Ticket Access acls: @@ -329,3 +330,17 @@ access_keys: label: View - id: review.delete label: Delete + + - id: privacypolicy + label: Privacy Policy + acls: + - id: privacy_policy.menu + label: Menu + - id: privacy_policy.list + label: List + - id: privacy_policy.add + label: Add + - id: privacy_policy.update + label: Update + - id: privacy_policy.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index d470f680..b0a00d92 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -143,6 +143,10 @@ main_menu: acl: warranty.search label: Customer Battery Search parent: support + - id: privacy_policy_list + acl: privacy_policy.list + label: Privacy Policy + parent: support - id: service acl: service.menu diff --git a/config/routes/privacy_policy.yaml b/config/routes/privacy_policy.yaml new file mode 100644 index 00000000..6a5708b2 --- /dev/null +++ b/config/routes/privacy_policy.yaml @@ -0,0 +1,33 @@ +privacy_policy_list: + path: /privacy_policies + controller: App\Controller\PrivacyPolicyController::index + +privacy_policy_rows: + path: /privacy_policies/rows + controller: App\Controller\PrivacyPolicyController::rows + methods: [POST] + +privacy_policy_create: + path: /privacy_policies/create + controller: App\Controller\PrivacyPolicyController::addForm + methods: [GET] + +privacy_policy_create_submit: + path: /privacy_policies/create + controller: App\Controller\PrivacyPolicyController:addSubmit + methods: [POST] + +privacy_policy_update: + path: /privacy_policies/{id} + controller: App\Controller\PrivacyPolicyController::updateForm + methods: [GET] + +privacy_policy_update_submit: + path : /privacy_policies/{id} + controller: App\Controller\PrivacyPolicyController:updateSubmit + methods: [POST] + +privacy_policy_delete: + path: /privacy_policies/{id} + controller: App\Controller\PrivacyPolicyController:destroy + methods: [DELETE] diff --git a/src/Controller/PrivacyPolicyController.php b/src/Controller/PrivacyPolicyController.php new file mode 100644 index 00000000..70b39278 --- /dev/null +++ b/src/Controller/PrivacyPolicyController.php @@ -0,0 +1,272 @@ +denyAccessUnlessGranted('privacy_policy.list', null, 'No access.'); + + return $this->render('privacy-policy/list.html.twig'); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('privacy_policy.list', null, 'No access.'); + + // build query + $qb = $this->getDoctrine() + ->getRepository(PrivacyPolicy::class) + ->createQueryBuilder('q'); + + // get datatable params + $datatable = $req->request->get('datatable'); + + // count total records + $tquery = $qb->select('COUNT(q)'); + + // add fitlers 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 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('privacy_policy.update')) + $row['meta']['update_url'] = $this->generateUrl('privacy_policy_update', ['id' => $row['id']]); + if ($this->isGranted('privacy_policy.delete')) + $row['meta']['delete_url'] = $this->generateUrl('privacy_policy_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="privacy_policy_list") + */ + public function addForm() + { + $this->denyAccessUnlessGranted('privacy_policy.add', null, 'No access.'); + + $params = []; + $params['obj'] = new PrivacyPolicy(); + $params['mode'] = 'create'; + + // response + return $this->render('privacy-policy/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('privacy_policy.add', null, 'No access.'); + + // create new object + $em = $this->getDoctrine()->getManager(); + $row = new PrivacyPolicy(); + + // set and save values + $row->setName($req->request->get('name')); + $row->setContent($req->request->get('content')); + + // 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="privacy_policy_list") + */ + public function updateForm($id) + { + $this->denyAccessUnlessGranted('privacy_policy.update', null, 'No access.'); + + $params = []; + $params['mode'] = 'update'; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(PrivacyPolicy::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('privacy-policy/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('privacy_policy.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(PrivacyPolicy::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!' + ]); + } + } + + /** + * @Menu(selected="privacy_policy_list") + */ + public function destroy($id) + { + $this->denyAccessUnlessGranted('privacy_policy.delete', null, 'No access.'); + + $params = []; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(PrivacyPolicy::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(); + } + + 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/privacy-policy/form.html.twig b/templates/privacy-policy/form.html.twig new file mode 100644 index 00000000..efd2f3b3 --- /dev/null +++ b/templates/privacy-policy/form.html.twig @@ -0,0 +1,142 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +