| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Models; |
| 4 |
|
| 5 |
use FluentBoards\App\Services\Constant; |
| 6 |
|
| 7 |
class Notification extends Model |
| 8 |
{ |
| 9 |
protected $table = 'fbs_notifications'; |
| 10 |
|
| 11 |
protected $guarded = ['id']; |
| 12 |
|
| 13 |
protected $fillable = [ |
| 14 |
'object_id', |
| 15 |
'object_type', |
| 16 |
'activity_by', |
| 17 |
'task_id', |
| 18 |
'action', |
| 19 |
'description', |
| 20 |
'settings' |
| 21 |
]; |
| 22 |
public static function boot() |
| 23 |
{ |
| 24 |
parent::boot(); |
| 25 |
static::creating(function ($model) { |
| 26 |
$model->activity_by = $model->activity_by ?: get_current_user_id(); |
| 27 |
$model->created_at = current_time('mysql'); |
| 28 |
$model->updated_at = current_time('mysql'); |
| 29 |
}); |
| 30 |
} |
| 31 |
|
| 32 |
public function setSettingsAttribute($settings) |
| 33 |
{ |
| 34 |
$this->attributes['settings'] = \maybe_serialize($settings); |
| 35 |
} |
| 36 |
|
| 37 |
public function getSettingsAttribute($settings) |
| 38 |
{ |
| 39 |
return \maybe_unserialize($settings); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* One2One: Notification belongs to an activitist |
| 44 |
* |
| 45 |
* @return \FluentBoards\Framework\Database\Orm\Relations\BelongsTo |
| 46 |
*/ |
| 47 |
public function activitist() |
| 48 |
{ |
| 49 |
return $this->belongsTo(User::class, 'activity_by', 'ID'); |
| 50 |
} |
| 51 |
|
| 52 |
public function board() |
| 53 |
{ |
| 54 |
return $this->belongsTo(Board::class, 'object_id', 'id'); |
| 55 |
} |
| 56 |
|
| 57 |
public function users() |
| 58 |
{ |
| 59 |
return $this->belongsToMany( |
| 60 |
User::class, |
| 61 |
'fbs_notification_users', |
| 62 |
'notification_id', |
| 63 |
'user_id' |
| 64 |
)->withTimestamps() |
| 65 |
->withPivot('marked_read_at'); |
| 66 |
} |
| 67 |
|
| 68 |
public function task() |
| 69 |
{ |
| 70 |
return $this->belongsTo(Task::class, 'task_id', 'id')->with('stage', 'board'); |
| 71 |
} |
| 72 |
|
| 73 |
public function checkReadOrNot() |
| 74 |
{ |
| 75 |
$user = wp_get_current_user(); |
| 76 |
$userNotification = NotificationUser::where('user_id', $user->ID) |
| 77 |
->where('notification_id', $this->id) |
| 78 |
->first(); |
| 79 |
$marked_read_at = $userNotification->marked_read_at; |
| 80 |
|
| 81 |
return $marked_read_at ? true : false; |
| 82 |
|
| 83 |
|
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
|