PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 4.3.0
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v4.3.0
5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / Widget / WidgetsList.php
matomo / app / core / Widget Last commit date
Widget.php 5 years ago WidgetConfig.php 5 years ago WidgetContainerConfig.php 5 years ago WidgetsList.php 5 years ago
WidgetsList.php
249 lines
1 <?php
2 /**
3 * Matomo - free/libre analytics platform
4 *
5 * @link https://matomo.org
6 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
7 *
8 */
9 namespace Piwik\Widget;
10
11 use Piwik\Container\StaticContainer;
12 use Piwik\Development;
13 use Piwik\Piwik;
14 use Piwik\Report\ReportWidgetFactory;
15
16 /**
17 * Manages the global list of reports that can be displayed as dashboard widgets.
18 *
19 * Widgets are added through the {@hook WidgetsList.addWidgets} and filtered through the {@hook Widgets.filterWidgets}
20 * event. Observers for this event should call the {@link addWidget()} method to add widgets or use any of the other
21 * methods to remove widgets.
22 *
23 * @api since Piwik 3.0.0
24 */
25 class WidgetsList
26 {
27 /**
28 * List of widgets
29 *
30 * @var WidgetConfig[]
31 */
32 private $widgets = array();
33
34 /**
35 * @var WidgetContainerConfig[]
36 */
37 private $container;
38
39 /**
40 * @var array
41 */
42 private $containerWidgets;
43
44 /**
45 * Adds a new widget to the widget config. Please make sure the widget is enabled before adding a widget as
46 * no such checks will be performed.
47 *
48 * @param WidgetConfig $widget
49 */
50 public function addWidgetConfig(WidgetConfig $widget)
51 {
52 if ($widget instanceof WidgetContainerConfig) {
53 $this->addContainer($widget);
54 } elseif (Development::isEnabled()) {
55 $this->checkIsValidWidget($widget);
56 }
57
58 $this->widgets[] = $widget;
59 }
60
61 /**
62 * Add multiple widget configs at once. See {@link addWidgetConfig()}.
63 *
64 * @param WidgetConfig[] $widgets
65 */
66 public function addWidgetConfigs($widgets)
67 {
68 foreach ($widgets as $widget) {
69 $this->addWidgetConfig($widget);
70 }
71 }
72
73 private function addContainer(WidgetContainerConfig $containerWidget)
74 {
75 $widgetId = $containerWidget->getId();
76
77 $this->container[$widgetId] = $containerWidget;
78
79 // widgets were added to this container, but the container did not exist yet.
80 if (isset($this->containerWidgets[$widgetId])) {
81 foreach ($this->containerWidgets[$widgetId] as $widget) {
82 $containerWidget->addWidgetConfig($widget);
83 }
84 unset($this->containerWidgets[$widgetId]);
85 }
86 }
87
88 /**
89 * Get all added widget configs.
90 *
91 * @return WidgetConfig[]
92 */
93 public function getWidgetConfigs()
94 {
95 return $this->widgets;
96 }
97
98 private function checkIsValidWidget(WidgetConfig $widget)
99 {
100 if (!$widget->getModule()) {
101 Development::error('No module is defined for added widget having name "' . $widget->getName());
102 }
103
104 if (!$widget->getAction()) {
105 Development::error('No action is defined for added widget having name "' . $widget->getName());
106 }
107 }
108
109 /**
110 * Add a widget to a widget container. It doesn't matter whether the container was added to this list already
111 * or whether the container is added later. As long as a container having the same containerId is added at
112 * some point the widget will be added to that container. If no container having this id is added the widget
113 * will not be recognized.
114 *
115 * @param string $containerId eg 'Products' or 'Contents'. See {@link WidgetContainerConfig::setId}
116 * @param WidgetConfig $widget
117 */
118 public function addToContainerWidget($containerId, WidgetConfig $widget)
119 {
120 if (isset($this->container[$containerId])) {
121 $this->container[$containerId]->addWidgetConfig($widget);
122 } else {
123 if (!isset($this->containerWidgets[$containerId])) {
124 $this->containerWidgets[$containerId] = array();
125 }
126
127 $this->containerWidgets[$containerId][] = $widget;
128 }
129 }
130
131 /**
132 * Removes one or more widgets from the widget list.
133 *
134 * @param string $widgetCategoryId The widget category id. Can be a translation token eg 'General_Visits'
135 * see {@link WidgetConfig::setCategoryId()}.
136 * @param string|false $widgetName The name of the widget to remove eg 'VisitTime_ByServerTimeWidgetName'.
137 * If not supplied, all widgets within that category will be removed.
138 */
139 public function remove($widgetCategoryId, $widgetName = false)
140 {
141 foreach ($this->widgets as $index => $widget) {
142 if ($widget->getCategoryId() === $widgetCategoryId) {
143 if (!$widgetName || $widget->getName() === $widgetName) {
144 unset($this->widgets[$index]);
145 }
146 }
147 }
148 }
149
150 /**
151 * Returns `true` if a widget exists in the widget list, `false` if otherwise.
152 *
153 * @param string $module The controller name of the widget.
154 * @param string $action The controller action of the widget.
155 * @return bool
156 */
157 public function isDefined($module, $action)
158 {
159 foreach ($this->widgets as $widget) {
160 if ($widget->getModule() === $module && $widget->getAction() === $action) {
161 return true;
162 }
163 }
164
165 return false;
166 }
167
168 /**
169 * Get all widgets defined in the Piwik platform.
170 * @ignore
171 * @return static
172 */
173 public static function get()
174 {
175 $list = new static;
176
177 $widgets = StaticContainer::get('Piwik\Plugin\WidgetsProvider');
178
179 $widgetContainerConfigs = $widgets->getWidgetContainerConfigs();
180 foreach ($widgetContainerConfigs as $config) {
181 if ($config->isEnabled()) {
182 $list->addWidgetConfig($config);
183 }
184 }
185
186 $widgetConfigs = $widgets->getWidgetConfigs();
187 foreach ($widgetConfigs as $widget) {
188 if ($widget->isEnabled()) {
189 $list->addWidgetConfig($widget);
190 }
191 }
192
193 $reports = StaticContainer::get('Piwik\Plugin\ReportsProvider');
194 $reports = $reports->getAllReports();
195 foreach ($reports as $report) {
196 if ($report->isEnabled()) {
197 $factory = new ReportWidgetFactory($report);
198 $report->configureWidgets($list, $factory);
199 }
200 }
201
202 /**
203 * Triggered to filter widgets.
204 *
205 * **Example**
206 *
207 * public function removeWidgetConfigs(Piwik\Widget\WidgetsList $list)
208 * {
209 * $list->remove($category='General_Visits'); // remove all widgets having this category
210 * }
211 *
212 * @param WidgetsList $list An instance of the WidgetsList. You can change the list of widgets this way.
213 */
214 Piwik::postEvent('Widget.filterWidgets', array($list));
215
216 return $list;
217 }
218
219 /**
220 * CAUTION! If you ever change this method, existing updates will fail as they currently use that method!
221 * If you change the output the uniqueId for existing widgets would not be found anymore
222 *
223 * Returns the unique id of an widget with the given parameters
224 *
225 * @param $controllerName
226 * @param $controllerAction
227 * @param array $customParameters
228 * @return string
229 */
230 public static function getWidgetUniqueId($controllerName, $controllerAction, $customParameters = array())
231 {
232 $widgetUniqueId = 'widget' . $controllerName . $controllerAction;
233
234 foreach ($customParameters as $name => $value) {
235 if (is_array($value)) {
236 // use 'Array' for backward compatibility;
237 // could we switch to using $value[0]?
238 $value = 'Array';
239 }
240 $value = urlencode($value);
241 $value = str_replace('%', '', $value);
242 $widgetUniqueId .= $name . $value;
243 }
244
245 return $widgetUniqueId;
246 }
247
248 }
249