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