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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
|
<?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\Plugin;
use Piwik\API\Proxy;
use Piwik\API\Request;
use Piwik\Cache;
use Piwik\Columns\Dimension;
use Piwik\Common;
use Piwik\DataTable;
use Piwik\DataTable\Filter\Sort;
use Piwik\Metrics;
use Piwik\Cache as PiwikCache;
use Piwik\Piwik;
use Piwik\Plugins\CoreVisualizations\Visualizations\HtmlTable;
use Piwik\Plugins\CoreVisualizations\Visualizations\JqplotGraph\Evolution;
use Piwik\ViewDataTable\Factory as ViewDataTableFactory;
use Exception;
use Piwik\Widget\WidgetsList;
use Piwik\Report\ReportWidgetFactory;
/**
* Defines a new report. This class contains all information a report defines except the corresponding API method which
* needs to be defined in the 'API.php'. You can define the name of the report, a documentation, the supported metrics,
* how the report should be displayed, which features the report has (eg search) and much more.
*
* You can create a new report using the console command `./console generate:report`. The generated report will guide
* you through the creation of a report.
*
* @since 2.5.0
* @api
*/
class Report
{
/**
* The sub-namespace name in a plugin where Report components are stored.
*/
const COMPONENT_SUBNAMESPACE = 'Reports';
/**
* When added to the menu, a given report eg 'getCampaigns'
* will be routed as &action=menuGetCampaigns
*/
const PREFIX_ACTION_IN_MENU = 'menu';
/**
* The name of the module which is supposed to be equal to the name of the plugin. The module is detected
* automatically.
* @var string
*/
protected $module;
/**
* The name of the action. The action is detected automatically depending on the file name. A corresponding action
* should exist in the API as well.
* @var string
*/
protected $action;
/**
* The translated name of the report. The name will be used for instance in the mobile app or if another report
* defines this report as a related report.
* @var string
* @api
*/
protected $name;
/**
* A translated documentation which explains the report.
* @var string
*/
protected $documentation;
/**
* The translation key of the category the report belongs to.
* @var string
* @api
*/
protected $categoryId;
/**
* The translation key of the subcategory the report belongs to.
* @var string
* @api
*/
protected $subcategoryId;
/**
* An array of supported metrics. Eg `array('nb_visits', 'nb_actions', ...)`. Defaults to the platform default
* metrics see {@link Metrics::getDefaultProcessedMetrics()}.
* @var array
* @api
*/
protected $metrics = array('nb_visits', 'nb_uniq_visitors', 'nb_actions', 'nb_users');
// for a little performance improvement we avoid having to call Metrics::getDefaultMetrics for each report
/**
* The processed metrics this report supports, eg `avg_time_on_site` or `nb_actions_per_visit`. Defaults to the
* platform default processed metrics, see {@link Metrics::getDefaultProcessedMetrics()}. Set it to boolean `false`
* if your report does not support any processed metrics at all. Otherwise an array of metric names.
* Eg `array('avg_time_on_site', 'nb_actions_per_visit', ...)`
* @var array
* @api
*/
protected $processedMetrics = array('nb_actions_per_visit', 'avg_time_on_site', 'bounce_rate', 'conversion_rate');
// for a little performance improvement we avoid having to call Metrics::getDefaultProcessedMetrics for each report
/**
* Set this property to true in case your report supports goal metrics. In this case, the goal metrics will be
* automatically added to the report metadata and the report will be displayed in the Goals UI.
* @var bool
* @api
*/
protected $hasGoalMetrics = false;
/**
* Set this property to false in case your report can't/shouldn't be flattened.
* In this case, flattener won't be applied even if parameter is provided in a request
* @var bool
* @api
*/
protected $supportsFlatten = true;
/**
* Set it to boolean `true` if your report always returns a constant count of rows, for instance always 24 rows
* for 1-24 hours.
* @var bool
* @api
*/
protected $constantRowsCount = false;
/**
* Set it to boolean `true` if this report is a subtable report and won't be used as a standalone report.
* @var bool
* @api
*/
protected $isSubtableReport = false;
/**
* Some reports may require additional URL parameters that need to be sent when a report is requested. For instance
* a "goal" report might need a "goalId": `array('idgoal' => 5)`.
* @var null|array
* @api
*/
protected $parameters = null;
/**
* An instance of a dimension if the report has one. You can create a new dimension using the Piwik console CLI tool
* if needed.
* @var \Piwik\Columns\Dimension
*/
protected $dimension;
/**
* The name of the API action to load a subtable if supported. The action has to be of the same module. For instance
* a report "getKeywords" might support a subtable "getSearchEngines" which shows how often a keyword was searched
* via a specific search engine.
* @var string
* @api
*/
protected $actionToLoadSubTables = '';
/**
* The order of the report. Depending on the order the report gets a different position in the list of widgets,
* the menu and the mobile app.
* @var int
* @api
*/
protected $order = 1;
/**
* Separator for building recursive labels (or paths)
* @var string
* @api
*/
protected $recursiveLabelSeparator = ' - ';
/**
* Default sort column. Either a column name or a column id.
*
* @var string|int
*/
protected $defaultSortColumn = 'nb_visits';
/**
* Default sort desc. If true will sort by default desc, if false will sort by default asc
*
* @var bool
*/
protected $defaultSortOrderDesc = true;
/**
* The constructur initializes the module, action and the default metrics. If you want to overwrite any of those
* values or if you want to do any work during initializing overwrite the method {@link init()}.
* @ignore
*/
final public function __construct()
{
$classname = get_class($this);
$parts = explode('\\', $classname);
if (5 === count($parts)) {
$this->module = $parts[2];
$this->action = lcfirst($parts[4]);
}
$this->init();
}
/**
* Here you can do any instance initialization and overwrite any default values. You should avoid doing time
* consuming initialization here and if possible delay as long as possible. An instance of this report will be
* created in most page requests.
* @api
*/
protected function init()
{
}
/**
* Defines whether a report is enabled or not. For instance some reports might not be available to every user or
* might depend on a setting (such as Ecommerce) of a site. In such a case you can perform any checks and then
* return `true` or `false`. If your report is only available to users having super user access you can do the
* following: `return Piwik::hasUserSuperUserAccess();`
* @return bool
* @api
*/
public function isEnabled()
{
return true;
}
/**
* This method checks whether the report is available, see {@isEnabled()}. If not, it triggers an exception
* containing a message that will be displayed to the user. You can overwrite this message in case you want to
* customize the error message. Eg.
* ```
* if (!$this->isEnabled()) {
* throw new Exception('Setting XYZ is not enabled or the user has not enough permission');
* }
* ```
* @throws \Exception
* @api
*/
public function checkIsEnabled()
{
if (!$this->isEnabled()) {
throw new Exception(Piwik::translate('General_ExceptionReportNotEnabled'));
}
}
/**
* Returns the id of the default visualization for this report. Eg 'table' or 'pie'. Defaults to the HTML table.
* @return string
* @api
*/
public function getDefaultTypeViewDataTable()
{
return HtmlTable::ID;
}
/**
* Returns if the default viewDataTable type should always be used. e.g. the type won't be changeable through config or url params.
* Defaults to false
* @return bool
*/
public function alwaysUseDefaultViewDataTable()
{
return false;
}
/**
* Here you can configure how your report should be displayed and which capabilities your report has. For instance
* whether your report supports a "search" or not. EG `$view->config->show_search = false`. You can also change the
* default request config. For instance you can change how many rows are displayed by default:
* `$view->requestConfig->filter_limit = 10;`. See {@link ViewDataTable} for more information.
* @param ViewDataTable $view
* @api
*/
public function configureView(ViewDataTable $view)
{
}
/**
* Renders a report depending on the configured ViewDataTable see {@link configureView()} and
* {@link getDefaultTypeViewDataTable()}. If you want to customize the render process or just render any custom view
* you can overwrite this method.
*
* @return string
* @throws \Exception In case the given API action does not exist yet.
* @api
*/
public function render()
{
$viewDataTable = Common::getRequestVar('viewDataTable', false, 'string');
$fixed = Common::getRequestVar('forceView', 0, 'int');
$module = $this->getModule();
$action = $this->getAction();
$apiProxy = Proxy::getInstance();
if (!$apiProxy->isExistingApiAction($module, $action)) {
throw new Exception("Invalid action name '$module' for '$action' plugin.");
}
$apiAction = $apiProxy->buildApiActionName($module, $action);
$view = ViewDataTableFactory::build($viewDataTable, $apiAction, $module . '.' . $action, $fixed);
return $view->render();
}
/**
*
* Processing a uniqueId for each report, can be used by UIs as a key to match a given report
* @return string
*/
public function getId()
{
$params = $this->getParameters();
$paramsKey = $this->getModule() . '.' . $this->getAction();
if (!empty($params)) {
foreach ($params as $key => $value) {
$paramsKey .= '_' . $key . '--' . $value;
}
}
return $paramsKey;
}
/**
* lets you add any amount of widgets for this report. If a report defines a {@link $categoryId} and a
* {@link $subcategoryId} a widget will be generated automatically.
*
* Example to add a widget manually by overwriting this method in your report:
* $widgetsList->addWidgetConfig($factory->createWidget());
*
* If you want to have the name and the order of the widget differently to the name and order of the report you can
* do the following:
* $widgetsList->addWidgetConfig($factory->createWidget()->setName('Custom')->setOrder(5));
*
* If you want to add a widget to any container defined by your plugin or by another plugin you can do
* this:
* $widgetsList->addToContainerWidget($containerId = 'Products', $factory->createWidget());
*
* @param WidgetsList $widgetsList
* @param ReportWidgetFactory $factory
* @api
*/
public function configureWidgets(WidgetsList $widgetsList, ReportWidgetFactory $factory)
{
if ($this->categoryId && $this->subcategoryId) {
$widgetsList->addWidgetConfig($factory->createWidget());
}
}
/**
* @ignore
* @see $recursiveLabelSeparator
*/
public function getRecursiveLabelSeparator()
{
return $this->recursiveLabelSeparator;
}
/**
* Returns an array of supported metrics and their corresponding translations. Eg `array('nb_visits' => 'Visits')`.
* By default the given {@link $metrics} are used and their corresponding translations are looked up automatically.
* If a metric is not translated, you should add the default metric translation for this metric using
* the {@hook Metrics.getDefaultMetricTranslations} event. If you want to overwrite any default metric translation
* you should overwrite this method, call this parent method to get all default translations and overwrite any
* custom metric translations.
* @return array
* @api
*/
public function getMetrics()
{
return $this->getMetricTranslations($this->metrics);
}
/**
* Returns the list of metrics required at minimum for a report factoring in the columns requested by
* the report requester.
*
* This will return all the metrics requested (or all the metrics in the report if nothing is requested)
* **plus** the metrics required to calculate the requested processed metrics.
*
* This method should be used in **Plugin.get** API methods.
*
* @param string[]|null $allMetrics The list of all available unprocessed metrics. Defaults to this report's
* metrics.
* @param string[]|null $restrictToColumns The requested columns.
* @return string[]
*/
public function getMetricsRequiredForReport($allMetrics = null, $restrictToColumns = null)
{
if (empty($allMetrics)) {
$allMetrics = $this->metrics;
}
if (empty($restrictToColumns)) {
$restrictToColumns = array_merge($allMetrics, array_keys($this->getProcessedMetrics()));
}
$processedMetricsById = $this->getProcessedMetricsById();
$metricsSet = array_flip($allMetrics);
$metrics = array();
foreach ($restrictToColumns as $column) {
if (isset($processedMetricsById[$column])) {
$metrics = array_merge($metrics, $processedMetricsById[$column]->getDependentMetrics());
} elseif (isset($metricsSet[$column])) {
$metrics[] = $column;
}
}
return array_unique($metrics);
}
/**
* Returns an array of supported processed metrics and their corresponding translations. Eg
* `array('nb_visits' => 'Visits')`. By default the given {@link $processedMetrics} are used and their
* corresponding translations are looked up automatically. If a metric is not translated, you should add the
* default metric translation for this metric using the {@hook Metrics.getDefaultMetricTranslations} event. If you
* want to overwrite any default metric translation you should overwrite this method, call this parent method to
* get all default translations and overwrite any custom metric translations.
* @return array|mixed
* @api
*/
public function getProcessedMetrics()
{
if (!is_array($this->processedMetrics)) {
return $this->processedMetrics;
}
return $this->getMetricTranslations($this->processedMetrics);
}
/**
* Returns the array of all metrics displayed by this report.
*
* @return array
* @api
*/
public function getAllMetrics()
{
$processedMetrics = $this->getProcessedMetrics() ?: array();
return array_keys(array_merge($this->getMetrics(), $processedMetrics));
}
/**
* Use this method to register metrics to process report totals.
*
* When a metric is registered, it will process the report total values and as a result show percentage values
* in the HTML Table reporting visualization.
*
* @return string[] metricId => metricColumn, if the report has only column names and no IDs, it should return
* metricColumn => metricColumn, eg array('13' => 'nb_pageviews') or array('mymetric' => 'mymetric')
*/
public function getMetricNamesToProcessReportTotals()
{
return array();
}
/**
* Returns an array of metric documentations and their corresponding translations. Eg
* `array('nb_visits' => 'If a visitor comes to your website for the first time or if they visit a page more than 30 minutes after...')`.
* By default the given {@link $metrics} are used and their corresponding translations are looked up automatically.
* If there is a metric documentation not found, you should add the default metric documentation translation for
* this metric using the {@hook Metrics.getDefaultMetricDocumentationTranslations} event. If you want to overwrite
* any default metric translation you should overwrite this method, call this parent method to get all default
* translations and overwrite any custom metric translations.
* @return array
* @api
*/
protected function getMetricsDocumentation()
{
$translations = Metrics::getDefaultMetricsDocumentation();
$documentation = array();
foreach ($this->metrics as $metric) {
if (!empty($translations[$metric])) {
$documentation[$metric] = $translations[$metric];
}
}
$processedMetrics = $this->processedMetrics ?: array();
foreach ($processedMetrics as $processedMetric) {
if (is_string($processedMetric) && !empty($translations[$processedMetric])) {
$documentation[$processedMetric] = $translations[$processedMetric];
} elseif ($processedMetric instanceof ProcessedMetric) {
$name = $processedMetric->getName();
$metricDocs = $processedMetric->getDocumentation();
if (empty($metricDocs)) {
$metricDocs = @$translations[$name];
}
if (!empty($metricDocs)) {
$documentation[$processedMetric->getName()] = $metricDocs;
}
}
}
return $documentation;
}
/**
* @return bool
* @ignore
*/
public function hasGoalMetrics()
{
return $this->hasGoalMetrics;
}
/**
* @return bool
* @ignore
*/
public function supportsFlatten()
{
return $this->supportsFlatten;
}
/**
* If the report is enabled the report metadata for this report will be built and added to the list of available
* reports. Overwrite this method and leave it empty in case you do not want your report to be added to the report
* metadata. In this case your report won't be visible for instance in the mobile app and scheduled reports
* generator. We recommend to change this behavior only if you are familiar with the Piwik core. `$infos` contains
* the current requested date, period and site.
* @param $availableReports
* @param $infos
* @api
*/
public function configureReportMetadata(&$availableReports, $infos)
{
if (!$this->isEnabled()) {
return;
}
$report = $this->buildReportMetadata();
if (!empty($report)) {
$availableReports[] = $report;
}
}
/**
* Get report documentation.
* @return string
*/
public function getDocumentation()
{
return $this->documentation;
}
/**
* Builts the report metadata for this report. Can be useful in case you want to change the behavior of
* {@link configureReportMetadata()}.
* @return array
* @ignore
*
* TODO we should move this out to API::getReportMetadata
*/
protected function buildReportMetadata()
{
$report = array(
'category' => $this->getCategoryId(),
'subcategory' => $this->getSubcategoryId(),
'name' => $this->getName(),
'module' => $this->getModule(),
'action' => $this->getAction()
);
if (null !== $this->parameters) {
$report['parameters'] = $this->parameters;
}
if (!empty($this->dimension)) {
$report['dimension'] = $this->dimension->getName();
}
if (!empty($this->documentation)) {
$report['documentation'] = $this->documentation;
}
if (true === $this->isSubtableReport) {
$report['isSubtableReport'] = $this->isSubtableReport;
}
$report['metrics'] = $this->getMetrics();
$report['metricsDocumentation'] = $this->getMetricsDocumentation();
$report['processedMetrics'] = $this->getProcessedMetrics();
if (!empty($this->actionToLoadSubTables)) {
$report['actionToLoadSubTables'] = $this->actionToLoadSubTables;
}
if (true === $this->constantRowsCount) {
$report['constantRowsCount'] = $this->constantRowsCount;
}
$relatedReports = $this->getRelatedReports();
if (!empty($relatedReports)) {
$report['relatedReports'] = array();
foreach ($relatedReports as $relatedReport) {
if (!empty($relatedReport)) {
$report['relatedReports'][] = array(
'name' => $relatedReport->getName(),
'module' => $relatedReport->getModule(),
'action' => $relatedReport->getAction()
);
}
}
}
$report['order'] = $this->order;
return $report;
}
/**
* @ignore
*/
public function getDefaultSortColumn()
{
return $this->defaultSortColumn;
}
/**
* @ignore
*/
public function getDefaultSortOrder()
{
if ($this->defaultSortOrderDesc) {
return Sort::ORDER_DESC;
}
return Sort::ORDER_ASC;
}
/**
* Get the list of related reports if there are any. They will be displayed for instance below a report as a
* recommended related report.
*
* @return Report[]
* @api
*/
public function getRelatedReports()
{
return array();
}
/**
* Get the name of the report
* @return string
* @ignore
*/
public function getName()
{
return $this->name;
}
/**
* Get the name of the module.
* @return string
* @ignore
*/
public function getModule()
{
return $this->module;
}
/**
* Get the name of the action.
* @return string
* @ignore
*/
public function getAction()
{
return $this->action;
}
public function getParameters()
{
return $this->parameters;
}
/**
* Get the translated name of the category the report belongs to.
* @return string
* @ignore
*/
public function getCategoryId()
{
return $this->categoryId;
}
/**
* Get the translated name of the subcategory the report belongs to.
* @return string
* @ignore
*/
public function getSubcategoryId()
{
return $this->subcategoryId;
}
/**
* @return \Piwik\Columns\Dimension
* @ignore
*/
public function getDimension()
{
return $this->dimension;
}
/**
* Returns the order of the report
* @return int
* @ignore
*/
public function getOrder()
{
return $this->order;
}
/**
* Get the action to load sub tables if one is defined.
* @return string
* @ignore
*/
public function getActionToLoadSubTables()
{
return $this->actionToLoadSubTables;
}
/**
* Returns the Dimension instance of this report's subtable report.
*
* @return Dimension|null The subtable report's dimension or null if there is subtable report or
* no dimension for the subtable report.
* @api
*/
public function getSubtableDimension()
{
if (empty($this->actionToLoadSubTables)) {
return null;
}
list($subtableReportModule, $subtableReportAction) = $this->getSubtableApiMethod();
$subtableReport = ReportsProvider::factory($subtableReportModule, $subtableReportAction);
if (empty($subtableReport)) {
return null;
}
return $subtableReport->getDimension();
}
/**
* Returns true if the report is for another report's subtable, false if otherwise.
*
* @return bool
*/
public function isSubtableReport()
{
return $this->isSubtableReport;
}
/**
* Fetches the report represented by this instance.
*
* @param array $paramOverride Query parameter overrides.
* @return DataTable
* @api
*/
public function fetch($paramOverride = array())
{
return Request::processRequest($this->module . '.' . $this->action, $paramOverride);
}
/**
* Fetches a subtable for the report represented by this instance.
*
* @param int $idSubtable The subtable ID.
* @param array $paramOverride Query parameter overrides.
* @return DataTable
* @api
*/
public function fetchSubtable($idSubtable, $paramOverride = array())
{
$paramOverride = array('idSubtable' => $idSubtable) + $paramOverride;
list($module, $action) = $this->getSubtableApiMethod();
return Request::processRequest($module . '.' . $action, $paramOverride);
}
private function getMetricTranslations($metricsToTranslate)
{
$translations = Metrics::getDefaultMetricTranslations();
$metrics = array();
foreach ($metricsToTranslate as $metric) {
if ($metric instanceof Metric) {
$metricName = $metric->getName();
$translation = $metric->getTranslatedName();
} else {
$metricName = $metric;
$translation = @$translations[$metric];
}
$metrics[$metricName] = $translation ?: $metricName;
}
return $metrics;
}
private function getSubtableApiMethod()
{
if (strpos($this->actionToLoadSubTables, '.') !== false) {
return explode('.', $this->actionToLoadSubTables);
} else {
return array($this->module, $this->actionToLoadSubTables);
}
}
/**
* Finds a top level report that provides stats for a specific Dimension.
*
* @param Dimension $dimension The dimension whose report we're looking for.
* @return Report|null The
* @api
*/
public static function getForDimension(Dimension $dimension)
{
return ComponentFactory::getComponentIf(__CLASS__, $dimension->getModule(), function (Report $report) use ($dimension) {
return !$report->isSubtableReport()
&& $report->getDimension()
&& $report->getDimension()->getId() == $dimension->getId();
});
}
/**
* Returns an array mapping the ProcessedMetrics served by this report by their string names.
*
* @return ProcessedMetric[]
*/
public function getProcessedMetricsById()
{
$processedMetrics = $this->processedMetrics ?: array();
$result = array();
foreach ($processedMetrics as $processedMetric) {
if ($processedMetric instanceof ProcessedMetric) { // instanceof check for backwards compatibility
$result[$processedMetric->getName()] = $processedMetric;
}
}
return $result;
}
/**
* Returns the Metrics that are displayed by a DataTable of a certain Report type.
*
* Includes ProcessedMetrics and Metrics.
*
* @param DataTable $dataTable
* @param Report|null $report
* @param string $baseType The base type each metric class needs to be of.
* @return Metric[]
* @api
*/
public static function getMetricsForTable(DataTable $dataTable, Report $report = null, $baseType = 'Piwik\\Plugin\\Metric')
{
$metrics = $dataTable->getMetadata(DataTable::EXTRA_PROCESSED_METRICS_METADATA_NAME) ?: array();
if (!empty($report)) {
$metrics = array_merge($metrics, $report->getProcessedMetricsById());
}
$result = array();
/** @var Metric $metric */
foreach ($metrics as $metric) {
if (!($metric instanceof $baseType)) {
continue;
}
$result[$metric->getName()] = $metric;
}
return $result;
}
/**
* Returns the ProcessedMetrics that should be computed and formatted for a DataTable of a
* certain report. The ProcessedMetrics returned are those specified by the Report metadata
* as well as the DataTable metadata.
*
* @param DataTable $dataTable
* @param Report|null $report
* @return ProcessedMetric[]
* @api
*/
public static function getProcessedMetricsForTable(DataTable $dataTable, Report $report = null)
{
return self::getMetricsForTable($dataTable, $report, 'Piwik\\Plugin\\ProcessedMetric');
}
}
|