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
|
<?php
/**
* Piwik - free/libre analytics platform
*
* @link https://matomo.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*
*/
namespace Piwik\DataTable\Filter;
use Piwik\DataTable;
use Piwik\DataTable\BaseFilter;
use Piwik\Development;
/**
* Executes a filter for each row of a {@link DataTable} and generates a segment filter for each row.
*
* **Basic usage example**
*
* $dataTable->filter('AddSegmentByRangeLabel', array('segmentName'));
*
* @api
*/
class AddSegmentByRangeLabel extends BaseFilter
{
private $segments;
private $delimiter;
/**
* Generates a segment filter based on the label column and the given segment name
*
* @param DataTable $table
* @param string $segment one segment
*/
public function __construct($table, $segment)
{
parent::__construct($table);
$this->segment = $segment;
}
/**
* @param DataTable $table
*/
public function filter($table)
{
if (empty($this->segment)) {
$msg = 'AddSegmentByRangeLabel is called without having any segment defined';
Development::error($msg);
return;
}
foreach ($table->getRowsWithoutSummaryRow() as $key => $row) {
$label = $row->getColumn('label');
if (empty($label)) {
return;
}
if ($label == 'General_NewVisits') {
$row->setMetadata('segment', 'visitorType==new');
continue;
}
// if there's more than one element, handle as a range w/ an upper bound
if (strpos($label, "-") !== false) {
// get the range
sscanf($label, "%d - %d", $lowerBound, $upperBound);
if ($lowerBound == $upperBound) {
$row->setMetadata('segment', $this->segment . '==' . urlencode($lowerBound));
} else {
$row->setMetadata('segment', $this->segment . '>=' . urlencode($lowerBound) . ';' .
$this->segment . '<=' . urlencode($upperBound));
}
} // if there's one element, handle as a range w/ no upper bound
else {
// get the lower bound
sscanf($label, "%d", $lowerBound);
if ($lowerBound !== null) {
$row->setMetadata('segment', $this->segment . '>=' . urlencode($lowerBound));
}
}
}
}
}
|