blob: 64882283c516022a8e9a6c798b184ea10ad4bcd1 (
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
|
<?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
*/
/**
* Adds a new column to every row of a DataTable based on the result of callback.
*
* @package Piwik
* @subpackage Piwik_DataTable
*/
class Piwik_DataTable_Filter_ColumnCallbackAddColumn extends Piwik_DataTable_Filter
{
/**
* The names of the columns to pass to the callback.
*/
private $columns;
/**
* The name of the column to add.
*/
private $columnToAdd;
/**
* The callback to apply to each row of the DataTable. The result is added as
* the value of a new column.
*/
private $functionToApply;
/**
* Extra parameters to pass to the callback.
*/
private $functionParameters;
/**
* Constructor.
*
* @param Piwik_DataTable $table The DataTable that will be filtered.
* @param array|string $columns The names of the columns to pass to the callback.
* @param string $columnToAdd The name of the column to add.
* @param mixed $functionToApply The callback to apply to each row of a DataTable.
* @param array $functionParameters Extra parameters to pass to $functionToApply.
*/
public function __construct( $table, $columns, $columnToAdd, $functionToApply, $functionParameters = array() )
{
parent::__construct($table);
if (!is_array($columns))
{
$columns = array($columns);
}
$this->columns = $columns;
$this->columnToAdd = $columnToAdd;
$this->functionToApply = $functionToApply;
$this->functionParameters = $functionParameters;
}
/**
* Executes a callback on every row of the supplied table and adds the result of
* the callback as a new column to each row.
*
* @param Piwik_DataTable $table The table to filter.
*/
public function filter( $table )
{
foreach ($table->getRows() as $row)
{
$columnValues = array();
foreach ($this->columns as $column)
{
$columnValues[] = $row->getColumn($column);
}
$parameters = array_merge($columnValues, $this->functionParameters);
$value = call_user_func_array($this->functionToApply, $parameters);
$row->setColumn($this->columnToAdd, $value);
$this->filterSubTable($row);
}
}
}
|