src/Controller/MagicSearchController.php line 33

  1. <?php
  2. namespace App\Controller;
  3. use App\Entity\Client;
  4. use App\Entity\Contact;
  5. use App\Entity\Module;
  6. use App\Entity\Identifiant;
  7. use App\Entity\Site;
  8. use App\Entity\Ticket;
  9. use Doctrine\ORM\EntityManagerInterface;
  10. use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
  11. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  12. use Symfony\Component\HttpFoundation\Request;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\Routing\Annotation\Route;
  15. #[Route('/magic_search'name'magic_search')]
  16. #[IsGranted('ROLE_USER')]
  17. /**
  18.  * Gestion de la recherche magique de la navbar
  19.  */
  20. class MagicSearchController extends AbstractController
  21. {
  22.     private $entityManager;
  23.     public function __construct(EntityManagerInterface $entityManager)
  24.     {
  25.         $this->entityManager $entityManager;
  26.     }
  27.     #[Route('/'name'')]
  28.     public function search(Request $request): Response
  29.     {
  30.         $searchResults = array();
  31.         $entities = array(Client::class, Contact::class, Site::class, Ticket::class, Module::class);
  32.         foreach ($entities as $entity) {
  33.             $search $this->entityManager->getRepository($entity)->createQueryBuilder('x')
  34.                 ->andWhere('x.name LIKE :name')
  35.                 ->setParameter('name''%' $request->get('search') . '%')
  36.                 ->getQuery()
  37.                 ->getResult();
  38.             if (!empty($search)) {
  39.                 foreach ($search as $item) {
  40.                     switch (substr($item->getReference(), 02)) {
  41.                         case 'CL':
  42.                             $searchResults[] = [
  43.                                 'reference' => $item->getReference(),
  44.                                 'name' => 'Client - ' $item->getName(),
  45.                                 'url' => $this->generateUrl('client_edit', array('reference' => $item->getReference()))
  46.                             ];
  47.                             break;
  48.                         case 'CO':
  49.                             $searchResults[] = [
  50.                                 'reference' => $item->getReference(),
  51.                                 'name' => 'Contact - ' $item->getName(),
  52.                                 'url' => $this->generateUrl('contact_edit', array('reference' => $item->getReference()))
  53.                             ];
  54.                             break;
  55.                         case 'SI':
  56.                             $searchResults[] = [
  57.                                 'reference' => $item->getReference(),
  58.                                 'name' => 'Site - ' $item->getName(),
  59.                                 'url' => $this->generateUrl('site_edit', array('reference' => $item->getReference()))
  60.                             ];
  61.                             break;
  62.                         case 'TI':
  63.                             $searchResults[] = [
  64.                                 'reference' => $item->getReference(),
  65.                                 'name' => 'Ticket - ' $item->getName(),
  66.                                 'url' => $this->generateUrl('ticket_edit', array('reference' => $item->getReference()))
  67.                             ];
  68.                             break;
  69.                         case 'MO':
  70.                             $searchResults[] = [
  71.                                 'reference' => $item->getReference(),
  72.                                 'name' => 'Module - ' $item->getName(),
  73.                                 'url' => $this->generateUrl('module_edit', array('reference' => $item->getReference()))
  74.                             ];
  75.                             break;
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.         return $this->json($searchResults);
  81.     }
  82. }