<?php
namespace App\Controller;
use Doctrine\Persistence\ManagerRegistry;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Psr\Log\LoggerInterface;
class AutocompleteController extends AbstractController
{
private $term;
public $columns;
public $query;
public $pagination = array(
'start' => 0,
'count' => 10,
);
public $limit=100;
public function __construct(private ManagerRegistry $doctrine, private LoggerInterface $logger)
{
}
#[Route('/autocomplete/{object}', name: 'autocomplete')]
public function indexAction($object, Request $request)
{
if ($request->query->get('appendNew') == 1)
$return[0] = array('label' => 'Click here to add a new entry', 'value' => 'new');
else
$return = array();
$method = 'ajaxGet'.ucfirst($object);
if (method_exists($this, $method))
{
$this->term = '%'.$request->get('term').'%';
$data = $this->$method($request);
$return = array_merge($return,$data);
}
else
{
$return = array(
'query' => 'Unit',
'suggestions' => array('suggest')
);
}
$response = new Response(json_encode($return));
$response->headers->set('Content-Type', 'application/json');
return $response;
}
public function ajaxGetAircraft($request)
{
$em = $this->doctrine->getManager();
$q = $em->createQueryBuilder();
$q->select('a')
->from('App\Entity\Aircraft','a')
->add('where', $q->expr()->orx(
$q->expr()->like('a.aircraftname', '?1')))
->orderby('a.aircraftname')
->setParameters(array(1 => $this->term));
$q->setMaxResults($this->limit);
$data = $q->getQuery()->getResult();
$final = array();
$i = 0;
foreach ($data as $d)
{
$final[$i]['label'] = $d->getAircraftname();
$final[$i]['value'] = $d->getAircraftid();
$i++;
}
return $final;
}
public function ajaxGetAirport($request)
{
$em = $this->doctrine->getManager();
$q = $em->createQueryBuilder();
$q->select('a')
->from('App\Entity\Airport','a')
->orWhere('a.cityTownSuburb LIKE \''.$this->term.'\'')
->add('where', $q->expr()->orx(
$q->expr()->like('a.airportname', '?1'),
$q->expr()->like('a.iatacode', '?2')
))
->setParameters(array(1 => $this->term, 2 => $this->term))
->add('orderBy','a.airportname ASC, a.iatacode ASC');
$data = $q->getQuery()->getResult();
$final = array();
$i = 0;
foreach ($data as $d)
{
$final[$i]['label'] = $d->__toString();
$final[$i]['value'] = $d->getAirportid();
$i++;
}
return $final;
}
public function ajaxGetCustomer($request)
{
$term = $request->get('term');
$user = $this->getUser();
$em = $this->doctrine->getManager();
$q = $em->createQueryBuilder();
$q->select('c')
->from('App\Entity\Customer','c')
->add('where', $q->expr()->orx(
$q->expr()->like('c.firstname', '?1'),
$q->expr()->like('c.surname', '?2'),
$q->expr()->like('c.email', '?3'),
$q->expr()->like('c.postcode', '?4')))
->orderBy('c.firstname', 'ASC')
->orderBy('c.surname', 'ASC')
->orderBy('c.email', 'ASC')
->orderBy('c.postcode', 'ASC')
->setParameters(array(1 => $this->term, 2 => $this->term, 3 => $this->term, 4 => $this->term));
if (!$this->isGranted('ROLE_ADMIN'))
$q->andWhere('c.createdby='.$user->getUserId());
$data = $q->getQuery()->getResult();
$final = array();
$i = 0;
foreach ($data as $d)
{
$final[$i]['label'] = $d->__toString();
$final[$i]['value'] = $d->getCustomerId();
$i++;
}
return $final;
}
}