Лайки для точек

This commit is contained in:
Krivchikov Dmitry 2014-11-03 13:18:28 +03:00
parent e378ee1b04
commit 46c3224370
7 changed files with 132 additions and 9 deletions

View File

@ -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'] : '';

View File

@ -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
{

View File

@ -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)));
}
}

75
ground/models/Like.php Normal file
View File

@ -0,0 +1,75 @@
<?php
class Like extends Model
{
function __construct($fromArray = array())
{
$this->_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)));
}
}

View File

@ -86,14 +86,14 @@
<hr/>
<p>
[ <span class="glyphicon glyphicon-star"></span> 13 - в закладках ]
[ <span class="glyphicon glyphicon-thumbs-up"></span> 120 - нравится ]
[ <span class="glyphicon glyphicon-star"></span> <?= $countInFavs ?> - в закладках ]
[ <span class="glyphicon glyphicon-thumbs-up"></span> <?= $countLikes ?> - нравится ]
[ <span class="glyphicon glyphicon-star"></span>
<!-- [ <span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star"></span>
<span class="glyphicon glyphicon-star-empty"></span> ]
<span class="glyphicon glyphicon-star-empty"></span> ] -->
</p>
</div>
<div class="col-md-2" style="width: 250px!important;">

View File

@ -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)

View File

@ -206,7 +206,7 @@ function showInfo(id)
// description += '<a type="button" class="btn btn-default" href="/page/point/id/' + id + '"><span class="glyphicon glyphicon-eye-open"></span> <span class="label">Подробнее</span></a>';
description += '<button type="button" class="btn btn-default" onclick="addToRoute(' + id + ')"><span class="glyphicon glyphicon-map-marker"></span> <span class="label">В маршрут</span></button>';
// description += '<button type="button" class="btn btn-default"><span class="glyphicon glyphicon-ok"></span> <span class="label">Был здесь</span></button>';
// description += '<button type="button" class="btn btn-default"><span class="glyphicon glyphicon-thumbs-up"></span> <span class="label">Нравится</span></button>';
description += '<button type="button" class="btn btn-default" onclick="addRemoveLike(' + id + ')"><span id="myLikesButton" class="glyphicon glyphicon-' + (data.iLike ? 'ok' : 'thumbs-up') + '"></span> <span class="label">Нравится</span></button>';
description += '<button type="button" class="btn btn-default" onclick="addRemoveFavorite(' + id + ')"><span id="myFavoritesButton" class="glyphicon glyphicon-star' + (data.inFavorites ? '' : '-empty') + ' "></span> <span class="label">Хочу посетить</span></button>';
if (isAdmin == '1')