Widget.php
3 months ago
WidgetConfig.php
1 month ago
WidgetContainerConfig.php
3 months ago
WidgetsList.php
1 month ago
WidgetsList.php
218 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\Container\StaticContainer; |
| 12 | use Piwik\Development; |
| 13 | use Piwik\Piwik; |
| 14 | use Piwik\Report\ReportWidgetFactory; |
| 15 | /** |
| 16 | * Manages the global list of reports that can be displayed as dashboard widgets. |
| 17 | * |
| 18 | * Widgets are added through the {@hook WidgetsList.addWidgets} and filtered through the {@hook Widgets.filterWidgets} |
| 19 | * event. Observers for this event should call the {@link addWidget()} method to add widgets or use any of the other |
| 20 | * methods to remove widgets. |
| 21 | * |
| 22 | * @api since Piwik 3.0.0 |
| 23 | */ |
| 24 | class WidgetsList |
| 25 | { |
| 26 | /** |
| 27 | * List of widgets |
| 28 | * |
| 29 | * @var WidgetConfig[] |
| 30 | */ |
| 31 | private $widgets = array(); |
| 32 | /** |
| 33 | * @var WidgetContainerConfig[] |
| 34 | */ |
| 35 | private $container; |
| 36 | /** |
| 37 | * @var array |
| 38 | */ |
| 39 | private $containerWidgets; |
| 40 | /** |
| 41 | * Adds a new widget to the widget config. Please make sure the widget is enabled before adding a widget as |
| 42 | * no such checks will be performed. |
| 43 | */ |
| 44 | public function addWidgetConfig(\Piwik\Widget\WidgetConfig $widget) |
| 45 | { |
| 46 | if ($widget instanceof \Piwik\Widget\WidgetContainerConfig) { |
| 47 | $this->addContainer($widget); |
| 48 | } elseif (Development::isEnabled()) { |
| 49 | $this->checkIsValidWidget($widget); |
| 50 | } |
| 51 | $this->widgets[] = $widget; |
| 52 | } |
| 53 | /** |
| 54 | * Add multiple widget configs at once. See {@link addWidgetConfig()}. |
| 55 | * |
| 56 | * @param WidgetConfig[] $widgets |
| 57 | */ |
| 58 | public function addWidgetConfigs($widgets) |
| 59 | { |
| 60 | foreach ($widgets as $widget) { |
| 61 | $this->addWidgetConfig($widget); |
| 62 | } |
| 63 | } |
| 64 | private function addContainer(\Piwik\Widget\WidgetContainerConfig $containerWidget) |
| 65 | { |
| 66 | $widgetId = $containerWidget->getId(); |
| 67 | $this->container[$widgetId] = $containerWidget; |
| 68 | // widgets were added to this container, but the container did not exist yet. |
| 69 | if (isset($this->containerWidgets[$widgetId])) { |
| 70 | foreach ($this->containerWidgets[$widgetId] as $widget) { |
| 71 | $containerWidget->addWidgetConfig($widget); |
| 72 | } |
| 73 | unset($this->containerWidgets[$widgetId]); |
| 74 | } |
| 75 | } |
| 76 | /** |
| 77 | * Get all added widget configs. |
| 78 | * |
| 79 | * @return WidgetConfig[] |
| 80 | */ |
| 81 | public function getWidgetConfigs() |
| 82 | { |
| 83 | return $this->widgets; |
| 84 | } |
| 85 | private function checkIsValidWidget(\Piwik\Widget\WidgetConfig $widget) |
| 86 | { |
| 87 | if (!$widget->getModule()) { |
| 88 | Development::error('No module is defined for added widget having name "' . $widget->getName()); |
| 89 | } |
| 90 | if (!$widget->getAction()) { |
| 91 | Development::error('No action is defined for added widget having name "' . $widget->getName()); |
| 92 | } |
| 93 | } |
| 94 | /** |
| 95 | * Add a widget to a widget container. It doesn't matter whether the container was added to this list already |
| 96 | * or whether the container is added later. As long as a container having the same containerId is added at |
| 97 | * some point the widget will be added to that container. If no container having this id is added the widget |
| 98 | * will not be recognized. |
| 99 | * |
| 100 | * @param string $containerId eg 'Products' or 'Contents'. See {@link WidgetContainerConfig::setId} |
| 101 | */ |
| 102 | public function addToContainerWidget($containerId, \Piwik\Widget\WidgetConfig $widget) |
| 103 | { |
| 104 | if (isset($this->container[$containerId])) { |
| 105 | $this->container[$containerId]->addWidgetConfig($widget); |
| 106 | } else { |
| 107 | if (!isset($this->containerWidgets[$containerId])) { |
| 108 | $this->containerWidgets[$containerId] = array(); |
| 109 | } |
| 110 | $this->containerWidgets[$containerId][] = $widget; |
| 111 | } |
| 112 | } |
| 113 | /** |
| 114 | * Removes one or more widgets from the widget list. |
| 115 | * |
| 116 | * @param string $widgetCategoryId The widget category id. Can be a translation token eg 'General_Visits' |
| 117 | * see {@link WidgetConfig::setCategoryId()}. |
| 118 | * @param string|false $widgetName The name of the widget to remove eg 'VisitTime_ByServerTimeWidgetName'. |
| 119 | * If not supplied, all widgets within that category will be removed. |
| 120 | */ |
| 121 | public function remove($widgetCategoryId, $widgetName = \false) |
| 122 | { |
| 123 | foreach ($this->widgets as $index => $widget) { |
| 124 | if ($widget->getCategoryId() === $widgetCategoryId) { |
| 125 | if (!$widgetName || $widget->getName() === $widgetName) { |
| 126 | unset($this->widgets[$index]); |
| 127 | } |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | /** |
| 132 | * Returns `true` if a widget exists in the widget list, `false` if otherwise. |
| 133 | * |
| 134 | * @param string $module The controller name of the widget. |
| 135 | * @param string $action The controller action of the widget. |
| 136 | * @return bool |
| 137 | */ |
| 138 | public function isDefined($module, $action) |
| 139 | { |
| 140 | foreach ($this->widgets as $widget) { |
| 141 | if ($widget->getModule() === $module && $widget->getAction() === $action) { |
| 142 | return \true; |
| 143 | } |
| 144 | } |
| 145 | return \false; |
| 146 | } |
| 147 | /** |
| 148 | * Get all widgets defined in the Piwik platform. |
| 149 | * @ignore |
| 150 | * @return static |
| 151 | */ |
| 152 | public static function get() |
| 153 | { |
| 154 | $list = new static(); |
| 155 | $widgets = StaticContainer::get('Piwik\\Plugin\\WidgetsProvider'); |
| 156 | $widgetContainerConfigs = $widgets->getWidgetContainerConfigs(); |
| 157 | foreach ($widgetContainerConfigs as $config) { |
| 158 | if ($config->isEnabled()) { |
| 159 | $list->addWidgetConfig($config); |
| 160 | } |
| 161 | } |
| 162 | $widgetConfigs = $widgets->getWidgetConfigs(); |
| 163 | foreach ($widgetConfigs as $widget) { |
| 164 | if ($widget->isEnabled()) { |
| 165 | $list->addWidgetConfig($widget); |
| 166 | } |
| 167 | } |
| 168 | $reports = StaticContainer::get('Piwik\\Plugin\\ReportsProvider'); |
| 169 | $reports = $reports->getAllReports(); |
| 170 | foreach ($reports as $report) { |
| 171 | if ($report->isEnabled()) { |
| 172 | $factory = new ReportWidgetFactory($report); |
| 173 | $report->configureWidgets($list, $factory); |
| 174 | } |
| 175 | } |
| 176 | /** |
| 177 | * Triggered to filter widgets. |
| 178 | * |
| 179 | * **Example** |
| 180 | * |
| 181 | * public function removeWidgetConfigs(Piwik\Widget\WidgetsList $list) |
| 182 | * { |
| 183 | * $list->remove($category='General_Visits'); // remove all widgets having this category |
| 184 | * } |
| 185 | * |
| 186 | * @param WidgetsList $list An instance of the WidgetsList. You can change the list of widgets this way. |
| 187 | */ |
| 188 | Piwik::postEvent('Widget.filterWidgets', array($list)); |
| 189 | return $list; |
| 190 | } |
| 191 | /** |
| 192 | * CAUTION! If you ever change this method, existing updates will fail as they currently use that method! |
| 193 | * If you change the output the uniqueId for existing widgets would not be found anymore |
| 194 | * |
| 195 | * Returns the unique id of an widget with the given parameters |
| 196 | * |
| 197 | * @param $controllerName |
| 198 | * @param $controllerAction |
| 199 | * @param array $customParameters |
| 200 | * @return string |
| 201 | */ |
| 202 | public static function getWidgetUniqueId($controllerName, $controllerAction, $customParameters = array()) |
| 203 | { |
| 204 | $widgetUniqueId = 'widget' . $controllerName . $controllerAction; |
| 205 | foreach ($customParameters as $name => $value) { |
| 206 | if (is_array($value)) { |
| 207 | // use 'Array' for backward compatibility; |
| 208 | // could we switch to using $value[0]? |
| 209 | $value = 'Array'; |
| 210 | } |
| 211 | $value = urlencode($value); |
| 212 | $value = str_replace('%', '', $value); |
| 213 | $widgetUniqueId .= $name . $value; |
| 214 | } |
| 215 | return $widgetUniqueId; |
| 216 | } |
| 217 | } |
| 218 |