31
31
*/
32
32
class PaginatorComponent extends Component {
33
33
34
- /**
35
- * The current request instance.
36
- *
37
- * @var Cake\Network\Request
38
- */
39
- public $ request ;
40
-
41
34
/**
42
35
* Default pagination settings.
43
36
*
@@ -67,18 +60,6 @@ class PaginatorComponent extends Component {
67
60
'limit ' , 'sort ' , 'page ' , 'direction '
68
61
);
69
62
70
- /**
71
- * Constructor
72
- *
73
- * @param ComponentRegistry $collection A ComponentRegistry this component can use to lazy load its components
74
- * @param array $settings Array of configuration settings.
75
- */
76
- public function __construct (ComponentRegistry $ collection , $ settings = []) {
77
- $ settings = array_merge ($ this ->_defaultConfig , (array )$ settings );
78
- $ this ->request = $ collection ->getController ()->request ;
79
- parent ::__construct ($ collection , $ settings );
80
- }
81
-
82
63
/**
83
64
* Handles automatic pagination of model records.
84
65
*
@@ -106,7 +87,7 @@ public function __construct(ComponentRegistry $collection, $settings = []) {
106
87
* 'Articles' => [
107
88
* 'limit' => 20,
108
89
* 'maxLimit' => 100
109
- * [ ,
90
+ * ] ,
110
91
* 'Comments' => [ ... ]
111
92
* ];
112
93
* $results = $paginator->paginate($table, $settings);
@@ -142,9 +123,6 @@ public function paginate($object, $settings = array(), $whitelist = array()) {
142
123
$ alias = $ object ->alias ();
143
124
144
125
$ options = $ this ->mergeOptions ($ alias , $ settings );
145
-
146
- // TODO perhaps move this until after the query has been created.
147
- // Then we could look at the fields in the query.
148
126
$ options = $ this ->validateSort ($ object , $ options , $ whitelist );
149
127
$ options = $ this ->checkLimit ($ options );
150
128
@@ -181,7 +159,6 @@ public function paginate($object, $settings = array(), $whitelist = array()) {
181
159
$ query = $ object ->find ($ type , array_merge ($ parameters , $ extra ));
182
160
183
161
// TODO Validate sort and apply them here.
184
-
185
162
$ results = $ query ->execute ();
186
163
$ numResults = count ($ results );
187
164
@@ -192,7 +169,8 @@ public function paginate($object, $settings = array(), $whitelist = array()) {
192
169
$ count = 0 ;
193
170
} else {
194
171
$ parameters = compact ('conditions ' );
195
- $ count = $ object ->find ($ type , array_merge ($ parameters , $ extra ))->count ();
172
+ $ query = $ object ->find ($ type , array_merge ($ parameters , $ extra ));
173
+ $ count = $ query ->count ();
196
174
}
197
175
198
176
$ pageCount = intval (ceil ($ count / $ limit ));
@@ -202,6 +180,8 @@ public function paginate($object, $settings = array(), $whitelist = array()) {
202
180
throw new Error \NotFoundException ();
203
181
}
204
182
183
+ $ request = $ this ->_registry ->getController ()->request ;
184
+
205
185
if (!is_array ($ order )) {
206
186
$ order = (array )$ order ;
207
187
}
@@ -219,11 +199,11 @@ public function paginate($object, $settings = array(), $whitelist = array()) {
219
199
'limit ' => $ defaults ['limit ' ] != $ options ['limit ' ] ? $ options ['limit ' ] : null ,
220
200
);
221
201
222
- if (!isset ($ this -> request ['paging ' ])) {
223
- $ this -> request ['paging ' ] = array ();
202
+ if (!isset ($ request ['paging ' ])) {
203
+ $ request ['paging ' ] = array ();
224
204
}
225
- $ this -> request ['paging ' ] = array_merge (
226
- (array )$ this -> request ['paging ' ],
205
+ $ request ['paging ' ] = array_merge (
206
+ (array )$ request ['paging ' ],
227
207
array ($ alias => $ paging )
228
208
);
229
209
return $ results ;
@@ -247,8 +227,8 @@ public function paginate($object, $settings = array(), $whitelist = array()) {
247
227
*/
248
228
public function mergeOptions ($ alias , $ settings ) {
249
229
$ defaults = $ this ->getDefaults ($ alias , $ settings );
250
- $ request = $ this ->request -> query ;
251
- $ request = array_intersect_key ($ request , array_flip ($ this ->whitelist ));
230
+ $ request = $ this ->_registry -> getController ()-> request ;
231
+ $ request = array_intersect_key ($ request-> query , array_flip ($ this ->whitelist ));
252
232
return array_merge ($ defaults , $ request );
253
233
}
254
234
@@ -302,26 +282,28 @@ public function validateSort(Table $object, array $options, array $whitelist = a
302
282
}
303
283
$ options ['order ' ] = array ($ options ['sort ' ] => $ direction );
304
284
}
285
+ unset($ options ['sort ' ], $ options ['direction ' ]);
305
286
306
- if (empty ($ options ['order ' ]) || (isset ($ options ['order ' ]) && is_array ($ options ['order ' ]))) {
307
- $ options ['order ' ] = null ;
308
- return $ options ;
287
+ if (empty ($ options ['order ' ])) {
288
+ $ options ['order ' ] = [];
289
+ }
290
+ if (!is_array ($ options ['order ' ])) {
291
+ $ options ['order ' ] = (array )$ options ['order ' ];
309
292
}
310
293
311
294
if (!empty ($ whitelist )) {
312
295
$ field = key ($ options ['order ' ]);
313
296
$ inWhitelist = in_array ($ field , $ whitelist , true );
314
297
if (!$ inWhitelist ) {
315
- $ options ['order ' ] = null ;
298
+ $ options ['order ' ] = [] ;
316
299
}
317
300
return $ options ;
318
301
}
319
302
320
- if (! empty ( $ options [ ' order ' ]) && is_array ($ options ['order ' ])) {
303
+ if (is_array ($ options ['order ' ])) {
321
304
$ tableAlias = $ object ->alias ();
322
- $ order = array () ;
305
+ $ order = [] ;
323
306
324
- // TODO Remove associated field checks and rely on the whitelist.
325
307
foreach ($ options ['order ' ] as $ key => $ value ) {
326
308
$ field = $ key ;
327
309
$ alias = $ tableAlias ;
@@ -332,16 +314,10 @@ public function validateSort(Table $object, array $options, array $whitelist = a
332
314
333
315
if ($ correctAlias && $ object ->hasField ($ field )) {
334
316
$ order [$ tableAlias . '. ' . $ field ] = $ value ;
335
- } elseif ($ correctAlias && $ object ->hasField ($ key , true )) {
336
- $ order [$ field ] = $ value ;
337
- } elseif (isset ($ object ->{$ alias }) && $ object ->{$ alias }->hasField ($ field , true )) {
338
- // TODO fix associated sorting.
339
- $ order [$ alias . '. ' . $ field ] = $ value ;
340
317
}
341
318
}
342
319
$ options ['order ' ] = $ order ;
343
320
}
344
- unset($ options ['sort ' ], $ options ['direction ' ]);
345
321
346
322
return $ options ;
347
323
}
0 commit comments