Files
librenms-librenms/app/Models/Notification.php
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

144 lines
3.6 KiB
PHP
Raw Normal View History

2018-05-09 08:05:17 -05:00
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Auth;
2018-05-09 08:05:17 -05:00
use Illuminate\Support\Facades\DB;
class Notification extends Model
{
/**
* Indicates if the model should be timestamped.
*
* @var bool
*/
public $timestamps = false;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'notifications';
/**
* The primary key column name.
*
* @var string
*/
protected $primaryKey = 'notifications_id';
2022-09-25 22:47:58 -05:00
protected $fillable = [
'title',
'body',
'severity',
'source',
'checksum',
'datetime',
];
public static function boot()
{
parent::boot();
// delete attribs for this notification
static::deleting(function (Notification $notification) {
$notification->attribs()->delete();
});
}
2018-05-09 08:05:17 -05:00
// ---- Helper Functions ----
/**
* Mark this notification as read or unread
*
2021-09-08 23:35:56 +02:00
* @param bool $enabled
2018-05-09 08:05:17 -05:00
* @return bool
*/
2021-03-24 15:13:43 +01:00
public function markRead(bool $enabled = true): bool
2018-05-09 08:05:17 -05:00
{
return $this->setAttrib('read', $enabled);
}
/**
* Mark this notification as sticky or unsticky
*/
2021-03-24 15:13:43 +01:00
public function markSticky(bool $enabled = true): bool
2018-05-09 08:05:17 -05:00
{
return $this->setAttrib('sticky', $enabled);
}
/**
2021-09-08 23:35:56 +02:00
* @param string $name
* @param bool $enabled
2018-05-09 08:05:17 -05:00
* @return bool
*/
2021-03-24 15:13:43 +01:00
private function setAttrib($name, bool $enabled): bool
2018-05-09 08:05:17 -05:00
{
if ($enabled === true) {
$read = new NotificationAttrib;
$read->user_id = Auth::user()->user_id;
2018-05-09 08:05:17 -05:00
$read->key = $name;
2021-03-31 17:29:22 +02:00
$read->value = '1';
2018-05-09 08:05:17 -05:00
$this->attribs()->save($read);
2020-09-21 14:54:51 +02:00
2018-05-09 08:05:17 -05:00
return true;
} else {
return $this->attribs()->where('key', $name)->delete();
}
}
// ---- Query Scopes ----
/**
2021-09-08 23:35:56 +02:00
* @param Builder<Notification> $query
* @param User $user
2018-05-09 08:05:17 -05:00
* @return mixed
*/
public function scopeIsUnread(Builder $query, User $user)
{
return $query->whereNotExists(function ($query) use ($user) {
$query->select(DB::raw(1))
->from('notifications_attribs')
->whereRaw('notifications.notifications_id = notifications_attribs.notifications_id')
->where('notifications_attribs.user_id', $user->user_id);
});
}
/**
* Get all sticky notifications
*
2021-09-08 23:35:56 +02:00
* @param Builder<Notification> $query
2018-05-09 08:05:17 -05:00
*/
public function scopeIsSticky(Builder $query)
{
$query->leftJoin('notifications_attribs', 'notifications_attribs.notifications_id', 'notifications.notifications_id')
->where(['notifications_attribs.key' => 'sticky', 'notifications_attribs.value' => 1]);
}
/**
2021-09-08 23:35:56 +02:00
* @param Builder<Notification> $query
2021-03-28 19:18:47 +02:00
* @return Builder<Notification>
2018-05-09 08:05:17 -05:00
*/
public function scopeLimit(Builder $query)
{
return $query->select('notifications.*', 'key', 'users.username');
}
/**
2021-09-08 23:35:56 +02:00
* @param Builder<Notification> $query
2018-05-09 08:05:17 -05:00
* @return Builder|static
*/
public function scopeSource(Builder $query)
{
return $query->leftJoin('users', 'notifications.source', '=', 'users.user_id');
}
// ---- Define Relationships ----
public function attribs(): HasMany
2018-05-09 08:05:17 -05:00
{
return $this->hasMany(\App\Models\NotificationAttrib::class, 'notifications_id', 'notifications_id');
2018-05-09 08:05:17 -05:00
}
}