forked from DallasMuseumArt/OctoberFriends
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotification.php
108 lines (88 loc) · 2.31 KB
/
Notification.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
<?php namespace DMA\Friends\Models;
use Event;
use Model;
use Carbon\Carbon;
use DMA\Friends\Models\Settings;
/**
* DMA Notification model
* @package DMA\Friends\Models
* @author Carlos Arroyo
*
*/
class Notification extends Model{
/**
* @var string The database table used by the model.
*/
protected $table = 'dma_friends_notification';
/**
* @var array Validation rules
*/
public $rules = [];
/**
* @var array List of datetime attributes to convert to an instance of Carbon/DateTime objects.
*/
public $dates = ['created_at', 'updated_at', 'sent_at'];
/**
* @var array Relations
*/
public $belongsTo = [
'user' => ['Rainlab\User\Models\User']
];
/**
* @var array Polyphormic relations
*/
public $morphTo = [
'object',
];
/**
* {@inheritDoc}
*/
public function save(array $data = NULL, $sessionKey = NULL)
{
if(is_null($this->sent_at)){
$this->sent_at = $this->freshTimestamp();
}
parent::save($data, $sessionKey);
}
/**
* Helper to mark notifications as read
*/
public function markAsRead()
{
$this->is_read = true;
$this->save();
}
/**
* Call this scope to expire unread notifications
* @param mixed $query
* @return mixed
* Return query unmodified
*/
public function scopeExpire($query)
{
$expireQuery = clone($query);
$expireDays = Settings::get('kiosk_notification_max_age', 60);
$expireDays = (empty($expireDays)) ? 60 : $expireDays;
$expireDate = Carbon::today()->subDays($expireDays);
$expireQuery->where("created_at", "<=", $expireDate)
->unread()
->update(['is_read' => true]);
return $query;
}
/**
* Scope for selecting un-read notifications.
* @param mixed $query
*/
public function scopeUnread($query)
{
return $query->where('is_read', '=', false);
}
/**
* Scope method for mark all selected messages
* as read.
*/
public function scopeMarkAllAsRead($query)
{
return $query->unread()->update(['is_read' => true ]);
}
}