|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- <?php
-
- namespace Lc\CaracoleBundle\Controller\Address;
-
- use Lc\CaracoleBundle\Controller\AbstractController;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\JsonResponse;
- use Symfony\Component\Routing\Annotation\Route;
-
- class AddressApiController extends AbstractController
- {
- /**
- * @Route("/api-cities", name="api_cities")
- */
- public function cities(Request $request): JsonResponse
- {
- $citiesComponent = $this->getComponentContainer()->getCitiesComponent();
- $term = $request->get('term');
- $context = $request->get('context');
- $data = [
- 'boost' => 'population',
- ];
-
- if (strlen($term) == 5 && is_numeric($term)) {
- $data['codePostal'] = $term;
- } else {
- $data['nom'] = $term;
- }
-
- // Uniquement bourgogne franche-comté
- /*$result = array_merge(
- json_decode($citiesComponent->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))),
- json_decode($citiesComponent->callCitiesApi('get', 'communes', array_merge($data, ['codeRegion' => 44])))
- );*/
-
- $result = json_decode($citiesComponent->callCitiesApi('get', 'communes', $data));
-
- $return = [];
- foreach ($result as $city) {
- $codesPostaux = $city->codesPostaux;
-
- if ($context == 'frontend') {
- $label = '<span class="city">' . $city->nom . '</span> <span class="zip">' . $codesPostaux[0] . '</span>';
- } else {
- $label = $city->nom . ' - ' . $codesPostaux[0];
- }
-
- $return[] = [
- 'label' => $label,
- 'city' => $city->nom,
- 'value' => $city->code
- ];
- }
-
- if ($context == 'frontend') {
- $return = [
- 'items' => $return
- ];
- }
-
- return new JsonResponse($return);
- }
-
- /**
- * @Route("/api-addresses", name="api_addresses")
- */
- public function addresses(Request $request): JsonResponse
- {
- $citiesComponent = $this->getComponentContainer()->getCitiesComponent();
- $return = [];
- $address = $request->get('address');
- $context = $request->get('context');
- $results = $citiesComponent->callAddressApi($address);
-
- foreach ($results as $result) {
- if ($result->getStreetNumber() && strlen($result->getStreetNumber()) > 0) {
- $streetNameNumber = $result->getStreetNumber() . ' ' . $result->getStreetName();
- $return[] = [
- 'label' => $streetNameNumber,
- 'value' => $streetNameNumber,
- 'latitude' => $result->getCoordinates()->getLatitude(),
- 'longitude' => $result->getCoordinates()->getLongitude()
- ];
- }
- }
-
- if ($context == 'frontend') {
- $return = [
- 'items' => $return
- ];
- }
-
- return new JsonResponse($return);
- }
- }
|