diff --git a/config/acl.yaml b/config/acl.yaml index ecccd5f5..57a1c427 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -368,3 +368,17 @@ access_keys: label: Add - id: warranty.update label: Update + + - id: staticcontent + label: Static Content + acls: + - id: static_content.menu + label: Menu + - id: static_content.list + label: List + - id: static_content.add + label: Add + - id: static_content.update + label: Update + - id: static_content.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index 491e51aa..b0096cf5 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -155,6 +155,10 @@ main_menu: acl: warranty.upload label: Warranty Upload parent: support + - id: static_content_list + acl: static_content.list + label: Static Content + parent: support - id: service acl: service.menu diff --git a/config/routes/static_content.yaml b/config/routes/static_content.yaml new file mode 100644 index 00000000..88fe3cf4 --- /dev/null +++ b/config/routes/static_content.yaml @@ -0,0 +1,33 @@ +static_content_list: + path: /static_content + controller: App\Controller\StaticContentController::index + +static_content_rows: + path: /static_content/rows + controller: App\Controller\StaticContentController::rows + methods: [POST] + +static_content_create: + path: /static_content/create + controller: App\Controller\StaticContentController::addForm + methods: [GET] + +static_content_create_submit: + path: /static_content/create + controller: App\Controller\StaticContentController::addSubmit + methods: [POST] + +static_content_update: + path: /static_content/{id} + controller: App\Controller\StaticContentController::updateForm + methods: [GET] + +static_content_update_submit: + path: /static_content/{id} + controller: App\Controller\StaticContentController::updateSubmit + methods: [POST] + +static_content_delete: + path: /static_content/{id} + controller: App\Controller\StaticContentController::destroy + methods: [DELETE] diff --git a/src/Controller/StaticContentController.php b/src/Controller/StaticContentController.php new file mode 100644 index 00000000..395736d4 --- /dev/null +++ b/src/Controller/StaticContentController.php @@ -0,0 +1,289 @@ +denyAccessUnlessGranted('static_content.list', null, 'No access.'); + + return $this->render('static-content/list.html.twig'); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('static_content.list', null, 'No access.'); + + // build query + $qb = $this->getDoctrine() + ->getRepository(StaticContent::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(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('static_content.update')) + $row['meta']['update_url'] = $this->generateUrl('static_content_update', ['id' => $row['id']]); + if ($this->isGranted('static_content.delete')) + $row['meta']['delete_url'] = $this->generateUrl('static_content_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="static_content_list") + */ + public function addForm() + { + $this->denyAccessUnlessGranted('static_content.add', null, 'No access.'); + + $params = []; + $params['obj'] = new StaticContent(); + $params['mode'] = 'create'; + + // response + return $this->render('static-content/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('static_content.add', null, 'No access.'); + + // create new object + $em = $this->getDoctrine()->getManager(); + $row = new StaticContent(); + + // set and save values + $id = $req->request->get('id'); + $row->setID($id); + $row->setContent($req->request->get('content')); + + // validate + $errors = $validator->validate($row); + + // initialize error list + $error_array = []; + + // check for duplicate ID + $result = $em->getRepository(StaticContent::class)->find($id); + if ($result != null) + { + error_log($id); + $error_array['id'] = 'Duplicate ID exists.'; + } + + // 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="static_content_list") + */ + public function updateForm($id) + { + $this->denyAccessUnlessGranted('static_content.update', null, 'No access.'); + + $params = []; + $params['mode'] = 'update'; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(StaticContent::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('static-content/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('static_content.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(StaticContent::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')) + ->setContent($req->request->get('content')); + + // validate + $errors = $validator->validate($row); + + // initialize error list + $error_array = []; + + // check for duplicate ID + $result = $em->getRepository(StaticContent::class)->find($id); + if ($result != null) + { + error_log($id); + $error_array['id'] = 'Duplicate ID exists.'; + } + + // 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="static_content_list") + */ + public function destroy($id) + { + $this->denyAccessUnlessGranted('static_content.delete', null, 'No access.'); + + $params = []; + + // get row data + $em = $this->getDoctrine()->getManager(); + $row = $em->getRepository(StaticContent::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/src/Entity/StaticContent.php b/src/Entity/StaticContent.php new file mode 100644 index 00000000..b450bb2a --- /dev/null +++ b/src/Entity/StaticContent.php @@ -0,0 +1,54 @@ +id = $id; + return $this; + } + + public function getID() + { + return $this->id; + } + + public function setContent($content) + { + $this->content = $content; + return $this; + } + + public function getContent() + { + return $this->content; + } +} + diff --git a/templates/static-content/form.html.twig b/templates/static-content/form.html.twig new file mode 100644 index 00000000..dece1005 --- /dev/null +++ b/templates/static-content/form.html.twig @@ -0,0 +1,142 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Static Content

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

+ {% if mode == 'update' %} + Edit Static Content + {{ obj.getID() }} + {% else %} + New Static Content + {% endif %} +

+
+
+
+
+
+
+
+ + + +
+
+
+
+ + + +
+
+
+
+
+
+
+ + Back +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/templates/static-content/list.html.twig b/templates/static-content/list.html.twig new file mode 100644 index 00000000..186f0958 --- /dev/null +++ b/templates/static-content/list.html.twig @@ -0,0 +1,138 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Static Content +

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