/
PaginatorComponent.php
239 lines (217 loc) · 7.52 KB
/
PaginatorComponent.php
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 2.0.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Controller\Component;
use Cake\Controller\Component;
use Cake\Controller\ComponentRegistry;
use Cake\Network\Exception\NotFoundException;
use Cake\ORM\Paginator;
/**
* This component is used to handle automatic model data pagination. The primary way to use this
* component is to call the paginate() method. There is a convenience wrapper on Controller as well.
*
* ### Configuring pagination
*
* You configure pagination when calling paginate(). See that method for more details.
*
* @link http://book.cakephp.org/3.0/en/controllers/components/pagination.html
*/
class PaginatorComponent extends Component
{
/**
* Default pagination settings.
*
* When calling paginate() these settings will be merged with the configuration
* you provide.
*
* - `maxLimit` - The maximum limit users can choose to view. Defaults to 100
* - `limit` - The initial number of items per page. Defaults to 20.
* - `page` - The starting page, defaults to 1.
* - `whitelist` - A list of parameters users are allowed to set using request
* parameters. Modifying this list will allow users to have more influence
* over pagination, be careful with what you permit.
*
* @var array
*/
protected $_defaultConfig = [
'page' => 1,
'limit' => 20,
'maxLimit' => 100,
'whitelist' => ['limit', 'sort', 'page', 'direction']
];
protected $_paginator;
public function __construct(ComponentRegistry $registry, array $config = [])
{
$this->_paginator = new Paginator();
parent::__construct($registry, $config);
}
/**
* Events supported by this component.
*
* @return array
*/
public function implementedEvents()
{
return [];
}
/**
* Handles automatic pagination of model records.
*
* ### Configuring pagination
*
* When calling `paginate()` you can use the $settings parameter to pass in pagination settings.
* These settings are used to build the queries made and control other pagination settings.
*
* If your settings contain a key with the current table's alias. The data inside that key will be used.
* Otherwise the top level configuration will be used.
*
* ```
* $settings = [
* 'limit' => 20,
* 'maxLimit' => 100
* ];
* $results = $paginator->paginate($table, $settings);
* ```
*
* The above settings will be used to paginate any Table. You can configure Table specific settings by
* keying the settings with the Table alias.
*
* ```
* $settings = [
* 'Articles' => [
* 'limit' => 20,
* 'maxLimit' => 100
* ],
* 'Comments' => [ ... ]
* ];
* $results = $paginator->paginate($table, $settings);
* ```
*
* This would allow you to have different pagination settings for `Articles` and `Comments` tables.
*
* ### Controlling sort fields
*
* By default CakePHP will automatically allow sorting on any column on the table object being
* paginated. Often times you will want to allow sorting on either associated columns or calculated
* fields. In these cases you will need to define a whitelist of all the columns you wish to allow
* sorting on. You can define the whitelist in the `$settings` parameter:
*
* ```
* $settings = [
* 'Articles' => [
* 'finder' => 'custom',
* 'sortWhitelist' => ['title', 'author_id', 'comment_count'],
* ]
* ];
* ```
*
* Passing an empty array as whitelist disallows sorting altogether.
*
* ### Paginating with custom finders
*
* You can paginate with any find type defined on your table using the `finder` option.
*
* ```
* $settings = [
* 'Articles' => [
* 'finder' => 'popular'
* ]
* ];
* $results = $paginator->paginate($table, $settings);
* ```
*
* Would paginate using the `find('popular')` method.
*
* You can also pass an already created instance of a query to this method:
*
* ```
* $query = $this->Articles->find('popular')->matching('Tags', function ($q) {
* return $q->where(['name' => 'CakePHP'])
* });
* $results = $paginator->paginate($query);
* ```
*
* ### Scoping Request parameters
*
* By using request parameter scopes you can paginate multiple queries in the same controller action:
*
* ```
* $articles = $paginator->paginate($articlesQuery, ['scope' => 'articles']);
* $tags = $paginator->paginate($tagsQuery, ['scope' => 'tags']);
* ```
*
* Each of the above queries will use different query string parameter sets
* for pagination data. An example URL paginating both results would be:
*
* ```
* /dashboard?articles[page]=1&tags[page]=2
* ```
*
* @param \Cake\Datasource\RepositoryInterface|\Cake\Datasource\QueryInterface $object The table or query to paginate.
* @param array $settings The settings/configuration used for pagination.
* @return \Cake\Datasource\ResultSetInterface Query results
* @throws \Cake\Network\Exception\NotFoundException
*/
public function paginate($object, array $settings = [])
{
$request = $this->_registry->getController()->request;
try {
$results = $this->_paginator->paginate(
$object,
$request->getQueryParams(),
$settings
);
$this->_setPagingParams();
} catch (NotFoundException $e) {
$this->_setPagingParams();
throw $e;
}
return $results;
}
public function mergeOptions($alias, $settings)
{
$request = $this->_registry->getController()->request;
$this->_paginator->setParams($request->getQueryParams());
return $this->_paginator->mergeOptions($alias, $settings);
}
protected function _setPagingParams()
{
$request = $this->_registry->getController()->request;
if (!$request->getParam('paging')) {
$request->params['paging'] = [];
}
$request->params['paging'] = $this->_paginator->getPagingParams()
+ (array)$request->getParam('paging');
}
public function config($key = null, $value = null, $merge = true)
{
return $this->_paginator->config($key, $value, $merge);
}
public function setConfig($key, $value = null, $merge = true)
{
return $this->_paginator->setConfig($key, $value, $merge);
}
public function getConfig($key = null)
{
return $this->_paginator->getConfig($key);
}
public function configShallow($key, $value = null)
{
return $this->_paginator->configShallow($key, $value = null);
}
public function __call($method, $args)
{
return call_user_func_array([$this->_paginator, $method], $args);
}
}