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