src/Controller/MagicSearchController.php line 33
<?phpnamespace App\Controller;use App\Entity\Client;use App\Entity\Contact;use App\Entity\Module;use App\Entity\Identifiant;use App\Entity\Site;use App\Entity\Ticket;use Doctrine\ORM\EntityManagerInterface;use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\Routing\Annotation\Route;#[Route('/magic_search', name: 'magic_search')]#[IsGranted('ROLE_USER')]/*** Gestion de la recherche magique de la navbar*/class MagicSearchController extends AbstractController{private $entityManager;public function __construct(EntityManagerInterface $entityManager){$this->entityManager = $entityManager;}#[Route('/', name: '')]public function search(Request $request): Response{$searchResults = array();$entities = array(Client::class, Contact::class, Site::class, Ticket::class, Module::class);foreach ($entities as $entity) {$search = $this->entityManager->getRepository($entity)->createQueryBuilder('x')->andWhere('x.name LIKE :name')->setParameter('name', '%' . $request->get('search') . '%')->getQuery()->getResult();if (!empty($search)) {foreach ($search as $item) {switch (substr($item->getReference(), 0, 2)) {case 'CL':$searchResults[] = ['reference' => $item->getReference(),'name' => 'Client - ' . $item->getName(),'url' => $this->generateUrl('client_edit', array('reference' => $item->getReference()))];break;case 'CO':$searchResults[] = ['reference' => $item->getReference(),'name' => 'Contact - ' . $item->getName(),'url' => $this->generateUrl('contact_edit', array('reference' => $item->getReference()))];break;case 'SI':$searchResults[] = ['reference' => $item->getReference(),'name' => 'Site - ' . $item->getName(),'url' => $this->generateUrl('site_edit', array('reference' => $item->getReference()))];break;case 'TI':$searchResults[] = ['reference' => $item->getReference(),'name' => 'Ticket - ' . $item->getName(),'url' => $this->generateUrl('ticket_edit', array('reference' => $item->getReference()))];break;case 'MO':$searchResults[] = ['reference' => $item->getReference(),'name' => 'Module - ' . $item->getName(),'url' => $this->generateUrl('module_edit', array('reference' => $item->getReference()))];break;}}}}return $this->json($searchResults);}}