/
NotifableBasic.php
177 lines (156 loc) · 4.7 KB
/
NotifableBasic.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
namespace Fenos\Notifynder\Traits;
use Fenos\Notifynder\Helpers\TypeChecker;
/**
* Class NotifableBasic.
*/
trait NotifableBasic
{
/**
* Get the notifications Relationship without any eager loading.
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany
*/
abstract public function getLazyNotificationRelation();
/**
* Get the notifications Relationship.
*
* @param array|bool $eagerLoad
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function getNotificationRelation($eagerLoad = null)
{
$with = [];
if (is_null($eagerLoad)) {
$eagerLoad = notifynder_config('eager_load', false);
}
if ($eagerLoad === true) {
$with = ['category', 'from', 'to']; // all relations
} elseif (is_array($eagerLoad)) {
$with = $eagerLoad;
}
return $this->getLazyNotificationRelation()->with($with);
}
/**
* Get a new NotifynderManager instance with the given category.
*
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
* @return \Fenos\Notifynder\Managers\NotifynderManager
*/
public function notifynder($category)
{
return app('notifynder')->category($category);
}
/**
* Get a new NotifynderManager instance with the given category and $this as the sender.
*
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
* @return \Fenos\Notifynder\Managers\NotifynderManager
*/
public function sendNotificationFrom($category)
{
return $this->notifynder($category)->from($this);
}
/**
* Get a new NotifynderManager instance with the given category and $this as the receiver.
*
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category
* @return \Fenos\Notifynder\Managers\NotifynderManager
*/
public function sendNotificationTo($category)
{
return $this->notifynder($category)->to($this);
}
/**
* Read a single Notification.
*
* @param int $notification
* @return bool
*/
public function readNotification($notification)
{
return $this->updateSingleReadStatus($notification, 1);
}
/**
* Unread a single Notification.
*
* @param int $notification
* @return bool
*/
public function unreadNotification($notification)
{
return $this->updateSingleReadStatus($notification, 0);
}
/**
* @param int $notification
* @param int $value
* @return bool
*/
protected function updateSingleReadStatus($notification, $value)
{
if (! TypeChecker::isNotification($notification, false)) {
$notification = $this->getLazyNotificationRelation()->findOrFail($notification);
}
if ($this->getLazyNotificationRelation()->where($notification->getKeyName(), $notification->getKey())->exists()) {
return $value ? $notification->read() : $notification->unread();
}
return false;
}
/**
* Read all Notifications.
*
* @return mixed
*/
public function readAllNotifications()
{
return $this->getLazyNotificationRelation()->update(['read' => 1]);
}
/**
* Unread all Notifications.
*
* @return mixed
*/
public function unreadAllNotifications()
{
return $this->getLazyNotificationRelation()->update(['read' => 0]);
}
/**
* Count unread notifications.
*
* @return int
*/
public function countUnreadNotifications()
{
return $this->getLazyNotificationRelation()->byRead(0)->count();
}
/**
* Get all Notifications ordered by creation and optional limit.
*
* @param null|int $limit
* @param string $order
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getNotifications($limit = null, $order = 'desc')
{
$query = $this->getNotificationRelation()->orderBy('created_at', $order);
if (! is_null($limit)) {
$query->limit($limit);
}
return $query->get();
}
/**
* Get all unread Notifications.
*
* @param null|int $limit
* @param string $order
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getNotificationsNotRead($limit = null, $order = 'desc')
{
$query = $this->getNotificationRelation()->byRead(0)->orderBy('created_at', $order);
if (! is_null($limit)) {
$query->limit($limit);
}
return $query->get();
}
}