PageRenderTime 52ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/ImageGraph/ImageGraph.php

https://github.com/CodeYellowBV/piwik
PHP | 161 lines | 114 code | 22 blank | 25 comment | 19 complexity | ad380731061054ec6832be785aca35e5 MD5 | raw file
Possible License(s): LGPL-3.0, JSON, MIT, GPL-3.0, LGPL-2.1, GPL-2.0, AGPL-1.0, BSD-2-Clause, BSD-3-Clause
  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\Plugins\ImageGraph;
  10. use Piwik\Common;
  11. use Piwik\Config;
  12. use Piwik\Period;
  13. use Piwik\Period\Range;
  14. use Piwik\Site;
  15. use Piwik\TaskScheduler;
  16. use Piwik\Url;
  17. class ImageGraph extends \Piwik\Plugin
  18. {
  19. public function getInformation()
  20. {
  21. $suffix = ' Debug: <a href="' . Url::getCurrentQueryStringWithParametersModified(
  22. array('module' => 'ImageGraph', 'action' => 'index')) . '">All images</a>';
  23. $info = parent::getInformation();
  24. $info['description'] .= ' ' . $suffix;
  25. return $info;
  26. }
  27. static private $CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS = array(
  28. 'Referrers_getReferrerType',
  29. );
  30. // row evolution support not yet implemented for these APIs
  31. static private $REPORTS_DISABLED_EVOLUTION_GRAPH = array(
  32. 'Referrers_getAll',
  33. );
  34. /**
  35. * @see Piwik\Plugin::getListHooksRegistered
  36. */
  37. public function getListHooksRegistered()
  38. {
  39. $hooks = array(
  40. 'API.getReportMetadata.end' => array('function' => 'getReportMetadata',
  41. 'after' => true),
  42. );
  43. return $hooks;
  44. }
  45. // Number of periods to plot on an evolution graph
  46. const GRAPH_EVOLUTION_LAST_PERIODS = 30;
  47. /**
  48. * @param array $reports
  49. * @param array $info
  50. * @return mixed
  51. */
  52. public function getReportMetadata(&$reports, $info)
  53. {
  54. $idSites = $info['idSites'];
  55. // If only one website is selected, we add the Graph URL
  56. if (count($idSites) != 1) {
  57. return;
  58. }
  59. $idSite = reset($idSites);
  60. // in case API.getReportMetadata was not called with date/period we use sane defaults
  61. if (empty($info['period'])) {
  62. $info['period'] = 'day';
  63. }
  64. if (empty($info['date'])) {
  65. $info['date'] = 'today';
  66. }
  67. // need two sets of period & date, one for single period graphs, one for multiple periods graphs
  68. if (Period::isMultiplePeriod($info['date'], $info['period'])) {
  69. $periodForMultiplePeriodGraph = $info['period'];
  70. $dateForMultiplePeriodGraph = $info['date'];
  71. $periodForSinglePeriodGraph = 'range';
  72. $dateForSinglePeriodGraph = $info['date'];
  73. } else {
  74. $periodForSinglePeriodGraph = $info['period'];
  75. $dateForSinglePeriodGraph = $info['date'];
  76. $piwikSite = new Site($idSite);
  77. if ($periodForSinglePeriodGraph == 'range') {
  78. $periodForMultiplePeriodGraph = Config::getInstance()->General['graphs_default_period_to_plot_when_period_range'];
  79. $dateForMultiplePeriodGraph = $dateForSinglePeriodGraph;
  80. } else {
  81. $periodForMultiplePeriodGraph = $periodForSinglePeriodGraph;
  82. $dateForMultiplePeriodGraph = Range::getRelativeToEndDate(
  83. $periodForSinglePeriodGraph,
  84. 'last' . self::GRAPH_EVOLUTION_LAST_PERIODS,
  85. $dateForSinglePeriodGraph,
  86. $piwikSite
  87. );
  88. }
  89. }
  90. $token_auth = Common::getRequestVar('token_auth', false);
  91. $urlPrefix = "index.php?";
  92. foreach ($reports as &$report) {
  93. $reportModule = $report['module'];
  94. $reportAction = $report['action'];
  95. $reportUniqueId = $reportModule . '_' . $reportAction;
  96. $parameters = array();
  97. $parameters['module'] = 'API';
  98. $parameters['method'] = 'ImageGraph.get';
  99. $parameters['idSite'] = $idSite;
  100. $parameters['apiModule'] = $reportModule;
  101. $parameters['apiAction'] = $reportAction;
  102. if (!empty($token_auth)) {
  103. $parameters['token_auth'] = $token_auth;
  104. }
  105. // Forward custom Report parameters to the graph URL
  106. if (!empty($report['parameters'])) {
  107. $parameters = array_merge($parameters, $report['parameters']);
  108. }
  109. if (empty($report['dimension'])) {
  110. $parameters['period'] = $periodForMultiplePeriodGraph;
  111. $parameters['date'] = $dateForMultiplePeriodGraph;
  112. } else {
  113. $parameters['period'] = $periodForSinglePeriodGraph;
  114. $parameters['date'] = $dateForSinglePeriodGraph;
  115. }
  116. // add the idSubtable if it exists
  117. $idSubtable = Common::getRequestVar('idSubtable', false);
  118. if ($idSubtable !== false) {
  119. $parameters['idSubtable'] = $idSubtable;
  120. }
  121. if (!empty($_GET['_restrictSitesToLogin']) && TaskScheduler::isTaskBeingExecuted()) {
  122. $parameters['_restrictSitesToLogin'] = $_GET['_restrictSitesToLogin'];
  123. }
  124. $report['imageGraphUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters);
  125. // thanks to API.getRowEvolution, reports with dimensions can now be plotted using an evolution graph
  126. // however, most reports with a fixed set of dimension values are excluded
  127. // this is done so Piwik Mobile and Scheduled Reports do not display them
  128. $reportWithDimensionsSupportsEvolution = empty($report['constantRowsCount']) || in_array($reportUniqueId, self::$CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS);
  129. $reportSupportsEvolution = !in_array($reportUniqueId, self::$REPORTS_DISABLED_EVOLUTION_GRAPH);
  130. if ($reportSupportsEvolution
  131. && $reportWithDimensionsSupportsEvolution
  132. ) {
  133. $parameters['period'] = $periodForMultiplePeriodGraph;
  134. $parameters['date'] = $dateForMultiplePeriodGraph;
  135. $report['imageGraphEvolutionUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters);
  136. }
  137. }
  138. }
  139. }