_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) { $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(Favorite::model()->delete($res->id)); } else { // Оповещение автору точки if ($ownerId != App::$user->id) { Notifications::model()->add(array( 'userId' => $ownerId, 'fromUserId' => App::$user->id, 'objectId' => $pointId, 'type' => Notifications::typePointFav, 'date' => time(), )); } 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))); } }