<?php | |||||
namespace Lc\ShopBundle\Controller ; | |||||
use Lc\ShopBundle\Services\Cities; | |||||
use Symfony\Component\HttpFoundation\Request; | |||||
use Symfony\Component\HttpFoundation\JsonResponse; | |||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; | |||||
class CitiesController extends AbstractController | |||||
{ | |||||
protected $cities ; | |||||
public function __construct(Cities $cities) | |||||
{ | |||||
$this->cities = $cities ; | |||||
} | |||||
public function index(Request $request) : JsonResponse | |||||
{ | |||||
$term = $request->get('term') ; | |||||
$context = $request->get('context') ; | |||||
$data = [ | |||||
'boost' => 'population', | |||||
] ; | |||||
if(strlen($term) == 5) { | |||||
$data['codePostal'] = $term ; | |||||
} | |||||
else { | |||||
$data['nom'] = $term; | |||||
} | |||||
$result = array_merge( | |||||
json_decode($this->cities->callApi('get', 'communes', array_merge($data, ['codeRegion' => 27]))), | |||||
json_decode($this->cities->callApi('get', 'communes', array_merge($data, ['codeRegion' => 44]))) | |||||
); | |||||
$return = [] ; | |||||
foreach($result as $city) { | |||||
$return[] = [ | |||||
'label' => $city->nom, | |||||
'value' => $city->code | |||||
] ; | |||||
} | |||||
if($context == 'frontend') { | |||||
$return = [ | |||||
'items' => $return | |||||
] ; | |||||
} | |||||
return new JsonResponse($return) ; | |||||
} | |||||
} |
]) | ]) | ||||
->add('lastName', TextType::class, [ | ->add('lastName', TextType::class, [ | ||||
'label' => 'Nom' | 'label' => 'Nom' | ||||
]) | |||||
->add('isSubscribedNewsletter', CheckboxType::class, [ | |||||
'label' => 'S\'inscrire à la newsletter', | |||||
'required' => false | |||||
]); | ]); | ||||
} | } | ||||
} | } | ||||
public function findByMerchantQuery($merchant) | |||||
public function findByMerchantQuery() | |||||
{ | { | ||||
return $this->createQueryBuilder('e') | return $this->createQueryBuilder('e') | ||||
->where('e.merchant = :currentMerchant') | ->where('e.merchant = :currentMerchant') | ||||
->setParameter('currentMerchant', $merchant->getId()) ; | |||||
->setParameter('currentMerchant', $this->globalParam->getCurrentMerchant()->getId()) ; | |||||
} | } | ||||
public function findAll() | public function findAll() |
public function findAllParents() | public function findAllParents() | ||||
{ | { | ||||
return $this->findByMerchantQuery($this->globalParam->getCurrentMerchant()) | |||||
return $this->findByMerchantQuery() | |||||
->andWhere('e.parent is NULL') | ->andWhere('e.parent is NULL') | ||||
->andWhere('e.status >= 0') | ->andWhere('e.status >= 0') | ||||
->orderBy('e.position', 'ASC') | ->orderBy('e.position', 'ASC') |
roles: Rôle attribué | roles: Rôle attribué | ||||
addresses: Adresses | addresses: Adresses | ||||
enabled: Activé | enabled: Activé | ||||
isSubscribedNewsletter: Inscris à la newsletter | |||||
zips: Codes postaux | zips: Codes postaux | ||||
cities: Villes | cities: Villes | ||||
orderPriceMin: Montant minimum de commande | orderPriceMin: Montant minimum de commande |
<?php | |||||
namespace Lc\ShopBundle\Services ; | |||||
class Cities | |||||
{ | |||||
function callApi($method, $url, $data = false) | |||||
{ | |||||
$url = 'https://geo.api.gouv.fr/'.$url ; | |||||
$curl = curl_init(); | |||||
switch ($method) | |||||
{ | |||||
case "POST": | |||||
curl_setopt($curl, CURLOPT_POST, 1); | |||||
if ($data) | |||||
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); | |||||
break; | |||||
case "PUT": | |||||
curl_setopt($curl, CURLOPT_PUT, 1); | |||||
break; | |||||
default: | |||||
if ($data) | |||||
$url = sprintf("%s?%s", $url, http_build_query($data)); | |||||
} | |||||
// Optional Authentication: | |||||
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); | |||||
curl_setopt($curl, CURLOPT_USERPWD, "username:password"); | |||||
curl_setopt($curl, CURLOPT_URL, $url); | |||||
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); | |||||
$result = curl_exec($curl); | |||||
curl_close($curl); | |||||
return $result; | |||||
} | |||||
} |