Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AddressApiController.php 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. $result = array_merge(
  26. json_decode($citiesComponent->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))),
  27. json_decode($citiesComponent->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 44])))
  28. );
  29. $return = [];
  30. foreach ($result as $city) {
  31. $codesPostaux = $city->codesPostaux;
  32. if ($context == 'frontend') {
  33. $label = '<span class="city">' . $city->nom . '</span> <span class="zip">' . $codesPostaux[0] . '</span>';
  34. } else {
  35. $label = $city->nom . ' - ' . $codesPostaux[0];
  36. }
  37. $return[] = [
  38. 'label' => $label,
  39. 'city' => $city->nom,
  40. 'value' => $city->code
  41. ];
  42. }
  43. if ($context == 'frontend') {
  44. $return = [
  45. 'items' => $return
  46. ];
  47. }
  48. return new JsonResponse($return);
  49. }
  50. /**
  51. * @Route("/api-addresses", name="api_addresses")
  52. */
  53. public function addresses(Request $request): JsonResponse
  54. {
  55. $citiesComponent = $this->getComponentContainer()->getCitiesComponent();
  56. $return = [];
  57. $address = $request->get('address');
  58. $context = $request->get('context');
  59. $results = $citiesComponent->callAddressApi($address);
  60. foreach ($results as $result) {
  61. if ($result->getStreetNumber() && strlen($result->getStreetNumber()) > 0) {
  62. $streetNameNumber = $result->getStreetNumber() . ' ' . $result->getStreetName();
  63. $return[] = [
  64. 'label' => $streetNameNumber,
  65. 'value' => $streetNameNumber,
  66. 'latitude' => $result->getCoordinates()->getLatitude(),
  67. 'longitude' => $result->getCoordinates()->getLongitude()
  68. ];
  69. }
  70. }
  71. if ($context == 'frontend') {
  72. $return = [
  73. 'items' => $return
  74. ];
  75. }
  76. return new JsonResponse($return);
  77. }
  78. }