-
Notifications
You must be signed in to change notification settings - Fork 20
/
CustomAlerts.php
executable file
·266 lines (227 loc) · 8.27 KB
/
CustomAlerts.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
*/
namespace Piwik\Plugins\CustomAlerts;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API as SitesManagerApi;
use Piwik\Scheduler\Task;
/**
*
*/
class CustomAlerts extends \Piwik\Plugin
{
/**
* @var null|string
*/
public static $currentlyRunningScheduledTaskName = null;
/**
* @var int
*/
public static $currentlyRunningScheduledTaskRetryCount = 0;
public function registerEvents()
{
return array(
'MobileMessaging.deletePhoneNumber' => 'removePhoneNumberFromAllAlerts',
'AssetManager.getJavaScriptFiles' => 'getJavaScriptFiles',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'API.Request.dispatch' => 'checkApiPermission',
'Request.dispatch' => 'checkControllerPermission',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'UsersManager.deleteUser' => 'deleteAlertsForLogin',
'SitesManager.deleteSite.end' => 'deleteAlertsForSite',
'Db.getTablesInstalled' => 'getTablesInstalled',
'ScheduledTasks.execute' => 'startingScheduledTask',
'ScheduledTasks.execute.end' => 'endingScheduledTask',
);
}
/**
* Register the new tables, so Matomo knows about them.
*
* @param array $allTablesInstalled
*/
public function getTablesInstalled(&$allTablesInstalled)
{
$allTablesInstalled[] = Common::prefixTable('alert');
$allTablesInstalled[] = Common::prefixTable('alert_site');
$allTablesInstalled[] = Common::prefixTable('alert_triggered');
}
public function checkApiPermission(&$parameters, $pluginName, $methodName)
{
if ($pluginName == 'CustomAlerts') {
$this->checkPermission();
}
}
private function checkPermission()
{
Piwik::checkUserIsNotAnonymous();
}
public function checkControllerPermission($module, $action)
{
if ($module != 'CustomAlerts') {
return;
}
if ($action == 'formatAlerts') {
throw new \Exception('This action does not exist');
}
$this->checkPermission();
}
public function install()
{
Model::install();
}
public function uninstall()
{
Model::uninstall();
}
public function getJavaScriptFiles(&$jsFiles)
{
}
public function getStylesheetFiles(&$cssFiles)
{
$cssFiles[] = "plugins/CustomAlerts/stylesheets/alerts.less";
}
public function deleteAlertsForLogin($userLogin)
{
$model = $this->getModel();
$alerts = $this->getAllAlerts();
foreach ($alerts as $alert) {
if ($alert['login'] == $userLogin) {
$model->deleteAlert($alert['idalert']);
}
}
}
private function getModel()
{
return new Model();
}
/**
* @return array
*/
private function getAllAlerts()
{
return $this->getModel()->getAllAlerts();
}
public function deleteAlertsForSite($idSite)
{
$model = $this->getModel();
$model->deleteTriggeredAlertsForSite($idSite);
$alerts = $this->getAllAlerts();
foreach ($alerts as $alert) {
$key = array_search($idSite, $alert['id_sites']);
if (false !== $key) {
unset($alert['id_sites'][$key]);
$model->setSiteIds($alert['idalert'], array_values($alert['id_sites']));
// TODO also delete logs
}
}
}
public function removePhoneNumberFromAllAlerts($phoneNumber)
{
$model = $this->getModel();
$alerts = $this->getAllAlerts();
foreach ($alerts as $alert) {
if (empty($alert['phone_numbers']) || !is_array($alert['phone_numbers'])) {
continue;
}
$key = array_search($phoneNumber, $alert['phone_numbers'], true);
if (false !== $key) {
unset($alert['phone_numbers'][$key]);
$model->updateAlert(
$alert['idalert'],
$alert['name'],
$alert['id_sites'],
$alert['period'],
$alert['email_me'],
$alert['additional_emails'],
array_values($alert['phone_numbers']),
$alert['metric'],
$alert['metric_condition'],
$alert['metric_matched'],
$alert['compared_to'],
$alert['report'],
$alert['report_condition'],
$alert['report_matched']
);
}
}
}
public function getSiteIdsHavingAlerts()
{
$siteIds = SitesManagerApi::getInstance()->getAllSitesId();
$model = new Model();
$alerts = $model->getAlerts($siteIds);
$siteIdsHavingAlerts = array();
foreach ($alerts as $alert) {
$siteIdsHavingAlerts = array_merge($siteIdsHavingAlerts, $alert['id_sites']);
}
return array_values(array_unique($siteIdsHavingAlerts));
}
/**
* If the task is for CustomAlerts, save the name of the task as static property so that we know what the currently
* running task is.
*
* @param Task $task
* @return void
*/
public function startingScheduledTask(Task $task): void
{
if (strpos($taskName = $task->getName(), 'Piwik\Plugins\CustomAlerts\Tasks') === false) {
return;
}
self::$currentlyRunningScheduledTaskName = $taskName;
/** @var \Piwik\Scheduler\Timetable $timetable */
$timetable = StaticContainer::getContainer()->get('Piwik\Scheduler\Timetable');
// Look up the retry count so that we know whether this is a retry or not
self::$currentlyRunningScheduledTaskRetryCount = $timetable->getRetryCount($taskName);
}
/**
* If the task is for CustomAlerts, clear the property containing the name of the task that just ran.
*
* @param Task $task
* @return void
*/
public function endingScheduledTask(Task $task): void
{
if (strpos($task->getName(), 'Piwik\Plugins\CustomAlerts\Tasks') === false) {
return;
}
self::$currentlyRunningScheduledTaskName = null;
self::$currentlyRunningScheduledTaskRetryCount = 0;
}
public function getClientSideTranslationKeys(&$translations)
{
$translations[] = 'General_Value';
$translations[] = 'CustomAlerts_InvalidMetricValue';
$translations[] = 'General_Report';
$translations[] = 'CustomAlerts_NoAlertsDefined';
$translations[] = 'CustomAlerts_CreateNewAlert';
$translations[] = 'CustomAlerts_AlertsHistory';
$translations[] = 'CustomAlerts_AlertName';
$translations[] = 'CustomAlerts_ApplyTo';
$translations[] = 'ScheduledReports_SendReportTo';
$translations[] = 'ScheduledReports_SentToMe';
$translations[] = 'ScheduledReports_AlsoSendReportToTheseEmails';
$translations[] = 'CustomAlerts_ThisAppliesTo';
$translations[] = 'CustomAlerts_AlertCondition';
$translations[] = 'CustomAlerts_When';
$translations[] = 'CustomAlerts_AlertMeWhen';
$translations[] = 'CustomAlerts_ComparedToThe';
$translations[] = 'CustomAlerts_YouCanChoosePeriodFrom';
$translations[] = 'CustomAlerts_PeriodDayDescription';
$translations[] = 'CustomAlerts_PeriodWeekDescription';
$translations[] = 'CustomAlerts_PeriodMonthDescription';
$translations[] = 'MobileMessaging_PhoneNumbers';
$translations[] = 'CustomAlerts_MobileMessagingPluginNotActivated';
$translations[] = 'CustomAlerts_ManageAlerts';
$translations[] = 'CustomAlerts_AreYouSureDeleteAlert';
$translations[] = 'CustomAlerts_ThisAppliesToHelp';
$translations[] = 'General_Yes';
$translations[] = 'General_No';
}
}