blob: 2be32c6394ccb441a774504038561bca933499ce (
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
|
<?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\DataTable\Filter;
use Piwik\DataTable\BaseFilter;
use Piwik\DataTable;
/**
* Converts for each row of a {@link DataTable} a segmentValue to a segmentFilter. The name of the segment
* is automatically detected based on the given report.
*
* **Basic usage example**
*
* $dataTable->filter('AddSegmentFilterBySegmentValue', array($reportInstance));
*
* @api
*/
class AddSegmentFilterBySegmentValue extends BaseFilter
{
/**
* @var \Piwik\Plugin\Report
*/
private $report;
/**
* @param DataTable $table
* @param $report
*/
public function __construct($table, $report)
{
parent::__construct($table);
$this->report = $report;
}
/**
* See {@link AddSegmentFilterBySegmentValue}.
*
* @param DataTable $table
* @return int The number of deleted rows.
*/
public function filter($table)
{
if (empty($this->report) || !$table->getRowsCount()) {
return;
}
$dimension = $this->report->getDimension();
if (empty($dimension)) {
return;
}
$segments = $dimension->getSegments();
if (empty($segments)) {
return;
}
/** @var \Piwik\Plugin\Segment $segment */
$segment = array_shift($segments);
$segmentName = $segment->getSegment();
foreach ($table->getRows() as $row) {
$value = $row->getMetadata('segmentValue');
$filter = $row->getMetadata('segmentFilter');
if ($value !== false && $filter === false) {
$row->setMetadata('segmentFilter', sprintf('%s==%s', $segmentName, urlencode($value)));
}
}
}
}
|