Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GenerateGraphHTML.php « ViewDataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dc7eacdc044cf79814a59b89a4d067a3de54d19f (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
/**
 * Piwik - Open source web analytics
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 * @category Piwik
 * @package Piwik
 */
use Piwik\Piwik;
use Piwik\Common;

/**
 * This class generates the HTML code to embed graphs in the page.
 * It doesn't call the API but simply prints the html snippet.
 *
 * @package Piwik
 * @subpackage Piwik_ViewDataTable
 */
abstract class Piwik_ViewDataTable_GenerateGraphHTML extends Piwik_ViewDataTable
{

    protected $width = '100%';
    protected $height = 250;
    protected $graphType = 'unknown';

    /**
     * Default constructor.
     */
    public function __construct()
    {
        parent::__construct();
        
        $this->dataTableTemplate = '@CoreHome/_dataTableGraph';
        $this->disableOffsetInformationAndPaginationControls();
        $this->disableExcludeLowPopulation();
        $this->disableSearchBox();
        $this->enableShowExportAsImageIcon();
    }
    
    public function init($currentControllerName,
                         $currentControllerAction,
                         $apiMethodToRequestDataTable,
                         $controllerActionCalledWhenRequestSubTable = null,
                         $defaultProperties = array())
    {
        parent::init($currentControllerName, $currentControllerAction, $apiMethodToRequestDataTable,
                     $controllerActionCalledWhenRequestSubTable, $defaultProperties);
        
        $this->parametersToModify = array(
            'viewDataTable' => $this->getViewDataTableIdToLoad(),
            // in the case this controller is being executed by another controller
            // eg. when being widgetized in an IFRAME
            // we need to put in the URL of the graph data the real module and action
            'module'        => $currentControllerName,
            'action'        => $currentControllerAction,
        );
    }

    public function enableShowExportAsImageIcon()
    {
        $this->viewProperties['show_export_as_image_icon'] = true;
    }

    public function addRowEvolutionSeriesToggle($initiallyShowAllMetrics)
    {
        $this->viewProperties['externalSeriesToggle'] = 'RowEvolutionSeriesToggle';
        $this->viewProperties['externalSeriesToggleShowAll'] = $initiallyShowAllMetrics;
    }

    /**
     * Sets parameters to modify in the future generated URL
     * @param array $array array('nameParameter' => $newValue, ...)
     */
    public function setParametersToModify($array)
    {
        $this->parametersToModify = array_merge($this->parametersToModify, $array);
    }
    
    /**
     * Returns the names of the view properties to send to GenerateGraphData instance.
     * Properties are passed via the $_GET array.
     * 
     * @return array
     */
    public function getViewPropertiesToForward()
    {
        return array('show_all_ticks', 'add_total_row');
    }

    /**
     * Show every x-axis tick instead of just every other one.
     */
    public function showAllTicks()
    {
        $this->viewProperties['show_all_ticks'] = 1;
    }

    /**
     * Adds a row to the report containing totals for contained metrics. Mainly useful
     * for evolution graphs where displaying the totals w/ the metrics is useful.
     */
    public function addTotalRow()
    {
        $this->viewProperties['add_total_row'] = 1;
    }

    /**
     * We persist the parametersToModify values in the javascript footer.
     * This is used by the "export links" that use the "date" attribute
     * from the json properties array in the datatable footer.
     * @return array
     */
    protected function getJavascriptVariablesToSet()
    {
        $original = parent::getJavascriptVariablesToSet();
        $originalViewDataTable = $original['viewDataTable'];

        $result = $this->parametersToModify + $original;
        $result['viewDataTable'] = $originalViewDataTable;

        return $result;
    }

    /**
     * @see Piwik_ViewDataTable::main()
     * @return null
     */
    public function main()
    {
        if ($this->mainAlreadyExecuted) {
            return;
        }
        $this->mainAlreadyExecuted = true;

        $this->view = $this->buildView();
    }

    protected function buildView()
    {
        // access control
        $idSite = Common::getRequestVar('idSite', 1, 'int');
        Piwik_API_Request::reloadAuthUsingTokenAuth();
        if (!Piwik::isUserHasViewAccess($idSite)) {
            throw new Exception(Piwik_TranslateException('General_ExceptionPrivilegeAccessWebsite', array("'view'", $idSite)));
        }

        // collect data
        $this->parametersToModify['action'] = $this->currentControllerAction;
        $this->graphData = $this->getGraphData();

        // build view
        $view = new Piwik_View($this->dataTableTemplate);

        $view->width = $this->width;
        $view->height = $this->height;
        $view->graphType = $this->graphType;

        $view->data = $this->graphData;
        $view->isDataAvailable = strpos($this->graphData, '"series":[]') === false;

        $view->javascriptVariablesToSet = $this->getJavascriptVariablesToSet();
        $view->properties = $this->getViewProperties();

        $view->reportDocumentation = $this->getReportDocumentation();

        // if it's likely that the report data for this data table has been purged,
        // set whether we should display a message to that effect.
        $view->showReportDataWasPurgedMessage = $this->hasReportBeenPurged();
        $view->deleteReportsOlderThan = Piwik_GetOption('delete_reports_older_than');

        return $view;
    }

    protected function getGraphData()
    {
        $saveGet = $_GET;

        $params = array_merge($this->getGenerateGraphDataParams(), $this->parametersToModify);
        foreach ($params as $key => $val) {
            // We do not forward filter data to the graph controller.
            // This would cause the graph to have filter_limit=5 set by default,
            // which would break them (graphs need the full dataset to build the "Others" aggregate value)
            if (strpos($key, 'filter_') !== false) {
                continue;
            }
            if (is_array($val)) {
                $val = implode(',', $val);
            }
            $_GET[$key] = $val;
        }
        $content = Piwik_FrontController::getInstance()->fetchDispatch($this->currentControllerName, $this->currentControllerAction, array());

        $_GET = $saveGet;

        return str_replace(array("\r", "\n"), '', $content);
    }
    
    private function getGenerateGraphDataParams()
    {
        $result = array();
        foreach ($this->getViewPropertiesToForward() as $name) {
            if (isset($this->viewProperties[$name])) {
                $result[$name] = $this->viewProperties[$name];
            }
        }
        return $result;
    }
}