You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
3.2KB

  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. $return[] = [
  36. 'label' => '<span class="city">'.$city->nom.'</span> <span class="zip">'.$codesPostaux[0].'</span>',
  37. 'city' => $city->nom,
  38. 'value' => $city->code
  39. ] ;
  40. }
  41. if($context == 'frontend') {
  42. $return = [
  43. 'items' => $return
  44. ] ;
  45. }
  46. return new JsonResponse($return) ;
  47. }
  48. public function addresses(Request $request) : JsonResponse
  49. {
  50. $return = [] ;
  51. $address = $request->get('address') ;
  52. $context = $request->get('context') ;
  53. $results = $this->utils->callAddressApi($address) ;
  54. foreach($results as $result) {
  55. if($result->getStreetNumber() && strlen($result->getStreetNumber()) > 0) {
  56. $streetNameNumber = $result->getStreetNumber().' '.$result->getStreetName() ;
  57. $return[] = [
  58. 'label' => $streetNameNumber,
  59. 'value' => $streetNameNumber,
  60. 'latitude' => $result->getCoordinates()->getLatitude(),
  61. 'longitude' => $result->getCoordinates()->getLongitude()
  62. ] ;
  63. }
  64. }
  65. if($context == 'frontend') {
  66. $return = [
  67. 'items' => $return
  68. ] ;
  69. }
  70. return new JsonResponse($return) ;
  71. }
  72. }