-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPagination.php
More file actions
137 lines (118 loc) · 2.86 KB
/
Pagination.php
File metadata and controls
137 lines (118 loc) · 2.86 KB
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
<?php
/**
* @package framework
* @copyright Copyright (c) 2005-2020 The Regents of the University of California.
* @license http://opensource.org/licenses/MIT MIT
*/
namespace Qubeshub\Database;
/**
* Pagination class
*/
class Pagination
{
/**
* Total rows available
*
* @var int
**/
public $total;
/**
* Pagination starting point
*
* @var int
**/
public $start;
/**
* Pagination page limit
*
* @var int
**/
public $limit;
/**
* The HUBzero paginator
*
* @var object
**/
private $paginator;
/**
* Attempts to forward calls to the paginator itself
*
* @param string $name The method name being called
* @param array $arguments The method arguments provided
* @return $this
* @since 2.0.0
**/
public function __call($name, $arguments)
{
// See if we need to call a method on the HUBzero paginator
if (in_array($name, get_class_methods($this->getPaginator())))
{
$result = call_user_func_array(array($this->getPaginator(), $name), $arguments);
// If we got back something other than the class itself, return it
if (!($result instanceof \Hubzero\Pagination\Paginator))
{
return $result;
}
}
return $this;
}
/**
* Initializes pagination object
*
* @param string $namespace The session state variable namespace
* @param int $total Total number of records
* @param string $start The variable name representing the pagination start number
* @param string $limit The variable name representing the pagination limit number
* @return object
* @since 2.0.0
**/
public static function init($namespace, $total, $start = 'start', $limit = 'limit')
{
$instance = new self;
$instance->total = $total;
$instance->start = \Request::getInt(
$start,
\User::getState($namespace . '.start', 0)
);
$instance->limit = \Request::getInt(
$limit,
\User::getState($namespace . '.limit', \Config::get('list_limit'))
);
if ($instance->limit < 0)
{
$instance->limit = \Config::get('list_limit');
}
$instance->start = ($instance->limit != 0 ? (int)(floor($instance->start / $instance->limit) * $instance->limit) : 0);
if ($instance->start < 0)
{
$instance->start = 0;
}
\User::setState($namespace . '.start', $instance->start);
\User::setState($namespace . '.limit', $instance->limit);
return $instance;
}
/**
* Returns the html pagination output
*
* @return string
* @since 2.0.0
**/
public function __toString()
{
return $this->getPaginator()->render();
}
/**
* Gets the HUBzero paginator, or creates a new one
*
* @return \Hubzero\Pagination\Paginator
* @since 2.0.0
**/
protected function getPaginator()
{
if (!isset($this->paginator))
{
$this->paginator = new \Hubzero\Pagination\Paginator($this->total, $this->start, $this->limit);
}
return $this->paginator;
}
}