При первой загрузке точек они отсортированы по дистанции от пользователя

This commit is contained in:
Krivchikov Dmitry 2016-04-24 12:14:20 +03:00
parent 30289776bd
commit eb9569b143
2 changed files with 31 additions and 1 deletions

View File

@ -48,7 +48,12 @@ class ApiController extends Controller
{ {
$minEditDate = isset($data['time']) ? intval($data['time']) : 0; $minEditDate = isset($data['time']) ? intval($data['time']) : 0;
if ($minEditDate == 0) {
$points = Point::model()->getSortedByDistance(App::$userGeo['lat'], App::$userGeo['lng']);
} else {
$points = Point::model()->getAllPublished(false, $minEditDate); $points = Point::model()->getAllPublished(false, $minEditDate);
}
$categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true)); $categories = Category::model()->getAll(array('order' => 'ord ASC', 'asArray' => true));
$result = array(); $result = array();

View File

@ -86,6 +86,31 @@ class Point extends Model
return $ready; 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) 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); $res = App::DB()->query('SELECT count(`id`) AS `cnt` FROM ' . $this->_tableName_ . ' WHERE `author` = ' . $userId . ' AND `status` = "published";')->fetch(PDO::FETCH_ASSOC);