From eb9569b1437eb5aedf369272dd0ed49c21daab7a Mon Sep 17 00:00:00 2001 From: Krivchikov Dmitry Date: Sun, 24 Apr 2016 12:14:20 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D1=80=D0=B8=20=D0=BF=D0=B5=D1=80=D0=B2?= =?UTF-8?q?=D0=BE=D0=B9=20=D0=B7=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=BA=D0=B5?= =?UTF-8?q?=20=D1=82=D0=BE=D1=87=D0=B5=D0=BA=20=D0=BE=D0=BD=D0=B8=20=D0=BE?= =?UTF-8?q?=D1=82=D1=81=D0=BE=D1=80=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0?= =?UTF-8?q?=D0=BD=D1=8B=20=D0=BF=D0=BE=20=D0=B4=D0=B8=D1=81=D1=82=D0=B0?= =?UTF-8?q?=D0=BD=D1=86=D0=B8=D0=B8=20=D0=BE=D1=82=20=D0=BF=D0=BE=D0=BB?= =?UTF-8?q?=D1=8C=D0=B7=D0=BE=D0=B2=D0=B0=D1=82=D0=B5=D0=BB=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controllers/ApiController.php | 7 ++++++- models/Point.php | 25 +++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/controllers/ApiController.php b/controllers/ApiController.php index b9c0a32..f0a1d9f 100644 --- a/controllers/ApiController.php +++ b/controllers/ApiController.php @@ -48,7 +48,12 @@ class ApiController extends Controller { $minEditDate = isset($data['time']) ? intval($data['time']) : 0; - $points = Point::model()->getAllPublished(false, $minEditDate); + if ($minEditDate == 0) { + $points = Point::model()->getSortedByDistance(App::$userGeo['lat'], App::$userGeo['lng']); + } else { + $points = Point::model()->getAllPublished(false, $minEditDate); + } + $categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true)); $result = array(); diff --git a/models/Point.php b/models/Point.php index 346d198..54f038b 100644 --- a/models/Point.php +++ b/models/Point.php @@ -86,6 +86,31 @@ class Point extends Model return $ready; } + public function getSortedByDistance($lat, $lng) + { + $lat = floatval($lat); + $lng = floatval($lng); + + $query = ' + SELECT *, ( + (POW(`lat`-:lat,2)+POW(`lng`-:lng,2)) + ) AS `dist` FROM `' . $this->_tableName_ . '` + WHERE + `status` = "published" + ORDER BY `dist`;'; + + $res = App::DB()->prepare($query, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY)); + $res->execute(array(':lat' => $lat, ':lng' => $lng)); + + $ready = array(); + $calledClass = get_called_class(); + while ($res && $row = $res->fetch(PDO::FETCH_ASSOC)) { + $ready[] = new $calledClass($row); + } + + return $ready; + } + public function getCountByUser($userId) { $res = App::DB()->query('SELECT count(`id`) AS `cnt` FROM ' . $this->_tableName_ . ' WHERE `author` = ' . $userId . ' AND `status` = "published";')->fetch(PDO::FETCH_ASSOC);