wikipoints/ground/models/Favorite.php

71 lines
1.7 KiB
PHP

<?php
class Favorite extends Model
{
function __construct($fromArray = array())
{
$this->_tableName_ = 'favorites';
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(Favorite::model()->delete($res->id));
} else
{
return Favorite::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)));
}
public function countInFavs($pointId)
{
return count($this->getAll(array('where' => '`pointId` = ' . (int) $pointId)));
}
}