Config.php
2 weeks ago
Factory.php
1 year ago
Manager.php
3 months ago
Request.php
6 months ago
RequestConfig.php
2 weeks ago
Factory.php
217 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\ViewDataTable; |
| 10 | |
| 11 | use Piwik\Common; |
| 12 | use Piwik\Piwik; |
| 13 | use Piwik\Plugin\Report; |
| 14 | use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable; |
| 15 | use Piwik\Plugin\ReportsProvider; |
| 16 | /** |
| 17 | * Provides a means of creating {@link Piwik\Plugin\ViewDataTable} instances by ID. |
| 18 | * |
| 19 | * ### Examples |
| 20 | * |
| 21 | * **Creating a ViewDataTable for a report** |
| 22 | * |
| 23 | * // method in MyPlugin\Controller |
| 24 | * public function myReport() |
| 25 | * { |
| 26 | * $view = Factory::build('table', 'MyPlugin.myReport'); |
| 27 | * $view->config->show_limit_control = true; |
| 28 | * $view->config->translations['myFancyMetric'] = "My Fancy Metric"; |
| 29 | * return $view->render(); |
| 30 | * } |
| 31 | * |
| 32 | * **Displaying a report in another way** |
| 33 | * |
| 34 | * // method in MyPlugin\Controller |
| 35 | * // use the same data that's used in myReport() above, but transform it in some way before |
| 36 | * // displaying. |
| 37 | * public function myReportShownDifferently() |
| 38 | * { |
| 39 | * $view = Factory::build('table', 'MyPlugin.myReport', 'MyPlugin.myReportShownDifferently'); |
| 40 | * $view->config->filters[] = array('MyMagicFilter', array('an arg', 'another arg')); |
| 41 | * return $view->render(); |
| 42 | * } |
| 43 | * |
| 44 | * **Force a report to be shown as a bar graph** |
| 45 | * |
| 46 | * // method in MyPlugin\Controller |
| 47 | * // force the myReport report to show as a bar graph if there is no viewDataTable query param, |
| 48 | * // even though it is configured to show as a table. |
| 49 | * public function myReportShownAsABarGraph() |
| 50 | * { |
| 51 | * $view = Factory::build('graphVerticalBar', 'MyPlugin.myReport', 'MyPlugin.myReportShownAsABarGraph', |
| 52 | * $forceDefault = true); |
| 53 | * return $view->render(); |
| 54 | * } |
| 55 | * |
| 56 | * |
| 57 | * @api |
| 58 | */ |
| 59 | class Factory |
| 60 | { |
| 61 | public const DEFAULT_VIEW = HtmlTable::ID; |
| 62 | /** |
| 63 | * Cache for getDefaultTypeViewDataTable result. |
| 64 | * |
| 65 | * @var array |
| 66 | */ |
| 67 | private static $defaultViewTypes = null; |
| 68 | /** |
| 69 | * Creates a {@link Piwik\Plugin\ViewDataTable} instance by ID. If the **viewDataTable** query parameter is set, |
| 70 | * this parameter's value is used as the ID. |
| 71 | * |
| 72 | * See {@link Piwik\Plugin\ViewDataTable} to read about the visualizations that are packaged with Piwik. |
| 73 | * |
| 74 | * @param string|null $defaultType A ViewDataTable ID representing the default ViewDataTable type to use. If |
| 75 | * the **viewDataTable** query parameter is not found, this value is used as |
| 76 | * the ID of the ViewDataTable to create. |
| 77 | * |
| 78 | * If a visualization type is configured for the report being displayed, it |
| 79 | * is used instead of the default type. (See {@hook ViewDataTable.getDefaultType}). |
| 80 | * If nothing is configured for the report and `null` is supplied for this |
| 81 | * argument, **table** is used. |
| 82 | * @param bool|false|string $apiAction The API method for the report that will be displayed, eg, |
| 83 | * `'DevicesDetection.getBrowsers'`. |
| 84 | * @param bool|false|string $controllerAction The controller name and action dedicated to displaying the report. This |
| 85 | * action is used when reloading reports or changing the report visualization. |
| 86 | * Defaulted to `$apiAction` if `false` is supplied. |
| 87 | * @param bool $forceDefault If true, then the visualization type that was configured for the report will be |
| 88 | * ignored and `$defaultType` will be used as the default. |
| 89 | * @param bool $loadViewDataTableParametersForUser Whether the per-user parameters for this user, this ViewDataTable and this Api action |
| 90 | * should be loaded from the user preferences and override the default params values. |
| 91 | * @throws \Exception |
| 92 | * @return \Piwik\Plugin\ViewDataTable |
| 93 | */ |
| 94 | public static function build($defaultType = null, $apiAction = \false, $controllerAction = \false, $forceDefault = \false, $loadViewDataTableParametersForUser = null) |
| 95 | { |
| 96 | if (\false === $controllerAction) { |
| 97 | $controllerAction = $apiAction; |
| 98 | } |
| 99 | $report = self::getReport($apiAction); |
| 100 | $defaultViewType = self::getDefaultViewTypeForReport($report, $apiAction); |
| 101 | $params = array(); |
| 102 | $containerId = Common::getRequestVar('containerId', '', 'string'); |
| 103 | if (!isset($loadViewDataTableParametersForUser)) { |
| 104 | $loadViewDataTableParametersForUser = $containerId != '' || '0' == Common::getRequestVar('widget', '0', 'string'); |
| 105 | } |
| 106 | if ($loadViewDataTableParametersForUser) { |
| 107 | $login = Piwik::getCurrentUserLogin(); |
| 108 | $paramsKey = $controllerAction; |
| 109 | if (!empty($report) && $controllerAction === $apiAction) { |
| 110 | $paramsKey = $report->getId(); |
| 111 | } |
| 112 | $params = \Piwik\ViewDataTable\Manager::getViewDataTableParameters($login, $paramsKey, $containerId); |
| 113 | } |
| 114 | if (!self::isDefaultViewTypeForReportFixed($report)) { |
| 115 | $savedViewDataTable = \false; |
| 116 | if (!empty($params['viewDataTable'])) { |
| 117 | $savedViewDataTable = $params['viewDataTable']; |
| 118 | } |
| 119 | // order of default viewDataTables' priority is: function specified default, saved default, configured default for report |
| 120 | // function specified default is preferred |
| 121 | // -> force default == true : defaultType ?: saved ?: defaultView |
| 122 | // -> force default == false : saved ?: defaultType ?: defaultView |
| 123 | if ($forceDefault) { |
| 124 | $defaultType = ($defaultType ?: $savedViewDataTable) ?: $defaultViewType; |
| 125 | } else { |
| 126 | $defaultType = ($savedViewDataTable ?: $defaultType) ?: $defaultViewType; |
| 127 | } |
| 128 | $type = Common::getRequestVar('viewDataTable', $defaultType, 'string'); |
| 129 | // Common::getRequestVar removes backslashes from the defaultValue in case magic quotes are enabled. |
| 130 | // therefore do not pass this as a default value to getRequestVar() |
| 131 | if ('' === $type) { |
| 132 | $type = $defaultType ?: self::DEFAULT_VIEW; |
| 133 | } |
| 134 | } else { |
| 135 | $type = $defaultType ?: $defaultViewType; |
| 136 | } |
| 137 | $params['viewDataTable'] = $type; |
| 138 | $visualizations = \Piwik\ViewDataTable\Manager::getAvailableViewDataTables(); |
| 139 | if (array_key_exists($type, $visualizations)) { |
| 140 | return self::createViewDataTableInstance($visualizations[$type], $controllerAction, $apiAction, $params); |
| 141 | } |
| 142 | if (array_key_exists($defaultType, $visualizations)) { |
| 143 | return self::createViewDataTableInstance($visualizations[$defaultType], $controllerAction, $apiAction, $params); |
| 144 | } |
| 145 | if (array_key_exists(self::DEFAULT_VIEW, $visualizations)) { |
| 146 | return self::createViewDataTableInstance($visualizations[self::DEFAULT_VIEW], $controllerAction, $apiAction, $params); |
| 147 | } |
| 148 | throw new \Exception('No visualization found to render ViewDataTable'); |
| 149 | } |
| 150 | /** |
| 151 | * Return the report object for the given apiAction |
| 152 | * @param $apiAction |
| 153 | * @return null|Report |
| 154 | */ |
| 155 | private static function getReport($apiAction) |
| 156 | { |
| 157 | if (strpos($apiAction, '.') === \false) { |
| 158 | return; |
| 159 | } |
| 160 | list($module, $action) = explode('.', $apiAction); |
| 161 | $report = ReportsProvider::factory($module, $action); |
| 162 | return $report; |
| 163 | } |
| 164 | /** |
| 165 | * Returns the default viewDataTable ID to use when determining which visualization to use. |
| 166 | * |
| 167 | * @param Report $report |
| 168 | * @param string $apiAction |
| 169 | * |
| 170 | * @return bool|string |
| 171 | */ |
| 172 | private static function getDefaultViewTypeForReport($report, $apiAction) |
| 173 | { |
| 174 | if (!empty($report) && $report->isEnabled()) { |
| 175 | return $report->getDefaultTypeViewDataTable(); |
| 176 | } |
| 177 | return \false; |
| 178 | } |
| 179 | /** |
| 180 | * Returns if the default viewDataTable ID to use is fixed. |
| 181 | * |
| 182 | * @param Report $report |
| 183 | * @return bool |
| 184 | */ |
| 185 | private static function isDefaultViewTypeForReportFixed($report) |
| 186 | { |
| 187 | if (!empty($report) && $report->isEnabled()) { |
| 188 | return $report->alwaysUseDefaultViewDataTable(); |
| 189 | } |
| 190 | return \false; |
| 191 | } |
| 192 | /** |
| 193 | * @param string $klass |
| 194 | * @param string $controllerAction |
| 195 | * @param string $apiAction |
| 196 | * @param array $params |
| 197 | * |
| 198 | * @internal param string $viewDataTableId |
| 199 | * @return \Piwik\Plugin\ViewDataTable |
| 200 | */ |
| 201 | private static function createViewDataTableInstance($klass, $controllerAction, $apiAction, $params) |
| 202 | { |
| 203 | if (empty($params)) { |
| 204 | $params = array(); |
| 205 | } |
| 206 | if (!is_subclass_of($klass, 'Piwik\\Plugin\\Visualization')) { |
| 207 | // for now we ignore those params in case it is not a visualization. We do not want to apply |
| 208 | // any of those saved parameters to sparklines etc. Need to find a better solution here |
| 209 | $params = array(); |
| 210 | } |
| 211 | if (!is_subclass_of($klass, 'Piwik\\View\\ViewInterface')) { |
| 212 | throw new \Exception("viewDataTable {$klass} must implement Piwik\\View\\ViewInterface interface."); |
| 213 | } |
| 214 | return new $klass($controllerAction, $apiAction, $params); |
| 215 | } |
| 216 | } |
| 217 |