Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

96 lines
3.1KB

  1. <?php
  2. namespace Lc\CaracoleBundle\Controller\Address;
  3. use Lc\CaracoleBundle\Controller\AbstractController;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\JsonResponse;
  6. use Symfony\Component\Routing\Annotation\Route;
  7. class AddressApiController extends AbstractController
  8. {
  9. /**
  10. * @Route("/api-cities", name="api_cities")
  11. */
  12. public function cities(Request $request): JsonResponse
  13. {
  14. $citiesComponent = $this->getComponentContainer()->getCitiesComponent();
  15. $term = $request->get('term');
  16. $context = $request->get('context');
  17. $data = [
  18. 'boost' => 'population',
  19. ];
  20. if (strlen($term) == 5 && is_numeric($term)) {
  21. $data['codePostal'] = $term;
  22. } else {
  23. $data['nom'] = $term;
  24. }
  25. // Uniquement bourgogne franche-comté
  26. /*$result = array_merge(
  27. json_decode($citiesComponent->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))),
  28. json_decode($citiesComponent->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 44])))
  29. );*/
  30. $result = json_decode($citiesComponent->callCitiesApi('get', 'communes', $data));
  31. $return = [];
  32. foreach ($result as $city) {
  33. $codesPostaux = $city->codesPostaux;
  34. if ($context == 'frontend') {
  35. $label = '<span class="city">' . $city->nom . '</span> <span class="zip">' . $codesPostaux[0] . '</span>';
  36. } else {
  37. $label = $city->nom . ' - ' . $codesPostaux[0];
  38. }
  39. $return[] = [
  40. 'label' => $label,
  41. 'city' => $city->nom,
  42. 'value' => $city->code
  43. ];
  44. }
  45. if ($context == 'frontend') {
  46. $return = [
  47. 'items' => $return
  48. ];
  49. }
  50. return new JsonResponse($return);
  51. }
  52. /**
  53. * @Route("/api-addresses", name="api_addresses")
  54. */
  55. public function addresses(Request $request): JsonResponse
  56. {
  57. $citiesComponent = $this->getComponentContainer()->getCitiesComponent();
  58. $return = [];
  59. $address = $request->get('address');
  60. $context = $request->get('context');
  61. $results = $citiesComponent->callAddressApi($address);
  62. foreach ($results as $result) {
  63. if ($result->getStreetNumber() && strlen($result->getStreetNumber()) > 0) {
  64. $streetNameNumber = $result->getStreetNumber() . ' ' . $result->getStreetName();
  65. $return[] = [
  66. 'label' => $streetNameNumber,
  67. 'value' => $streetNameNumber,
  68. 'latitude' => $result->getCoordinates()->getLatitude(),
  69. 'longitude' => $result->getCoordinates()->getLongitude()
  70. ];
  71. }
  72. }
  73. if ($context == 'frontend') {
  74. $return = [
  75. 'items' => $return
  76. ];
  77. }
  78. return new JsonResponse($return);
  79. }
  80. }