/usr/share/php/Icinga/Web/Widget/Paginator.php is in php-icinga 2.1.0-1ubuntu1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | <?php
/* Icinga Web 2 | (c) 2013-2015 Icinga Development Team | GPLv2+ */
namespace Icinga\Web\Widget;
use Icinga\Data\Paginatable;
use Icinga\Exception\ProgrammingError;
/**
* Paginator
*/
class Paginator extends AbstractWidget
{
/**
* The query the paginator widget is created for
*
* @var Paginatable
*/
protected $query;
/**
* The view script in use
*
* @var string|array
*/
protected $viewScript = array('mixedPagination.phtml', 'default');
/**
* Set the query to create the paginator widget for
*
* @param Paginatable $query
*
* @return $this
*/
public function setQuery(Paginatable $query)
{
$this->query = $query;
return $this;
}
/**
* Set the view script to use
*
* @param string|array $script
*
* @return $this
*/
public function setViewScript($script)
{
$this->viewScript = $script;
return $this;
}
/**
* Render this paginator
*/
public function render()
{
if ($this->query === null) {
throw new ProgrammingError('Need a query to create the paginator widget for');
}
$itemCountPerPage = $this->query->getLimit();
if (! $itemCountPerPage) {
return ''; // No pagination required
}
$totalItemCount = count($this->query);
$pageCount = (int) ceil($totalItemCount / $itemCountPerPage);
$currentPage = $this->query->hasOffset() ? ($this->query->getOffset() / $itemCountPerPage) + 1 : 1;
$pagesInRange = $this->getPages($pageCount, $currentPage);
$variables = array(
'totalItemCount' => $totalItemCount,
'pageCount' => $pageCount,
'itemCountPerPage' => $itemCountPerPage,
'first' => 1,
'current' => $currentPage,
'last' => $pageCount,
'pagesInRange' => $pagesInRange,
'firstPageInRange' => min($pagesInRange),
'lastPageInRange' => max($pagesInRange)
);
if ($currentPage > 1) {
$variables['previous'] = $currentPage - 1;
}
if ($currentPage < $pageCount) {
$variables['next'] = $currentPage + 1;
}
if (is_array($this->viewScript)) {
if ($this->viewScript[1] !== null) {
return $this->view()->partial($this->viewScript[0], $this->viewScript[1], $variables);
}
return $this->view()->partial($this->viewScript[0], $variables);
}
return $this->view()->partial($this->viewScript, $variables);
}
/**
* Returns an array of "local" pages given the page count and current page number
*
* @return array
*/
protected function getPages($pageCount, $currentPage)
{
$range = array();
if ($pageCount < 10) {
// Show all pages if we have less than 10
for ($i = 1; $i < 10; $i++) {
if ($i > $pageCount) {
break;
}
$range[$i] = $i;
}
} else {
// More than 10 pages:
foreach (array(1, 2) as $i) {
$range[$i] = $i;
}
if ($currentPage < 6 ) {
// We are on page 1-5 from
for ($i = 1; $i <= 7; $i++) {
$range[$i] = $i;
}
} else {
// Current page > 5
$range[] = '...';
if (($pageCount - $currentPage) < 5) {
// Less than 5 pages left
$start = 5 - ($pageCount - $currentPage);
} else {
$start = 1;
}
for ($i = $currentPage - $start; $i < ($currentPage + (4 - $start)); $i++) {
if ($i > $pageCount) {
break;
}
$range[$i] = $i;
}
}
if ($currentPage < ($pageCount - 2)) {
$range[] = '...';
}
foreach (array($pageCount - 1, $pageCount) as $i) {
$range[$i] = $i;
}
}
if (empty($range)) {
$range[] = 1;
}
return $range;
}
}
|