render($this->twig.'list.html.twig', [ 'useheader' => true, 'usemenu' => false, 'usesidebar' => true, 'access' => $access, ]); } public function tablelist($access, Request $request, ManagerRegistry $em): Response { $query = $request->query->all(); $start = $query['start']; $length = $query['length']; $search = $query['search']; $draw = $query['draw']; $ordercolumn = $query['order'][0]['column']; $orderdir = $query['order'][0]['dir']; $total = null; $totalf = null; // Nombre total d'enregistrement switch ($access) { case 'admin': $total = $em->getManager()->createQueryBuilder()->select('COUNT(entity)')->from($this->entity, 'entity')->getQuery()->getSingleScalarResult(); break; case 'modo': $total = $em->getManager()->createQueryBuilder() ->select('COUNT(entity)') ->from($this->entity, 'entity') ->from("App\Entity\UserModo", 'usermodo') ->from("App\Entity\Niveau02", 'niveau02') ->where('entity.niveau02 = niveau02.id') ->andwhere('usermodo.niveau01 = niveau02.niveau01') ->andWhere('usermodo.user = :user') ->setParameter('user', $this->getUser()) ->getQuery()->getSingleScalarResult(); break; } // Nombre d'enregistrement filtré if (!$search || '' == $search['value']) { $totalf = $total; } else { switch ($access) { case 'admin': $totalf = $em->getManager()->createQueryBuilder() ->select('COUNT(entity)') ->from($this->entity, 'entity') ->from("App\Entity\Niveau01", 'niveau01') ->from("App\Entity\Niveau02", 'niveau02') ->where('entity.niveau02 = niveau02.id') ->andwhere('niveau02.niveau01=niveau01.id') ->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value OR niveau02.label LIKE :value') ->setParameter('value', '%'.$search['value'].'%') ->getQuery() ->getSingleScalarResult(); break; case 'modo': $totalf = $em->getManager()->createQueryBuilder() ->select('COUNT(entity)') ->from($this->entity, 'entity') ->from("App\Entity\Niveau01", 'niveau01') ->from("App\Entity\Niveau02", 'niveau02') ->from("App\Entity\UserModo", 'usermodo') ->where('entity.niveau02 = niveau02.id') ->andwhere('niveau02.niveau01=niveau01.id') ->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value OR niveau02.label LIKE :value') ->andWhere('usermodo.niveau01 = niveau01.id') ->andWhere('usermodo.user = :user') ->setParameter('value', '%'.$search['value'].'%') ->setParameter('user', $this->getUser()) ->getQuery() ->getSingleScalarResult(); break; } } // Construction du tableau de retour $output = [ 'draw' => $draw, 'recordsFiltered' => $totalf, 'recordsTotal' => $total, 'data' => [], ]; // Parcours des Enregistrement $qb = $em->getManager()->createQueryBuilder(); switch ($access) { case 'admin': $qb->select('entity') ->from($this->entity, 'entity') ->from('App:Niveau01', 'niveau01') ->from('App:Niveau02', 'niveau02') ->where('entity.niveau02=niveau02.id') ->andwhere('niveau02.niveau01=niveau01.id'); break; case 'modo': $qb->select('entity') ->from($this->entity, 'entity') ->from('App:Niveau01', 'niveau01') ->from('App:Niveau02', 'niveau02') ->from("App\Entity\UserModo", 'usermodo') ->where('entity.niveau02=niveau02.id') ->andwhere('niveau02.niveau01=niveau01.id') ->andWhere('usermodo.niveau01 = niveau01.id') ->andWhere('usermodo.user = :user') ->setParameter('user', $this->getUser()); break; } if ($search && '' != $search['value']) { $qb->andwhere('entity.label LIKE :value OR niveau01.label LIKE :value OR niveau02.label LIKE :value') ->setParameter('value', '%'.$search['value'].'%'); } if ($ordercolumn) { switch ($ordercolumn) { case 1: $qb->orderBy('niveau01.label', $orderdir); break; case 2: $qb->orderBy('niveau02.label', $orderdir); break; case 3: $qb->orderBy('entity.label', $orderdir); break; } } $datas = $qb->setFirstResult($start)->setMaxResults($length)->getQuery()->getResult(); foreach ($datas as $data) { // Action $action = ''; switch ($access) { case 'admin': $action .= " $data->getId()])."'>"; break; case 'modo': $action .= "route).'_update', ['id' => $data->getId()])."'>"; break; } $tmp = []; array_push($tmp, $action); array_push($tmp, $data->getNiveau02()->getNiveau01()->getLabel()); array_push($tmp, $data->getNiveau02()->getLabel()); array_push($tmp, $data->getLabel()); array_push($output['data'], $tmp); } // Retour return new JsonResponse($output); } public function selectlist(Request $request, ManagerRegistry $em): Response { $output = []; $page_limit = $request->query->get('page_limit'); $q = $request->query->get('q'); $niveau02id = $request->get('niveau02'); $qb = $em->getManager()->createQueryBuilder(); $qb->select('entity') ->from($this->entity, 'entity') ->where('entity.label LIKE :value') ->andwhere('entity.niveau02=:niveau02') ->setParameter('value', '%'.$q.'%') ->setParameter('niveau02', $niveau02id) ->orderBy('entity.label'); $datas = $qb->setFirstResult(0)->setMaxResults($page_limit)->getQuery()->getResult(); foreach ($datas as $data) { array_push($output, ['id' => $data->getId(), 'text' => $data->getLabel()]); } $ret_string['results'] = $output; $response = new Response(json_encode($ret_string)); $response->headers->set('Content-Type', 'application/json'); return $response; } public function submit($access, Request $request, ManagerRegistry $em): Response { // Initialisation de l'enregistrement $data = new Entity(); $data->setApikey(Uuid::uuid4()); // Controler les permissions $this->cansubmit($access, $em); // Création du formulaire $form = $this->createForm(Form::class, $data, [ 'mode' => 'submit', 'access' => $access, 'userid' => $this->getUser()->getId(), 'appMasteridentity' => $this->GetParameter('appMasteridentity'), 'appNiveau01label' => $this->GetParameter('appNiveau01label'), 'appNiveau02label' => $this->GetParameter('appNiveau02label'), 'appNiveau03label' => $this->GetParameter('appNiveau03label'), ]); // Récupération des data du formulaire $form->handleRequest($request); // Sur validation if ($form->get('submit')->isClicked() && $form->isValid()) { $data = $form->getData(); // Sauvegarde $em->getManager()->persist($data); $em->getManager()->flush(); // Retour à la liste return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route)); } // Affichage du formulaire return $this->render($this->twig.'edit.html.twig', [ 'useheader' => true, 'usemenu' => false, 'usesidebar' => true, 'mode' => 'submit', 'access' => $access, 'form' => $form->createView(), $this->data => $data, ]); } public function update($id, $access, Request $request, ManagerRegistry $em): Response { // Initialisation de l'enregistrement $data = $em->getRepository($this->entity)->find($id); if (!$data) { throw $this->createNotFoundException('Unable to find entity.'); } // Controler les permissions $this->canupdate($access, $data, $em); // Création du formulaire $form = $this->createForm(Form::class, $data, [ 'mode' => 'update', 'appMasteridentity' => $this->GetParameter('appMasteridentity'), 'appNiveau01label' => $this->GetParameter('appNiveau01label'), 'appNiveau02label' => $this->GetParameter('appNiveau02label'), 'appNiveau03label' => $this->GetParameter('appNiveau03label'), ]); // Récupération des data du formulaire $form->handleRequest($request); // Sur validation if ($form->get('submit')->isClicked() && $form->isValid()) { $data = $form->getData(); $em->getManager()->flush(); // Retour à la liste return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route)); } // Affichage du formulaire return $this->render($this->twig.'edit.html.twig', [ 'useheader' => true, 'usemenu' => false, 'usesidebar' => true, $this->data => $data, 'mode' => 'update', 'access' => $access, 'form' => $form->createView(), ]); } public function delete($id, $access, Request $request, ManagerRegistry $em): Response { // Récupération de l'enregistrement courant $data = $em->getRepository($this->entity)->find($id); if (!$data) { throw $this->createNotFoundException('Unable to find entity.'); } // Controler les permissions $this->canupdate($access, $data, $em); // Tentative de suppression try { $em->getManager()->remove($data); $em->getManager()->flush(); } catch (\Exception $e) { $request->getSession()->getFlashBag()->add('error', $e->getMessage()); return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route).'_update', ['id' => $id]); } return $this->redirectToRoute(str_replace('_admin_', '_'.$access.'_', $this->route)); } private function cansubmit($access, $em) { switch ($access) { case 'admin': return true; break; case 'modo': return true; break; } throw $this->createAccessDeniedException('Permission denied'); } private function canupdate($access, $entity, $em) { switch ($access) { case 'admin': return true; break; case 'modo': $usermodo = $em->getRepository("App\Entity\UserModo")->findOneBy(['user' => $this->getUser(), 'niveau01' => $entity->getNiveau02()->getNiveau01()]); if (!$usermodo) { throw $this->createAccessDeniedException('Permission denied'); } return true; break; } throw $this->createAccessDeniedException('Permission denied'); } }