PluginProbe ʕ •ᴥ•ʔ
Matomo Analytics – Powerful, Privacy-First Insights for WordPress / 5.0.3
Matomo Analytics – Powerful, Privacy-First Insights for WordPress v5.0.3
5.13.0 5.12.1 5.12.0 5.11.1 5.11.0 5.10.2 5.10.1 trunk 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.3.0 1.3.1 1.3.2 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.1.0 4.1.1 4.1.2 4.1.3 4.10.0 4.11.0 4.12.0 4.13.0 4.13.2 4.13.3 4.13.4 4.13.5 4.14.0 4.14.1 4.14.2 4.15.0 4.15.1 4.15.2 4.15.3 4.2.0 4.3.0 4.3.1 4.4.1 4.4.2 4.5.0 4.6.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 5.0.6 5.0.7 5.0.8 5.1.0 5.1.1 5.1.2 5.1.3 5.1.4 5.1.5 5.1.6 5.1.7 5.10.0 5.2.0 5.2.1 5.2.2 5.3.0 5.3.1 5.3.2 5.3.3 5.6.0 5.6.1 5.7.0 5.7.1 5.8.0 5.8.1 5.8.2
matomo / app / core / ViewDataTable / Factory.php
matomo / app / core / ViewDataTable Last commit date
Config.php 2 years ago Factory.php 2 years ago Manager.php 2 years ago Request.php 2 years ago RequestConfig.php 2 years ago
Factory.php
218 lines
1 <?php
2
3 /**
4 * Matomo - free/libre analytics platform
5 *
6 * @link https://matomo.org
7 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
8 *
9 */
10 namespace Piwik\ViewDataTable;
11
12 use Piwik\Common;
13 use Piwik\Piwik;
14 use Piwik\Plugin\Report;
15 use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
16 use Piwik\Plugin\ReportsProvider;
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 * Cache for getDefaultTypeViewDataTable result.
65 *
66 * @var array
67 */
68 private static $defaultViewTypes = null;
69 /**
70 * Creates a {@link Piwik\Plugin\ViewDataTable} instance by ID. If the **viewDataTable** query parameter is set,
71 * this parameter's value is used as the ID.
72 *
73 * See {@link Piwik\Plugin\ViewDataTable} to read about the visualizations that are packaged with Piwik.
74 *
75 * @param string|null $defaultType A ViewDataTable ID representing the default ViewDataTable type to use. If
76 * the **viewDataTable** query parameter is not found, this value is used as
77 * the ID of the ViewDataTable to create.
78 *
79 * If a visualization type is configured for the report being displayed, it
80 * is used instead of the default type. (See {@hook ViewDataTable.getDefaultType}).
81 * If nothing is configured for the report and `null` is supplied for this
82 * argument, **table** is used.
83 * @param bool|false|string $apiAction The API method for the report that will be displayed, eg,
84 * `'DevicesDetection.getBrowsers'`.
85 * @param bool|false|string $controllerAction The controller name and action dedicated to displaying the report. This
86 * action is used when reloading reports or changing the report visualization.
87 * Defaulted to `$apiAction` if `false` is supplied.
88 * @param bool $forceDefault If true, then the visualization type that was configured for the report will be
89 * ignored and `$defaultType` will be used as the default.
90 * @param bool $loadViewDataTableParametersForUser Whether the per-user parameters for this user, this ViewDataTable and this Api action
91 * should be loaded from the user preferences and override the default params values.
92 * @throws \Exception
93 * @return \Piwik\Plugin\ViewDataTable
94 */
95 public static function build($defaultType = null, $apiAction = false, $controllerAction = false, $forceDefault = false, $loadViewDataTableParametersForUser = null)
96 {
97 if (false === $controllerAction) {
98 $controllerAction = $apiAction;
99 }
100 $report = self::getReport($apiAction);
101 $defaultViewType = self::getDefaultViewTypeForReport($report, $apiAction);
102 $params = array();
103 $containerId = Common::getRequestVar('containerId', '', 'string');
104 if (!isset($loadViewDataTableParametersForUser)) {
105 $loadViewDataTableParametersForUser = $containerId != '' || '0' == Common::getRequestVar('widget', '0', 'string');
106 }
107 if ($loadViewDataTableParametersForUser) {
108 $login = Piwik::getCurrentUserLogin();
109 $paramsKey = $controllerAction;
110 if (!empty($report) && $controllerAction === $apiAction) {
111 $paramsKey = $report->getId();
112 }
113 $params = \Piwik\ViewDataTable\Manager::getViewDataTableParameters($login, $paramsKey, $containerId);
114 }
115 if (!self::isDefaultViewTypeForReportFixed($report)) {
116 $savedViewDataTable = false;
117 if (!empty($params['viewDataTable'])) {
118 $savedViewDataTable = $params['viewDataTable'];
119 }
120 // order of default viewDataTables' priority is: function specified default, saved default, configured default for report
121 // function specified default is preferred
122 // -> force default == true : defaultType ?: saved ?: defaultView
123 // -> force default == false : saved ?: defaultType ?: defaultView
124 if ($forceDefault) {
125 $defaultType = ($defaultType ?: $savedViewDataTable) ?: $defaultViewType;
126 } else {
127 $defaultType = ($savedViewDataTable ?: $defaultType) ?: $defaultViewType;
128 }
129 $type = Common::getRequestVar('viewDataTable', $defaultType, 'string');
130 // Common::getRequestVar removes backslashes from the defaultValue in case magic quotes are enabled.
131 // therefore do not pass this as a default value to getRequestVar()
132 if ('' === $type) {
133 $type = $defaultType ?: self::DEFAULT_VIEW;
134 }
135 } else {
136 $type = $defaultType ?: $defaultViewType;
137 }
138 $params['viewDataTable'] = $type;
139 $visualizations = \Piwik\ViewDataTable\Manager::getAvailableViewDataTables();
140 if (array_key_exists($type, $visualizations)) {
141 return self::createViewDataTableInstance($visualizations[$type], $controllerAction, $apiAction, $params);
142 }
143 if (array_key_exists($defaultType, $visualizations)) {
144 return self::createViewDataTableInstance($visualizations[$defaultType], $controllerAction, $apiAction, $params);
145 }
146 if (array_key_exists(self::DEFAULT_VIEW, $visualizations)) {
147 return self::createViewDataTableInstance($visualizations[self::DEFAULT_VIEW], $controllerAction, $apiAction, $params);
148 }
149 throw new \Exception('No visualization found to render ViewDataTable');
150 }
151 /**
152 * Return the report object for the given apiAction
153 * @param $apiAction
154 * @return null|Report
155 */
156 private static function getReport($apiAction)
157 {
158 if (strpos($apiAction, '.') === false) {
159 return;
160 }
161 list($module, $action) = explode('.', $apiAction);
162 $report = ReportsProvider::factory($module, $action);
163 return $report;
164 }
165 /**
166 * Returns the default viewDataTable ID to use when determining which visualization to use.
167 *
168 * @param Report $report
169 * @param string $apiAction
170 *
171 * @return bool|string
172 */
173 private static function getDefaultViewTypeForReport($report, $apiAction)
174 {
175 if (!empty($report) && $report->isEnabled()) {
176 return $report->getDefaultTypeViewDataTable();
177 }
178 return false;
179 }
180 /**
181 * Returns if the default viewDataTable ID to use is fixed.
182 *
183 * @param Report $report
184 * @return bool
185 */
186 private static function isDefaultViewTypeForReportFixed($report)
187 {
188 if (!empty($report) && $report->isEnabled()) {
189 return $report->alwaysUseDefaultViewDataTable();
190 }
191 return false;
192 }
193 /**
194 * @param string $klass
195 * @param string $controllerAction
196 * @param string $apiAction
197 * @param array $params
198 *
199 * @internal param string $viewDataTableId
200 * @return \Piwik\Plugin\ViewDataTable
201 */
202 private static function createViewDataTableInstance($klass, $controllerAction, $apiAction, $params)
203 {
204 if (empty($params)) {
205 $params = array();
206 }
207 if (!is_subclass_of($klass, 'Piwik\\Plugin\\Visualization')) {
208 // for now we ignore those params in case it is not a visualization. We do not want to apply
209 // any of those saved parameters to sparklines etc. Need to find a better solution here
210 $params = array();
211 }
212 if (!is_subclass_of($klass, 'Piwik\\View\\ViewInterface')) {
213 throw new \Exception("viewDataTable {$klass} must implement Piwik\\View\\ViewInterface interface.");
214 }
215 return new $klass($controllerAction, $apiAction, $params);
216 }
217 }
218