98 lines
3.4 KiB
PHP
98 lines
3.4 KiB
PHP
<?php
|
|
|
|
|
|
namespace app\services;
|
|
|
|
|
|
use app\models\ExplorerObjects;
|
|
use app\models\UserPublic;
|
|
|
|
class GeoService
|
|
{
|
|
|
|
public static function getNearestObjects($latitude, $longitude, $DISTANCE_KILOMETERS = 600, $table = 'tour_objects')
|
|
{
|
|
|
|
$lg = \Yii::$app->language;
|
|
|
|
$sql_distance = "(((acos(sin((" . $latitude . "*pi()/180)) * sin((`latitude`*pi()/180))+cos((" . $latitude . "*pi()/180)) * cos((`latitude`*pi()/180)) * cos(((" . $longitude . "-`longitude`)*pi()/180))))*180/pi())*60*1.1515*1.609344) as distance ";
|
|
$having = " HAVING (distance <= $DISTANCE_KILOMETERS)";
|
|
$order_by = 'ORDER BY distance ASC';
|
|
$sql = "SELECT *, $sql_distance FROM $table $having $order_by";
|
|
$res = \Yii::$app->db->createCommand($sql);
|
|
$result = $res->queryAll();
|
|
|
|
$resultArray = [];
|
|
$bindData = [];
|
|
$ids = [];
|
|
foreach ($result as $r) {
|
|
$resultArray[] = [
|
|
'id' => $r['id'],
|
|
'dist' => $r['distance'],
|
|
'latitude' => $r['latitude'],
|
|
'longitude' => $r['longitude']
|
|
];
|
|
|
|
$bindData['id_'. $r['id']] = [
|
|
'id' => $r['id'],
|
|
'dist' => $r['distance'],
|
|
'latitude' => $r['latitude'],
|
|
'longitude' => $r['longitude'],
|
|
'name' => $r['name'],
|
|
'name_en' => $r['name_en']
|
|
];
|
|
|
|
$ids[] = $r['id'];
|
|
}
|
|
return ['data' => $resultArray, 'bindData' => $bindData, 'ids' => $ids];
|
|
}
|
|
|
|
public static function orderList($nearest) {
|
|
$ids = $nearest['ids'];
|
|
$exploreObjectList = ExplorerObjects::find()->where(['IN','tour_object_id', $ids])->all();
|
|
$exploreObjectListBuffer = [];
|
|
foreach ($exploreObjectList as $explorerObject) {
|
|
$exploreObjectListBuffer[$explorerObject->tour_object_id] = $explorerObject;
|
|
}
|
|
|
|
$lang = \Yii::$app->language;
|
|
|
|
$data = [];
|
|
$explorerObjectIds = [];
|
|
|
|
$pointSystemActive = false;
|
|
if(!empty($_GET['tn'])) {
|
|
$user = Auth::getUserByToken($_GET['tn']);
|
|
if($user && $user->club_card) {
|
|
$pointSystemActive = true;
|
|
$explorerObjectIds = $user->getExplorerObjectsIds();
|
|
}
|
|
}
|
|
|
|
foreach ($nearest['bindData'] as $key => $objectBuffer) {
|
|
if(isset($exploreObjectListBuffer[$objectBuffer['id']])) {
|
|
$object = $exploreObjectListBuffer[$objectBuffer['id']];
|
|
$dist = number_format($objectBuffer['dist'], 1, '.', '');
|
|
|
|
$dec = explode('.', $dist);
|
|
|
|
$dist = $dec[1] == '0' ? $dec[0] : $dist;
|
|
$data[] = [
|
|
'id' => $object->id,
|
|
'name' => $lang == 'en' ? $object->tourObject->name_en : $object->tourObject->name,
|
|
'points' => $object->points,
|
|
'img' => $object->getSrcOfSingleImage('explorer_object_image', '1:1'),
|
|
'distance' => $dist,
|
|
'can_visit' => $dist < 1,
|
|
'visited' => in_array($object->id, $explorerObjectIds),
|
|
'point_system' => $pointSystemActive,
|
|
'latitude' => $object->tourObject->latitude,
|
|
'longitude' => $object->tourObject->longitude
|
|
];
|
|
}
|
|
}
|
|
return $data;
|
|
}
|
|
|
|
}
|