src/Controller/NotesController.php line 161

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Notes;
  4. use App\Form\NotesType;
  5. use App\Repository\NotesRepository;
  6. use App\Repository\FamilyRepository;
  7. use App\Repository\UserRepository;
  8. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  9. use Symfony\Component\HttpFoundation\Request;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. use Symfony\Component\Validator\Validator\ValidatorInterface;
  13. use Symfony\Component\Security\Core\Security;
  14. use Doctrine\ORM\EntityManagerInterface;
  15. use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
  16. /**
  17.  * @Route("/notes")
  18.  */
  19. class NotesController extends AbstractController
  20. {
  21.     /**
  22.      * @var Security
  23.      */
  24.     private $security;
  25.     private EntityManagerInterface $em;
  26.     public function __construct(Security $securityCsrfTokenManagerInterface $tokenManagerEntityManagerInterface $em)
  27.     {
  28.         $this->security $security;
  29.         $this->tokenManager $tokenManager;
  30.         $this->em $em;
  31.     }
  32.     /**
  33.      * @Route("/", name="app_notes_index", methods={"GET"})
  34.      */
  35.     public function index(NotesRepository $notesRepository): Response
  36.     {
  37.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  38.         return $this->render('notes/index.html.twig', [
  39.             'notes' => $notesRepository->findAll(),
  40.         ]);
  41.     }
  42.     /**
  43.      * @Route("/new", name="app_notes_new", methods={"GET", "POST"} , options={"expose"=true})
  44.      */
  45.     public function new(Request $requestNotesRepository $notesRepositoryFamilyRepository $FamilyRepositoryValidatorInterface $validator): Response
  46.     {
  47.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  48.         $error null;
  49.         $note = new Notes();
  50.         $form $this->createForm(NotesType::class, $note);
  51.         $form->handleRequest($request);
  52.         if ($form->isSubmitted() && !$form->isValid()) {
  53.             $errors $validator->validate($familyMember);
  54.             if (count($errors) > 0) {
  55.                 $error $errors[0]['constraint']['message'];
  56.             } else {
  57.                 $error 'Technisches Problem .';
  58.             }
  59.             return new Response($error);
  60.         } else if ($form->isSubmitted() && $form->isValid()) {
  61.             //$user = $this->security->getUser()->getRoles() ;
  62.             $user $this->security->getUser();
  63.             $note->setCreatedAt(\DateTime::createFromFormat('Y-m-d H:i:s'date('Y-m-d H:i:s'strtotime(date('Y-m-d H:i:s')))));
  64.             $notesRepository->add($notetrue);
  65.             return new Response('OK');
  66.         }
  67.         return $this->renderForm('notes/new.html.twig', [
  68.             'note' => $note,
  69.             'form' => $form,
  70.         ]);
  71.     }
  72.     /**
  73.      * @Route("/{id}/show", name="app_notes_show", methods={"GET"})
  74.      */
  75.     public function show(Notes $note): Response
  76.     {
  77.         return $this->render('notes/show.html.twig', [
  78.             'note' => $note,
  79.         ]);
  80.     }
  81.     /**
  82.      * @Route("/{id}/edit", name="app_notes_edit", methods={"GET", "POST"}, options={"expose"=true})
  83.      */
  84.     public function edit(Request $requestNotes $noteNotesRepository $notesRepository): Response
  85.     {
  86.         $form $this->createForm(NotesType::class, $note);
  87.         $form->handleRequest($request);
  88.         if ($form->isSubmitted() && $form->isValid()) {
  89.             $notesRepository->add($notetrue);
  90.             return new Response('OK');
  91.         }
  92.         return $this->renderForm('notes/edit.html.twig', [
  93.             'note' => $note,
  94.             'form' => $form,
  95.         ]);
  96.     }
  97.     /**
  98.      * @Route("/{id}/delete", name="app_notes_delete", methods={"POST"}, options={"expose"=true})
  99.      */
  100.     public function delete(Request $requestNotes $noteNotesRepository $notesRepository): Response
  101.     {
  102.         if ($this->isCsrfTokenValid('delete' $note->getId(), $request->request->get('_token_note'))) {
  103.             $notesRepository->remove($notetrue);
  104.         }
  105.         return new Response('OK');
  106.     }
  107.     /**
  108.      * @Route("/serverNotes", name="serverNotes", methods={"GET"} , options = {"expose" = true } )
  109.      */
  110.     public function serverNotes(Request $requestUserRepository $userRepositoryEntityManagerInterface $entityManager): Response
  111.     {
  112.         $this->denyAccessUnlessGranted('IS_AUTHENTICATED_FULLY');
  113.         $user $userRepository->find($this->security->getToken()->getUser()->getId());
  114.         $sql_where '';
  115.         if (($request->get('searchByEtat') != "all") && ($request->get('searchByEtat') != "")) {
  116.             $sql_where .= ' AND p.etat = ' $request->get('searchByEtat');
  117.         }
  118.         if (($request->get('searchByEtat') != "all") && ($request->get('searchByEtat') != "")) {
  119.             $sql_where .= ' AND p.etat = ' $request->get('searchByEtat');
  120.         }
  121.         if ( (!$this->security->isGranted('ROLE_ADMIN')) && (!$this->security->isGranted('ROLE_SUPERVISOR')) ) {
  122.             
  123.             $authorizedFamilies $user->getFamilies();
  124.             
  125.             $familieIds = [];
  126.             foreach($authorizedFamilies as $authorizedFamily){
  127.                 $familieIds[] = $authorizedFamily->getId();
  128.             }
  129.             
  130.             if(count($familieIds)>0) { 
  131.                 $sql_where .= ' AND  f.id IN ('.implode(','$familieIds).')';
  132.             } else {
  133.                 $sql_where .= ' AND f.id = 0 ';
  134.             }
  135.         }
  136.         if (($request->get('family') != "")) {
  137.             $sql_where .= ' AND p.family = ' $request->get('family');
  138.         }
  139.         $columns = ['p.id''f.firstname''f.lastname''p.id'];
  140.         $sql_search '';
  141.         if ($request->get('search')['value'] != "") {
  142.             $sql_where_glob_arr = [];
  143.             foreach ($columns as $column) {
  144.                 $sql_where_glob_arr[] = ' ' $column ' LIKE \'%' $request->get('search')['value'] . '%\' ';
  145.             }
  146.             if (count($sql_where_glob_arr) > 0) {
  147.                 $sql_search ' AND ( ' implode(' OR '$sql_where_glob_arr) . ' ) ';
  148.             }
  149.         }
  150.         $orders = [];
  151.         for ($i 0$i count($request->get('order')); $i++) {
  152.             $orders[] = $columns[$request->get('order')[$i]['column']] . ' ' $request->get('order')[$i]['dir'];
  153.         }
  154.         if (count($orders) > 0) {
  155.             $order " ORDER BY " implode(' , '$orders);
  156.         } else {
  157.             $order " ORDER BY p.id DESC";
  158.         }
  159.         $existance $entityManager->createQuery(
  160.             'SELECT p
  161.             FROM App\Entity\Notes p
  162.             LEFT JOIN p.family f
  163.             
  164.             WHERE 1=1 ' $sql_where ' ' $sql_search '
  165.              
  166.             '
  167.         )
  168.             ->getResult();
  169.         $I_nbResultatsTotal count($existance);
  170.         $limit =  $request->get('length');
  171.         $offset $request->get('start');
  172.         $array_search = array();
  173.         //->setParameter('nom', '%'.$request->get('searchByNom').'%')
  174.         //p.nom LIKE :nom
  175.         $notes $entityManager->createQuery(
  176.             'SELECT p
  177.             FROM App\Entity\Notes p
  178.             LEFT JOIN p.family f
  179.             
  180.             WHERE 1=1 ' $sql_where ' ' $sql_search ' ' $order '
  181.             
  182.             
  183.             '
  184.         )
  185.             ->setMaxResults($limit)
  186.             ->setFirstResult($offset)
  187.             ->getResult();
  188.         $output = [];
  189.         $status = ["Storniert""In Bearbeitung""Erledigt"];
  190.         foreach ($notes as $note) {
  191.             $token $this->tokenManager->getToken('delete' $note->getId())->getValue();
  192.             $output[] = [
  193.                 'family' => $note->getFamily() != null $note->getFamily()->getLastname() . ' ' $note->getFamily()->getFirstname() : 'All families',
  194.                 'family_id' => $note->getFamily() != null $note->getFamily()->getId() : '',
  195.                 'created_at' => date_format($note->getCreatedAt(), 'd/m/Y H:i:s'),
  196.                 'note' => $note->getNote(),
  197.                 'status' => $status[$note->getStatus()],
  198.                 'user' => $note->getUser() != null $note->getUser()->getFirstname() . ' ' $note->getUser()->getLastname() : 'All users',
  199.                 'id' => $note->getId(),
  200.                 'token' => $token
  201.             ];
  202.         }
  203.         $JSON json_encode($output);
  204.         $JSON '{"draw": ' $request->get('draw') . ',"recordsTotal":' $I_nbResultatsTotal ',"recordsFiltered":' $I_nbResultatsTotal ',"data":' $JSON '}';
  205.         $response   = new Response($JSON200, ['Content-Type' => 'application/json']);
  206.         return $response;
  207.     }
  208. }