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