PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.8.2
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.8.2
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 / WidgetConfig.php
matomo / app / core / Widget Last commit date
Widget.php 3 months ago WidgetConfig.php 1 year ago WidgetContainerConfig.php 3 months ago WidgetsList.php 3 months ago
WidgetConfig.php
331 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 */
9 namespace Piwik\Widget;
10
11 use Piwik\Access;
12 use Piwik\Piwik;
13 use Exception;
14 /**
15 * Configures a widget. Use this class to configure a {@link Piwik\Widget\Widget`} or to
16 * add a widget to the WidgetsList via {@link WidgetsList::addWidget}.
17 *
18 * @api since Piwik 3.0.0
19 */
20 class WidgetConfig
21 {
22 protected $categoryId = '';
23 protected $subcategoryId = '';
24 protected $module = '';
25 protected $action = '';
26 protected $parameters = array();
27 protected $middlewareParameters = array();
28 protected $name = '';
29 protected $order = 99;
30 protected $isEnabled = \true;
31 protected $isWidgetizable = \true;
32 protected $isWide = \false;
33 /**
34 * Set the id of the category the widget belongs to.
35 * @param string $categoryId Usually a translation key, eg 'General_Visits', 'Goals_Goals', ...
36 * @return static
37 */
38 public function setCategoryId($categoryId)
39 {
40 $this->categoryId = $categoryId;
41 return $this;
42 }
43 /**
44 * Get the id of the category the widget belongs to.
45 * @return string
46 */
47 public function getCategoryId()
48 {
49 return $this->categoryId;
50 }
51 /**
52 * Set the id of the subcategory the widget belongs to. If a subcategory is specified, the widget
53 * will be shown in the Piwik reporting UI. The subcategoryId will be used as a translation key for
54 * the submenu item.
55 *
56 * @param string $subcategoryId Usually a translation key, eg 'General_Overview', 'Actions_Pages', ...
57 * @return static
58 */
59 public function setSubcategoryId($subcategoryId)
60 {
61 $this->subcategoryId = $subcategoryId;
62 return $this;
63 }
64 /**
65 * Get the currently set category ID.
66 * @return string
67 */
68 public function getSubcategoryId()
69 {
70 return $this->subcategoryId;
71 }
72 /**
73 * Set the module (aka plugin name) of the widget. The correct module is usually detected automatically and
74 * not needed to be configured manually.
75 *
76 * @param string $module eg 'CoreHome'
77 * @return static
78 */
79 public function setModule($module)
80 {
81 $this->module = $module;
82 return $this;
83 }
84 public function getModule()
85 {
86 return $this->module;
87 }
88 /**
89 * Set the action of the widget that shall be used in the URL to render the widget.
90 * The correct action is usually detected automatically and not needed to be configured manually.
91 *
92 * @param string $action eg 'renderMyWidget'
93 * @return static
94 */
95 public function setAction($action)
96 {
97 $this->action = $action;
98 return $this;
99 }
100 /**
101 * Get the currently set action.
102 * @return string
103 */
104 public function getAction()
105 {
106 return $this->action;
107 }
108 /**
109 * Sets (overwrites) the parameters of the widget. These parameters will be added to the URL when rendering the
110 * widget. You can access these parameters via `Piwik\Common::getRequestVar(...)`.
111 *
112 * @param array $parameters eg. ('urlparam' => 'urlvalue')
113 * @return static
114 */
115 public function setParameters($parameters)
116 {
117 $this->parameters = $parameters;
118 return $this;
119 }
120 /**
121 * Add new parameters and only overwrite parameters that have the same name. See {@link setParameters()}
122 *
123 * @param array $parameters eg. ('urlparam' => 'urlvalue')
124 * @return static
125 */
126 public function addParameters($parameters)
127 {
128 $this->parameters = array_merge($this->parameters, $parameters);
129 return $this;
130 }
131 /**
132 * Get all URL parameters needed to render this widget.
133 * @return array Eg ('urlparam' => 'urlvalue').
134 */
135 public function getParameters()
136 {
137 $defaultParams = array('module' => $this->getModule(), 'action' => $this->getAction());
138 return $defaultParams + $this->parameters;
139 }
140 /**
141 * Set the name of the widget.
142 *
143 * @param string $name Usually a translation key, eg 'VisitTime_ByServerTimeWidgetName'
144 * @return static
145 */
146 public function setName($name)
147 {
148 $this->name = $name;
149 return $this;
150 }
151 /**
152 * Get the name of the widget.
153 *
154 * @return string
155 */
156 public function getName()
157 {
158 return $this->name;
159 }
160 /**
161 * Set the order of the widget.
162 *
163 * @param int $order eg. 5
164 * @return static
165 */
166 public function setOrder($order)
167 {
168 $this->order = (int) $order;
169 return $this;
170 }
171 /**
172 * Returns the order of the widget.
173 * @return int
174 */
175 public function getOrder()
176 {
177 return $this->order;
178 }
179 /**
180 * Defines whether a widget is enabled or not. For instance some widgets might not be available to every user or
181 * might depend on a setting (such as Ecommerce) of a site. In such a case you can perform any checks and then
182 * return `true` or `false`. If your report is only available to users having super user access you can do the
183 * following: `return Piwik::hasUserSuperUserAccess();`
184 * @return bool
185 */
186 public function isEnabled()
187 {
188 return $this->isEnabled;
189 }
190 /**
191 * Enable / disable the widget. See {@link isEnabled()}
192 *
193 * @param bool $isEnabled
194 * @return static
195 */
196 public function setIsEnabled($isEnabled)
197 {
198 $this->isEnabled = (bool) $isEnabled;
199 return $this;
200 }
201 /**
202 * Enables the widget. See {@link isEnabled()}
203 */
204 public function enable()
205 {
206 $this->setIsEnabled(\true);
207 }
208 /**
209 * Disables the widget. See {@link isEnabled()}
210 */
211 public function disable()
212 {
213 $this->setIsEnabled(\false);
214 }
215 /**
216 * This method checks whether the widget is available, see {@link isEnabled()}. If not, it triggers an exception
217 * containing a message that will be displayed to the user. You can overwrite this message in case you want to
218 * customize the error message. Eg.
219 * ```
220 * if (!$this->isEnabled()) {
221 * throw new Exception('Setting XYZ is not enabled or the user has not enough permission');
222 * }
223 * ```
224 * @throws \Exception
225 */
226 public function checkIsEnabled()
227 {
228 if (!$this->isEnabled()) {
229 // Some widgets are disabled when the user is not superuser. If the user is not logged in, we should
230 // prompt them to do this first rather than showing them the "widget not enabled" error
231 Access::getInstance()->checkUserIsNotAnonymous();
232 throw new Exception(Piwik::translate('General_ExceptionWidgetNotEnabled'));
233 }
234 }
235 /**
236 * Returns the unique id of an widget based on module, action and the set parameters.
237 *
238 * @return string
239 */
240 public function getUniqueId()
241 {
242 $parameters = $this->getParameters();
243 unset($parameters['module']);
244 unset($parameters['action']);
245 return \Piwik\Widget\WidgetsList::getWidgetUniqueId($this->getModule(), $this->getAction(), $parameters);
246 }
247 /**
248 * Sets the widget as not widgetizable {@link isWidgetizeable()}.
249 *
250 * @return static
251 */
252 public function setIsNotWidgetizable()
253 {
254 $this->isWidgetizable = \false;
255 return $this;
256 }
257 /**
258 * Sets the widget as widgetizable {@link isWidgetizeable()}.
259 *
260 * @return static
261 */
262 public function setIsWidgetizable()
263 {
264 $this->isWidgetizable = \true;
265 return $this;
266 }
267 /**
268 * Detect whether the widget is widgetizable meaning it won't be able to add it to the dashboard and it won't
269 * be possible to export the widget via an iframe if it is not widgetizable. This is usually not needed but useful
270 * when you eg want to display a widget within the Piwik UI but not want to have it widgetizable.
271 *
272 * @return bool
273 */
274 public function isWidgetizeable()
275 {
276 return $this->isWidgetizable;
277 }
278 /**
279 * If middleware parameters are specified, the corresponding action will be executed before showing the
280 * actual widget in the UI. Only if this action (can be a controller method or API method) returns JSON `true`
281 * the widget will be actually shown. It is similar to `isEnabled()` but the specified action is performed each
282 * time the widget is requested in the UI whereas `isEnabled` is only checked once on the initial page load when
283 * we load the initial list of widgets. So if your widget's visibility depends on archived data
284 * (aka idSite/period/date) you should specify middle parameters. This has mainly two reasons:
285 *
286 * - This way the initial page load time is faster as we won't have to request archived data on the initial page
287 * load for widgets that are potentially never shown.
288 * - We execute that action every time before showing it. As the initial list of widgets is loaded on page load
289 * it is possible that some archives have no data yet, but at a later time there might be actually archived data.
290 * As we never reload the initial list of widgets we would still not show the widget even there we should. Example:
291 * On page load there are no conversions, a few minutes later there might be conversions. As the middleware is
292 * executed before showing it, we detect correctly that there are now conversions whereas `isEnabled` is only
293 * checked once on the initial Piwik page load.
294 *
295 * @param array $parameters URL parameters eg array('module' => 'Goals', 'action' => 'Conversions')
296 * @return static
297 */
298 public function setMiddlewareParameters($parameters)
299 {
300 $this->middlewareParameters = $parameters;
301 return $this;
302 }
303 /**
304 * Get defined middleware parameters (if any).
305 *
306 * @return array
307 */
308 public function getMiddlewareParameters()
309 {
310 return $this->middlewareParameters;
311 }
312 /**
313 * Marks this widget as a "wide" widget that requires the full width.
314 *
315 * @return $this
316 */
317 public function setIsWide()
318 {
319 $this->isWide = \true;
320 return $this;
321 }
322 /**
323 * Detect whether the widget should be shown wide or not.
324 * @return bool
325 */
326 public function isWide()
327 {
328 return $this->isWide;
329 }
330 }
331