1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Canvas\Notifications; |
6
|
|
|
|
7
|
|
|
use Canvas\Contracts\Notifications\NotificationInterfase; |
8
|
|
|
use Canvas\Models\AbstractModel; |
9
|
|
|
use Canvas\Models\NotificationType; |
10
|
|
|
use Baka\Mail\Message; |
11
|
|
|
use Canvas\Models\Users; |
12
|
|
|
use Canvas\Models\Notifications; |
13
|
|
|
use Phalcon\Traits\EventManagerAwareTrait; |
14
|
|
|
use Phalcon\Di; |
15
|
|
|
use Canvas\Queue\Queue; |
16
|
|
|
use Phalcon\Mvc\Model; |
17
|
|
|
|
18
|
|
|
class Notification implements NotificationInterfase |
19
|
|
|
{ |
20
|
|
|
use EventManagerAwareTrait; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* |
24
|
|
|
* @var Users |
25
|
|
|
*/ |
26
|
|
|
protected $toUser = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* |
30
|
|
|
* @var Users |
31
|
|
|
*/ |
32
|
|
|
protected $fromUser = null; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Send this notification to the queue? |
36
|
|
|
* |
37
|
|
|
* @var boolean |
38
|
|
|
*/ |
39
|
|
|
protected $useQueue = false; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* |
43
|
|
|
* @var NotificationType |
44
|
|
|
*/ |
45
|
|
|
protected $type = null; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* @var AbstractModel |
50
|
|
|
*/ |
51
|
|
|
protected $entity = null; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* |
55
|
|
|
* @var Baka\Mail\Manager |
|
|
|
|
56
|
|
|
*/ |
57
|
|
|
protected $mail; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Constructor. |
61
|
|
|
* |
62
|
|
|
* @param AbstractModel $entity |
63
|
|
|
*/ |
64
|
|
|
public function __construct(Model $entity) |
65
|
|
|
{ |
66
|
|
|
$this->entity = $entity; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Set the notification type. |
71
|
|
|
* |
72
|
|
|
* @param NotificationType $type |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function setType(NotificationType $type): void |
76
|
|
|
{ |
77
|
|
|
$this->type = $type; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the message from the current notification type. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function message(): string |
86
|
|
|
{ |
87
|
|
|
return $this->type->template ?: ''; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Define a Baka Mail to send a email. |
92
|
|
|
* |
93
|
|
|
* @todo add Interfase to bakaMail |
94
|
|
|
* @return Message |
95
|
|
|
*/ |
96
|
|
|
protected function toMail(): ?Message |
97
|
|
|
{ |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* To send push notification. |
102
|
|
|
* |
103
|
|
|
* @return void |
104
|
|
|
*/ |
105
|
|
|
protected function toPushNotification() |
106
|
|
|
{ |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Send to websocket / realtime. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
protected function toRealtime() |
115
|
|
|
{ |
116
|
|
|
//set the channel |
117
|
|
|
//key_user_id |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Set the usre we are sending the notification to. |
122
|
|
|
* |
123
|
|
|
* @param Users $user |
124
|
|
|
* @return void |
125
|
|
|
*/ |
126
|
|
|
public function setTo(Users $user): void |
127
|
|
|
{ |
128
|
|
|
$this->toUser = $user; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Set the user from who the notification if comming from. |
133
|
|
|
* |
134
|
|
|
* @param User $user |
|
|
|
|
135
|
|
|
* @return void |
136
|
|
|
*/ |
137
|
|
|
public function setFrom(Users $user): void |
138
|
|
|
{ |
139
|
|
|
$this->fromUser = $user; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Disable this notification queue in runtime |
144
|
|
|
* |
145
|
|
|
* @return void |
146
|
|
|
*/ |
147
|
|
|
public function disableQueue(): void |
148
|
|
|
{ |
149
|
|
|
$this->useQueue = false; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Process the notification |
154
|
|
|
* - handle the db |
155
|
|
|
* - trigger the notification |
156
|
|
|
* - knows if we have to send it to queu. |
157
|
|
|
* |
158
|
|
|
* @return boolean |
159
|
|
|
*/ |
160
|
|
|
public function process(): bool |
161
|
|
|
{ |
162
|
|
|
//if the user didnt provide the type get it based on the class name |
163
|
|
|
if (is_null($this->type)) { |
164
|
|
|
$this->setType(NotificationType::getByKey(static::class)); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
if (Di::getDefault()->has('mail')) { |
168
|
|
|
$this->mail = Di::getDefault()->getMail(); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($this->useQueue) { |
172
|
|
|
$this->sendToQueue(); |
173
|
|
|
return true; //send it to the queue |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
$this->trigger(); |
177
|
|
|
|
178
|
|
|
return true; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Send to our internal Notification queue |
183
|
|
|
* |
184
|
|
|
* @return boolean |
185
|
|
|
*/ |
186
|
|
|
protected function sendToQueue(): bool |
187
|
|
|
{ |
188
|
|
|
$notificationData = [ |
189
|
|
|
'from' => $this->fromUser, |
190
|
|
|
'to' => $this->toUser, |
191
|
|
|
'entity' => $this->entity, |
192
|
|
|
'type' => $this->type, |
193
|
|
|
'notification' => get_class($this) |
194
|
|
|
]; |
195
|
|
|
|
196
|
|
|
return Queue::send(Queue::NOTIFICATIONS, serialize($notificationData)); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Send the noficiatino to the places the user defined. |
201
|
|
|
* |
202
|
|
|
* @return boolean |
203
|
|
|
*/ |
204
|
|
|
public function trigger(): bool |
205
|
|
|
{ |
206
|
|
|
$content = $this->message(); |
207
|
|
|
$app = Di::getDefault()->getApp(); |
208
|
|
|
|
209
|
|
|
//save to DB |
210
|
|
|
$notification = new Notifications(); |
211
|
|
|
$notification->from_users_id = $this->fromUser->getId(); |
212
|
|
|
$notification->users_id = $this->toUser->getId(); |
213
|
|
|
$notification->companies_id = $this->fromUser->currentCompanyId(); |
214
|
|
|
$notification->apps_id = $app->getId(); |
215
|
|
|
$notification->system_modules_id = $this->type->system_modules_id; |
216
|
|
|
$notification->notification_type_id = $this->type->getId(); |
217
|
|
|
$notification->entity_id = $this->entity->getId(); |
218
|
|
|
$notification->content = $content; |
219
|
|
|
$notification->read = 0; |
220
|
|
|
$notification->saveOrFail(); |
221
|
|
|
|
222
|
|
|
if ($this->toMail() instanceof Message) { |
|
|
|
|
223
|
|
|
$this->fire('notification:sendMail', $this->toMail()); |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* @todo send to push ontification |
228
|
|
|
*/ |
229
|
|
|
|
230
|
|
|
if ($this->type->with_realtime) { |
231
|
|
|
$this->fire('notification:sendRealtime', $this); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return true; |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
|