diff --git a/config/acl.yaml b/config/acl.yaml index 64540ecf..a675b44e 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -586,3 +586,17 @@ access_keys: label: Update - id: ownership_type.delete label: Delete + + - id: job_order_source + label: Job Order Source Access + acls: + - id: job_order_source.menu + label: Menu + - id: job_order_source.list + label: List + - id: job_order_source.add + label: Add + - id: job_order_source.update + label: Update + - id: job_order_source.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index b75a9d38..956c84ae 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -249,3 +249,7 @@ main_menu: acl: ownership_type.menu label: Ownership Types parent: database + - id: job_order_source_list + acl: job_order_source.menu + label: Job Order Source + parent: database diff --git a/config/routes/job_order_source.yaml b/config/routes/job_order_source.yaml new file mode 100644 index 00000000..387520d1 --- /dev/null +++ b/config/routes/job_order_source.yaml @@ -0,0 +1,35 @@ +job_order_source_list: + path: /job-order-sources + controller: App\Controller\JobOrderSourceController::index + methods: [GET] + +job_order_source_rows: + path: /job-order-sources/rowdata + controller: App\Controller\JobOrderSourceController::datatableRows + methods: [POST] + +job_order_source_add_form: + path: /job-order-sources/newform + controller: App\Controller\JobOrderSourceController::addForm + methods: [GET] + +job_order_source_add_submit: + path: /job-order-sources + controller: App\Controller\JobOrderSourceController::addSubmit + methods: [POST] + +job_order_source_update_form: + path: /job-order-sources/{id} + controller: App\Controller\JobOrderSourceController::updateForm + methods: [GET] + +job_order_source_update_submit: + path: /job-order-sources/{id} + controller: App\Controller\JobOrderSourceController::updateSubmit + methods: [POST] + +job_order_source_delete: + path: /job-order-sources/{id} + controller: App\Controller\JobOrderSourceController::deleteSubmit + methods: [DELETE] + diff --git a/src/Controller/JobOrderSourceController.php b/src/Controller/JobOrderSourceController.php new file mode 100644 index 00000000..63c06cf5 --- /dev/null +++ b/src/Controller/JobOrderSourceController.php @@ -0,0 +1,255 @@ +denyAccessUnlessGranted('job_order_source.list', null, 'No access.'); + + return $this->render('job-order-source/list.html.twig'); + } + + /** + * @IsGranted("job_order_source.list") + */ + public function datatableRows(Request $req) + { + // get query builder + $qb = $this->getDoctrine() + ->getRepository(JobOrderSource::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['source_id'] = $orow->getSourceID(); + + // add row metadata + $row['meta'] = [ + 'update_url' => '', + 'delete_url' => '' + ]; + + // add crud urls + if ($this->isGranted('job_order_source.update')) + $row['meta']['update_url'] = $this->generateUrl('job_order_source_update_form', ['id' => $row['id']]); + if ($this->isGranted('job_order_source.delete')) + $row['meta']['delete_url'] = $this->generateUrl('job_order_source_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + /** + * @Menu(selected="job_order_source.list") + * @IsGranted("job_order_source.add") + */ + public function addForm() + { + $jo_source = new JobOrderSource(); + $params = [ + 'jo_source' => $jo_source, + 'mode' => 'create', + ]; + + // response + return $this->render('job-order-source/form.html.twig', $params); + } + + /** + * @IsGranted("job_order_source.add") + */ + public function addSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator) + { + $jo_source = new JobOrderSource(); + + $this->setObject($jo_source, $req); + + // validate + $errors = $validator->validate($jo_source); + + // 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($jo_source); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + + } + + /** + * @Menu(selected="job_order_source_list") + * @ParamConverter("jo_source", class="App\Entity\JobOrderSource") + * @IsGranted("job_order_source.update") + */ + public function updateForm($id, EntityManagerInterface $em, JobOrderSource $jo_source) + { + $params = []; + $params['jo_source'] = $jo_source; + $params['mode'] = 'update'; + + // response + return $this->render('job-order-source/form.html.twig', $params); + } + + /** + * @ParamConverter("jo_source", class="App\Entity\JobOrderSource") + * @IsGranted("job_order_source.update") + */ + public function updateSubmit(Request $req, EntityManagerInterface $em, ValidatorInterface $validator, JobOrderSource $jo_source) + { + $this->setObject($jo_source, $req); + + // validate + $errors = $validator->validate($jo_source); + + // 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("jo_source", class="App\Entity\JobOrderSource") + * @IsGranted("job_order_source.update") + */ + public function deleteSubmit(EntityManagerInterface $em, JobOrderSource $jo_source) + { + // delete this object + $em->remove($jo_source); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } + + + protected function setObject(JobOrderSource $obj, Request $req) + { + // set and save values + $obj->setSourceID($req->request->get('source_id')) + ->setCallbackURL($req->request->get('callback_url')); + } + + protected function setQueryFilters($datatable, QueryBuilder $query) + { + if (isset($datatable['query']['data-rows-search']) && !empty($datatable['query']['data-rows-search'])) { + $query->where('q.source_id LIKE :filter') + ->setParameter('filter', '%' . $datatable['query']['data-rows-search'] . '%'); + } + } + +} diff --git a/src/Entity/JobOrderSource.php b/src/Entity/JobOrderSource.php new file mode 100644 index 00000000..c8928874 --- /dev/null +++ b/src/Entity/JobOrderSource.php @@ -0,0 +1,68 @@ +source_id = ''; + $this->callback_url = ''; + } + + public function getID() + { + return $this->id; + } + + public function setSourceID($source_id) + { + $this->source_id = $source_id; + return $this; + } + + public function getSourceID() + { + return $this->source_id; + } + + public function setCallbackURL($callback_url) + { + $this->callback_url = $callback_url; + return $this; + } + + public function getCallbackURL() + { + return $this->callback_url; + } +} + diff --git a/templates/job-order-source/form.html.twig b/templates/job-order-source/form.html.twig new file mode 100644 index 00000000..1d9c1fc5 --- /dev/null +++ b/templates/job-order-source/form.html.twig @@ -0,0 +1,142 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +