From fdb0a0bdb5643b1ccd28092b4a3b033c581b0271 Mon Sep 17 00:00:00 2001
From: Krivchikov Dmitry
Date: Sat, 16 Apr 2016 13:18:52 +0300
Subject: [PATCH] ADD Aviasales
---
app.php | 21 ++++++++
controllers/IndexController.php | 13 +----
controllers/PointController.php | 2 +
models/Airports.php | 49 +++++++++++++++++++
views/point.php | 86 +++++++++++++++++++++++++++++++++
5 files changed, 160 insertions(+), 11 deletions(-)
create mode 100644 models/Airports.php
diff --git a/app.php b/app.php
index 2f3e7b2..a93cd52 100644
--- a/app.php
+++ b/app.php
@@ -34,6 +34,8 @@ class App
public static $user = null;
public static $request = array();
public static $defaultTitle = 'Лучшие выходные — это выходные в путешествии! – wikipoints.ru';
+ public static $userIp = null;
+ public static $userGeo = array();
/**
* Разбивает урл на контроллер, метод и выбирает параметры.
@@ -75,6 +77,7 @@ class App
self::parseUrl();
self::userInit();
+ self::ipAndGeoInit();
$controller = self::$controller;
$action = self::$action;
@@ -129,6 +132,24 @@ class App
}
}
+ private static function ipAndGeoInit()
+ {
+ App::$userIp = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR'];
+
+ $loc = Storage::get(App::$userIp);
+ if(!$loc) {
+ $loc = IpInfo::geo(App::$userIp);
+ Storage::model()->set(App::$userIp, $loc, 604800); // ttl = week
+ } else {
+ App::log('reuseIp', App::$userIp);
+ }
+
+ App::$userGeo = array(
+ 'lat' => $loc['lat'],
+ 'lng' => $loc['lng'],
+ );
+ }
+
public static function userLogin($user)
{
$_SESSION['user_id'] = $user->id;
diff --git a/controllers/IndexController.php b/controllers/IndexController.php
index 6e09dcb..9a1213d 100644
--- a/controllers/IndexController.php
+++ b/controllers/IndexController.php
@@ -60,17 +60,8 @@ class IndexController extends Controller
self::addVar('userLat', $_COOKIE['userLat']);
self::addVar('userLng', $_COOKIE['userLng']);
} else {
- $ip = isset($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['HTTP_X_REAL_IP'] : $_SERVER['REMOTE_ADDR'];
-// $ip = '91.204.150.217';
- $loc = Storage::get($ip);
- if(!$loc) {
- $loc = IpInfo::geo($ip);
- Storage::model()->set($ip, $loc, 604800); // ttl = week
- } else {
- App::log('reuseIp', $ip);
- }
- self::addVar('userLat', $loc['lat']);
- self::addVar('userLng', $loc['lng']);
+ self::addVar('userLat', App::$userGeo['lat']);
+ self::addVar('userLng', App::$userGeo['lng']);
}
$og = array();
diff --git a/controllers/PointController.php b/controllers/PointController.php
index ee7ea25..dd93857 100644
--- a/controllers/PointController.php
+++ b/controllers/PointController.php
@@ -111,6 +111,8 @@ class PointController extends Controller
'photoLinks' => $photoLinksRes,
'articles' => $articles,
'extraParams' => $point->getExtraParams(),
+ 'usersAirport' => Airports::getNearest(App::$userGeo['lat'], App::$userGeo['lng']),
+ 'pointsAirport' => Airports::getNearest($point->lat, $point->lng),
));
}
diff --git a/models/Airports.php b/models/Airports.php
new file mode 100644
index 0000000..cba6a09
--- /dev/null
+++ b/models/Airports.php
@@ -0,0 +1,49 @@
+_tableName_ = 'airports';
+ parent::__construct($fromArray);
+ }
+
+ static function model()
+ {
+ return new self();
+ }
+
+ public static function getNearest($lat, $lng)
+ {
+ $degrees = 3;
+
+ $lat = floatval($lat);
+ $lng = floatval($lng);
+
+ $delta = (1+abs(SIN($lat/100)*SIN($lat/100)*5))*$degrees;
+
+ $query = '
+ SELECT *,
+ (POW(`lat`-:lat,2)+POW(`lng`-:lng,2)) AS `dist`
+ FROM `airports`
+ WHERE
+ `lat` BETWEEN :minLat AND :maxLat
+ AND
+ `lng` BETWEEN :minLng AND :maxLng
+ ORDER BY `dist`
+ LIMIT 1;';
+
+ $res = App::DB()->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
+ $res->execute(array(':lat' => $lat, ':lng' => $lng, ':minLat' => $lat - $degrees, ':minLng' => $lng - $delta, ':maxLat' => $lat + $degrees, ':maxLng' => $lng + $delta));
+
+ if ($res && $row = $res->fetch(PDO::FETCH_ASSOC)) {
+ return new Airports($row);
+ }
+
+ return null;
+
+
+ return self::model()->getAll(array('where'=>'`lat`>0 AND `lng`>0', 'order','limit'=>1));
+ }
+}
diff --git a/views/point.php b/views/point.php
index b82104e..c0f8276 100644
--- a/views/point.php
+++ b/views/point.php
@@ -119,6 +119,91 @@ $index = 0;
+
+ id != $usersAirport->id): ?>
+
+
@@ -133,6 +218,7 @@ $index = 0;
+