Widget.php
3 months ago
WidgetConfig.php
1 month ago
WidgetContainerConfig.php
3 months ago
WidgetsList.php
1 month ago
WidgetConfig.php
389 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 $clientSideComponent = array(); |
| 29 | protected $clientSideProps = array(); |
| 30 | protected $name = ''; |
| 31 | protected $order = 99; |
| 32 | protected $isEnabled = \true; |
| 33 | protected $isWidgetizable = \true; |
| 34 | protected $isWide = \false; |
| 35 | /** |
| 36 | * Set the id of the category the widget belongs to. |
| 37 | * @param string $categoryId Usually a translation key, eg 'General_Visits', 'Goals_Goals', ... |
| 38 | * @return static |
| 39 | */ |
| 40 | public function setCategoryId($categoryId) |
| 41 | { |
| 42 | $this->categoryId = $categoryId; |
| 43 | return $this; |
| 44 | } |
| 45 | /** |
| 46 | * Get the id of the category the widget belongs to. |
| 47 | * @return string |
| 48 | */ |
| 49 | public function getCategoryId() |
| 50 | { |
| 51 | return $this->categoryId; |
| 52 | } |
| 53 | /** |
| 54 | * Set the id of the subcategory the widget belongs to. If a subcategory is specified, the widget |
| 55 | * will be shown in the Piwik reporting UI. The subcategoryId will be used as a translation key for |
| 56 | * the submenu item. |
| 57 | * |
| 58 | * @param string $subcategoryId Usually a translation key, eg 'General_Overview', 'Actions_Pages', ... |
| 59 | * @return static |
| 60 | */ |
| 61 | public function setSubcategoryId($subcategoryId) |
| 62 | { |
| 63 | $this->subcategoryId = $subcategoryId; |
| 64 | return $this; |
| 65 | } |
| 66 | /** |
| 67 | * Get the currently set category ID. |
| 68 | * @return string |
| 69 | */ |
| 70 | public function getSubcategoryId() |
| 71 | { |
| 72 | return $this->subcategoryId; |
| 73 | } |
| 74 | /** |
| 75 | * Set the module (aka plugin name) of the widget. The correct module is usually detected automatically and |
| 76 | * not needed to be configured manually. |
| 77 | * |
| 78 | * @param string $module eg 'CoreHome' |
| 79 | * @return static |
| 80 | */ |
| 81 | public function setModule($module) |
| 82 | { |
| 83 | $this->module = $module; |
| 84 | return $this; |
| 85 | } |
| 86 | public function getModule() |
| 87 | { |
| 88 | return $this->module; |
| 89 | } |
| 90 | /** |
| 91 | * Set the action of the widget that shall be used in the URL to render the widget. |
| 92 | * The correct action is usually detected automatically and not needed to be configured manually. |
| 93 | * |
| 94 | * @param string $action eg 'renderMyWidget' |
| 95 | * @return static |
| 96 | */ |
| 97 | public function setAction($action) |
| 98 | { |
| 99 | $this->action = $action; |
| 100 | return $this; |
| 101 | } |
| 102 | /** |
| 103 | * Get the currently set action. |
| 104 | * @return string |
| 105 | */ |
| 106 | public function getAction() |
| 107 | { |
| 108 | return $this->action; |
| 109 | } |
| 110 | /** |
| 111 | * Sets (overwrites) the parameters of the widget. These parameters will be added to the URL when rendering the |
| 112 | * widget. You can access these parameters via `Piwik\Common::getRequestVar(...)`. |
| 113 | * |
| 114 | * This applies to widgets rendered through their controller/action request. Client-rendered widgets do not receive |
| 115 | * these parameters automatically and should instead derive request state from the browser context or load data via |
| 116 | * API requests. |
| 117 | * |
| 118 | * @param array $parameters eg. ('urlparam' => 'urlvalue') |
| 119 | * @return static |
| 120 | */ |
| 121 | public function setParameters($parameters) |
| 122 | { |
| 123 | $this->parameters = $parameters; |
| 124 | return $this; |
| 125 | } |
| 126 | /** |
| 127 | * Add new parameters and only overwrite parameters that have the same name. See {@link setParameters()} |
| 128 | * |
| 129 | * Like {@link setParameters()}, these parameters are only used for widgets rendered through their |
| 130 | * controller/action request and are not forwarded automatically to client-rendered widgets. |
| 131 | * |
| 132 | * @param array $parameters eg. ('urlparam' => 'urlvalue') |
| 133 | * @return static |
| 134 | */ |
| 135 | public function addParameters($parameters) |
| 136 | { |
| 137 | $this->parameters = array_merge($this->parameters, $parameters); |
| 138 | return $this; |
| 139 | } |
| 140 | /** |
| 141 | * Get all URL parameters needed to render this widget. |
| 142 | * @return array Eg ('urlparam' => 'urlvalue'). |
| 143 | */ |
| 144 | public function getParameters() |
| 145 | { |
| 146 | $defaultParams = array('module' => $this->getModule(), 'action' => $this->getAction()); |
| 147 | return $defaultParams + $this->parameters; |
| 148 | } |
| 149 | /** |
| 150 | * Set the name of the widget. |
| 151 | * |
| 152 | * @param string $name Usually a translation key, eg 'VisitTime_ByServerTimeWidgetName' |
| 153 | * @return static |
| 154 | */ |
| 155 | public function setName($name) |
| 156 | { |
| 157 | $this->name = $name; |
| 158 | return $this; |
| 159 | } |
| 160 | /** |
| 161 | * Get the name of the widget. |
| 162 | * |
| 163 | * @return string |
| 164 | */ |
| 165 | public function getName() |
| 166 | { |
| 167 | return $this->name; |
| 168 | } |
| 169 | /** |
| 170 | * Set the order of the widget. |
| 171 | * |
| 172 | * @param int $order eg. 5 |
| 173 | * @return static |
| 174 | */ |
| 175 | public function setOrder($order) |
| 176 | { |
| 177 | $this->order = (int) $order; |
| 178 | return $this; |
| 179 | } |
| 180 | /** |
| 181 | * Returns the order of the widget. |
| 182 | * @return int |
| 183 | */ |
| 184 | public function getOrder() |
| 185 | { |
| 186 | return $this->order; |
| 187 | } |
| 188 | /** |
| 189 | * Defines whether a widget is enabled or not. For instance some widgets might not be available to every user or |
| 190 | * might depend on a setting (such as Ecommerce) of a site. In such a case you can perform any checks and then |
| 191 | * return `true` or `false`. If your report is only available to users having super user access you can do the |
| 192 | * following: `return Piwik::hasUserSuperUserAccess();` |
| 193 | * @return bool |
| 194 | */ |
| 195 | public function isEnabled() |
| 196 | { |
| 197 | return $this->isEnabled; |
| 198 | } |
| 199 | /** |
| 200 | * Enable / disable the widget. See {@link isEnabled()} |
| 201 | * |
| 202 | * @param bool $isEnabled |
| 203 | * @return static |
| 204 | */ |
| 205 | public function setIsEnabled($isEnabled) |
| 206 | { |
| 207 | $this->isEnabled = (bool) $isEnabled; |
| 208 | return $this; |
| 209 | } |
| 210 | /** |
| 211 | * Enables the widget. See {@link isEnabled()} |
| 212 | */ |
| 213 | public function enable() |
| 214 | { |
| 215 | $this->setIsEnabled(\true); |
| 216 | } |
| 217 | /** |
| 218 | * Disables the widget. See {@link isEnabled()} |
| 219 | */ |
| 220 | public function disable() |
| 221 | { |
| 222 | $this->setIsEnabled(\false); |
| 223 | } |
| 224 | /** |
| 225 | * This method checks whether the widget is available, see {@link isEnabled()}. If not, it triggers an exception |
| 226 | * containing a message that will be displayed to the user. You can overwrite this message in case you want to |
| 227 | * customize the error message. Eg. |
| 228 | * ``` |
| 229 | * if (!$this->isEnabled()) { |
| 230 | * throw new Exception('Setting XYZ is not enabled or the user has not enough permission'); |
| 231 | * } |
| 232 | * ``` |
| 233 | * @throws \Exception |
| 234 | */ |
| 235 | public function checkIsEnabled() |
| 236 | { |
| 237 | if (!$this->isEnabled()) { |
| 238 | // Some widgets are disabled when the user is not superuser. If the user is not logged in, we should |
| 239 | // prompt them to do this first rather than showing them the "widget not enabled" error |
| 240 | Access::getInstance()->checkUserIsNotAnonymous(); |
| 241 | throw new Exception(Piwik::translate('General_ExceptionWidgetNotEnabled')); |
| 242 | } |
| 243 | } |
| 244 | /** |
| 245 | * Returns the unique id of an widget based on module, action and the set parameters. |
| 246 | * |
| 247 | * @return string |
| 248 | */ |
| 249 | public function getUniqueId() |
| 250 | { |
| 251 | $parameters = $this->getParameters(); |
| 252 | unset($parameters['module']); |
| 253 | unset($parameters['action']); |
| 254 | return \Piwik\Widget\WidgetsList::getWidgetUniqueId($this->getModule(), $this->getAction(), $parameters); |
| 255 | } |
| 256 | /** |
| 257 | * Sets the widget as not widgetizable {@link isWidgetizeable()}. |
| 258 | * |
| 259 | * @return static |
| 260 | */ |
| 261 | public function setIsNotWidgetizable() |
| 262 | { |
| 263 | $this->isWidgetizable = \false; |
| 264 | return $this; |
| 265 | } |
| 266 | /** |
| 267 | * Sets the widget as widgetizable {@link isWidgetizeable()}. |
| 268 | * |
| 269 | * @return static |
| 270 | */ |
| 271 | public function setIsWidgetizable() |
| 272 | { |
| 273 | $this->isWidgetizable = \true; |
| 274 | return $this; |
| 275 | } |
| 276 | /** |
| 277 | * Detect whether the widget is widgetizable meaning it won't be able to add it to the dashboard and it won't |
| 278 | * be possible to export the widget via an iframe if it is not widgetizable. This is usually not needed but useful |
| 279 | * when you eg want to display a widget within the Piwik UI but not want to have it widgetizable. |
| 280 | * |
| 281 | * @return bool |
| 282 | */ |
| 283 | public function isWidgetizeable() |
| 284 | { |
| 285 | return $this->isWidgetizable; |
| 286 | } |
| 287 | /** |
| 288 | * If middleware parameters are specified, the corresponding action will be executed before showing the |
| 289 | * actual widget in the UI. Only if this action (can be a controller method or API method) returns JSON `true` |
| 290 | * the widget will be actually shown. It is similar to `isEnabled()` but the specified action is performed each |
| 291 | * time the widget is requested in the UI whereas `isEnabled` is only checked once on the initial page load when |
| 292 | * we load the initial list of widgets. So if your widget's visibility depends on archived data |
| 293 | * (aka idSite/period/date) you should specify middle parameters. This has mainly two reasons: |
| 294 | * |
| 295 | * - This way the initial page load time is faster as we won't have to request archived data on the initial page |
| 296 | * load for widgets that are potentially never shown. |
| 297 | * - We execute that action every time before showing it. As the initial list of widgets is loaded on page load |
| 298 | * it is possible that some archives have no data yet, but at a later time there might be actually archived data. |
| 299 | * As we never reload the initial list of widgets we would still not show the widget even there we should. Example: |
| 300 | * On page load there are no conversions, a few minutes later there might be conversions. As the middleware is |
| 301 | * executed before showing it, we detect correctly that there are now conversions whereas `isEnabled` is only |
| 302 | * checked once on the initial Piwik page load. |
| 303 | * |
| 304 | * @param array $parameters URL parameters eg array('module' => 'Goals', 'action' => 'Conversions') |
| 305 | * @return static |
| 306 | */ |
| 307 | public function setMiddlewareParameters($parameters) |
| 308 | { |
| 309 | $this->middlewareParameters = $parameters; |
| 310 | return $this; |
| 311 | } |
| 312 | /** |
| 313 | * Get defined middleware parameters (if any). |
| 314 | * |
| 315 | * @return array |
| 316 | */ |
| 317 | public function getMiddlewareParameters() |
| 318 | { |
| 319 | return $this->middlewareParameters; |
| 320 | } |
| 321 | /** |
| 322 | * Marks this widget as client-rendered by a Vue component exported by the given plugin bundle. |
| 323 | * |
| 324 | * Client-rendered widgets do not execute the widget controller/action in a separate request before rendering. |
| 325 | * They should derive dynamic state from the current browser request or load data through API requests instead. |
| 326 | * |
| 327 | * @param string $plugin eg 'Transitions' |
| 328 | * @param string $component eg 'TransitionsPage' |
| 329 | * @return static |
| 330 | * @since 5.10.0 |
| 331 | */ |
| 332 | public function setClientSideComponent(string $plugin, string $component) |
| 333 | { |
| 334 | $this->clientSideComponent = array('plugin' => $plugin, 'name' => $component); |
| 335 | return $this; |
| 336 | } |
| 337 | /** |
| 338 | * Returns the configured client-rendered component definition. |
| 339 | * @return array{}|array{plugin: string, name: string} |
| 340 | * @since 5.10.0 |
| 341 | */ |
| 342 | public function getClientSideComponent() : array |
| 343 | { |
| 344 | return $this->clientSideComponent; |
| 345 | } |
| 346 | /** |
| 347 | * Sets static props that should be passed to the client-rendered Vue widget. |
| 348 | * |
| 349 | * Use this for configuration known when the widget is registered. Request-specific widget parameters are not |
| 350 | * forwarded to client-rendered widgets through this mechanism. |
| 351 | * |
| 352 | * @param array $props |
| 353 | * @return static |
| 354 | * @since 5.10.0 |
| 355 | */ |
| 356 | public function setClientSideProps(array $props) |
| 357 | { |
| 358 | $this->clientSideProps = $props; |
| 359 | return $this; |
| 360 | } |
| 361 | /** |
| 362 | * Returns props configured for the client-rendered Vue widget. |
| 363 | * @return array |
| 364 | * @since 5.10.0 |
| 365 | */ |
| 366 | public function getClientSideProps() : array |
| 367 | { |
| 368 | return $this->clientSideProps; |
| 369 | } |
| 370 | /** |
| 371 | * Marks this widget as a "wide" widget that requires the full width. |
| 372 | * |
| 373 | * @return $this |
| 374 | */ |
| 375 | public function setIsWide() |
| 376 | { |
| 377 | $this->isWide = \true; |
| 378 | return $this; |
| 379 | } |
| 380 | /** |
| 381 | * Detect whether the widget should be shown wide or not. |
| 382 | * @return bool |
| 383 | */ |
| 384 | public function isWide() |
| 385 | { |
| 386 | return $this->isWide; |
| 387 | } |
| 388 | } |
| 389 |