wikipoints/ground/models/Notifications.php

51 lines
1.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
class Notifications extends Model
{
private static $count = NULL;
const typePointConfirmed = 'pointConfirmed';
const typeReply = 'reply';
const typeNewCommentPoint = 'newCommentPoint';
const typeNewCommentArticle = 'newCommentArticle';
function __construct($fromArray = array())
{
$this->_tableName_ = 'notifications';
parent::__construct($fromArray);
$this->setRelation('RefUser', 'userId', 'User', 'id', self::TO_ONE);
$this->setRelation('RefFromUser', 'fromUserId', 'User', 'id', self::TO_ONE);
$this->setRelation('RefPoint', 'objectId', 'Point', 'id', self::TO_ONE);
$this->setRelation('RefComment', 'objectId', 'Comment', 'id', self::TO_ONE);
}
static function model()
{
return new self();
}
public static function getUnreadedCount()
{
if (!App::$user) {
return 0;
}
if (self::$count === NULL) {
self::$count = self::model()->getCount(array(
'where' => '`userId` = '.App::$user->id.' AND `readed` = 0',
));
}
return (int) self::$count;
}
public function setAllReaded()
{
if (!App::$user) {
throw new Exception('Попытка пометить все новости прочитанными у неизвестного пользователя.');
}
return App::DB()->query('UPDATE `' . $this->_tableName_ . '` SET `readed` = 1 WHERE `userId` = ' . App::$user->id);
}
}