No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

AddressApiController.php 3.4KB

hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
hace 4 años
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Lc\ShopBundle\Controller ;
  3. use Lc\ShopBundle\Services\CityUtils;
  4. use Lc\ShopBundle\Services\Utils;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\JsonResponse;
  7. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  8. class AddressApiController extends AbstractController
  9. {
  10. protected $utils ;
  11. public function __construct(Utils $utils)
  12. {
  13. $this->utils = $utils ;
  14. }
  15. public function cities(Request $request) : JsonResponse
  16. {
  17. $term = $request->get('term') ;
  18. $context = $request->get('context') ;
  19. $data = [
  20. 'boost' => 'population',
  21. ] ;
  22. if(strlen($term) == 5 && is_numeric($term)) {
  23. $data['codePostal'] = $term ;
  24. }
  25. else {
  26. $data['nom'] = $term;
  27. }
  28. $result = array_merge(
  29. json_decode($this->utils->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))),
  30. json_decode($this->utils->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 44])))
  31. );
  32. $return = [] ;
  33. foreach($result as $city) {
  34. $codesPostaux = $city->codesPostaux ;
  35. if($context == 'frontend') {
  36. $label = '<span class="city">'.$city->nom.'</span> <span class="zip">'.$codesPostaux[0].'</span>' ;
  37. }
  38. else {
  39. $label = $city->nom.' - '.$codesPostaux[0] ;
  40. }
  41. $return[] = [
  42. 'label' => $label,
  43. 'city' => $city->nom,
  44. 'value' => $city->code
  45. ] ;
  46. }
  47. if($context == 'frontend') {
  48. $return = [
  49. 'items' => $return
  50. ] ;
  51. }
  52. return new JsonResponse($return) ;
  53. }
  54. public function addresses(Request $request) : JsonResponse
  55. {
  56. $return = [] ;
  57. $address = $request->get('address') ;
  58. $context = $request->get('context') ;
  59. $results = $this->utils->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. }