1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\Plugins\ImageGraph;
use Piwik\API\Request;
use Piwik\Common;
use Piwik\Config;
use Piwik\Container\StaticContainer;
use Piwik\Period;
use Piwik\Period\Range;
use Piwik\Piwik;
use Piwik\Scheduler\Scheduler;
use Piwik\Site;
use Piwik\Url;
use Piwik\Period\Factory as PeriodFactory;
class ImageGraph extends \Piwik\Plugin
{
private static $CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS = array(
'Referrers_getReferrerType',
);
// row evolution support not yet implemented for these APIs
private static $REPORTS_DISABLED_EVOLUTION_GRAPH = array(
'Referrers_getAll',
);
/**
* @see Piwik\Plugin::registerEvents
*/
public function registerEvents()
{
$hooks = array(
'API.getReportMetadata.end' => array('function' => 'getReportMetadata',
'after' => true),
);
return $hooks;
}
// Number of periods to plot on an evolution graph
const GRAPH_EVOLUTION_LAST_PERIODS = 30;
/**
* @param array $reports
* @param array $info
* @return mixed
*/
public function getReportMetadata(&$reports, $info)
{
$idSite = $info['idSite'];
// If only one website is selected, we add the Graph URL
if (empty($idSite)) {
return;
}
// in case API.getReportMetadata was not called with date/period we use sane defaults
if (empty($info['period'])) {
$info['period'] = 'day';
}
if (empty($info['date'])) {
$info['date'] = 'today';
}
// need two sets of period & date, one for single period graphs, one for multiple periods graphs
if (Period::isMultiplePeriod($info['date'], $info['period'])) {
$periodForMultiplePeriodGraph = $info['period'];
$dateForMultiplePeriodGraph = $info['date'];
$periodForSinglePeriodGraph = 'range';
$dateForSinglePeriodGraph = $info['date'];
} else {
$periodForSinglePeriodGraph = $info['period'];
$dateForSinglePeriodGraph = $info['date'];
$piwikSite = new Site($idSite);
if ($periodForSinglePeriodGraph == 'range') {
// for period=range, show the configured sub-periods
$periodForMultiplePeriodGraph = Config::getInstance()->General['graphs_default_period_to_plot_when_period_range'];
$dateForMultiplePeriodGraph = $dateForSinglePeriodGraph;
} else if ($info['period'] == 'day' || !Config::getInstance()->General['graphs_show_evolution_within_selected_period']) {
// for period=day, always show the last n days
// if graphs_show_evolution_within_selected_period=false, show the last n periods
$periodForMultiplePeriodGraph = $periodForSinglePeriodGraph;
$dateForMultiplePeriodGraph = Range::getRelativeToEndDate(
$periodForSinglePeriodGraph,
'last' . self::getDefaultGraphEvolutionLastPeriods(),
$dateForSinglePeriodGraph,
$piwikSite
);
} else {
// if graphs_show_evolution_within_selected_period=true, show the days within the period
// (except if the period is day, see above)
$periodForMultiplePeriodGraph = 'day';
$period = PeriodFactory::build($info['period'], $info['date']);
$start = $period->getDateStart()->toString();
$end = $period->getDateEnd()->toString();
$dateForMultiplePeriodGraph = $start . ',' . $end;
}
}
$token_auth = Common::getRequestVar('token_auth', false);
$segment = Request::getRawSegmentFromRequest();
/** @var Scheduler $scheduler */
$scheduler = StaticContainer::getContainer()->get('Piwik\Scheduler\Scheduler');
$isRunningTask = $scheduler->isRunningTask();
// add the idSubtable if it exists
$idSubtable = Common::getRequestVar('idSubtable', false);
$urlPrefix = "index.php?";
foreach ($reports as &$report) {
$reportModule = $report['module'];
$reportAction = $report['action'];
$reportUniqueId = $reportModule . '_' . $reportAction;
$parameters = array();
$parameters['module'] = 'API';
$parameters['method'] = 'ImageGraph.get';
$parameters['idSite'] = $idSite;
$parameters['apiModule'] = $reportModule;
$parameters['apiAction'] = $reportAction;
if (!empty($token_auth)) {
$parameters['token_auth'] = $token_auth;
}
// Forward custom Report parameters to the graph URL
if (!empty($report['parameters'])) {
$parameters = array_merge($parameters, $report['parameters']);
}
if (empty($report['dimension'])) {
$parameters['period'] = $periodForMultiplePeriodGraph;
$parameters['date'] = $dateForMultiplePeriodGraph;
} else {
$parameters['period'] = $periodForSinglePeriodGraph;
$parameters['date'] = $dateForSinglePeriodGraph;
}
if ($idSubtable !== false) {
$parameters['idSubtable'] = $idSubtable;
}
if (!empty($_GET['_restrictSitesToLogin']) && $isRunningTask) {
$parameters['_restrictSitesToLogin'] = $_GET['_restrictSitesToLogin'];
}
if (!empty($segment)) {
$parameters['segment'] = $segment;
}
$report['imageGraphUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters);
// thanks to API.getRowEvolution, reports with dimensions can now be plotted using an evolution graph
// however, most reports with a fixed set of dimension values are excluded
// this is done so Piwik Mobile and Scheduled Reports do not display them
$reportWithDimensionsSupportsEvolution = empty($report['constantRowsCount']) || in_array($reportUniqueId, self::$CONSTANT_ROW_COUNT_REPORT_EXCEPTIONS);
$reportSupportsEvolution = !in_array($reportUniqueId, self::$REPORTS_DISABLED_EVOLUTION_GRAPH);
if ($reportSupportsEvolution
&& $reportWithDimensionsSupportsEvolution
) {
$parameters['period'] = $periodForMultiplePeriodGraph;
$parameters['date'] = $dateForMultiplePeriodGraph;
$report['imageGraphEvolutionUrl'] = $urlPrefix . Url::getQueryStringFromParameters($parameters);
}
}
}
public static function getDefaultGraphEvolutionLastPeriods()
{
$lastPeriods = (int) Config::getInstance()->General['graphs_default_evolution_graph_last_days_amount'];
if ($lastPeriods <= 0) {
throw new \Exception("Invalid value '$lastPeriods' supplied for [General] graphs_default_evolution_graph_last_days_amount config.");
}
return $lastPeriods;
}
}
|