src/AdminBundle/Controller/AutocompleteController.php line 11

Open in your IDE?
  1. <?php
  2. namespace AdminBundle\Controller;
  3. use PafBundle\Entity\PafAddress;
  4. use PafBundle\Entity\PafAddressUs;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. class AutocompleteController extends BaseController
  8. {
  9. public function getAction(Request $request)
  10. {
  11. $data = $request->query->all();
  12. $main_location = $this->getParameter('main_location');
  13. try {
  14. $search = $data['search'] ? $data['search'] : '';
  15. $sphinxService = $this->get('sphinx.service');
  16. if ($main_location == 'US') {
  17. $results = $sphinxService->autocompleteUS($search);
  18. if (empty($results)) {
  19. $mapboxService = $this->get('mapbox.service');
  20. $results = $mapboxService->autocomplete($search);
  21. }
  22. } else {
  23. $sphinxService = $this->get('sphinx.service');
  24. $results = $sphinxService->autocomplete($search);
  25. }
  26. return new JsonResponse($results);
  27. } catch (\Exception $e) {
  28. return new JsonResponse($e->getMessage());
  29. }
  30. }
  31. public function updateUsageAddressAction(Request $request)
  32. {
  33. $data = $request->request->all();
  34. $main_location = $this->getParameter('main_location');
  35. if ($main_location === 'UK') {
  36. $pafManager = $this->get('doctrine.orm.paf_entity_manager');
  37. $class = PafAddress::class;
  38. } else {
  39. $pafManager = $this->get('doctrine.orm.paf_us_entity_manager');
  40. $class = PafAddressUs::class;
  41. }
  42. try {
  43. $pafAddress = $pafManager
  44. ->getRepository($class, 'paf')
  45. ->findOneBy(['addressKey' => $data['addressKey']])
  46. ;
  47. if ($pafAddress) {
  48. $usage = 0;
  49. if ($main_location === 'UK') {
  50. if ($pafAddress->getUsage() !== null) {
  51. $usage = $pafAddress->getUsage() + 1;
  52. }
  53. $pafAddress->setUsage($usage);
  54. } else {
  55. if ($pafAddress->getUsed() !== null) {
  56. $usage = $pafAddress->getUsed() + 1;
  57. }
  58. $pafAddress->setUsed($usage);
  59. }
  60. $pafManager->persist($pafAddress);
  61. $pafManager->flush();
  62. }
  63. return new JsonResponse('true');
  64. } catch (\Exception $e) {
  65. return new JsonResponse('false');
  66. }
  67. }
  68. }