StaticGraph
1 year ago
config
2 years ago
fonts
6 years ago
templates
6 years ago
API.php
1 year ago
Controller.php
2 years ago
ImageGraph.php
1 year ago
StaticGraph.php
1 year ago
ImageGraph.php
147 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\Plugins\ImageGraph; |
| 10 | |
| 11 | use Piwik\API\Request; |
| 12 | use Piwik\Common; |
| 13 | use Piwik\Config; |
| 14 | use Piwik\Container\StaticContainer; |
| 15 | use Piwik\Period; |
| 16 | use Piwik\Period\Range; |
| 17 | use Piwik\Scheduler\Scheduler; |
| 18 | use Piwik\Site; |
| 19 | use Piwik\Url; |
| 20 | use Piwik\Period\Factory as PeriodFactory; |
| 21 | class ImageGraph extends \Piwik\Plugin |
| 22 | { |
| 23 | private static $CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS = array('Referrers_getReferrerType'); |
| 24 | // row evolution support not yet implemented for these APIs |
| 25 | private static $REPORTS_DISABLED_EVOLUTION_GRAPH = array('Referrers_getAll'); |
| 26 | /** |
| 27 | * @see \Piwik\Plugin::registerEvents |
| 28 | */ |
| 29 | public function registerEvents() |
| 30 | { |
| 31 | $hooks = array('API.getReportMetadata.end' => array('function' => 'getReportMetadata', 'after' => \true)); |
| 32 | return $hooks; |
| 33 | } |
| 34 | // Number of periods to plot on an evolution graph |
| 35 | public const GRAPH_EVOLUTION_LAST_PERIODS = 30; |
| 36 | /** |
| 37 | * @param array $reports |
| 38 | * @param array $info |
| 39 | * @return mixed |
| 40 | */ |
| 41 | public function getReportMetadata(&$reports, $info) |
| 42 | { |
| 43 | $idSite = $info['idSite']; |
| 44 | // If only one website is selected, we add the Graph URL |
| 45 | if (empty($idSite) || !is_numeric($idSite)) { |
| 46 | return; |
| 47 | } |
| 48 | // in case API.getReportMetadata was not called with date/period we use sane defaults |
| 49 | if (empty($info['period'])) { |
| 50 | $info['period'] = 'day'; |
| 51 | } |
| 52 | if (empty($info['date'])) { |
| 53 | $info['date'] = 'today'; |
| 54 | } |
| 55 | // need two sets of period & date, one for single period graphs, one for multiple periods graphs |
| 56 | if (Period::isMultiplePeriod($info['date'], $info['period'])) { |
| 57 | $periodForMultiplePeriodGraph = $info['period']; |
| 58 | $dateForMultiplePeriodGraph = $info['date']; |
| 59 | $periodForSinglePeriodGraph = 'range'; |
| 60 | $dateForSinglePeriodGraph = $info['date']; |
| 61 | } else { |
| 62 | $periodForSinglePeriodGraph = $info['period']; |
| 63 | $dateForSinglePeriodGraph = $info['date']; |
| 64 | $piwikSite = new Site($idSite); |
| 65 | if ($periodForSinglePeriodGraph == 'range') { |
| 66 | // for period=range, show the configured sub-periods |
| 67 | $periodForMultiplePeriodGraph = Config::getInstance()->General['graphs_default_period_to_plot_when_period_range']; |
| 68 | $dateForMultiplePeriodGraph = $dateForSinglePeriodGraph; |
| 69 | } elseif ($info['period'] == 'day' || !Config::getInstance()->General['graphs_show_evolution_within_selected_period']) { |
| 70 | // for period=day, always show the last n days |
| 71 | // if graphs_show_evolution_within_selected_period=false, show the last n periods |
| 72 | $periodForMultiplePeriodGraph = $periodForSinglePeriodGraph; |
| 73 | $dateForMultiplePeriodGraph = Range::getRelativeToEndDate($periodForSinglePeriodGraph, 'last' . self::getDefaultGraphEvolutionLastPeriods(), $dateForSinglePeriodGraph, $piwikSite); |
| 74 | } else { |
| 75 | // if graphs_show_evolution_within_selected_period=true, show the days within the period |
| 76 | // (except if the period is day, see above) |
| 77 | $periodForMultiplePeriodGraph = 'day'; |
| 78 | $period = PeriodFactory::build($info['period'], $info['date']); |
| 79 | $start = $period->getDateStart()->toString(); |
| 80 | $end = $period->getDateEnd()->toString(); |
| 81 | $dateForMultiplePeriodGraph = $start . ',' . $end; |
| 82 | } |
| 83 | } |
| 84 | $token_auth = Common::getRequestVar('token_auth', \false); |
| 85 | $segment = Request::getRawSegmentFromRequest(); |
| 86 | /** @var Scheduler $scheduler */ |
| 87 | $scheduler = StaticContainer::getContainer()->get('Piwik\\Scheduler\\Scheduler'); |
| 88 | $isRunningTask = $scheduler->isRunningTask(); |
| 89 | // add the idSubtable if it exists |
| 90 | $idSubtable = Common::getRequestVar('idSubtable', \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 | if ($idSubtable !== \false) { |
| 117 | $parameters['idSubtable'] = $idSubtable; |
| 118 | } |
| 119 | if (!empty($_GET['_restrictSitesToLogin']) && $isRunningTask) { |
| 120 | $parameters['_restrictSitesToLogin'] = $_GET['_restrictSitesToLogin']; |
| 121 | } |
| 122 | if (!empty($segment)) { |
| 123 | $parameters['segment'] = $segment; |
| 124 | } |
| 125 | $report['imageGraphUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters); |
| 126 | // thanks to API.getRowEvolution, reports with dimensions can now be plotted using an evolution graph |
| 127 | // however, most reports with a fixed set of dimension values are excluded |
| 128 | // this is done so Piwik Mobile and Scheduled Reports do not display them |
| 129 | $reportWithDimensionsSupportsEvolution = empty($report['constantRowsCount']) || in_array($reportUniqueId, self::$CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS); |
| 130 | $reportSupportsEvolution = !in_array($reportUniqueId, self::$REPORTS_DISABLED_EVOLUTION_GRAPH); |
| 131 | if ($reportSupportsEvolution && $reportWithDimensionsSupportsEvolution) { |
| 132 | $parameters['period'] = $periodForMultiplePeriodGraph; |
| 133 | $parameters['date'] = $dateForMultiplePeriodGraph; |
| 134 | $report['imageGraphEvolutionUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | public static function getDefaultGraphEvolutionLastPeriods() |
| 139 | { |
| 140 | $lastPeriods = (int) Config::getInstance()->General['graphs_default_evolution_graph_last_days_amount']; |
| 141 | if ($lastPeriods <= 0) { |
| 142 | throw new \Exception("Invalid value '{$lastPeriods}' supplied for [General] graphs_default_evolution_graph_last_days_amount config."); |
| 143 | } |
| 144 | return $lastPeriods; |
| 145 | } |
| 146 | } |
| 147 |