src/Controller/AutocompleteController.php line 30

Open in your IDE?
  1. <?php
  2. namespace App\Controller;
  3. use Doctrine\Persistence\ManagerRegistry;
  4. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\Routing\Annotation\Route;
  8. use Psr\Log\LoggerInterface;
  9. class AutocompleteController extends AbstractController
  10. {
  11.     private $term;
  12.     public $columns;
  13.     public $query;
  14.     public $pagination = array(
  15.         'start' => 0,
  16.         'count' => 10,
  17.       );
  18.     public $limit=100;
  19.     public function __construct(private ManagerRegistry $doctrine, private LoggerInterface $logger)
  20.     {    
  21.         
  22.     }
  23.     
  24.     #[Route('/autocomplete/{object}'name'autocomplete')]
  25.     public function indexAction($objectRequest $request)
  26.     {
  27.         if ($request->query->get('appendNew') == 1)
  28.             $return[0] = array('label' => 'Click here to add a new entry''value' => 'new');
  29.         else 
  30.             $return = array();
  31.         
  32.         $method 'ajaxGet'.ucfirst($object);
  33.         if (method_exists($this$method))
  34.         {
  35.             $this->term '%'.$request->get('term').'%';
  36.             $data $this->$method($request);
  37.             $return array_merge($return,$data);
  38.         }
  39.         else
  40.         {
  41.             $return = array(
  42.                 'query' => 'Unit',
  43.                 'suggestions' => array('suggest')
  44.             );
  45.         }
  46.         $response = new Response(json_encode($return));
  47.         $response->headers->set('Content-Type''application/json');
  48.         return $response;
  49.     }
  50.     
  51.     public function ajaxGetAircraft($request)
  52.     {
  53.         $em $this->doctrine->getManager();
  54.         $q $em->createQueryBuilder();
  55.         $q->select('a')
  56.             ->from('App\Entity\Aircraft','a')
  57.             ->add('where'$q->expr()->orx(
  58.                        $q->expr()->like('a.aircraftname''?1')))
  59.             ->orderby('a.aircraftname')
  60.             ->setParameters(array(=> $this->term));
  61.         
  62.         $q->setMaxResults($this->limit);
  63.         $data $q->getQuery()->getResult();
  64.         $final = array();
  65.         $i 0;
  66.         foreach ($data as $d)
  67.         {
  68.             $final[$i]['label'] = $d->getAircraftname();
  69.             $final[$i]['value'] = $d->getAircraftid();
  70.             $i++;            
  71.         }
  72.         
  73.         return $final;
  74.     }
  75.     public function ajaxGetAirport($request)
  76.     {
  77.         $em $this->doctrine->getManager();
  78.         $q $em->createQueryBuilder();
  79.         $q->select('a')
  80.             ->from('App\Entity\Airport','a')
  81.             ->orWhere('a.cityTownSuburb LIKE \''.$this->term.'\'')
  82.             ->add('where'$q->expr()->orx(
  83.                     $q->expr()->like('a.airportname''?1'),
  84.                     $q->expr()->like('a.iatacode''?2')
  85.                     ))
  86.             ->setParameters(array(=> $this->term=> $this->term))
  87.             ->add('orderBy','a.airportname ASC, a.iatacode ASC');
  88.         
  89.         $data $q->getQuery()->getResult();
  90.         $final = array();
  91.         $i 0;
  92.         foreach ($data as $d)
  93.         {
  94.             $final[$i]['label'] = $d->__toString();
  95.             $final[$i]['value'] = $d->getAirportid();
  96.             $i++;            
  97.         }
  98.         
  99.         return $final;
  100.     }
  101.     public function ajaxGetCustomer($request)
  102.     {
  103.         $term $request->get('term');
  104.         $user $this->getUser();
  105.         
  106.         $em $this->doctrine->getManager();
  107.         $q $em->createQueryBuilder();
  108.             $q->select('c')
  109.             ->from('App\Entity\Customer','c')
  110.             ->add('where'$q->expr()->orx(
  111.                        $q->expr()->like('c.firstname''?1'),
  112.                        $q->expr()->like('c.surname''?2'),
  113.                        $q->expr()->like('c.email''?3'),
  114.                     $q->expr()->like('c.postcode''?4')))
  115.             ->orderBy('c.firstname''ASC')
  116.             ->orderBy('c.surname''ASC')
  117.             ->orderBy('c.email''ASC')
  118.             ->orderBy('c.postcode''ASC')
  119.             ->setParameters(array(=> $this->term=> $this->term=> $this->term=> $this->term));
  120.         if (!$this->isGranted('ROLE_ADMIN'))
  121.             $q->andWhere('c.createdby='.$user->getUserId());
  122.         
  123.         $data $q->getQuery()->getResult();
  124.         $final = array();
  125.         $i 0;
  126.         foreach ($data as $d)
  127.         {
  128.             $final[$i]['label'] = $d->__toString();
  129.             $final[$i]['value'] = $d->getCustomerId();
  130.             $i++;            
  131.         }
  132.         
  133.         return $final;
  134.     }
  135. }