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