wikipoints/models/Like.php

94 lines
2.2 KiB
PHP

<?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)
{
$pointId = intval($pointId);
if (!App::$user || !App::$user->id || !$pointId) {
return false;
}
$point = Point::model()->getByPK($pointId);
if (!$point) {
return FALSE;
}
$ownerId = $point->author;
$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 {
// Оповещение автору точки
if ($ownerId != App::$user->id) {
Notifications::model()->add(array(
'userId' => $ownerId,
'fromUserId' => App::$user->id,
'objectId' => $pointId,
'type' => Notifications::typePointLike,
'date' => time(),
));
}
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)));
}
}