diff --git a/config/acl.yaml b/config/acl.yaml index e694340c..0f858fa9 100644 --- a/config/acl.yaml +++ b/config/acl.yaml @@ -488,3 +488,17 @@ access_keys: label: Update - id: sap_csize.delete label: Delete + + - id: customer_tag + label: Customer Tags Access + acls: + - id: customer_tag.menu + label: Menu + - id: customer_tag.list + label: List + - id: customer_tag.add + label: Add + - id: customer_tag.update + label: Update + - id: customer_tag.delete + label: Delete diff --git a/config/menu.yaml b/config/menu.yaml index d90ba3da..35709ee0 100644 --- a/config/menu.yaml +++ b/config/menu.yaml @@ -185,6 +185,10 @@ main_menu: acl: static_content.list label: Static Content parent: support + - id: customertag_list + acl: customer_tag.list + label: Customer Tags + parent: support - id: service acl: service.menu diff --git a/config/routes/customer_tag.yaml b/config/routes/customer_tag.yaml new file mode 100644 index 00000000..9fd98906 --- /dev/null +++ b/config/routes/customer_tag.yaml @@ -0,0 +1,34 @@ +customertag_list: + path: /customer_tags + controller: App\Controller\CustomerTagController::index + +customertag_rows: + path: /customer_tags/rows + controller: App\Controller\CustomerTagController::rows + methods: [POST] + +customertag_create: + path: /customer_tags/create + controller: App\Controller\CustomerTagController::addForm + methods: [GET] + +customertag_create_submit: + path: /customer_tags/create + controller: App\Controller\CustomerTagController::addSubmit + methods: [POST] + +customertag_update: + path: /customer_tags/{id} + controller: App\Controller\CustomerTagController::updateForm + methods: [GET] + +customertag_update_submit: + path: /customer_tags/{id} + controller: App\Controller\CustomerTagController::updateSubmit + methods: [POST] + +customertag_delete: + path: /customer_tags/{id} + controller: App\Controller\CustomerTagController::destroy + methods: [DELETE] + diff --git a/src/Controller/CustomerTagController.php b/src/Controller/CustomerTagController.php new file mode 100644 index 00000000..ab4178f5 --- /dev/null +++ b/src/Controller/CustomerTagController.php @@ -0,0 +1,260 @@ +denyAccessUnlessGranted('customer_tag.list', null, 'No access.'); + + return $this->render('customer-tag/list.html.twig'); + } + + public function rows(Request $req) + { + $this->denyAccessUnlessGranted('customer_tag.list', null, 'No access.'); + + // get query builder + $qb = $this->getDoctrine() + ->getRepository(CustomerTag::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('customer_tag.update')) + $row['meta']['update_url'] = $this->generateUrl('customertag_update', ['id' => $row['id']]); + if ($this->isGranted('customer_tag.delete')) + $row['meta']['delete_url'] = $this->generateUrl('customertag_delete', ['id' => $row['id']]); + + $rows[] = $row; + } + + // response + return $this->json([ + 'meta' => $meta, + 'data' => $rows + ]); + } + + 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'] . '%'); + } + } + + /** + * @Menu(selected="customer_tag_list") + */ + public function addForm() + { + $this->denyAccessUnlessGranted('customer_tag.add', null, 'No access.'); + + $params['obj'] = new CustomerTag(); + $params['mode'] = 'create'; + + // response + return $this->render('customer-tag/form.html.twig', $params); + } + + public function addSubmit(Request $req, ValidatorInterface $validator) + { + $this->denyAccessUnlessGranted('customer_tag.add', null, 'No access.'); + + // create new object + $em = $this->getDoctrine()->getManager(); + $obj = new CustomerTag(); + + $obj->setID($req->request->get('id')) + ->setName($req->request->get('name')); + + // validate + $errors = $validator->validate($obj); + + // 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($obj); + $em->flush(); + + // return successful response + return $this->json([ + 'success' => 'Changes have been saved!' + ]); + } + } + + /** + * @Menu(selected="customer_tag_list") + */ + public function updateForm($id) + { + $this->denyAccessUnlessGranted('customer_tag.update', null, 'No access.'); + + // get row data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(CustomerTag::class)->find($id); + + // make sure this row exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $params['obj'] = $obj; + $params['mode'] = 'update'; + + // response + return $this->render('customer-tag/form.html.twig', $params); + } + + public function updateSubmit(Request $req, ValidatorInterface $validator, $id) + { + $this->denyAccessUnlessGranted('customer_tag.update', null, 'No access.'); + + // get object data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(CustomerTag::class)->find($id); + + // make sure this object exists + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + $obj->setID($req->request->get('id')) + ->setName($req->request->get('name')); + + // validate + $errors = $validator->validate($obj); + + // 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!' + ]); + } + + public function destroy($id) + { + $this->denyAccessUnlessGranted('customer_tag.delete', null, 'No access.'); + + // get object data + $em = $this->getDoctrine()->getManager(); + $obj = $em->getRepository(CustomerTag::class)->find($id); + + if (empty($obj)) + throw $this->createNotFoundException('The item does not exist'); + + // delete this object + $em->remove($obj); + $em->flush(); + + // response + $response = new Response(); + $response->setStatusCode(Response::HTTP_OK); + $response->send(); + } +} diff --git a/src/Entity/CustomerTag.php b/src/Entity/CustomerTag.php index a3c87423..f2d8f67e 100644 --- a/src/Entity/CustomerTag.php +++ b/src/Entity/CustomerTag.php @@ -4,6 +4,7 @@ namespace App\Entity; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; +use Symfony\Component\Validator\Constraints as Assert; use DateTime; /** @@ -12,17 +13,17 @@ use DateTime; */ class CustomerTag { - // unique id /** * @ORM\Id - * @ORM\Column(type="integer") - * @ORM\GeneratedValue(strategy="AUTO") + * @ORM\Column(type="string", length=80, nullable=false) + * @Assert\NotBlank() */ protected $id; // name of tag /** * @ORM\Column(type="string", length=80) + * @Assert\NotBlank() */ protected $name; @@ -48,8 +49,8 @@ class CustomerTag { $this->date_create = new DateTime(); $this->customers = new ArrayCollection(); - $this->car_club_member = false; - $this->car_club_promo = false; + $this->flag_car_club_member = false; + $this->flag_car_club_promo = false; } public function getID() @@ -57,6 +58,12 @@ class CustomerTag return $this->id; } + public function setID($id) + { + $this->id = $id; + return $this; + } + public function setName($name) { $this->name = $name; @@ -90,4 +97,26 @@ class CustomerTag { return $this->customers; } + + public function setCarClubMember($flag_car_club_member = true) + { + $this->flag_car_club_member = $flag_car_club_member; + return $this; + } + + public function isCarClubMember() + { + return $this->flag_car_club_member; + } + + public function setCarClubPromo($flag_car_club_promo = true) + { + $this->flag_car_club_promo = $flag_car_club_promo; + return $this; + } + + public function isCarClubPromo() + { + return $this->flag_car_club_promo; + } } diff --git a/templates/customer-tag/form.html.twig b/templates/customer-tag/form.html.twig new file mode 100644 index 00000000..ce574a44 --- /dev/null +++ b/templates/customer-tag/form.html.twig @@ -0,0 +1,145 @@ +{% extends 'base.html.twig' %} + +{% block body %} + +