forked from matomo-org/plugin-CustomAlerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotifier.php
executable file
·234 lines (184 loc) · 6.42 KB
/
Notifier.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
* @version $Id$
*
*/
namespace Piwik\Plugins\CustomAlerts;
use Piwik\Date;
use Piwik\Mail;
use Piwik\Period;
use Piwik\Piwik;
use Piwik\Plugin\Manager as PluginManager;
use Piwik\Plugins\MobileMessaging\API as APIMobileMessaging;
use Piwik\Plugins\UsersManager\API as UsersManagerApi;
use Piwik\Site;
use Piwik\View;
/**
*
*/
class Notifier extends \Piwik\Plugin
{
protected function getTriggeredAlerts($period, $idSite)
{
$now = $this->getToday()->getDatetime();
$model = new Model();
$alerts = $model->getTriggeredAlertsForPeriod($period, $now, false);
return array_filter($alerts, function ($alert) use ($idSite) {
return $idSite && (int) $alert['idsite'] === (int) $idSite && empty($alert['ts_last_sent']);
});
}
/**
* Sends a list of the triggered alerts to all recipients.
*
* @param string $period
* @param int $idSite
*/
public function sendNewAlerts($period, $idSite)
{
$triggeredAlerts = $this->getTriggeredAlerts($period, $idSite);
$alertsPerEmail = $this->groupAlertsPerEmailRecipient($triggeredAlerts);
foreach ($alertsPerEmail as $email => $alerts) {
$this->sendAlertsPerEmailToRecipient($alerts, new Mail(), $email, $period, $idSite);
}
$alertsPerSms = $this->groupAlertsPerSmsRecipient($triggeredAlerts);
foreach ($alertsPerSms as $phoneNumber => $alerts) {
$this->sendAlertsPerSmsToRecipient($alerts, APIMobileMessaging::getInstance(), $phoneNumber);
}
foreach($triggeredAlerts as $triggeredAlert) {
$this->markAlertAsSent($triggeredAlert);
}
}
private function groupAlertsPerSmsRecipient($triggeredAlerts)
{
$recipients = array();
foreach($triggeredAlerts as $triggeredAlert) {
$phoneNumbers = $triggeredAlert['phone_numbers'];
if (empty($phoneNumbers)) {
$phoneNumbers = array();
}
foreach ($phoneNumbers as $phoneNumber) {
if (!array_key_exists($phoneNumber, $recipients)) {
$recipients[$phoneNumber] = array();
}
$recipients[$phoneNumber][] = $triggeredAlert;
}
}
return $recipients;
}
private function groupAlertsPerEmailRecipient($triggeredAlerts)
{
$alertsPerEmail = array();
foreach($triggeredAlerts as $triggeredAlert) {
$emails = $this->getEmailRecipientsForAlert($triggeredAlert);
foreach ($emails as $mail) {
if (!array_key_exists($mail, $alertsPerEmail)) {
$alertsPerEmail[$mail] = array();
}
$alertsPerEmail[$mail][] = $triggeredAlert;
}
}
return $alertsPerEmail;
}
protected function markAlertAsSent($triggeredAlert)
{
$timestamp = Date::now()->getDatetime();
$model = new Model();
$model->markTriggeredAlertAsSent($triggeredAlert['idtriggered'], $timestamp);
}
protected function getEmailAddressFromLogin($login)
{
if (empty($login)) {
return '';
}
$user = UsersManagerApi::getInstance()->getUser($login);
if (empty($user) || empty($user['email'])) {
return '';
}
return $user['email'];
}
/**
* @param array $alerts
* @param APIMobileMessaging $mobileMessagingAPI
* @param string $phoneNumber
*/
protected function sendAlertsPerSmsToRecipient($alerts, $mobileMessagingAPI, $phoneNumber)
{
if (empty($phoneNumber) || empty($alerts)) {
return;
}
if (!PluginManager::getInstance()->isPluginActivated('MobileMessaging')) {
return;
}
$controller = new Controller();
$content = $controller->formatAlerts($alerts, 'sms');
$subject = Piwik::translate('CustomAlerts_SmsAlertFromName');
$mobileMessagingAPI->sendSMS(
$content,
$phoneNumber,
$subject
);
}
/**
* @param array $alerts
* @param Mail $mail
* @param string[] $recipient Email addresses
* @param $period
* @param $idSite
*/
protected function sendAlertsPerEmailToRecipient($alerts, Mail $mail, $recipient, $period, $idSite)
{
if (empty($recipient) || empty($alerts)) {
return;
}
$prettyDate = $this->getPrettyDateForSite($period, $idSite);
$websiteName = Site::getNameFor($idSite);
$mail->setDefaultFromPiwik();
$mail->addTo($recipient);
$mail->setSubject(Piwik::translate('CustomAlerts_MailAlertSubject', array($websiteName, $prettyDate)));
$controller = new Controller();
$viewHtml = new View('@CustomAlerts/alertHtmlMail');
$viewHtml->assign('triggeredAlerts', $controller->formatAlerts($alerts, 'html'));
$mail->setBodyHtml($viewHtml->render());
$viewText = new View('@CustomAlerts/alertTextMail');
$viewText->assign('triggeredAlerts', $controller->formatAlerts($alerts, 'text'));
$viewText->setContentType('text/plain');
$mail->setBodyText($viewText->render());
$mail->send();
}
/**
* @param $triggeredAlert
* @return array
*/
private function getEmailRecipientsForAlert($triggeredAlert)
{
$recipients = $triggeredAlert['additional_emails'];
if (empty($recipients)) {
$recipients = array();
}
if ($triggeredAlert['email_me']) {
$recipients[] = $this->getEmailAddressFromLogin($triggeredAlert['login']);
}
return $recipients;
}
protected function getToday()
{
return Date::now();
}
protected function getPrettyDateForSite($period, $idSite)
{
$timezone = Site::getTimezoneFor($idSite);
$date = $this->getToday();
$date = Date::factory($date->getDatetime(), $timezone);
// we ran the alerts for the period before...
$date = $date->subPeriod(1, $period);
// also make sure if period is month to display "2014-01" and not "2014-01-31"
$period = Period\Factory::build($period, $date);
$prettyDate = $period->getLocalizedLongString();
return $prettyDate;
}
}
?>