-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller.php
249 lines (214 loc) · 9.62 KB
/
Controller.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
<?php
/**
* Piwik - 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\UserCountryMapIslandora;
use Exception;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugins\Goals\API as APIGoals;
use Piwik\Plugins\VisitsSummary\API as VisitsSummaryAPI;
use Piwik\Site;
use Piwik\Translation\Translator;
use Piwik\View;
require_once PIWIK_INCLUDE_PATH . '/plugins/UserCountry/functions.php';
/**
*
*/
class Controller extends \Piwik\Plugin\Controller
{
// By default plot up to the last 3 days of visitors on the map, for low traffic sites
const REAL_TIME_WINDOW = 'last3';
/**
* @var Translator
*/
private $translator;
public function __construct(Translator $translator)
{
$this->translator = $translator;
parent::__construct();
}
public function visitorMap($fetch = false, $segmentOverride = false)
{
$this->checkUserCountryPluginEnabled();
$this->checkSitePermission();
Piwik::checkUserHasViewAccess($this->idSite);
$period = Common::getRequestVar('period');
$date = Common::getRequestVar('date');
if (!empty($segmentOverride)) {
$segment = $segmentOverride;
} else {
$segment = Request::getRawSegmentFromRequest();
if (empty($segment)) {
$segment = '';
}
}
$token_auth = Piwik::getCurrentUserTokenAuth();
$view = new View('@UserCountryMapIslandora/visitorMap');
// request visits summary
$request = new Request(
'method=VisitsSummary.get&format=php'
. '&idSite=' . $this->idSite
. '&period=' . $period
. '&date=' . $date
. '&segment=' . $segment
. '&token_auth=' . $token_auth
. '&filter_limit=-1'
);
$config = array();
$config['visitsSummary'] = Common::safe_unserialize($request->process());
$config['countryDataUrl'] = $this->_report('UserCountry', 'getCountry',
$this->idSite, $period, $date, $token_auth, false, $segment);
$config['regionDataUrl'] = $this->_report('UserCountry', 'getRegion',
$this->idSite, $period, $date, $token_auth, true, $segment);
$config['cityDataUrl'] = $this->_report('UserCountry', 'getCity',
$this->idSite, $period, $date, $token_auth, true, $segment);
$config['countrySummaryUrl'] = $this->getApiRequestUrl('VisitsSummary', 'get',
$this->idSite, $period, $date, $token_auth, true, $segment);
$view->defaultMetric = array_key_exists('nb_uniq_visitors', $config['visitsSummary']) ? 'nb_uniq_visitors' : 'nb_visits';
$noVisitTranslation = $this->translator->translate('UserCountryMapIslandora_NoVisit');
// some translations containing metric number
$translations = array(
'nb_visits' => $this->translator->translate('General_NVisits'),
'no_visit' => $noVisitTranslation,
'nb_actions' => $this->translator->translate('VisitsSummary_NbActionsDescription'),
'nb_actions_per_visit' => $this->translator->translate('VisitsSummary_NbActionsPerVisit'),
'bounce_rate' => $this->translator->translate('VisitsSummary_NbVisitsBounced'),
'avg_time_on_site' => $this->translator->translate('VisitsSummary_AverageVisitDuration'),
'and_n_others' => $this->translator->translate('UserCountryMapIslandora_AndNOthers'),
'nb_uniq_visitors' => $this->translator->translate('General_NUniqueVisitors'),
'nb_users' => $this->translator->translate('VisitsSummary_NbUsers'),
);
foreach ($translations as &$translation) {
if (false === strpos($translation, '%s')
&& $translation !== $noVisitTranslation) {
$translation = '%s ' . $translation;
}
}
$translations['one_visit'] = $this->translator->translate('General_OneVisit');
$translations['no_data'] = $this->translator->translate('CoreHome_ThereIsNoDataForThisReport');
$view->localeJSON = json_encode($translations);
$view->reqParamsJSON = $this->getEnrichedRequest($params = array(
'period' => $period,
'idSite' => $this->idSite,
'date' => $date,
'segment' => $segment,
'token_auth' => $token_auth,
'enable_filter_excludelowpop' => 1,
'filter_excludelowpop_value' => -1
));
$view->metrics = $config['metrics'] = $this->getMetrics($this->idSite, $period, $date, $token_auth);
// Reuse Original Plugins data
$config['svgBasePath'] = 'plugins/UserCountryMap/svg/';
// Allow our own CSS override
$config['mapCssPath'] = 'plugins/UserCountryMapIslandora/stylesheets/map.css';
$view->config = json_encode($config);
$view->noData = empty($config['visitsSummary']['nb_visits']);
$countriesByIso = array();
$regionDataProvider = StaticContainer::get('Piwik\Intl\Data\Provider\RegionDataProvider');
$countries = array_keys($regionDataProvider->getCountryList());
foreach ($countries AS $country) {
$countriesByIso[strtoupper($country)] = Piwik::translate('Intl_Country_'.strtoupper($country));
}
$view->countriesByIso = $countriesByIso;
$view->continents = array(
'AF' => \Piwik\Plugins\UserCountry\continentTranslate('afr'),
'AS' => \Piwik\Plugins\UserCountry\continentTranslate('asi'),
'EU' => \Piwik\Plugins\UserCountry\continentTranslate('eur'),
'NA' => \Piwik\Plugins\UserCountry\continentTranslate('amn'),
'OC' => \Piwik\Plugins\UserCountry\continentTranslate('oce'),
'SA' => \Piwik\Plugins\UserCountry\continentTranslate('ams')
);
return $view->render();
}
private function getEnrichedRequest($params, $encode = true)
{
$params['format'] = 'json';
$params['showRawMetrics'] = 1;
if (empty($params['segment'])) {
$segment = Request::getRawSegmentFromRequest();
if (!empty($segment)) {
$params['segment'] = $segment;
}
}
if (!empty($params['segment'])) {
$params['segment'] = urldecode($params['segment']);
}
if ($encode) {
$params = json_encode($params);
}
return $params;
}
private function checkUserCountryPluginEnabled()
{
if (!\Piwik\Plugin\Manager::getInstance()->isPluginActivated('UserCountry')) {
throw new Exception($this->translator->translate('General_Required', 'Plugin UserCountry'));
}
// Check also if we have the parent plugin enabled.
if (!\Piwik\Plugin\Manager::getInstance()->isPluginActivated('UserCountryMap')) {
throw new Exception($this->translator->translate('General_Required', 'Plugin UserCountryMap'));
}
}
private function getMetrics($idSite, $period, $date, $token_auth)
{
$request = new Request(
'method=API.getMetadata&format=PHP'
. '&apiModule=UserCountry&apiAction=getCountry'
. '&idSite=' . $idSite
. '&period=' . $period
. '&date=' . $date
. '&token_auth=' . $token_auth
. '&filter_limit=-1'
);
$metaData = Common::safe_unserialize($request->process());
$metrics = array();
if (!empty($metaData[0]['metrics']) && is_array($metaData[0]['metrics'])) {
foreach ($metaData[0]['metrics'] as $id => $val) {
// todo: should use SettingsPiwik::isUniqueVisitorsEnabled ?
if (Common::getRequestVar('period') == 'day' || $id != 'nb_uniq_visitors') {
$metrics[] = array($id, $val);
}
}
}
if (!empty($metaData[0]['processedMetrics']) && is_array($metaData[0]['processedMetrics'])) {
foreach ($metaData[0]['processedMetrics'] as $id => $val) {
$metrics[] = array($id, $val);
}
}
return $metrics;
}
private function getApiRequestUrl($module, $action, $idSite, $period, $date, $token_auth, $filter_by_country = false, $segmentOverride = false)
{
// use processed reports
$url = "?module=" . $module
. "&method=" . $module . "." . $action . "&format=JSON"
. "&idSite=" . $idSite
. "&period=" . $period
. "&date=" . $date
. "&token_auth=" . $token_auth
. "&segment=" . ($segmentOverride ? : Request::getRawSegmentFromRequest())
. "&enable_filter_excludelowpop=1"
. "&showRawMetrics=1";
if ($filter_by_country) {
$url .= "&filter_column=country"
. "&filter_sort_column=nb_visits"
. "&filter_limit=-1"
. "&filter_pattern=";
} else {
$url .= "&filter_limit=-1";
}
return $url;
}
private function _report($module, $action, $idSite, $period, $date, $token_auth, $filter_by_country = false, $segmentOverride = false)
{
return $this->getApiRequestUrl('API', 'getProcessedReport&apiModule=' . $module . '&apiAction=' . $action,
$idSite, $period, $date, $token_auth, $filter_by_country, $segmentOverride);
}
}