1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Fenos\Notifynder\Traits; |
4
|
|
|
|
5
|
|
|
use Fenos\Notifynder\Helpers\TypeChecker; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class NotifableBasic. |
9
|
|
|
*/ |
10
|
|
|
trait NotifableBasic |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Get the notifications Relationship without any eager loading. |
14
|
|
|
* |
15
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
16
|
|
|
*/ |
17
|
|
|
abstract public function getLazyNotificationRelation(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get the notifications Relationship. |
21
|
|
|
* |
22
|
|
|
* @param array|bool $eagerLoad |
23
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
24
|
|
|
*/ |
25
|
|
|
public function getNotificationRelation($eagerLoad = null) |
26
|
|
|
{ |
27
|
|
|
$with = []; |
28
|
|
|
if (is_null($eagerLoad)) { |
29
|
|
|
$eagerLoad = notifynder_config('eager_load', false); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
if ($eagerLoad === true) { |
33
|
|
|
$with = ['category', 'from', 'to']; // all relations |
34
|
|
|
} elseif (is_array($eagerLoad)) { |
35
|
|
|
$with = $eagerLoad; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
return $this->getLazyNotificationRelation()->with($with); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get a new NotifynderManager instance with the given category. |
43
|
|
|
* |
44
|
|
|
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
45
|
|
|
* @return \Fenos\Notifynder\Managers\NotifynderManager |
46
|
|
|
*/ |
47
|
|
|
public function notifynder($category) |
48
|
|
|
{ |
49
|
|
|
return app('notifynder')->category($category); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Get a new NotifynderManager instance with the given category and $this as the sender. |
54
|
|
|
* |
55
|
|
|
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
56
|
|
|
* @return \Fenos\Notifynder\Managers\NotifynderManager |
57
|
|
|
*/ |
58
|
|
|
public function sendNotificationFrom($category) |
59
|
|
|
{ |
60
|
|
|
return $this->notifynder($category)->from($this); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get a new NotifynderManager instance with the given category and $this as the receiver. |
65
|
|
|
* |
66
|
|
|
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
67
|
|
|
* @return \Fenos\Notifynder\Managers\NotifynderManager |
68
|
|
|
*/ |
69
|
|
|
public function sendNotificationTo($category) |
70
|
|
|
{ |
71
|
|
|
return $this->notifynder($category)->to($this); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Read a single Notification. |
76
|
|
|
* |
77
|
|
|
* @param int $notification |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function readNotification($notification) |
81
|
|
|
{ |
82
|
|
|
return $this->updateSingleReadStatus($notification, 1); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Unread a single Notification. |
87
|
|
|
* |
88
|
|
|
* @param int $notification |
89
|
|
|
* @return bool |
90
|
|
|
*/ |
91
|
|
|
public function unreadNotification($notification) |
92
|
|
|
{ |
93
|
|
|
return $this->updateSingleReadStatus($notification, 0); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param int $notification |
98
|
|
|
* @param int $value |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
protected function updateSingleReadStatus($notification, $value) |
102
|
|
|
{ |
103
|
|
|
if (! TypeChecker::isNotification($notification, false)) { |
104
|
|
|
$notification = $this->getLazyNotificationRelation()->findOrFail($notification); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
if ($this->getLazyNotificationRelation()->where($notification->getKeyName(), $notification->getKey())->exists()) { |
108
|
|
|
return $value ? $notification->read() : $notification->unread(); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
return false; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Read all Notifications. |
116
|
|
|
* |
117
|
|
|
* @return mixed |
118
|
|
|
*/ |
119
|
|
|
public function readAllNotifications() |
120
|
|
|
{ |
121
|
|
|
return $this->getLazyNotificationRelation()->update(['read' => 1]); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Unread all Notifications. |
126
|
|
|
* |
127
|
|
|
* @return mixed |
128
|
|
|
*/ |
129
|
|
|
public function unreadAllNotifications() |
130
|
|
|
{ |
131
|
|
|
return $this->getLazyNotificationRelation()->update(['read' => 0]); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Count unread notifications. |
136
|
|
|
* |
137
|
|
|
* @return int |
138
|
|
|
*/ |
139
|
|
|
public function countUnreadNotifications() |
140
|
|
|
{ |
141
|
|
|
return $this->getLazyNotificationRelation()->byRead(0)->count(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Get all Notifications ordered by creation and optional limit. |
146
|
|
|
* |
147
|
|
|
* @param null|int $limit |
148
|
|
|
* @param string $order |
149
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
150
|
|
|
*/ |
151
|
|
View Code Duplication |
public function getNotifications($limit = null, $order = 'desc') |
|
|
|
|
152
|
|
|
{ |
153
|
|
|
$query = $this->getNotificationRelation()->orderBy('created_at', $order); |
154
|
|
|
if (! is_null($limit)) { |
155
|
|
|
$query->limit($limit); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return $query->get(); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Get all unread Notifications. |
163
|
|
|
* |
164
|
|
|
* @param null|int $limit |
165
|
|
|
* @param string $order |
166
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
167
|
|
|
*/ |
168
|
|
View Code Duplication |
public function getNotificationsNotRead($limit = null, $order = 'desc') |
|
|
|
|
169
|
|
|
{ |
170
|
|
|
$query = $this->getNotificationRelation()->byRead(0)->orderBy('created_at', $order); |
171
|
|
|
if (! is_null($limit)) { |
172
|
|
|
$query->limit($limit); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $query->get(); |
176
|
|
|
} |
177
|
|
|
} |
178
|
|
|
|
If you implement
__call
and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__call
is implemented by a parent class and only the child class knows which methods exist: