pictcode / lib / Cake / Controller / Component / PaginatorComponent.php @ 0b1b8047
履歴 | 表示 | アノテート | ダウンロード (13.313 KB)
| 1 |
<?php
|
|---|---|
| 2 |
/**
|
| 3 |
* Paginator Component
|
| 4 |
*
|
| 5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
| 6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 7 |
*
|
| 8 |
* Licensed under The MIT License
|
| 9 |
* For full copyright and license information, please see the LICENSE.txt
|
| 10 |
* Redistributions of files must retain the above copyright notice.
|
| 11 |
*
|
| 12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
| 13 |
* @link http://cakephp.org CakePHP(tm) Project
|
| 14 |
* @package Cake.Controller.Component
|
| 15 |
* @since CakePHP(tm) v 2.0
|
| 16 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
| 17 |
*/
|
| 18 |
|
| 19 |
App::uses('Component', 'Controller'); |
| 20 |
App::uses('Hash', 'Utility'); |
| 21 |
|
| 22 |
/**
|
| 23 |
* This component is used to handle automatic model data pagination. The primary way to use this
|
| 24 |
* component is to call the paginate() method. There is a convenience wrapper on Controller as well.
|
| 25 |
*
|
| 26 |
* ### Configuring pagination
|
| 27 |
*
|
| 28 |
* You configure pagination using the PaginatorComponent::$settings. This allows you to configure
|
| 29 |
* the default pagination behavior in general or for a specific model. General settings are used when there
|
| 30 |
* are no specific model configuration, or the model you are paginating does not have specific settings.
|
| 31 |
*
|
| 32 |
* ```
|
| 33 |
* $this->Paginator->settings = array(
|
| 34 |
* 'limit' => 20,
|
| 35 |
* 'maxLimit' => 100
|
| 36 |
* );
|
| 37 |
* ```
|
| 38 |
*
|
| 39 |
* The above settings will be used to paginate any model. You can configure model specific settings by
|
| 40 |
* keying the settings with the model name.
|
| 41 |
*
|
| 42 |
* ```
|
| 43 |
* $this->Paginator->settings = array(
|
| 44 |
* 'Post' => array(
|
| 45 |
* 'limit' => 20,
|
| 46 |
* 'maxLimit' => 100
|
| 47 |
* ),
|
| 48 |
* 'Comment' => array( ... )
|
| 49 |
* );
|
| 50 |
* ```
|
| 51 |
*
|
| 52 |
* This would allow you to have different pagination settings for `Comment` and `Post` models.
|
| 53 |
*
|
| 54 |
* #### Paginating with custom finders
|
| 55 |
*
|
| 56 |
* You can paginate with any find type defined on your model using the `findType` option.
|
| 57 |
*
|
| 58 |
* ```
|
| 59 |
* $this->Paginator->settings = array(
|
| 60 |
* 'Post' => array(
|
| 61 |
* 'findType' => 'popular'
|
| 62 |
* )
|
| 63 |
* );
|
| 64 |
* ```
|
| 65 |
*
|
| 66 |
* Would paginate using the `find('popular')` method.
|
| 67 |
*
|
| 68 |
* @package Cake.Controller.Component
|
| 69 |
* @link http://book.cakephp.org/2.0/en/core-libraries/components/pagination.html
|
| 70 |
*/
|
| 71 |
class PaginatorComponent extends Component { |
| 72 |
|
| 73 |
/**
|
| 74 |
* Pagination settings. These settings control pagination at a general level.
|
| 75 |
* You can also define sub arrays for pagination settings for specific models.
|
| 76 |
*
|
| 77 |
* - `maxLimit` The maximum limit users can choose to view. Defaults to 100
|
| 78 |
* - `limit` The initial number of items per page. Defaults to 20.
|
| 79 |
* - `page` The starting page, defaults to 1.
|
| 80 |
* - `paramType` What type of parameters you want pagination to use?
|
| 81 |
* - `named` Use named parameters / routed parameters.
|
| 82 |
* - `querystring` Use query string parameters.
|
| 83 |
*
|
| 84 |
* @var array
|
| 85 |
*/
|
| 86 |
public $settings = array( |
| 87 |
'page' => 1, |
| 88 |
'limit' => 20, |
| 89 |
'maxLimit' => 100, |
| 90 |
'paramType' => 'named' |
| 91 |
); |
| 92 |
|
| 93 |
/**
|
| 94 |
* A list of parameters users are allowed to set using request parameters. Modifying
|
| 95 |
* this list will allow users to have more influence over pagination,
|
| 96 |
* be careful with what you permit.
|
| 97 |
*
|
| 98 |
* @var array
|
| 99 |
*/
|
| 100 |
public $whitelist = array( |
| 101 |
'limit', 'sort', 'page', 'direction' |
| 102 |
); |
| 103 |
|
| 104 |
/**
|
| 105 |
* Constructor
|
| 106 |
*
|
| 107 |
* @param ComponentCollection $collection A ComponentCollection this component can use to lazy load its components
|
| 108 |
* @param array $settings Array of configuration settings.
|
| 109 |
*/
|
| 110 |
public function __construct(ComponentCollection $collection, $settings = array()) { |
| 111 |
$settings = array_merge($this->settings, (array)$settings); |
| 112 |
$this->Controller = $collection->getController(); |
| 113 |
parent::__construct($collection, $settings); |
| 114 |
} |
| 115 |
|
| 116 |
/**
|
| 117 |
* Handles automatic pagination of model records.
|
| 118 |
*
|
| 119 |
* @param Model|string $object Model to paginate (e.g: model instance, or 'Model', or 'Model.InnerModel')
|
| 120 |
* @param string|array $scope Additional find conditions to use while paginating
|
| 121 |
* @param array $whitelist List of allowed fields for ordering. This allows you to prevent ordering
|
| 122 |
* on non-indexed, or undesirable columns. See PaginatorComponent::validateSort() for additional details
|
| 123 |
* on how the whitelisting and sort field validation works.
|
| 124 |
* @return array Model query results
|
| 125 |
* @throws MissingModelException
|
| 126 |
* @throws NotFoundException
|
| 127 |
*/
|
| 128 |
public function paginate($object = null, $scope = array(), $whitelist = array()) { |
| 129 |
if (is_array($object)) { |
| 130 |
$whitelist = $scope; |
| 131 |
$scope = $object; |
| 132 |
$object = null; |
| 133 |
} |
| 134 |
|
| 135 |
$object = $this->_getObject($object); |
| 136 |
|
| 137 |
if (!is_object($object)) { |
| 138 |
throw new MissingModelException($object); |
| 139 |
} |
| 140 |
|
| 141 |
$options = $this->mergeOptions($object->alias); |
| 142 |
$options = $this->validateSort($object, $options, $whitelist); |
| 143 |
$options = $this->checkLimit($options); |
| 144 |
|
| 145 |
$conditions = $fields = $order = $limit = $page = $recursive = null; |
| 146 |
|
| 147 |
if (!isset($options['conditions'])) { |
| 148 |
$options['conditions'] = array(); |
| 149 |
} |
| 150 |
|
| 151 |
$type = 'all'; |
| 152 |
|
| 153 |
if (isset($options[0])) { |
| 154 |
$type = $options[0]; |
| 155 |
unset($options[0]); |
| 156 |
} |
| 157 |
|
| 158 |
extract($options); |
| 159 |
|
| 160 |
if (is_array($scope) && !empty($scope)) { |
| 161 |
$conditions = array_merge($conditions, $scope); |
| 162 |
} elseif (is_string($scope)) { |
| 163 |
$conditions = array($conditions, $scope); |
| 164 |
} |
| 165 |
if ($recursive === null) { |
| 166 |
$recursive = $object->recursive; |
| 167 |
} |
| 168 |
|
| 169 |
$extra = array_diff_key($options, compact( |
| 170 |
'conditions', 'fields', 'order', 'limit', 'page', 'recursive' |
| 171 |
)); |
| 172 |
|
| 173 |
if (!empty($extra['findType'])) { |
| 174 |
$type = $extra['findType']; |
| 175 |
unset($extra['findType']); |
| 176 |
} |
| 177 |
|
| 178 |
if ($type !== 'all') { |
| 179 |
$extra['type'] = $type; |
| 180 |
} |
| 181 |
|
| 182 |
if ((int)$page < 1) { |
| 183 |
$page = 1; |
| 184 |
} |
| 185 |
$page = $options['page'] = (int)$page; |
| 186 |
|
| 187 |
if ($object->hasMethod('paginate')) { |
| 188 |
$results = $object->paginate( |
| 189 |
$conditions, $fields, $order, $limit, $page, $recursive, $extra |
| 190 |
); |
| 191 |
} else {
|
| 192 |
$parameters = compact('conditions', 'fields', 'order', 'limit', 'page'); |
| 193 |
if ($recursive != $object->recursive) { |
| 194 |
$parameters['recursive'] = $recursive; |
| 195 |
} |
| 196 |
$results = $object->find($type, array_merge($parameters, $extra)); |
| 197 |
} |
| 198 |
$defaults = $this->getDefaults($object->alias); |
| 199 |
unset($defaults[0]); |
| 200 |
|
| 201 |
if (!$results) { |
| 202 |
$count = 0; |
| 203 |
} elseif ($object->hasMethod('paginateCount')) { |
| 204 |
$count = $object->paginateCount($conditions, $recursive, $extra); |
| 205 |
} elseif ($page === 1 && count($results) < $limit) { |
| 206 |
$count = count($results); |
| 207 |
} else {
|
| 208 |
$parameters = compact('conditions'); |
| 209 |
if ($recursive != $object->recursive) { |
| 210 |
$parameters['recursive'] = $recursive; |
| 211 |
} |
| 212 |
$count = $object->find('count', array_merge($parameters, $extra)); |
| 213 |
} |
| 214 |
$pageCount = (int)ceil($count / $limit); |
| 215 |
$requestedPage = $page; |
| 216 |
$page = max(min($page, $pageCount), 1); |
| 217 |
|
| 218 |
$paging = array( |
| 219 |
'page' => $page, |
| 220 |
'current' => count($results), |
| 221 |
'count' => $count, |
| 222 |
'prevPage' => ($page > 1), |
| 223 |
'nextPage' => ($count > ($page * $limit)), |
| 224 |
'pageCount' => $pageCount, |
| 225 |
'order' => $order, |
| 226 |
'limit' => $limit, |
| 227 |
'options' => Hash::diff($options, $defaults), |
| 228 |
'paramType' => $options['paramType'] |
| 229 |
); |
| 230 |
|
| 231 |
if (!isset($this->Controller->request['paging'])) { |
| 232 |
$this->Controller->request['paging'] = array(); |
| 233 |
} |
| 234 |
$this->Controller->request['paging'] = array_merge( |
| 235 |
(array)$this->Controller->request['paging'], |
| 236 |
array($object->alias => $paging) |
| 237 |
); |
| 238 |
|
| 239 |
if ($requestedPage > $page) { |
| 240 |
throw new NotFoundException(); |
| 241 |
} |
| 242 |
|
| 243 |
if (!in_array('Paginator', $this->Controller->helpers) && |
| 244 |
!array_key_exists('Paginator', $this->Controller->helpers) |
| 245 |
) {
|
| 246 |
$this->Controller->helpers[] = 'Paginator'; |
| 247 |
} |
| 248 |
return $results; |
| 249 |
} |
| 250 |
|
| 251 |
/**
|
| 252 |
* Get the object pagination will occur on.
|
| 253 |
*
|
| 254 |
* @param string|Model $object The object you are looking for.
|
| 255 |
* @return mixed The model object to paginate on.
|
| 256 |
*/
|
| 257 |
protected function _getObject($object) { |
| 258 |
if (is_string($object)) { |
| 259 |
$assoc = null; |
| 260 |
if (strpos($object, '.') !== false) { |
| 261 |
list($object, $assoc) = pluginSplit($object); |
| 262 |
} |
| 263 |
if ($assoc && isset($this->Controller->{$object}->{$assoc})) { |
| 264 |
return $this->Controller->{$object}->{$assoc}; |
| 265 |
} |
| 266 |
if ($assoc && isset($this->Controller->{$this->Controller->modelClass}->{$assoc})) { |
| 267 |
return $this->Controller->{$this->Controller->modelClass}->{$assoc}; |
| 268 |
} |
| 269 |
if (isset($this->Controller->{$object})) { |
| 270 |
return $this->Controller->{$object}; |
| 271 |
} |
| 272 |
if (isset($this->Controller->{$this->Controller->modelClass}->{$object})) { |
| 273 |
return $this->Controller->{$this->Controller->modelClass}->{$object}; |
| 274 |
} |
| 275 |
} |
| 276 |
if (empty($object) || $object === null) { |
| 277 |
if (isset($this->Controller->{$this->Controller->modelClass})) { |
| 278 |
return $this->Controller->{$this->Controller->modelClass}; |
| 279 |
} |
| 280 |
|
| 281 |
$className = null; |
| 282 |
$name = $this->Controller->uses[0]; |
| 283 |
if (strpos($this->Controller->uses[0], '.') !== false) { |
| 284 |
list($name, $className) = explode('.', $this->Controller->uses[0]); |
| 285 |
} |
| 286 |
if ($className) { |
| 287 |
return $this->Controller->{$className}; |
| 288 |
} |
| 289 |
|
| 290 |
return $this->Controller->{$name}; |
| 291 |
} |
| 292 |
return $object; |
| 293 |
} |
| 294 |
|
| 295 |
/**
|
| 296 |
* Merges the various options that Pagination uses.
|
| 297 |
* Pulls settings together from the following places:
|
| 298 |
*
|
| 299 |
* - General pagination settings
|
| 300 |
* - Model specific settings.
|
| 301 |
* - Request parameters
|
| 302 |
*
|
| 303 |
* The result of this method is the aggregate of all the option sets combined together. You can change
|
| 304 |
* PaginatorComponent::$whitelist to modify which options/values can be set using request parameters.
|
| 305 |
*
|
| 306 |
* @param string $alias Model alias being paginated, if the general settings has a key with this value
|
| 307 |
* that key's settings will be used for pagination instead of the general ones.
|
| 308 |
* @return array Array of merged options.
|
| 309 |
*/
|
| 310 |
public function mergeOptions($alias) { |
| 311 |
$defaults = $this->getDefaults($alias); |
| 312 |
switch ($defaults['paramType']) { |
| 313 |
case 'named': |
| 314 |
$request = $this->Controller->request->params['named']; |
| 315 |
break;
|
| 316 |
case 'querystring': |
| 317 |
$request = $this->Controller->request->query; |
| 318 |
break;
|
| 319 |
} |
| 320 |
$request = array_intersect_key($request, array_flip($this->whitelist)); |
| 321 |
return array_merge($defaults, $request); |
| 322 |
} |
| 323 |
|
| 324 |
/**
|
| 325 |
* Get the default settings for a $model. If there are no settings for a specific model, the general settings
|
| 326 |
* will be used.
|
| 327 |
*
|
| 328 |
* @param string $alias Model name to get default settings for.
|
| 329 |
* @return array An array of pagination defaults for a model, or the general settings.
|
| 330 |
*/
|
| 331 |
public function getDefaults($alias) { |
| 332 |
$defaults = $this->settings; |
| 333 |
if (isset($this->settings[$alias])) { |
| 334 |
$defaults = $this->settings[$alias]; |
| 335 |
} |
| 336 |
$defaults += array( |
| 337 |
'page' => 1, |
| 338 |
'limit' => 20, |
| 339 |
'maxLimit' => 100, |
| 340 |
'paramType' => 'named' |
| 341 |
); |
| 342 |
return $defaults; |
| 343 |
} |
| 344 |
|
| 345 |
/**
|
| 346 |
* Validate that the desired sorting can be performed on the $object. Only fields or
|
| 347 |
* virtualFields can be sorted on. The direction param will also be sanitized. Lastly
|
| 348 |
* sort + direction keys will be converted into the model friendly order key.
|
| 349 |
*
|
| 350 |
* You can use the whitelist parameter to control which columns/fields are available for sorting.
|
| 351 |
* This helps prevent users from ordering large result sets on un-indexed values.
|
| 352 |
*
|
| 353 |
* Any columns listed in the sort whitelist will be implicitly trusted. You can use this to sort
|
| 354 |
* on synthetic columns, or columns added in custom find operations that may not exist in the schema.
|
| 355 |
*
|
| 356 |
* @param Model $object The model being paginated.
|
| 357 |
* @param array $options The pagination options being used for this request.
|
| 358 |
* @param array $whitelist The list of columns that can be used for sorting. If empty all keys are allowed.
|
| 359 |
* @return array An array of options with sort + direction removed and replaced with order if possible.
|
| 360 |
*/
|
| 361 |
public function validateSort(Model $object, array $options, array $whitelist = array()) { |
| 362 |
if (empty($options['order']) && is_array($object->order)) { |
| 363 |
$options['order'] = $object->order; |
| 364 |
} |
| 365 |
|
| 366 |
if (isset($options['sort'])) { |
| 367 |
$direction = null; |
| 368 |
if (isset($options['direction'])) { |
| 369 |
$direction = strtolower($options['direction']); |
| 370 |
} |
| 371 |
if (!in_array($direction, array('asc', 'desc'))) { |
| 372 |
$direction = 'asc'; |
| 373 |
} |
| 374 |
$options['order'] = array($options['sort'] => $direction); |
| 375 |
} |
| 376 |
|
| 377 |
if (!empty($whitelist) && isset($options['order']) && is_array($options['order'])) { |
| 378 |
$field = key($options['order']); |
| 379 |
$inWhitelist = in_array($field, $whitelist, true); |
| 380 |
if (!$inWhitelist) { |
| 381 |
$options['order'] = null; |
| 382 |
} |
| 383 |
return $options; |
| 384 |
} |
| 385 |
|
| 386 |
if (!empty($options['order']) && is_array($options['order'])) { |
| 387 |
$order = array(); |
| 388 |
foreach ($options['order'] as $key => $value) { |
| 389 |
if (is_int($key)) { |
| 390 |
$key = $value; |
| 391 |
$value = 'asc'; |
| 392 |
} |
| 393 |
$field = $key; |
| 394 |
$alias = $object->alias; |
| 395 |
if (strpos($key, '.') !== false) { |
| 396 |
list($alias, $field) = explode('.', $key); |
| 397 |
} |
| 398 |
$correctAlias = ($object->alias === $alias); |
| 399 |
|
| 400 |
if ($correctAlias && $object->hasField($field)) { |
| 401 |
$order[$object->alias . '.' . $field] = $value; |
| 402 |
} elseif ($correctAlias && $object->hasField($key, true)) { |
| 403 |
$order[$field] = $value; |
| 404 |
} elseif (isset($object->{$alias}) && $object->{$alias}->hasField($field, true)) { |
| 405 |
$order[$alias . '.' . $field] = $value; |
| 406 |
} |
| 407 |
} |
| 408 |
$options['order'] = $order; |
| 409 |
} |
| 410 |
|
| 411 |
return $options; |
| 412 |
} |
| 413 |
|
| 414 |
/**
|
| 415 |
* Check the limit parameter and ensure its within the maxLimit bounds.
|
| 416 |
*
|
| 417 |
* @param array $options An array of options with a limit key to be checked.
|
| 418 |
* @return array An array of options for pagination
|
| 419 |
*/
|
| 420 |
public function checkLimit(array $options) { |
| 421 |
$options['limit'] = (int)$options['limit']; |
| 422 |
if (empty($options['limit']) || $options['limit'] < 1) { |
| 423 |
$options['limit'] = 1; |
| 424 |
} |
| 425 |
$options['limit'] = min($options['limit'], $options['maxLimit']); |
| 426 |
return $options; |
| 427 |
} |
| 428 |
|
| 429 |
} |