File: /var/www/vhost/disk-apps/qas.sports-crowd.com/app/Http/Controllers/AddressController.php
<?php
namespace App\Http\Controllers;
use App\Address;
use App\City;
class AddressController extends Controller
{
public function create($tag, $direction, $district, $indications, $lat, $long, $city_id, $idUser)
{
$obj = new Address();
$obj->direction = $direction;
$obj->tag = $tag;
$obj->lat = $lat;
$obj->long = $long;
$obj->active = true;
$obj->district = $district;
$obj->user_id = $idUser;
$obj->indications = $indications;
$obj->city_id = $city_id;
$obj->last_used = 1;
$obj->save();
if ($obj) {
return $obj->id;
}
}
public function update($id, $tag, $district, $indications)
{
$obj = Address::find($id);
$obj->tag = $tag;
$obj->district = $district;
$obj->indications = $indications;
$obj->update();
if ($obj) {
return true;
}
}
// se encarga de colocar la ultima direccion usada en true
public function lastUsedDirection($id_user, $id_lastUsed)
{
$addresses = Address::where('user_id', $id_user)->get();
foreach ($addresses as $key => $address) {
if ($address->id == $id_lastUsed) {
$address->last_used = 1;
} else {
$address->last_used = 0;
}
$address->update();
}
}
// Se encarga de tomar una dirección p.e Calle 13 # 78 - 54, Cali, Colombia. y la transforma en Lat y Long con su poligono de desviación en error.
public function geocodeAddress($address)
{
// Peticion a Central para crear el "Cliente"
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://maps.googleapis.com',
'defaults' => [
'exceptions' => false,
],
]);
$responseGuzzle = $client->get('/maps/api/geocode/json?key=' . config('googlmapper.key') . '&address=' . rawurlencode($address), [
'headers' => [
'Accept' => 'application/json',
],
]);
$body = (string) $responseGuzzle->getBody();
$body = json_decode($body);
return $body->results;
}
public function geocodeAddressByCoordinates($latitude, $longitude)
{
// Peticion a Central para crear el "Cliente"
$client = new \GuzzleHttp\Client([
'base_uri' => 'https://maps.googleapis.com',
'defaults' => [
'exceptions' => false,
],
]);
$responseGuzzle = $client->get('/maps/api/geocode/json?key=' . config('googlmapper.key') . '&latlng=' . $latitude . ',' . $longitude, [
'headers' => [
'Accept' => 'application/json',
],
]);
$body = (string) $responseGuzzle->getBody();
$body = json_decode($body);
return $body->results;
}
public function searchCityByName($name)
{
if ($name && $name != "") {
$city = City::select('id', 'name')->where('name', $name)->first();
if ($city) {
return $city->id;
}
}
return 547;
}
public function searchAddressByAddress($address, $user_id)
{
if ($address && $address != "") {
$c_address = Address::select('id')->where([['direction', 'like', '%' . $address . '%'], ['user_id', $user_id]])->first();
if ($c_address) {
return $c_address->id;
}
}
return null;
}
}