1
0
mirror of https://github.com/flarum/core.git synced 2025-07-29 20:50:28 +02:00
Files
php-flarum/src/Core/Models/Notification.php
Toby Zerner d166757930 Extract mappedMorphTo function into a trait
Not sure if this is the best thing to do, it could also just be put on
the base Model class
2015-05-11 10:39:54 +09:30

114 lines
2.4 KiB
PHP

<?php namespace Flarum\Core\Models;
use Flarum\Core\Support\MappedMorphToTrait;
class Notification extends Model
{
use MappedMorphToTrait;
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'notifications';
/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['time'];
/**
*
*
* @var array
*/
protected static $subjects = [];
public static function alert($userId, $type, $senderId, $subjectId, $data)
{
$notification = new static;
$notification->user_id = $userId;
$notification->sender_id = $senderId;
$notification->type = $type;
$notification->subject_id = $subjectId;
$notification->data = $data;
$notification->time = time();
return $notification;
}
public function read()
{
$this->is_read = true;
}
/**
* Unserialize the data attribute.
*
* @param string $value
* @return string
*/
public function getDataAttribute($value)
{
return json_decode($value);
}
/**
* Serialize the data attribute.
*
* @param string $value
*/
public function setDataAttribute($value)
{
$this->attributes['data'] = json_encode($value);
}
/**
* Define the relationship with the notification's recipient.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function user()
{
return $this->belongsTo('Flarum\Core\Models\User', 'user_id');
}
/**
* Define the relationship with the notification's sender.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/
public function sender()
{
return $this->belongsTo('Flarum\Core\Models\User', 'sender_id');
}
public function subject()
{
return $this->mappedMorphTo(static::$subjects, 'subject', 'type', 'subject_id');
}
public static function getTypes()
{
return static::$subjects;
}
/**
* Register a notification type.
*
* @param string $type
* @param string $class
* @return void
*/
public static function registerType($class)
{
if ($subject = $class::getSubjectModel()) {
static::$subjects[$class::getType()] = $subject;
}
}
}