src/Controller/AutocompleteController.php line 30

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