/
WebsitesRequest.js
299 lines (241 loc) · 8.99 KB
/
WebsitesRequest.js
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/**
* Piwik - Web Analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html Gpl v3 or later
* @version $Id$
*/
/** @private */
var Piwik = require('Piwik');
/** @private */
var config = require('config');
/**
* @type Piwik.Network.RequestPool|null
*
* @private
*/
var requestPool = null;
/**
* @class Fetches a list of all available websites where the user has at least view access. Requests all available
* websites for all configured accounts.
*
* @exports WebsitesRequest as Piwik.Network.WebsitesRequest
* @augments Piwik.Network.Request
*/
function WebsitesRequest () {
/**
* This event will be fired as soon as all requests are finished.
*
* @name Piwik.Network.WebsitesRequest#event:onload
* @event
*
* @param {Object} event
* @param {string} event.type The name of the event.
* @param {Array} event.sites See {@link Piwik.Network.WebsitesRequest#sites}.
* @param {boolean} event.showMultiChart See {@link Piwik.Network.WebsitesRequest#showMultiChart}.
* @param {boolean} event.filterUsed See {@link Piwik.Network.WebsitesRequest#filterUsed}.
* @param {boolean} event.achievedSitesLimit See {@link Piwik.Network.WebsitesRequest#achievedSitesLimit}.
*/
/**
* Holds an array of all available websites where the user has at least view access.
*
* @type Array
*/
this.sites = [];
/**
* True if multichart is enabled, false otherwise. Multicharts are the sparklines displayed beside the website name.
*
* @defaults false
*
* @type boolean
*/
this.showMultiChart = false;
/**
* An array of all available accounts.
*
* @see Piwik.App.Accounts#getAccounts
*
* @type Array
*/
this.accounts = [];
/**
* True if search function was used, false otherwise.
*
* @defaults false
*
* @type boolean
*/
this.filterUsed = false;
/**
* True if the number of requestes sites (limit parameter) is achieved. False otherwise.
*
* @defaults false
*
* @type boolean
*/
this.achievedSitesLimit = false;
}
/**
* Extend Piwik.Network.Request
*/
WebsitesRequest.prototype = Piwik.require('Network/Request');
/**
* Initialize / reset all previous defined or fetched values. We have to do this cause it is possible to call the
* 'send' method multiple times.
*/
WebsitesRequest.prototype.init = function () {
this.sites = [];
this.filterUsed = false;
this.accounts = null;
this.achievedSitesLimit = false;
var settings = Piwik.require('App/Settings');
this.showMultiChart = settings.getPiwikMultiChart();
settings = null;
var accountManager = Piwik.require('App/Accounts');
this.accounts = accountManager.getAccounts();
accountManager = null;
};
/**
* Sends a request for each configured account to determine all websites the user has at least view access. Executes
* the {@link Piwik.Network.WebsitesRequest#loaded} method as soon as all responses are received. Executes the
* {@link Piwik.Network.WebsitesRequest#onReceiveSitesWithAtLeastViewAccess} for each received request result (for
* each account). Requests by default only 5 sites per account (if filterName is not given).
*
* @param {Object} params
* @param {Object} params.filterName Search only for sites which contains the given filterName.
* @param {boolean} [params.reload="false"] If true, it will not use an already cached result.
*/
WebsitesRequest.prototype.send = function (params) {
this.init();
if (!params) {
params = {};
}
if (!params.reload) {
params.reload = false;
}
var piwikRequest = null;
if (!this.accounts || !this.accounts.length) {
// no accounts configured
this.loaded();
return;
}
if (!params.reload && (!params || !params.filterName)) {
// no filter used, use cached result if it exist
var session = Piwik.require('App/Session');
var cachedSites = session.get('piwik_sites_allowed');
session = null;
if (cachedSites) {
this.filterUsed = false;
this.sites = cachedSites;
cachedSites = null;
this.loaded();
return;
}
}
var parameter = null;
for (var index = 0; index < this.accounts.length; index++) {
if (!this.accounts[index] || !Boolean(this.accounts[index].active)) {
// account is not set or not active
continue;
}
// create a request to fetch all sites the user has at least view access
piwikRequest = Piwik.require('Network/PiwikApiRequest');
parameter = {accountId: this.accounts[index].id, limit: config.piwik.numDisplayedWebsites};
if (params && params.filterName) {
parameter.pattern = params.filterName;
piwikRequest.setMethod('SitesManager.getPatternMatchSites');
this.filterUsed = true;
} else {
piwikRequest.setMethod('SitesManager.getSitesWithAtLeastViewAccess');
this.filterUsed = false;
}
piwikRequest.setParameter(parameter);
piwikRequest.setAccount(this.accounts[index]);
piwikRequest.setCallback(this, this.onReceiveSitesWithAtLeastViewAccess);
// attach the request to the request pool. So all attached requests will be send in parallel
requestPool.attach(piwikRequest);
Piwik.require('App/Accounts').updatePiwikVersion(this.accounts[index]);
piwikRequest = null;
}
requestPool.send(this.loaded);
requestPool = null;
params = null;
parameter = null;
};
/**
* Abort all previous fired requests. No callback method will be called.
*/
WebsitesRequest.prototype.abort = function () {
if (requestPool && requestPool.abort) {
requestPool.abort();
}
};
/**
* Will be called for each "SitesManager.getSitesWithAtLeastViewAccess" response. See <a href="http://piwik.org/docs/analytics-api/reference/#SitesManager">SitesManager</a> for more
* information. Adds each allowed website to the {@link Piwik.Network.WebsitesRequest#sites} Array.
*
* @param {Array} response The received response of the request.
* @param {Object} parameter The used parameters to send the request.
*/
WebsitesRequest.prototype.onReceiveSitesWithAtLeastViewAccess = function (response, parameter) {
var allowedSites = response;
if (!allowedSites || !Piwik.isArray(allowedSites) || 0 == allowedSites.length) {
// the user has no access to any website
return;
}
// try to find the account that was used to send the request, depending on the accountId.
var account = {};
for (var index = 0; index < this.accounts.length; index++) {
if (this.accounts[index] && this.accounts[index].id == parameter.accountId) {
// this account was used to send the request
account = this.accounts[index];
break;
}
}
var numFoundSitesPerAccount = 0;
for (var sitesIndex = 0; sitesIndex < allowedSites.length; sitesIndex++) {
var site = allowedSites[sitesIndex];
if (!site) {
continue;
}
site.sparklineUrl = '';
if (this.showMultiChart) {
var graph = Piwik.require('PiwikGraph');
site.sparklineUrl = graph.getSparklineUrl(site.idsite, account.accessUrl, account.tokenAuth);
graph = null;
}
site.accountId = parameter.accountId;
numFoundSitesPerAccount++;
this.sites.push(site);
site = null;
}
if (numFoundSitesPerAccount && config.piwik.numDisplayedWebsites <= numFoundSitesPerAccount) {
this.achievedSitesLimit = true;
}
allowedSites = null;
response = null;
account = null;
return;
};
/**
* Will be called as soon as all requests have received and processed their result/callbacks. We can now fire an
* event containing all sites the user has at least view access and other additional information.
*
* @fires Piwik.Network.WebsitesRequest#event:onload
*/
WebsitesRequest.prototype.loaded = function () {
if (!this.filterUsed) {
// cache only if no filter was used
var session = Piwik.require('App/Session');
session.set('piwik_sites_allowed', this.sites);
session = null;
}
var eventResult = {type: 'onload',
sites: this.sites,
filterUsed: this.filterUsed,
achievedSitesLimit: this.achievedSitesLimit,
showMultiChart: this.showMultiChart};
this.fireEvent('onload', eventResult);
eventResult = null;
};
module.exports = WebsitesRequest;