forked from matomo-org/plugin-CustomAlerts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomAlerts.php
executable file
·180 lines (148 loc) · 4.69 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
<?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\Db;
use Piwik\Piwik;
use Piwik\Plugins\SitesManager\API as SitesManagerApi;
/**
*
*/
class CustomAlerts extends \Piwik\Plugin
{
public function getListHooksRegistered()
{
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'
);
}
public function checkApiPermission(&$parameters, $pluginName, $methodName)
{
if ($pluginName == 'CustomAlerts') {
$this->checkPermission();
}
}
public function checkControllerPermission($module, $action)
{
if ($module != 'CustomAlerts') {
return;
}
if ($action == 'formatAlerts') {
throw new \Exception('This action does not exist');
}
$this->checkPermission();
}
private function checkPermission()
{
Piwik::checkUserIsNotAnonymous();
}
public function install()
{
Model::install();
}
public function uninstall()
{
Model::uninstall();
}
public function getJavaScriptFiles(&$jsFiles)
{
$jsFiles[] = "plugins/CustomAlerts/javascripts/alerts.js";
}
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']);
}
}
}
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));
}
public function getClientSideTranslationKeys(&$translations)
{
$translations[] = 'CustomAlerts_InvalidMetricValue';
}
private function getModel()
{
return new Model();
}
/**
* @return array
*/
private function getAllAlerts()
{
return $this->getModel()->getAllAlerts();
}
}