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 - нравится ] + [ = $countInFavs ?> - в закладках ] + [ = $countLikes ?> - нравится ] - [ +