File: /var/www/heifetz/heifetz-app/models/Notification.php
<?php
declare(strict_types=1);
namespace Models;
use Core\DbLib\NewDbModel;
use Core\Models\OldUser;
use Helpers\TemplateHelper;
/**
* Class Notification
*
* @property int $id
* @property int $user_id
* @property int $author_id
* @property int $factory_id
* @property string $message
* @property string $created_at
* @property int $read_at
* @property int $important
* @property int $global
* @property int $company_id
*/
class Notification extends NewDbModel
{
static $limit = 30;
static string $tableName = 'notification';
public static function getRules(): array
{
return [
'user_id' => 'required|integer',
'author_id' => 'integer',
'factory_id' => 'integer',
'message' => 'required|size:1000',
'created_at' => 'required|timestamp',
'read_at' => 'timestamp',
'important' => 'required|integer',
'global' => 'required|integer',
];
}
protected static function getTranslations(): array
{
return [
'user_id' => 'ID пользователя',
'author_id' => 'ID автора',
'factory_id' => 'ID предприятия',
'message' => 'Сообщение',
'created_at' => 'Дата создания',
'read_at' => 'Дата прочтения',
'important' => 'Важное',
'global' => 'Глобальное',
];
}
public static function markReadAll(int $important = null): int
{
$where = ['read_at' => null, 'user_id' => OldUser::getId(), 'global' => 0];
if (isset($important)) {
$where['important'] = $important;
}
return self::update(['read_at' => date('Y-m-d H:i:s')], $where);
}
/**
* @param bool $read
* @param int $page
* @param bool $important
* @param int $factoryId
*
* @return object[]|self[]
*/
public static function getUserNotifications($read = false, $page = 0, $important = null)
{
$sql = self::model()
->where('user_id = ?', OldUser::getId())
->where('global = 0')
->limit(500)
->order('created_at', 'DESC');
if (isset($important)) {
$sql->where('important = ?', (int) $important);
}
if (isset($read)) {
if ($read) {
$sql->whereIsNotNull('read_at');
} else {
$sql->whereIsNull('read_at');
}
}
$sql->limit(self::$limit, $page * self::$limit);
return $sql->fetchAll();
}
public static function getUserNotificationsCount($read = false, $important = false)
{
$sql = self::model()
->where('user_id = ?', OldUser::getId())
->where('global = 0');
if (isset($important)) {
$sql->where('important = ?', (int) $important);
}
if (isset($read)) {
if ($read) {
$sql->whereIsNotNull('read_at');
} else {
$sql->whereIsNull('read_at');
}
}
return $sql->totalCount();
}
/**
* @param $userId
* @param $text
* @param null $factoryId
* @param bool $important
* @param null $authorId
* @param bool $forKing
* @param bool $global
*
* @return int
*
*/
public static function add($userId, $text, $factoryId = null, $important = false, $authorId = null, $forKing = false, $global = false): int
{
if (is_null($authorId)) {
$authorId = OldUser::getRealId();
}
if (!$global && $userId == OldUser::getRealId()) {
return 0;
}
$user = OldUser::find($userId);
$notification = new self();
$notification->user_id = $userId;
$notification->author_id = $authorId;
$notification->message = $text;
$notification->important = (int) $important;
$notification->global = (int) $global;
$notification->save();
return $notification->id ?? 0;
}
public static function render($notification)
{
ob_start();
TemplateHelper::part('notification', compact('notification'));
return ob_get_clean();
}
public static function getLastGlobalMessage($userId = null)
{
$userId = $userId ?? OldUser::getId();
return self::model(['message'])
->where('user_id = ?', $userId)
->where('global = 1')
->whereIsNull('read_at')
->where('created_at >= ?', date('Y-m-d', strtotime('- 7 days')))
->order('created_at', 'DESC')
->fetchCol()[0] ?? [];
}
public static function markReadGlobal(int $userId): void
{
$lastGlobalMessage = self::model()
->whereIsNull('read_at')
->where('user_id = ?', $userId)
->where('global = 1')
->order('created_at', 'DESC')
->fetchRow();
if (!empty($lastGlobalMessage)) {
$lastGlobalMessage->read_at = date('Y-m-d H:i:s');
$lastGlobalMessage->save();
}
}
}