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. |
14
|
|
|
* |
15
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany|\Illuminate\Database\Eloquent\Relations\MorphMany |
16
|
|
|
*/ |
17
|
|
|
abstract public function getNotificationRelation(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Get a new NotifynderManager instance with the given category. |
21
|
|
|
* |
22
|
|
|
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
23
|
|
|
* @return \Fenos\Notifynder\Managers\NotifynderManager |
24
|
|
|
*/ |
25
|
|
|
public function notifynder($category) |
26
|
|
|
{ |
27
|
|
|
return app('notifynder')->category($category); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Get a new NotifynderManager instance with the given category and $this as the sender. |
32
|
|
|
* |
33
|
|
|
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
34
|
|
|
* @return \Fenos\Notifynder\Managers\NotifynderManager |
35
|
|
|
*/ |
36
|
|
|
public function sendNotificationFrom($category) |
37
|
|
|
{ |
38
|
|
|
return $this->notifynder($category)->from($this); |
|
|
|
|
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Get a new NotifynderManager instance with the given category and $this as the receiver. |
43
|
|
|
* |
44
|
|
|
* @param string|int|\Fenos\Notifynder\Models\NotificationCategory $category |
45
|
|
|
* @return \Fenos\Notifynder\Managers\NotifynderManager |
46
|
|
|
*/ |
47
|
|
|
public function sendNotificationTo($category) |
48
|
|
|
{ |
49
|
|
|
return $this->notifynder($category)->to($this); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Read a single Notification. |
54
|
|
|
* |
55
|
|
|
* @param int $notification |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
public function readNotification($notification) |
59
|
|
|
{ |
60
|
|
|
return $this->updateSingleReadStatus($notification, 1); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Unread a single Notification. |
65
|
|
|
* |
66
|
|
|
* @param int $notification |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function unreadNotification($notification) |
70
|
|
|
{ |
71
|
|
|
return $this->updateSingleReadStatus($notification, 0); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param int $notification |
76
|
|
|
* @param int $value |
77
|
|
|
* @return bool |
78
|
|
|
*/ |
79
|
|
|
protected function updateSingleReadStatus($notification, $value) |
80
|
|
|
{ |
81
|
|
|
if (! TypeChecker::isNotification($notification, false)) { |
82
|
|
|
$notification = $this->getNotificationRelation()->findOrFail($notification); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
if ($this->getNotificationRelation()->where($notification->getKeyName(), $notification->getKey())->exists()) { |
86
|
|
|
return $value ? $notification->read() : $notification->unread(); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Read all Notifications. |
94
|
|
|
* |
95
|
|
|
* @return mixed |
96
|
|
|
*/ |
97
|
|
|
public function readAllNotifications() |
98
|
|
|
{ |
99
|
|
|
return $this->getNotificationRelation()->update(['read' => 1]); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Unread all Notifications. |
104
|
|
|
* |
105
|
|
|
* @return mixed |
106
|
|
|
*/ |
107
|
|
|
public function unreadAllNotifications() |
108
|
|
|
{ |
109
|
|
|
return $this->getNotificationRelation()->update(['read' => 0]); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Count unread notifications. |
114
|
|
|
* |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
public function countUnreadNotifications() |
118
|
|
|
{ |
119
|
|
|
return $this->getNotificationRelation()->byRead(0)->count(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Get all Notifications ordered by creation and optional limit. |
124
|
|
|
* |
125
|
|
|
* @param null|int $limit |
126
|
|
|
* @param string $order |
127
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
128
|
|
|
*/ |
129
|
|
|
public function getNotifications($limit = null, $order = 'desc') |
130
|
|
|
{ |
131
|
|
|
$query = $this->getNotificationRelation()->orderBy('created_at', $order); |
132
|
|
|
if (! is_null($limit)) { |
133
|
|
|
$query->limit($limit); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return $query->get(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get all unread Notifications. |
141
|
|
|
* |
142
|
|
|
* @param null|int $limit |
143
|
|
|
* @param string $order |
144
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
145
|
|
|
*/ |
146
|
|
|
public function getNotificationsNotRead($limit = null, $order = 'desc') |
147
|
|
|
{ |
148
|
|
|
$query = $this->getNotificationRelation()->orderBy('created_at', $order); |
149
|
|
|
$query = $query->where('notifications.read', 0); |
150
|
|
|
if (! is_null($limit)) { |
151
|
|
|
$query->limit($limit); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
return $query->get(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
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: