diff --git a/config/acl.yaml b/config/acl.yaml index fb623290..ee514ab5 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -518,3 +518,38 @@ access_keys: label: Update - id: dealer.delete label: Delete + + - id: database + label: Database Access + acls: + - id: database.menu + label: Menu + + - id: ticket_type + label: Ticket Type Access + acls: + - id: ticket_type.menu + label: Menu + - id: ticket_type.list + label: List + - id: ticket_type.add + label: Add + - id: ticket_type.update + label: Update + - id: ticket_type.delete + label: Delete + + - id: subticket_type + label: Sub Ticket Type Access + acls: + - id: subticket_type.menu + label: Menu + - id: subticket_type.list + label: List + - id: subticket_type.add + label: Add + - id: subticket_type.update + label: Update + - id: subticket_type.delete + label: Delete + diff --git a/config/menu.yaml b/config/menu.yaml index fc4751fb..f80daadd 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -224,3 +224,16 @@ main_menu: acl: analytics.forecast label: Forecasting parent: analytics + + - id: database + acl: database.menu + label: Database + icon: fa fa-database + - id: ticket_type_list + acl: ticket_type.menu + label: Ticket Types + parent: database + - id: subticket_type_list + acl: subticket_type.menu + label: Sub Ticket Types + parent: database diff --git a/config/routes/subticket_type.yaml b/config/routes/subticket_type.yaml new file mode 100644 index 00000000..fbd80e82 --- /dev/null +++ b/config/routes/subticket_type.yaml @@ -0,0 +1,35 @@ +subticket_type_list: + path: /subticket-types + controller: App\Controller\SubTicketTypeController::index + methods: [GET] + +subticket_type_rows: + path: /subticket-types/rowdata + controller: App\Controller\SubTicketTypeController::datatableRows + methods: [POST] + +subticket_type_add_form: + path: /subticket-types/newform + controller: App\Controller\SubTicketTypeController::addForm + methods: [GET] + +subticket_type_add_submit: + path: /subticket-types + controller: App\Controller\SubTicketTypeController::addSubmit + methods: [POST] + +subticket_type_update_form: + path: /subticket-types/{id} + controller: App\Controller\SubTicketTypeController::updateForm + methods: [GET] + +subticket_type_update_submit: + path: /subticket-types/{id} + controller: App\Controller\SubTicketTypeController::updateSubmit + methods: [POST] + +subticket_type_delete: + path: /subticket-types/{id} + controller: App\Controller\SubTicketTypeController::deleteSubmit + methods: [DELETE] + diff --git a/config/routes/ticket_type.yaml b/config/routes/ticket_type.yaml new file mode 100644 index 00000000..86544069 --- /dev/null +++ b/config/routes/ticket_type.yaml @@ -0,0 +1,35 @@ +ticket_type_list: + path: /ticket-types + controller: App\Controller\TicketTypeController::index + methods: [GET] + +ticket_type_rows: + path: /ticket-types/rowdata + controller: App\Controller\TicketTypeController::datatableRows + methods: [POST] + +ticket_type_add_form: + path: /ticket-types/newform + controller: App\Controller\TicketTypeController::addForm + methods: [GET] + +ticket_type_add_submit: + path: /ticket-types + controller: App\Controller\TicketTypeController::addSubmit + methods: [POST] + +ticket_type_update_form: + path: /ticket-types/{id} + controller: App\Controller\TicketTypeController::updateForm + methods: [GET] + +ticket_type_update_submit: + path: /ticket-types/{id} + controller: App\Controller\TicketTypeController::updateSubmit + methods: [POST] + +ticket_type_delete: + path: /ticket-types/{id} + controller: App\Controller\TicketTypeController::deleteSubmit + methods: [DELETE] + diff --git a/src/Controller/TicketTypeController.php b/src/Controller/TicketTypeController.php new file mode 100644 index 00000000..3023420b --- /dev/null +++ b/src/Controller/TicketTypeController.php @@ -0,0 +1,254 @@ +denyAccessUnlessGranted('ticket_type.list', null, 'No access.'); + + return $this->render('ticket-type/list.html.twig'); + } + + /** + * @IsGranted("ticket_type.list") + */ + public function datatableRows(Request $req) + { + // get query builder + $qb = $this->getDoctrine() + ->getRepository(TicketType::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(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('ticket_type.update')) + $row['meta']['update_url'] = $this->generateUrl('ticket_type_update_form', ['id' => $row['id']]); + if ($this->isGranted('ticket_type.delete')) + $row['meta']['delete_url'] = $this->generateUrl('ticket_type_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="ticket_type.list") + * @IsGranted("ticket_type.add") + */ + public function addForm() + { + $ticket_type = new TicketType(); + $params = [ + 'ticket_type' => $ticket_type, + 'mode' => 'create', + ]; + + // response + return $this->render('ticket-type/form.html.twig', $params); + } + + /** + * @IsGranted("ticket_type.add") + */ + public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator) + { + $ticket_type = new TicketType(); + + $this->setObject($ticket_type, $req); + + // validate + $errors = $validator->validate($ticket_type); + + // 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($ticket_type); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + + } + + /** + * @Menu(selected="dealer_list") + * @ParamConverter("ticket_type", class="App\Entity\TicketType") + * @IsGranted("ticket_type.update") + */ + public function updateForm($id, EntityManagerInterface $em, TicketType $ticket_type) + { + $params = []; + $params['ticket_type'] = $ticket_type; + $params['mode'] = 'update'; + + // response + return $this->render('ticket-type/form.html.twig', $params); + } + + /** + * @ParamConverter("ticket_type", class="App\Entity\TicketType") + * @IsGranted("ticket_type.update") + */ + public function updateSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator, TicketType $ticket_type) + { + $this->setObject($ticket_type, $req); + + // validate + $errors = $validator->validate($ticket_type); + + // 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("ticket_type", class="App\Entity\TicketType") + * @IsGranted("ticket_type.update") + */ + public function deleteSubmit(EntityManagerInterface $em, TicketType $ticket_type) + { + // delete this object + $em->remove($ticket_type); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + + + protected function setObject(TicketType $obj, Request $req) + { + // set and save values + $obj->setName($req->request->get('name')); + } + + 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/SubTicketType.php b/src/Entity/SubTicketType.php new file mode 100644 index 00000000..d992e414 --- /dev/null +++ b/src/Entity/SubTicketType.php @@ -0,0 +1,63 @@ +id; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } + + public function setTicketType(TicketType $ticket_type) + { + $this->ticket_type = $ticket_type; + return $this; + } + + public function getTicketType() + { + return $this->ticket_type; + } +} diff --git a/src/Entity/TicketType.php b/src/Entity/TicketType.php new file mode 100644 index 00000000..b52ebb73 --- /dev/null +++ b/src/Entity/TicketType.php @@ -0,0 +1,45 @@ +id; + } + + public function setName($name) + { + $this->name = $name; + return $this; + } + + public function getName() + { + return $this->name; + } +} diff --git a/templates/ticket-type/form.html.twig b/templates/ticket-type/form.html.twig new file mode 100644 index 00000000..4f3cce8d --- /dev/null +++ b/templates/ticket-type/form.html.twig @@ -0,0 +1,133 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

Ticket Types

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

+ {% if mode == 'update' %} + Edit Ticket Type + {{ ticket_type.getName() }} + {% else %} + New Ticket Type + {% endif %} +

+
+
+
+
+
+
+ +
+ + +
+
+
+
+
+
+
+ + Back +
+
+
+
+
+
+
+
+
+{% endblock %} + +{% block scripts %} + +{% endblock %} diff --git a/templates/ticket-type/list.html.twig b/templates/ticket-type/list.html.twig new file mode 100644 index 00000000..8b61946c --- /dev/null +++ b/templates/ticket-type/list.html.twig @@ -0,0 +1,146 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +
+
+
+

+ Ticket Types +

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