1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Model; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Database\Model; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* User Unread Notification. |
18
|
|
|
*/ |
19
|
|
|
class UserUnreadNotificationModel extends Model |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* SQL table name. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
const TABLE = 'user_has_unread_notifications'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Add unread notification to someone. |
30
|
|
|
* |
31
|
|
|
* @param int $user_id |
32
|
|
|
* @param string $event_name |
33
|
|
|
* @param array $event_data |
34
|
|
|
*/ |
35
|
|
|
public function create($user_id, $event_name, array $event_data) |
36
|
|
|
{ |
37
|
|
|
$this->db->table(self::TABLE)->insert([ |
|
|
|
|
38
|
|
|
'user_id' => $user_id, |
39
|
|
|
'date_creation' => time(), |
40
|
|
|
'event_name' => $event_name, |
41
|
|
|
'event_data' => json_encode($event_data), |
42
|
|
|
]); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get one notification. |
47
|
|
|
* |
48
|
|
|
* @param int $notification_id |
49
|
|
|
* |
50
|
|
|
* @return array|null |
51
|
|
|
*/ |
52
|
|
|
public function getById($notification_id) |
53
|
|
|
{ |
54
|
|
|
$notification = $this->db->table(self::TABLE)->eq('id', $notification_id)->findOne(); |
|
|
|
|
55
|
|
|
|
56
|
|
|
if (!empty($notification)) { |
57
|
|
|
$this->unserialize($notification); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $notification; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get all notifications for a user. |
65
|
|
|
* |
66
|
|
|
* @param int $user_id |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
View Code Duplication |
public function getAll($user_id) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
$events = $this->db->table(self::TABLE)->eq('user_id', $user_id)->desc('date_creation')->findAll(); |
|
|
|
|
73
|
|
|
|
74
|
|
|
foreach ($events as &$event) { |
75
|
|
|
$this->unserialize($event); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $events; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Mark a notification as read. |
83
|
|
|
* |
84
|
|
|
* @param int $user_id |
85
|
|
|
* @param int $notification_id |
86
|
|
|
* |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function markAsRead($user_id, $notification_id) |
90
|
|
|
{ |
91
|
|
|
return $this->db->table(self::TABLE)->eq('id', $notification_id)->eq('user_id', $user_id)->remove(); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Mark all notifications as read for a user. |
96
|
|
|
* |
97
|
|
|
* @param int $user_id |
98
|
|
|
* |
99
|
|
|
* @return bool |
100
|
|
|
*/ |
101
|
|
|
public function markAllAsRead($user_id) |
102
|
|
|
{ |
103
|
|
|
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->remove(); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Return true if the user as unread notifications. |
108
|
|
|
* |
109
|
|
|
* @param int $user_id |
110
|
|
|
* |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function hasNotifications($user_id) |
114
|
|
|
{ |
115
|
|
|
return $this->db->table(self::TABLE)->eq('user_id', $user_id)->exists(); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Unserialize the event. |
120
|
|
|
* |
121
|
|
|
* @param GenericEvent $event |
122
|
|
|
*/ |
123
|
|
|
private function unserialize(&$event) |
124
|
|
|
{ |
125
|
|
|
$event['event_data'] = json_decode($event['event_data'], true); |
126
|
|
|
$event['title'] = $this->notificationModel->getTitleWithoutAuthor($event['event_name'], $event['event_data']); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.