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
|
<?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
*/
namespace Piwik\DataTable\Filter;
use Piwik\DataTable;
use Piwik\DataTable\Filter;
use Piwik\DataTable\Row;
/**
* Add a new row to the table containing a summary
* of the rows from StartRowToSummarize to EndRowToSummarize.
* It then deletes the rows from StartRowToSummarize to EndRowToSummarize.
* The new row created has a label = 'other'
*
* This filter is useful to build a more compact view of a table,
* keeping the first records unchanged.
*
* For example we use this for the pie chart, to build the last pie part
* which is the sum of all the remaining data after the top 5 data.
* This row is assigned a label of 'Others'.
*
* @package Piwik
* @subpackage DataTable
*/
class AddSummaryRow extends Filter
{
/**
* Creates a new filter and set all required parameters
*
* @param DataTable $table
* @param int $startRowToSummarize
* @param int $labelSummaryRow
* @param null $columnToSortByBeforeTruncating
* @param bool $deleteRows
*/
public function __construct($table,
$startRowToSummarize,
$labelSummaryRow = DataTable::LABEL_SUMMARY_ROW,
$columnToSortByBeforeTruncating = null,
$deleteRows = true)
{
parent::__construct($table);
$this->startRowToSummarize = $startRowToSummarize;
$this->labelSummaryRow = $labelSummaryRow;
$this->columnToSortByBeforeTruncating = $columnToSortByBeforeTruncating;
$this->deleteRows = $deleteRows;
}
/**
* Adds a summary row to the given data table
*
* @param DataTable $table
*/
public function filter($table)
{
$table->filter('Sort',
array($this->columnToSortByBeforeTruncating, 'desc'));
if ($table->getRowsCount() <= $this->startRowToSummarize + 1) {
return;
}
$rows = $table->getRows();
$count = $table->getRowsCount();
$newRow = new Row();
for ($i = $this->startRowToSummarize; $i < $count; $i++) {
if (!isset($rows[$i])) {
// case when the last row is a summary row, it is not indexed by $cout but by DataTable::ID_SUMMARY_ROW
$summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW);
//FIXME: I'm not sure why it could return false, but it was reported in: http://forum.piwik.org/read.php?2,89324,page=1#msg-89442
if ($summaryRow) {
$newRow->sumRow($summaryRow, $enableCopyMetadata = false, $table->getColumnAggregationOperations());
}
} else {
$newRow->sumRow($rows[$i], $enableCopyMetadata = false, $table->getColumnAggregationOperations());
}
}
$newRow->setColumns(array('label' => $this->labelSummaryRow) + $newRow->getColumns());
if ($this->deleteRows) {
$table->filter('Limit', array(0, $this->startRowToSummarize));
}
$table->addSummaryRow($newRow);
unset($rows);
}
}
|