blob: 9763b6c560fee234009bec0638ff3c9544a59f55 (
plain)
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
|
<?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\Plugin;
use Piwik\Container\StaticContainer;
use Piwik\Piwik;
use Piwik\Plugin;
use Exception;
use Piwik\Widget\Widget;
use Piwik\Widget\WidgetConfig;
use Piwik\Widget\WidgetContainerConfig;
/**
* Get widgets that are defined by plugins.
*/
class WidgetsProvider
{
/**
* @var Plugin\Manager
*/
private $pluginManager;
public function __construct(Plugin\Manager $pluginManager)
{
$this->pluginManager = $pluginManager;
}
/**
* Get all existing widget configs.
*
* @return WidgetConfig[]
*/
public function getWidgetConfigs()
{
$widgetClasses = $this->getAllWidgetClassNames();
$configs = array();
/**
* Triggered to add custom widget configs. To filder widgets have a look at the {@hook Widget.filterWidgets}
* event.
*
* **Example**
*
* public function addWidgetConfigs(&$configs)
* {
* $config = new WidgetConfig();
* $config->setModule('PluginName');
* $config->setAction('renderDashboard');
* $config->setCategoryId('Dashboard_Dashboard');
* $config->setSubcategoryId('dashboardId');
* $configs[] = $config;
* }
*
* @param array &$configs An array containing a list of widget config entries.
*/
Piwik::postEvent('Widget.addWidgetConfigs', array(&$configs));
foreach ($widgetClasses as $widgetClass) {
$configs[] = $this->getWidgetConfigForClassName($widgetClass);
}
return $configs;
}
/**
* Get all existing widget container configs.
* @return WidgetContainerConfig[]
*/
public function getWidgetContainerConfigs()
{
$configs = array();
$widgetContainerConfigs = $this->getAllWidgetContainerConfigClassNames();
foreach ($widgetContainerConfigs as $widgetClass) {
$configs[] = StaticContainer::get($widgetClass);
}
return $configs;
}
/**
* Get the widget defined by the given module and action.
*
* @param string $module Aka plugin name, eg 'CoreHome'
* @param string $action An action eg 'renderMe'
* @return Widget|null
* @throws \Exception Throws an exception if the widget is not enabled.
*/
public function factory($module, $action)
{
if (empty($module) || empty($action)) {
return;
}
try {
if (!$this->pluginManager->isPluginActivated($module)) {
return;
}
$plugin = $this->pluginManager->getLoadedPlugin($module);
} catch (\Exception $e) {
// we are not allowed to use possible widgets, plugin is not active
return;
}
/** @var Widget[] $widgetContainer */
$widgets = $plugin->findMultipleComponents('Widgets', 'Piwik\\Widget\\Widget');
foreach ($widgets as $widgetClass) {
$config = $this->getWidgetConfigForClassName($widgetClass);
if ($config->getAction() === $action) {
$config->checkIsEnabled();
return StaticContainer::get($widgetClass);
}
}
}
private function getWidgetConfigForClassName($widgetClass)
{
/** @var string|Widget $widgetClass */
$config = new WidgetConfig();
$config->setModule($this->getModuleFromWidgetClassName($widgetClass));
$config->setAction($this->getActionFromWidgetClassName($widgetClass));
$widgetClass::configure($config);
return $config;
}
/**
* @return string[]
*/
private function getAllWidgetClassNames()
{
return $this->pluginManager->findMultipleComponents('Widgets', 'Piwik\\Widget\\Widget');
}
private function getModuleFromWidgetClassName($widgetClass)
{
$parts = explode('\\', $widgetClass);
return $parts[2];
}
private function getActionFromWidgetClassName($widgetClass)
{
$parts = explode('\\', $widgetClass);
if (count($parts) >= 4) {
return lcfirst(end($parts));
}
return '';
}
/**
* @return string[]
*/
private function getAllWidgetContainerConfigClassNames()
{
return $this->pluginManager->findMultipleComponents('Widgets', 'Piwik\\Widget\\WidgetContainerConfig');
}
}
|