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

Factory.php « ViewDataTable « core - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7f4f32be0ad82ba0d32b46b6080ac799250220d1 (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
211
212
<?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\ViewDataTable;

use Piwik\Common;
use Piwik\Piwik;
use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;

/**
 * Provides a means of creating {@link Piwik\Plugin\ViewDataTable} instances by ID.
 *
 * ### Examples
 * 
 * **Creating a ViewDataTable for a report**
 * 
 *     // method in MyPlugin\Controller
 *     public function myReport()
 *     {
 *         $view = Factory::build('table', 'MyPlugin.myReport');
 *         $view->config->show_limit_control = true;
 *         $view->config->translations['myFancyMetric'] = "My Fancy Metric";
 *         return $view->render();
 *     }
 * 
 * **Displaying a report in another way**
 * 
 *     // method in MyPlugin\Controller
 *     // use the same data that's used in myReport() above, but transform it in some way before
 *     // displaying.
 *     public function myReportShownDifferently()
 *     {
 *         $view = Factory::build('table', 'MyPlugin.myReport', 'MyPlugin.myReportShownDifferently');
 *         $view->config->filters[] = array('MyMagicFilter', array('an arg', 'another arg'));
 *         return $view->render();
 *     }
 * 
 * **Force a report to be shown as a bar graph**
 * 
 *     // method in MyPlugin\Controller
 *     // force the myReport report to show as a bar graph if there is no viewDataTable query param,
 *     // even though it is configured to show as a table.
 *     public function myReportShownAsABarGraph()
 *     {
 *         $view = Factory::build('graphVerticalBar', 'MyPlugin.myReport', 'MyPlugin.myReportShownAsABarGraph',
 *                                $forceDefault = true);
 *         return $view->render();
 *     }
 * 
 *
 * @api
 */
class Factory
{
    /**
     * Cache for getDefaultTypeViewDataTable result.
     *
     * @var array
     */
    private static $defaultViewTypes = null;

    /**
     * Creates a {@link Piwik\Plugin\ViewDataTable} instance by ID. If the **viewDataTable** query parameter is set,
     * this parameter's value is used as the ID.
     *
     * See {@link Piwik\Plugin\ViewDataTable} to read about the visualizations that are packaged with Piwik.
     *
     * @param string|null $defaultType A ViewDataTable ID representing the default ViewDataTable type to use. If
     *                                 the **viewDataTable** query parameter is not found, this value is used as
     *                                 the ID of the ViewDataTable to create.
     *
     *                                 If a visualization type is configured for the report being displayed, it
     *                                 is used instead of the default type. (See {@hook ViewDataTable.getDefaultType}).
     *                                 If nothing is configured for the report and `null` is supplied for this
     *                                 argument, **table** is used.
     * @param bool|false|string $apiAction The API method for the report that will be displayed, eg,
     *                               `'UserSettings.getBrowser'`.
     * @param bool|false|string $controllerAction The controller name and action dedicated to displaying the report. This
     *                                       action is used when reloading reports or changing the report visualization.
     *                                       Defaulted to `$apiAction` if `false` is supplied.
     * @param bool $forceDefault If true, then the visualization type that was configured for the report will be
     *                           ignored and `$defaultType` will be used as the default.
     * @throws \Exception
     * @return \Piwik\Plugin\ViewDataTable
     */
    public static function build($defaultType = null, $apiAction = false, $controllerAction = false, $forceDefault = false)
    {
        if (false === $controllerAction) {
            $controllerAction = $apiAction;
        }

        $defaultViewType = self::getDefaultViewTypeForReport($apiAction);

        if (!$forceDefault && !empty($defaultViewType)) {
            $defaultType = $defaultViewType;
        }

        $isWidget = Common::getRequestVar('widget', '0', 'string');

        if (!empty($isWidget)) {
            $params = array();
        } else {
            $login  = Piwik::getCurrentUserLogin();
            $params = Manager::getViewDataTableParameters($login, $controllerAction);
        }

        $savedViewDataTable = false;
        if (!empty($params['viewDataTable'])) {
            $savedViewDataTable = $params['viewDataTable'];
        }

        $type = Common::getRequestVar('viewDataTable', $savedViewDataTable, 'string');

        // Common::getRequestVar removes backslashes from the defaultValue in case magic quotes are enabled.
        // therefore do not pass this as a default value to getRequestVar()
        if ('' === $type) {
            $type = $defaultType ? : HtmlTable::ID;
        }

        $visualizations = Manager::getAvailableViewDataTables();

        if (array_key_exists($type, $visualizations)) {
            return self::createViewDataTableInstance($visualizations[$type], $controllerAction, $apiAction, $params);
        }

        if (class_exists($type)) {
            return self::createViewDataTableInstance($type, $controllerAction, $apiAction, $params);
        }

        if (array_key_exists($defaultType, $visualizations)) {
            return self::createViewDataTableInstance($visualizations[$defaultType], $controllerAction, $apiAction, $params);
        }

        if (array_key_exists(HtmlTable::ID, $visualizations)) {
            return self::createViewDataTableInstance($visualizations[HtmlTable::ID], $controllerAction, $apiAction, $params);
        }

        throw new \Exception('No visualization found to render ViewDataTable');
    }

    /**
     * Returns the default viewDataTable ID to use when determining which visualization to use.
     */
    private static function getDefaultViewTypeForReport($apiAction)
    {
        $defaultViewTypes = self::getDefaultTypeViewDataTable();
        return isset($defaultViewTypes[$apiAction]) ? $defaultViewTypes[$apiAction] : false;
    }

    /**
     * Returns a list of default viewDataTables ID to use when determining which visualization to use for multiple
     * reports.
     */
    private static function getDefaultTypeViewDataTable()
    {
        if (null === self::$defaultViewTypes) {
            self::$defaultViewTypes = array();
            /**
             * Triggered when gathering the default view types for all available reports.
             * 
             * If you define your own report, you may want to subscribe to this event to
             * make sure the correct default Visualization is used (for example, a pie graph,
             * bar graph, or something else).
             *
             * If there is no default type associated with a report, the **table** visualization
             * used.
             * 
             * **Example**
             * 
             *     public function getDefaultTypeViewDataTable(&$defaultViewTypes)
             *     {
             *         $defaultViewTypes['Referrers.getSocials']       = HtmlTable::ID;
             *         $defaultViewTypes['Referrers.getUrlsForSocial'] = Pie::ID;
             *     }
             * 
             * @param array &$defaultViewTypes The array mapping report IDs with visualization IDs.
             */
            Piwik::postEvent('ViewDataTable.getDefaultType', array(&self::$defaultViewTypes));
        }

        return self::$defaultViewTypes;
    }

    /**
     * @param string $klass
     * @param string $controllerAction
     * @param string $apiAction
     * @param array $params
     *
     * @internal param string $viewDataTableId
     * @return \Piwik\Plugin\ViewDataTable
     */
    private static function createViewDataTableInstance($klass, $controllerAction, $apiAction, $params)
    {
        if (empty($params)) {
            $params = array();
        }

        if (!is_subclass_of($klass, 'Piwik\Plugin\Visualization')) {
            // for now we ignore those params in case it is not a visualization. We do not want to apply
            // any of those saved parameters to sparklines etc. Need to find a better solution here
            $params = array();
        }

        return new $klass($controllerAction, $apiAction, $params);
    }
}