From 46c32243700233dc45c4be00bdb498224bb5c67a Mon Sep 17 00:00:00 2001 From: Krivchikov Dmitry Date: Mon, 3 Nov 2014 13:18:28 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9B=D0=B0=D0=B9=D0=BA=D0=B8=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D1=82=D0=BE=D1=87=D0=B5=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ground/controllers/JsonController.php | 11 ++++ ground/controllers/PageController.php | 10 ++-- ground/models/Favorite.php | 19 +++++++ ground/models/Like.php | 75 +++++++++++++++++++++++++++ ground/views/point.php | 8 +-- public/js/app.js | 16 ++++++ public/js/map.js | 2 +- 7 files changed, 132 insertions(+), 9 deletions(-) create mode 100644 ground/models/Like.php diff --git a/ground/controllers/JsonController.php b/ground/controllers/JsonController.php index cf95f56..af25267 100644 --- a/ground/controllers/JsonController.php +++ b/ground/controllers/JsonController.php @@ -68,6 +68,7 @@ class JsonController extends Controller $point->categoryName = $categories[$point->categoryId]['name']; $point->photosCount = $photosCount; $point->inFavorites = Favorite::model()->checkIsMy($id); + $point->iLike = Like::model()->checkIsMy($id); self::renderPartial('json.php', $point->getValues()); } else @@ -328,6 +329,16 @@ class JsonController extends Controller self::renderPartial('json.php', Helper::boolval(Favorite::addRemove($id))); } + static function actionAddRemoveLike() + { + $id = (int) App::getParam('id'); + if (!$id || !App::$user) + { + App::error404(); + } + self::renderPartial('json.php', Helper::boolval(Like::addRemove($id))); + } + static function actionSearch() { $search = isset($_GET['query']) ? $_GET['query'] : ''; diff --git a/ground/controllers/PageController.php b/ground/controllers/PageController.php index 4fb652d..5c314bd 100644 --- a/ground/controllers/PageController.php +++ b/ground/controllers/PageController.php @@ -44,8 +44,8 @@ class PageController extends Controller { self::$layout = 'layoutPage.php'; - $id = (int) App::getParam('id'); - if ($id) + $pointId = (int) App::getParam('id'); + if ($pointId) { self::addScript('/js/jquery-2.1.0.min.js'); self::addScript('/js/jquery-ui-1.10.4.custom.min.js'); @@ -59,7 +59,7 @@ class PageController extends Controller self::addStyle('/css/bootstrap-theme.min.css'); self::addStyle('/css/fotorama.css'); - $point = Point::model()->getByPK($id); + $point = Point::model()->getByPK($pointId); $point->descriptionHtml = nl2br($point->description); $point->name = htmlspecialchars($point->name); App::setConfig('title', $point->name. ' - wikipoints.ru'); @@ -71,7 +71,7 @@ class PageController extends Controller $randomPoint = Point::model()->getRandom(); $similarPoints = Point::model()->getAll(array('where' => 'categoryId = '.$point->categoryId.' AND `id` <> '.$point->id.' ORDER BY RAND() LIMIT 6;')); - $closestPoints = Point::model()->getClosestPoints($id); + $closestPoints = Point::model()->getClosestPoints($pointId); self::render('point.php', array( 'point' => $point, @@ -80,6 +80,8 @@ class PageController extends Controller 'closestPoints' => $closestPoints, 'categories' => $categories, 'photos' => $point->photos, + 'countInFavs' => Favorite::model()->countInFavs($pointId), + 'countLikes' => Like::model()->countLikes($pointId), )); } else { diff --git a/ground/models/Favorite.php b/ground/models/Favorite.php index f00202b..fa7d20f 100644 --- a/ground/models/Favorite.php +++ b/ground/models/Favorite.php @@ -16,6 +16,11 @@ class Favorite extends Model return new self(); } + /** + * Добавляет точку в избранное если её там нет и удаляет - если есть. + * @param integer $pointId + * @return boolean + */ public static function addRemove($pointId) { if (!App::$user || !App::$user->id || !$pointId) @@ -32,6 +37,10 @@ class Favorite extends Model } } + /** + * Возвращает все мои точки из избранного + * @return boolean + */ public function getAllMy() { if (!App::$user || !App::$user->id) @@ -40,6 +49,11 @@ class Favorite extends Model return $this->getAll(array('where' => '`userId` = ' . (int) App::$user->id)); } + /** + * Эта точка в моём избранном? + * @param integet $pointId + * @return boolean + */ public function checkIsMy($pointId) { if (!App::$user || !App::$user->id || !$pointId) @@ -48,4 +62,9 @@ class Favorite extends Model return Helper::boolval($this->getAll(array('where' => '`userId` = ' . (int) App::$user->id . ' AND `pointId` = ' . (int) $pointId))); } + public function countInFavs($pointId) + { + return count($this->getAll(array('where' => '`pointId` = ' . (int) $pointId))); + } + } diff --git a/ground/models/Like.php b/ground/models/Like.php new file mode 100644 index 0000000..4d9fd4a --- /dev/null +++ b/ground/models/Like.php @@ -0,0 +1,75 @@ +_tableName_ = 'likes'; + parent::__construct($fromArray); + $this->setRelation('user', 'userId', 'User', 'id', self::TO_ONE); + $this->setRelation('point', 'pointId', 'Point', 'id', self::TO_ONE); + } + + static function model() + { + return new self(); + } + + /** + * Добавляет точку в понравившиеся если её там нет и удаляет - если есть. + * @param integer $pointId + * @return boolean + */ + public static function addRemove($pointId) + { + if (!App::$user || !App::$user->id || !$pointId) + return false; + + $res = self::model()->getAll(array('where' => '`userId` = ' . (int) App::$user->id . ' AND `pointId` = ' . (int) $pointId, 'limit' => 1)); + + if ($res) + { + return Helper::boolval(Like::model()->delete($res->id)); + } else + { + return Like::model()->add(array('userId' => App::$user->id, 'pointId' => $pointId)); + } + } + + /** + * Возвращает все понравившиеся мне точки + * @return boolean + */ + public function getAllMy() + { + if (!App::$user || !App::$user->id) + return false; + + return $this->getAll(array('where' => '`userId` = ' . (int) App::$user->id)); + } + + /** + * Эта точка мне нравится? + * @param integet $pointId + * @return boolean + */ + public function checkIsMy($pointId) + { + if (!App::$user || !App::$user->id || !$pointId) + return false; + + return Helper::boolval($this->getAll(array('where' => '`userId` = ' . (int) App::$user->id . ' AND `pointId` = ' . (int) $pointId))); + } + + /** + * Кол-во лайков точки + * @param type $pointId + * @return type + */ + public function countLikes($pointId) + { + return count($this->getAll(array('where' => '`pointId` = ' . (int) $pointId))); + } + +} diff --git a/ground/views/point.php b/ground/views/point.php index f2e5571..9c81f75 100644 --- a/ground/views/point.php +++ b/ground/views/point.php @@ -86,14 +86,14 @@

- [ 13 - в закладках ] - [ 120 - нравится ] + [ - в закладках ] + [ - нравится ] - [ +

diff --git a/public/js/app.js b/public/js/app.js index 8a01cc1..92c20e3 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -127,6 +127,22 @@ function addRemoveFavorite(id) } } +function addRemoveLike(id) +{ + if (user != '0') + { + $.ajax({ + url: "/json/addRemoveLike/id/" + id, + success: function(data) { + if (data){ + $('#myLikesButton').toggleClass('glyphicon-thumbs-up').toggleClass('glyphicon-ok'); + } + }}); + } else { + $('#loginModal').modal('show'); + } +} + window.addEventListener('popstate', function(e) { if (typeof(e.state) == 'object' && e.state != null) diff --git a/public/js/map.js b/public/js/map.js index a7aef1e..baae659 100644 --- a/public/js/map.js +++ b/public/js/map.js @@ -206,7 +206,7 @@ function showInfo(id) // description += ' Подробнее'; description += ''; // description += ''; -// description += ''; + description += ''; description += ''; if (isAdmin == '1')