blob: 5d751ddbc47532a16481fb165d747828a59a8c7d (
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
|
<?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;
/**
* Delete all rows from the table that are not in the offset,offset+limit range
*
* @package Piwik
* @subpackage DataTable
*/
class Limit extends Filter
{
/**
* Filter constructor.
*
* @param DataTable $table
* @param int $offset Starting row (indexed from 0)
* @param int $limit Number of rows to keep (specify -1 to keep all rows)
* @param bool $keepSummaryRow Whether to keep the summary row or not.
*/
public function __construct($table, $offset, $limit = null, $keepSummaryRow = false)
{
parent::__construct($table);
$this->offset = $offset;
if (is_null($limit)) {
$limit = -1;
}
$this->limit = $limit;
$this->keepSummaryRow = $keepSummaryRow;
}
/**
* Limits the given data table
*
* @param DataTable $table
*/
public function filter($table)
{
$table->setRowsCountBeforeLimitFilter();
if ($this->keepSummaryRow) {
$summaryRow = $table->getRowFromId(DataTable::ID_SUMMARY_ROW);
}
// we delete from 0 to offset
if ($this->offset > 0) {
$table->deleteRowsOffset(0, $this->offset);
}
// at this point the array has offset less elements. We delete from limit to the end
if ($this->limit >= 0) {
$table->deleteRowsOffset($this->limit);
}
if ($this->keepSummaryRow && !empty($summaryRow)) {
$table->addSummaryRow($summaryRow);
}
}
}
|