| Server IP : 162.214.74.102 / Your IP : 216.73.217.114 Web Server : Apache System : Linux dedi-4363141.lrsys.com.br 3.10.0-1160.119.1.el7.tuxcare.els25.x86_64 #1 SMP Wed Oct 1 17:37:27 UTC 2025 x86_64 User : lrsys ( 1015) PHP Version : 5.6.40 Disable Function : exec,passthru,shell_exec,system MySQL : ON | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /home/lrsys/www/lrsys_apps/leo/application/plugins/module_imobles/models/ |
Upload File : |
<?php
include_once __DIR__ . "/../../../autoload/My_Model.php";
include_once __DIR__ . "/../factories/NeighborhoodFactory.php";
include_once __DIR__ . "/../helpers/google/GoogleMaps.php";
class NeighborhoodModel extends My_Model {
/**
* @var string
*/
public $table = 'module_imobles_neighborhoods';
public function createNeighborhood($data) {
$neighborhood = self::create();
if(empty($data['google_place_id'])){
$this->getGooglePlaceID($data);
}
// if(empty($data['google_place_details'])){
// $this->getGooglePlaceDetails($data);
// }
NeighborhoodFactory::create($data, $neighborhood, true);
$neighborhood->save();
$this->saveRelated($data, $neighborhood->id);
return $neighborhood;
}
public function updateNeighborhood($data, $neighborhood, $update = false) {
ORM::for_table($this->table)->create($neighborhood);
if(empty($data['google_place_id'])){
$this->getGooglePlaceID($data);
}
// if(empty($data['google_place_details']) || $data['google_place_details'] <> $neighborhood->google_place_details){
// $this->getGooglePlaceDetails($data);
// }
$neighborhood = NeighborhoodFactory::create($data, $neighborhood, false, $update);
$neighborhood->save();
$this->saveRelated($data, $neighborhood->id);
return $neighborhood;
}
public function saveRelated($data, $id) {
ORM::for_table('module_imobles_neighborhoods_related')
->where('neighborhood_id', $id)
->delete_many();
if (isset($data['neighborhood_id_related'])) {
foreach ($data['neighborhood_id_related'] as $t) {
$a = ORM::for_table("module_imobles_neighborhoods_related")->create();
$a->neighborhood_id = $id;
$a->related_neighborhood_id = $t;
$a->save();
}
}
}
public function deleteNeighborhood($neighborhood) {
ORM::for_table($this->table)->create($neighborhood);
$now = new DateTime('now', new DateTimeZone('America/Sao_Paulo'));
$user = User::_info();
$neighborhood->deleted_at = $now->format('Y-m-d H:i:s');
$neighborhood->deleted_by = $user->id;
$neighborhood->save();
return $neighborhood;
}
private function getGooglePlaceID(&$data){
$c = ORM::for_table('city')
->select_expr('c.city_name, s.uf_state')
->table_alias('c')
->left_outer_join('state', 's.id = c.id_state', 's')
->where_equal('c.id', $data['city_id'])
->find_array();
if (count($c) > 0) {
$textQuery = $data['name'] . ", " . $c[0]['city_name'] . " - " . $c[0]['uf_state'];
$googleMaps = new GoogleMaps();
$google_place = $googleMaps->findPlaceFromText($textQuery);
if(!empty($google_place->place_id)){
$neighborhood_details= $this->getGooglePlaceDetails($google_place->place_id);
if($neighborhood_details){
if(in_array('sublocality_level_1',$neighborhood_details->types)){
$data['google_details'] = $neighborhood_details;
$data['google_place_id'] = $google_place->place_id;
}
}
}
}
}
private function getGooglePlaceDetails($place_id){
$googleMaps = new GoogleMaps();
$details = $googleMaps->placeDetails($place_id);
return $details;
}
public function getOrCreateNeighborhood($name, $city_id, $state_id){
$neighborhood = ORM::for_table($this->table)
->where_equal('name', $name)
->where_equal('city_id', $city_id)
->find_one();
if($neighborhood){
//
} else {
$data = [
'name' => $name,
'city_id' => $city_id,
'state_id' => $state_id
];
$neighborhood = $this->createNeighborhood($data);
}
return $neighborhood;
}
}