Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Factory.php 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /**
  3. * Piwik - free/libre analytics platform
  4. *
  5. * @link http://piwik.org
  6. * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
  7. *
  8. */
  9. namespace Piwik\ViewDataTable;
  10. use Piwik\Common;
  11. use Piwik\Piwik;
  12. use Piwik\Plugin\Report;
  13. use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
  14. /**
  15. * Provides a means of creating {@link Piwik\Plugin\ViewDataTable} instances by ID.
  16. *
  17. * ### Examples
  18. *
  19. * **Creating a ViewDataTable for a report**
  20. *
  21. * // method in MyPlugin\Controller
  22. * public function myReport()
  23. * {
  24. * $view = Factory::build('table', 'MyPlugin.myReport');
  25. * $view->config->show_limit_control = true;
  26. * $view->config->translations['myFancyMetric'] = "My Fancy Metric";
  27. * return $view->render();
  28. * }
  29. *
  30. * **Displaying a report in another way**
  31. *
  32. * // method in MyPlugin\Controller
  33. * // use the same data that's used in myReport() above, but transform it in some way before
  34. * // displaying.
  35. * public function myReportShownDifferently()
  36. * {
  37. * $view = Factory::build('table', 'MyPlugin.myReport', 'MyPlugin.myReportShownDifferently');
  38. * $view->config->filters[] = array('MyMagicFilter', array('an arg', 'another arg'));
  39. * return $view->render();
  40. * }
  41. *
  42. * **Force a report to be shown as a bar graph**
  43. *
  44. * // method in MyPlugin\Controller
  45. * // force the myReport report to show as a bar graph if there is no viewDataTable query param,
  46. * // even though it is configured to show as a table.
  47. * public function myReportShownAsABarGraph()
  48. * {
  49. * $view = Factory::build('graphVerticalBar', 'MyPlugin.myReport', 'MyPlugin.myReportShownAsABarGraph',
  50. * $forceDefault = true);
  51. * return $view->render();
  52. * }
  53. *
  54. *
  55. * @api
  56. */
  57. class Factory
  58. {
  59. /**
  60. * Cache for getDefaultTypeViewDataTable result.
  61. *
  62. * @var array
  63. */
  64. private static $defaultViewTypes = null;
  65. /**
  66. * Creates a {@link Piwik\Plugin\ViewDataTable} instance by ID. If the **viewDataTable** query parameter is set,
  67. * this parameter's value is used as the ID.
  68. *
  69. * See {@link Piwik\Plugin\ViewDataTable} to read about the visualizations that are packaged with Piwik.
  70. *
  71. * @param string|null $defaultType A ViewDataTable ID representing the default ViewDataTable type to use. If
  72. * the **viewDataTable** query parameter is not found, this value is used as
  73. * the ID of the ViewDataTable to create.
  74. *
  75. * If a visualization type is configured for the report being displayed, it
  76. * is used instead of the default type. (See {@hook ViewDataTable.getDefaultType}).
  77. * If nothing is configured for the report and `null` is supplied for this
  78. * argument, **table** is used.
  79. * @param bool|false|string $apiAction The API method for the report that will be displayed, eg,
  80. * `'UserSettings.getBrowser'`.
  81. * @param bool|false|string $controllerAction The controller name and action dedicated to displaying the report. This
  82. * action is used when reloading reports or changing the report visualization.
  83. * Defaulted to `$apiAction` if `false` is supplied.
  84. * @param bool $forceDefault If true, then the visualization type that was configured for the report will be
  85. * ignored and `$defaultType` will be used as the default.
  86. * @throws \Exception
  87. * @return \Piwik\Plugin\ViewDataTable
  88. */
  89. public static function build($defaultType = null, $apiAction = false, $controllerAction = false, $forceDefault = false)
  90. {
  91. if (false === $controllerAction) {
  92. $controllerAction = $apiAction;
  93. }
  94. $defaultViewType = self::getDefaultViewTypeForReport($apiAction);
  95. if (!$forceDefault && !empty($defaultViewType)) {
  96. $defaultType = $defaultViewType;
  97. }
  98. $isWidget = Common::getRequestVar('widget', '0', 'string');
  99. if (!empty($isWidget)) {
  100. $params = array();
  101. } else {
  102. $login = Piwik::getCurrentUserLogin();
  103. $params = Manager::getViewDataTableParameters($login, $controllerAction);
  104. }
  105. $savedViewDataTable = false;
  106. if (!empty($params['viewDataTable'])) {
  107. $savedViewDataTable = $params['viewDataTable'];
  108. }
  109. $type = Common::getRequestVar('viewDataTable', $savedViewDataTable, 'string');
  110. // Common::getRequestVar removes backslashes from the defaultValue in case magic quotes are enabled.
  111. // therefore do not pass this as a default value to getRequestVar()
  112. if ('' === $type) {
  113. $type = $defaultType ? : HtmlTable::ID;
  114. }
  115. $visualizations = Manager::getAvailableViewDataTables();
  116. if (array_key_exists($type, $visualizations)) {
  117. return self::createViewDataTableInstance($visualizations[$type], $controllerAction, $apiAction, $params);
  118. }
  119. if (class_exists($type)) {
  120. return self::createViewDataTableInstance($type, $controllerAction, $apiAction, $params);
  121. }
  122. if (array_key_exists($defaultType, $visualizations)) {
  123. return self::createViewDataTableInstance($visualizations[$defaultType], $controllerAction, $apiAction, $params);
  124. }
  125. if (array_key_exists(HtmlTable::ID, $visualizations)) {
  126. return self::createViewDataTableInstance($visualizations[HtmlTable::ID], $controllerAction, $apiAction, $params);
  127. }
  128. throw new \Exception('No visualization found to render ViewDataTable');
  129. }
  130. /**
  131. * Returns the default viewDataTable ID to use when determining which visualization to use.
  132. */
  133. private static function getDefaultViewTypeForReport($apiAction)
  134. {
  135. list($module, $action) = explode('.', $apiAction);
  136. $report = Report::factory($module, $action);
  137. if (!empty($report) && $report->isEnabled()) {
  138. return $report->getDefaultTypeViewDataTable();
  139. }
  140. $defaultViewTypes = self::getDefaultTypeViewDataTable();
  141. return isset($defaultViewTypes[$apiAction]) ? $defaultViewTypes[$apiAction] : false;
  142. }
  143. /**
  144. * Returns a list of default viewDataTables ID to use when determining which visualization to use for multiple
  145. * reports.
  146. */
  147. private static function getDefaultTypeViewDataTable()
  148. {
  149. if (null === self::$defaultViewTypes) {
  150. self::$defaultViewTypes = array();
  151. /**
  152. * @ignore
  153. */
  154. Piwik::postEvent('ViewDataTable.getDefaultType', array(&self::$defaultViewTypes));
  155. }
  156. return self::$defaultViewTypes;
  157. }
  158. /**
  159. * @param string $klass
  160. * @param string $controllerAction
  161. * @param string $apiAction
  162. * @param array $params
  163. *
  164. * @internal param string $viewDataTableId
  165. * @return \Piwik\Plugin\ViewDataTable
  166. */
  167. private static function createViewDataTableInstance($klass, $controllerAction, $apiAction, $params)
  168. {
  169. if (empty($params)) {
  170. $params = array();
  171. }
  172. if (!is_subclass_of($klass, 'Piwik\Plugin\Visualization')) {
  173. // for now we ignore those params in case it is not a visualization. We do not want to apply
  174. // any of those saved parameters to sparklines etc. Need to find a better solution here
  175. $params = array();
  176. }
  177. return new $klass($controllerAction, $apiAction, $params);
  178. }
  179. }