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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
<?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\Dashboard;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Category\Subcategory;
use Piwik\Widget\WidgetConfig;
use Piwik\Plugin;
/**
*/
class Dashboard extends \Piwik\Plugin
{
/**
* @see \Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
return array(
'AssetManager.getJavaScriptFiles' => 'getJsFiles',
'AssetManager.getStylesheetFiles' => 'getStylesheetFiles',
'UsersManager.deleteUser' => 'deleteDashboardLayout',
'Translate.getClientSideTranslationKeys' => 'getClientSideTranslationKeys',
'Widget.addWidgetConfigs' => 'addWidgetConfigs',
'Category.addSubcategories' => 'addSubcategories',
'Widgetize.shouldEmbedIframeEmpty' => 'shouldEmbedIframeEmpty'
);
}
public function shouldEmbedIframeEmpty(&$shouldEmbedEmpty, $controllerName, $actionName)
{
if ($controllerName == 'Dashboard' && $actionName == 'index') {
$shouldEmbedEmpty = true;
}
}
public function addWidgetConfigs(&$widgets)
{
if (Piwik::isUserIsAnonymous()) {
$this->addDefaultDashboard($widgets);
} else {
$dashboards = $this->getDashboards();
if (empty($dashboards)) {
$this->addDefaultDashboard($widgets);
} else {
foreach ($dashboards as $dashboard) {
$config = new WidgetConfig();
$config->setIsNotWidgetizable();
$config->setModule('Dashboard');
$config->setAction('embeddedIndex');
$config->setCategoryId('Dashboard_Dashboard');
$config->setSubcategoryId($dashboard['id']);
$config->setParameters(array('idDashboard' => $dashboard['id']));
$widgets[] = $config;
}
}
}
}
private function getDashboards()
{
return Request::processRequest('Dashboard.getDashboards',
['filter_limit' => '-1', 'filter_offset' => 0],
[]
);
}
private function addDefaultDashboard(&$widgets)
{
$config = new WidgetConfig();
$config->setIsNotWidgetizable();
$config->setModule('Dashboard');
$config->setAction('embeddedIndex');
$config->setCategoryId('Dashboard_Dashboard');
$config->setSubcategoryId('1');
$config->setParameters(array('idDashboard' => 1));
$widgets[] = $config;
}
public function addSubcategories(&$subcategories)
{
if (Piwik::isUserIsAnonymous()) {
$this->addDefaultSubcategory($subcategories);
} else {
$dashboards = $this->getDashboards();
if (empty($dashboards)) {
$this->addDefaultSubcategory($subcategories);
} else {
$order = 0;
foreach ($dashboards as $dashboard) {
$subcategory = new Subcategory();
$subcategory->setName($dashboard['name']);
$subcategory->setCategoryId('Dashboard_Dashboard');
$subcategory->setId($dashboard['id']);
$subcategory->setOrder($order++);
$subcategories[] = $subcategory;
}
}
}
}
private function addDefaultSubcategory(&$subcategories)
{
$subcategory = new Subcategory();
$subcategory->setName('Dashboard_Dashboard');
$subcategory->setCategoryId('Dashboard_Dashboard');
$subcategory->setId('1');
$subcategory->setOrder(1);
$subcategories[] = $subcategory;
}
/**
* Returns the layout in the DB for the given user, or false if the layout has not been set yet.
* Parameters must be checked BEFORE this function call
*
* @param string $login
* @param int $idDashboard
*
* @return bool|string
*/
public function getLayoutForUser($login, $idDashboard)
{
$return = $this->getModel()->getLayoutForUser($login, $idDashboard);
if (count($return) == 0) {
return false;
}
return $return[0]['layout'];
}
private function getModel()
{
return new Model();
}
public function getDefaultLayout()
{
$defaultLayout = $this->getLayoutForUser('', 1);
if (empty($defaultLayout)) {
$advertisingWidget = '';
$advertising = StaticContainer::get('Piwik\ProfessionalServices\Advertising');
if ($advertising->areAdsForProfessionalServicesEnabled() && Plugin\Manager::getInstance()->isPluginActivated('ProfessionalServices')) {
$advertisingWidget = '{"uniqueId":"widgetProfessionalServicespromoServices","parameters":{"module":"ProfessionalServices","action":"promoServices"}},';
}
$piwikPromoWidget = '{"uniqueId":"widgetCoreHomegetPromoVideo","parameters":{"module":"CoreHome","action":"getPromoVideo"}}';
$defaultLayout = '[
[
{"uniqueId":"widgetLivewidget","parameters":{"module":"Live","action":"widget"}},
' . $piwikPromoWidget . '
],
[
{"uniqueId":"widgetVisitsSummarygetEvolutionGraphforceView1viewDataTablegraphEvolution","parameters":{"forceView":"1","viewDataTable":"graphEvolution","module":"VisitsSummary","action":"getEvolutionGraph"}},
' . $advertisingWidget . '
{"uniqueId":"widgetVisitsSummarygetforceView1viewDataTablesparklines","parameters":{"forceView":"1","viewDataTable":"sparklines","module":"VisitsSummary","action":"get"}}
],
[
{"uniqueId":"widgetUserCountryMapvisitorMap","parameters":{"module":"UserCountryMap","action":"visitorMap"}},
{"uniqueId":"widgetReferrersgetReferrerType","parameters":{"module":"Referrers","action":"getReferrerType"}},
{"uniqueId":"widgetRssWidgetrssPiwik","parameters":{"module":"RssWidget","action":"rssPiwik"}}
]
]';
}
/**
* Allows other plugins to modify the default dashboard layout.
*
* @param string &$defaultLayout JSON encoded string of the default dashboard layout. Contains an
* array of columns where each column is an array of widgets. Each
* widget is an associative array w/ the following elements:
*
* * **uniqueId**: The widget's unique ID.
* * **parameters**: The array of query parameters that should be used to get this widget's report.
*/
Piwik::postEvent("Dashboard.changeDefaultDashboardLayout", array(&$defaultLayout));
$defaultLayout = $this->removeDisabledPluginFromLayout($defaultLayout);
return $defaultLayout;
}
public function getAllDashboards($login)
{
$dashboards = $this->getModel()->getAllDashboardsForUser($login);
$nameless = 1;
foreach ($dashboards as &$dashboard) {
if (empty($dashboard['name'])) {
$dashboard['name'] = Piwik::translate('Dashboard_DashboardOf', $login);
if ($nameless > 1) {
$dashboard['name'] .= " ($nameless)";
}
$nameless++;
}
$dashboard['name'] = Common::unsanitizeInputValue($dashboard['name']);
$layout = '[]';
if (!empty($dashboard['layout'])) {
$layout = $dashboard['layout'];
}
$dashboard['layout'] = $this->decodeLayout($layout);
}
return $dashboards;
}
private function isAlreadyDecodedLayout($layout)
{
return !is_string($layout);
}
public function removeDisabledPluginFromLayout($layout)
{
$layoutObject = $this->decodeLayout($layout);
// if the json decoding works (ie. new Json format)
// we will only return the widgets that are from enabled plugins
if (is_array($layoutObject)) {
$layoutObject = (object)array(
'config' => array('layout' => '33-33-33'),
'columns' => $layoutObject
);
}
if (empty($layoutObject) || empty($layoutObject->columns)) {
$layoutObject = (object)array(
'config' => array('layout' => '33-33-33'),
'columns' => array()
);
}
$layout = $this->encodeLayout($layoutObject);
return $layout;
}
public function decodeLayout($layout)
{
if ($this->isAlreadyDecodedLayout($layout)) {
return $layout;
}
$layout = html_entity_decode($layout, ENT_COMPAT | ENT_HTML401, 'UTF-8');
$layout = str_replace("\\\"", "\"", $layout);
$layout = str_replace("\n", "", $layout);
return json_decode($layout, $assoc = false);
}
public function encodeLayout($layout)
{
return json_encode($layout);
}
public function getJsFiles(&$jsFiles)
{
$jsFiles[] = "plugins/Dashboard/angularjs/common/services/dashboards-model.js";
$jsFiles[] = "plugins/Dashboard/javascripts/widgetMenu.js";
$jsFiles[] = "plugins/Dashboard/javascripts/dashboardObject.js";
$jsFiles[] = "plugins/Dashboard/javascripts/dashboardWidget.js";
$jsFiles[] = "plugins/Dashboard/javascripts/dashboard.js";
$jsFiles[] = "plugins/Dashboard/angularjs/dashboard/dashboard.directive.js";
}
public function getStylesheetFiles(&$stylesheets)
{
$stylesheets[] = "plugins/CoreHome/stylesheets/dataTable.less";
$stylesheets[] = "plugins/Dashboard/stylesheets/dashboard.less";
$stylesheets[] = "plugins/Dashboard/stylesheets/widget.less";
}
public function deleteDashboardLayout($userLogin)
{
$this->getModel()->deleteAllLayoutsForUser($userLogin);
}
public function install()
{
Model::install();
}
public function uninstall()
{
Model::uninstall();
}
public function getClientSideTranslationKeys(&$translationKeys)
{
$translationKeys[] = 'Dashboard_AddPreviewedWidget';
$translationKeys[] = 'Dashboard_WidgetPreview';
$translationKeys[] = 'Dashboard_Maximise';
$translationKeys[] = 'Dashboard_Minimise';
$translationKeys[] = 'Dashboard_LoadingWidget';
$translationKeys[] = 'Dashboard_WidgetNotFound';
$translationKeys[] = 'Dashboard_DashboardCopied';
$translationKeys[] = 'Dashboard_Dashboard';
$translationKeys[] = 'Dashboard_RemoveDefaultDashboardNotPossible';
$translationKeys[] = 'General_Close';
$translationKeys[] = 'General_HelpResources';
$translationKeys[] = 'General_Refresh';
}
}
|