CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (GitHub)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • Slack
    • Paid Support
CakePHP

C CakePHP 2.4 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.4
      • 4.2
      • 4.1
      • 4.0
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Packages

  • Cake
    • Cache
      • Engine
    • Configure
    • Console
      • Command
        • Task
    • Controller
      • Component
        • Acl
        • Auth
    • Core
    • Error
    • Event
    • I18n
    • Log
      • Engine
    • Model
      • Behavior
      • Datasource
        • Database
        • Session
      • Validator
    • Network
      • Email
      • Http
    • Routing
      • Filter
      • Route
    • TestSuite
      • Coverage
      • Fixture
      • Reporter
    • Utility
    • View
      • Helper

Classes

  • CacheHelper
  • FormHelper
  • HtmlHelper
  • JqueryEngineHelper
  • JsBaseEngineHelper
  • JsHelper
  • MootoolsEngineHelper
  • NumberHelper
  • PaginatorHelper
  • PrototypeEngineHelper
  • RssHelper
  • SessionHelper
  • TextHelper
  • TimeHelper
  1: <?php
  2: /**
  3:  * Pagination Helper class file.
  4:  *
  5:  * Generates pagination links
  6:  *
  7:  * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8:  * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9:  *
 10:  * Licensed under The MIT License
 11:  * For full copyright and license information, please see the LICENSE.txt
 12:  * Redistributions of files must retain the above copyright notice.
 13:  *
 14:  * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 15:  * @link          http://cakephp.org CakePHP(tm) Project
 16:  * @package       Cake.View.Helper
 17:  * @since         CakePHP(tm) v 1.2.0
 18:  * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 19:  */
 20: 
 21: App::uses('AppHelper', 'View/Helper');
 22: 
 23: /**
 24:  * Pagination Helper class for easy generation of pagination links.
 25:  *
 26:  * PaginationHelper encloses all methods needed when working with pagination.
 27:  *
 28:  * @package       Cake.View.Helper
 29:  * @property      HtmlHelper $Html
 30:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html
 31:  */
 32: class PaginatorHelper extends AppHelper {
 33: 
 34: /**
 35:  * Helper dependencies
 36:  *
 37:  * @var array
 38:  */
 39:     public $helpers = array('Html');
 40: 
 41: /**
 42:  * The class used for 'Ajax' pagination links. Defaults to JsHelper. You should make sure
 43:  * that JsHelper is defined as a helper before PaginatorHelper, if you want to customize the JsHelper.
 44:  *
 45:  * @var string
 46:  */
 47:     protected $_ajaxHelperClass = 'Js';
 48: 
 49: /**
 50:  * Holds the default options for pagination links
 51:  *
 52:  * The values that may be specified are:
 53:  *
 54:  * - `format` Format of the counter. Supported formats are 'range' and 'pages'
 55:  *    and custom (default). In the default mode the supplied string is parsed and constants are replaced
 56:  *    by their actual values.
 57:  *    placeholders: %page%, %pages%, %current%, %count%, %start%, %end% .
 58:  * - `separator` The separator of the actual page and number of pages (default: ' of ').
 59:  * - `url` Url of the action. See Router::url()
 60:  * - `url['sort']`  the key that the recordset is sorted.
 61:  * - `url['direction']` Direction of the sorting (default: 'asc').
 62:  * - `url['page']` Page number to use in links.
 63:  * - `model` The name of the model.
 64:  * - `escape` Defines if the title field for the link should be escaped (default: true).
 65:  * - `update` DOM id of the element updated with the results of the AJAX call.
 66:  *     If this key isn't specified Paginator will use plain HTML links.
 67:  * - `paging['paramType']` The type of parameters to use when creating links. Valid options are
 68:  *     'querystring' and 'named'. See PaginatorComponent::$settings for more information.
 69:  * - `convertKeys` - A list of keys in URL arrays that should be converted to querysting params
 70:  *    if paramType == 'querystring'.
 71:  *
 72:  * @var array
 73:  */
 74:     public $options = array(
 75:         'convertKeys' => array('page', 'limit', 'sort', 'direction')
 76:     );
 77: 
 78: /**
 79:  * Constructor for the helper. Sets up the helper that is used for creating 'AJAX' links.
 80:  *
 81:  * Use `public $helpers = array('Paginator' => array('ajax' => 'CustomHelper'));` to set a custom Helper
 82:  * or choose a non JsHelper Helper. If you want to use a specific library with JsHelper declare JsHelper and its
 83:  * adapter before including PaginatorHelper in your helpers array.
 84:  *
 85:  * The chosen custom helper must implement a `link()` method.
 86:  *
 87:  * @param View $View the view object the helper is attached to.
 88:  * @param array $settings Array of settings.
 89:  * @throws CakeException When the AjaxProvider helper does not implement a link method.
 90:  */
 91:     public function __construct(View $View, $settings = array()) {
 92:         $ajaxProvider = isset($settings['ajax']) ? $settings['ajax'] : 'Js';
 93:         $this->helpers[] = $ajaxProvider;
 94:         $this->_ajaxHelperClass = $ajaxProvider;
 95:         App::uses($ajaxProvider . 'Helper', 'View/Helper');
 96:         $classname = $ajaxProvider . 'Helper';
 97:         if (!class_exists($classname) || !method_exists($classname, 'link')) {
 98:             throw new CakeException(
 99:                 __d('cake_dev', '%s does not implement a %s method, it is incompatible with %s', $classname, 'link()', 'PaginatorHelper')
100:             );
101:         }
102:         parent::__construct($View, $settings);
103:     }
104: 
105: /**
106:  * Before render callback. Overridden to merge passed args with URL options.
107:  *
108:  * @param string $viewFile
109:  * @return void
110:  */
111:     public function beforeRender($viewFile) {
112:         $this->options['url'] = array_merge($this->request->params['pass'], $this->request->params['named']);
113:         if (!empty($this->request->query)) {
114:             $this->options['url']['?'] = $this->request->query;
115:         }
116:         parent::beforeRender($viewFile);
117:     }
118: 
119: /**
120:  * Gets the current paging parameters from the resultset for the given model
121:  *
122:  * @param string $model Optional model name. Uses the default if none is specified.
123:  * @return array The array of paging parameters for the paginated resultset.
124:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::params
125:  */
126:     public function params($model = null) {
127:         if (empty($model)) {
128:             $model = $this->defaultModel();
129:         }
130:         if (!isset($this->request->params['paging']) || empty($this->request->params['paging'][$model])) {
131:             return null;
132:         }
133:         return $this->request->params['paging'][$model];
134:     }
135: 
136: /**
137:  * Convenience access to any of the paginator params.
138:  *
139:  * @param string $key Key of the paginator params array to retrieve.
140:  * @param string $model Optional model name. Uses the default if none is specified.
141:  * @return mixed Content of the requested param.
142:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::params
143:  */
144:     public function param($key, $model = null) {
145:         $params = $this->params($model);
146:         if (!isset($params[$key])) {
147:             return null;
148:         }
149:         return $params[$key];
150:     }
151: 
152: /**
153:  * Sets default options for all pagination links
154:  *
155:  * @param array|string $options Default options for pagination links. If a string is supplied - it
156:  *   is used as the DOM id element to update. See PaginatorHelper::$options for list of keys.
157:  * @return void
158:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::options
159:  */
160:     public function options($options = array()) {
161:         if (is_string($options)) {
162:             $options = array('update' => $options);
163:         }
164: 
165:         if (!empty($options['paging'])) {
166:             if (!isset($this->request->params['paging'])) {
167:                 $this->request->params['paging'] = array();
168:             }
169:             $this->request->params['paging'] = array_merge($this->request->params['paging'], $options['paging']);
170:             unset($options['paging']);
171:         }
172:         $model = $this->defaultModel();
173: 
174:         if (!empty($options[$model])) {
175:             if (!isset($this->request->params['paging'][$model])) {
176:                 $this->request->params['paging'][$model] = array();
177:             }
178:             $this->request->params['paging'][$model] = array_merge(
179:                 $this->request->params['paging'][$model], $options[$model]
180:             );
181:             unset($options[$model]);
182:         }
183:         if (!empty($options['convertKeys'])) {
184:             $options['convertKeys'] = array_merge($this->options['convertKeys'], $options['convertKeys']);
185:         }
186:         $this->options = array_filter(array_merge($this->options, $options));
187:     }
188: 
189: /**
190:  * Gets the current page of the recordset for the given model
191:  *
192:  * @param string $model Optional model name. Uses the default if none is specified.
193:  * @return string The current page number of the recordset.
194:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::current
195:  */
196:     public function current($model = null) {
197:         $params = $this->params($model);
198: 
199:         if (isset($params['page'])) {
200:             return $params['page'];
201:         }
202:         return 1;
203:     }
204: 
205: /**
206:  * Gets the current key by which the recordset is sorted
207:  *
208:  * @param string $model Optional model name. Uses the default if none is specified.
209:  * @param array $options Options for pagination links. See #options for list of keys.
210:  * @return string The name of the key by which the recordset is being sorted, or
211:  *  null if the results are not currently sorted.
212:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortKey
213:  */
214:     public function sortKey($model = null, $options = array()) {
215:         if (empty($options)) {
216:             $params = $this->params($model);
217:             $options = $params['options'];
218:         }
219:         if (isset($options['sort']) && !empty($options['sort'])) {
220:             return $options['sort'];
221:         }
222:         if (isset($options['order'])) {
223:             return is_array($options['order']) ? key($options['order']) : $options['order'];
224:         }
225:         if (isset($params['order'])) {
226:             return is_array($params['order']) ? key($params['order']) : $params['order'];
227:         }
228:         return null;
229:     }
230: 
231: /**
232:  * Gets the current direction the recordset is sorted
233:  *
234:  * @param string $model Optional model name. Uses the default if none is specified.
235:  * @param array $options Options for pagination links. See #options for list of keys.
236:  * @return string The direction by which the recordset is being sorted, or
237:  *  null if the results are not currently sorted.
238:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortDir
239:  */
240:     public function sortDir($model = null, $options = array()) {
241:         $dir = null;
242: 
243:         if (empty($options)) {
244:             $params = $this->params($model);
245:             $options = $params['options'];
246:         }
247: 
248:         if (isset($options['direction'])) {
249:             $dir = strtolower($options['direction']);
250:         } elseif (isset($options['order']) && is_array($options['order'])) {
251:             $dir = strtolower(current($options['order']));
252:         } elseif (isset($params['order']) && is_array($params['order'])) {
253:             $dir = strtolower(current($params['order']));
254:         }
255: 
256:         if ($dir === 'desc') {
257:             return 'desc';
258:         }
259:         return 'asc';
260:     }
261: 
262: /**
263:  * Generates a "previous" link for a set of paged records
264:  *
265:  * ### Options:
266:  *
267:  * - `url` Allows sending routing parameters such as controllers, actions or passed arguments.
268:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'. Set this to false to disable this option
269:  * - `escape` Whether you want the contents html entity encoded, defaults to true
270:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
271:  * - `disabledTag` Tag to use instead of A tag when there is no previous page
272:  *
273:  * @param string $title Title for the link. Defaults to '<< Previous'.
274:  * @param array $options Options for pagination link. See #options for list of keys.
275:  * @param string $disabledTitle Title when the link is disabled.
276:  * @param array $disabledOptions Options for the disabled pagination link. See #options for list of keys.
277:  * @return string A "previous" link or $disabledTitle text if the link is disabled.
278:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::prev
279:  */
280:     public function prev($title = '<< Previous', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
281:         $defaults = array(
282:             'rel' => 'prev'
283:         );
284:         $options = array_merge($defaults, (array)$options);
285:         return $this->_pagingLink('Prev', $title, $options, $disabledTitle, $disabledOptions);
286:     }
287: 
288: /**
289:  * Generates a "next" link for a set of paged records
290:  *
291:  * ### Options:
292:  *
293:  * - `url` Allows sending routing parameters such as controllers, actions or passed arguments.
294:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'. Set this to false to disable this option
295:  * - `escape` Whether you want the contents html entity encoded, defaults to true
296:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
297:  * - `disabledTag` Tag to use instead of A tag when there is no next page
298:  *
299:  * @param string $title Title for the link. Defaults to 'Next >>'.
300:  * @param array $options Options for pagination link. See above for list of keys.
301:  * @param string $disabledTitle Title when the link is disabled.
302:  * @param array $disabledOptions Options for the disabled pagination link. See above for list of keys.
303:  * @return string A "next" link or $disabledTitle text if the link is disabled.
304:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::next
305:  */
306:     public function next($title = 'Next >>', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
307:         $defaults = array(
308:             'rel' => 'next'
309:         );
310:         $options = array_merge($defaults, (array)$options);
311:         return $this->_pagingLink('Next', $title, $options, $disabledTitle, $disabledOptions);
312:     }
313: 
314: /**
315:  * Generates a sorting link. Sets named parameters for the sort and direction. Handles
316:  * direction switching automatically.
317:  *
318:  * ### Options:
319:  *
320:  * - `escape` Whether you want the contents html entity encoded, defaults to true
321:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
322:  * - `direction` The default direction to use when this link isn't active.
323:  *
324:  * @param string $key The name of the key that the recordset should be sorted.
325:  * @param string $title Title for the link. If $title is null $key will be used
326:  *      for the title and will be generated by inflection.
327:  * @param array $options Options for sorting link. See above for list of keys.
328:  * @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
329:  *  key the returned link will sort by 'desc'.
330:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sort
331:  */
332:     public function sort($key, $title = null, $options = array()) {
333:         $options = array_merge(array('url' => array(), 'model' => null), $options);
334:         $url = $options['url'];
335:         unset($options['url']);
336: 
337:         if (empty($title)) {
338:             $title = $key;
339: 
340:             if (strpos($title, '.') !== false) {
341:                 $title = str_replace('.', ' ', $title);
342:             }
343: 
344:             $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
345:         }
346:         $dir = isset($options['direction']) ? $options['direction'] : 'asc';
347:         unset($options['direction']);
348: 
349:         $sortKey = $this->sortKey($options['model']);
350:         $defaultModel = $this->defaultModel();
351:         $isSorted = (
352:             $sortKey === $key ||
353:             $sortKey === $defaultModel . '.' . $key ||
354:             $key === $defaultModel . '.' . $sortKey
355:         );
356: 
357:         if ($isSorted) {
358:             $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
359:             $class = $dir === 'asc' ? 'desc' : 'asc';
360:             if (!empty($options['class'])) {
361:                 $options['class'] .= ' ' . $class;
362:             } else {
363:                 $options['class'] = $class;
364:             }
365:         }
366:         if (is_array($title) && array_key_exists($dir, $title)) {
367:             $title = $title[$dir];
368:         }
369: 
370:         $url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
371:         return $this->link($title, $url, $options);
372:     }
373: 
374: /**
375:  * Generates a plain or Ajax link with pagination parameters
376:  *
377:  * ### Options
378:  *
379:  * - `update` The Id of the DOM element you wish to update. Creates Ajax enabled links
380:  *    with the AjaxHelper.
381:  * - `escape` Whether you want the contents html entity encoded, defaults to true
382:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
383:  *
384:  * @param string $title Title for the link.
385:  * @param string|array $url URL for the action. See Router::url()
386:  * @param array $options Options for the link. See #options for list of keys.
387:  * @return string A link with pagination parameters.
388:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::link
389:  */
390:     public function link($title, $url = array(), $options = array()) {
391:         $options = array_merge(array('model' => null, 'escape' => true), $options);
392:         $model = $options['model'];
393:         unset($options['model']);
394: 
395:         if (!empty($this->options)) {
396:             $options = array_merge($this->options, $options);
397:         }
398:         if (isset($options['url'])) {
399:             $url = array_merge((array)$options['url'], (array)$url);
400:             unset($options['url']);
401:         }
402:         unset($options['convertKeys']);
403: 
404:         $url = $this->url($url, true, $model);
405: 
406:         $obj = isset($options['update']) ? $this->_ajaxHelperClass : 'Html';
407:         return $this->{$obj}->link($title, $url, $options);
408:     }
409: 
410: /**
411:  * Merges passed URL options with current pagination state to generate a pagination URL.
412:  *
413:  * @param array $options Pagination/URL options array
414:  * @param boolean $asArray Return the URL as an array, or a URI string
415:  * @param string $model Which model to paginate on
416:  * @return mixed By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
417:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::url
418:  */
419:     public function url($options = array(), $asArray = false, $model = null) {
420:         $paging = $this->params($model);
421:         $url = array_merge(array_filter($paging['options']), $options);
422: 
423:         if (isset($url['order'])) {
424:             $sort = $direction = null;
425:             if (is_array($url['order'])) {
426:                 list($sort, $direction) = array($this->sortKey($model, $url), current($url['order']));
427:             }
428:             unset($url['order']);
429:             $url = array_merge($url, compact('sort', 'direction'));
430:         }
431:         $url = $this->_convertUrlKeys($url, $paging['paramType']);
432:         if (!empty($url['page']) && $url['page'] == 1) {
433:             $url['page'] = null;
434:         }
435:         if (!empty($url['?']['page']) && $url['?']['page'] == 1) {
436:             unset($url['?']['page']);
437:         }
438:         if ($asArray) {
439:             return $url;
440:         }
441:         return parent::url($url);
442:     }
443: 
444: /**
445:  * Converts the keys being used into the format set by options.paramType
446:  *
447:  * @param array $url Array of URL params to convert
448:  * @param string $type
449:  * @return array converted URL params.
450:  */
451:     protected function _convertUrlKeys($url, $type) {
452:         if ($type === 'named') {
453:             return $url;
454:         }
455:         if (!isset($url['?'])) {
456:             $url['?'] = array();
457:         }
458:         foreach ($this->options['convertKeys'] as $key) {
459:             if (isset($url[$key])) {
460:                 $url['?'][$key] = $url[$key];
461:                 unset($url[$key]);
462:             }
463:         }
464:         return $url;
465:     }
466: 
467: /**
468:  * Protected method for generating prev/next links
469:  *
470:  * @param string $which
471:  * @param string $title
472:  * @param array $options
473:  * @param string $disabledTitle
474:  * @param array $disabledOptions
475:  * @return string
476:  */
477:     protected function _pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array()) {
478:         $check = 'has' . $which;
479:         $_defaults = array(
480:             'url' => array(), 'step' => 1, 'escape' => true, 'model' => null,
481:             'tag' => 'span', 'class' => strtolower($which), 'disabledTag' => null
482:         );
483:         $options = array_merge($_defaults, (array)$options);
484:         $paging = $this->params($options['model']);
485:         if (empty($disabledOptions)) {
486:             $disabledOptions = $options;
487:         }
488: 
489:         if (!$this->{$check}($options['model']) && (!empty($disabledTitle) || !empty($disabledOptions))) {
490:             if (!empty($disabledTitle) && $disabledTitle !== true) {
491:                 $title = $disabledTitle;
492:             }
493:             $options = array_merge($_defaults, (array)$disabledOptions);
494:         } elseif (!$this->{$check}($options['model'])) {
495:             return null;
496:         }
497: 
498:         foreach (array_keys($_defaults) as $key) {
499:             ${$key} = $options[$key];
500:             unset($options[$key]);
501:         }
502: 
503:         if ($this->{$check}($model)) {
504:             $url = array_merge(
505:                 array('page' => $paging['page'] + ($which === 'Prev' ? $step * -1 : $step)),
506:                 $url
507:             );
508:             if ($tag === false) {
509:                 return $this->link(
510:                     $title,
511:                     $url,
512:                     compact('escape', 'model', 'class') + $options
513:                 );
514:             }
515:             $link = $this->link($title, $url, compact('escape', 'model') + $options);
516:             return $this->Html->tag($tag, $link, compact('class'));
517:         }
518:         unset($options['rel']);
519:         if (!$tag) {
520:             if ($disabledTag) {
521:                 $tag = $disabledTag;
522:                 $disabledTag = null;
523:             } else {
524:                 $tag = $_defaults['tag'];
525:             }
526:         }
527:         if ($disabledTag) {
528:             $title = $this->Html->tag($disabledTag, $title, compact('escape') + $options);
529:             return $this->Html->tag($tag, $title, compact('class'));
530:         }
531:         return $this->Html->tag($tag, $title, compact('escape', 'class') + $options);
532:     }
533: 
534: /**
535:  * Returns true if the given result set is not at the first page
536:  *
537:  * @param string $model Optional model name. Uses the default if none is specified.
538:  * @return boolean True if the result set is not at the first page.
539:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPrev
540:  */
541:     public function hasPrev($model = null) {
542:         return $this->_hasPage($model, 'prev');
543:     }
544: 
545: /**
546:  * Returns true if the given result set is not at the last page
547:  *
548:  * @param string $model Optional model name. Uses the default if none is specified.
549:  * @return boolean True if the result set is not at the last page.
550:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasNext
551:  */
552:     public function hasNext($model = null) {
553:         return $this->_hasPage($model, 'next');
554:     }
555: 
556: /**
557:  * Returns true if the given result set has the page number given by $page
558:  *
559:  * @param string $model Optional model name. Uses the default if none is specified.
560:  * @param integer $page The page number - if not set defaults to 1.
561:  * @return boolean True if the given result set has the specified page number.
562:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPage
563:  */
564:     public function hasPage($model = null, $page = 1) {
565:         if (is_numeric($model)) {
566:             $page = $model;
567:             $model = null;
568:         }
569:         $paging = $this->params($model);
570:         return $page <= $paging['pageCount'];
571:     }
572: 
573: /**
574:  * Does $model have $page in its range?
575:  *
576:  * @param string $model Model name to get parameters for.
577:  * @param integer $page Page number you are checking.
578:  * @return boolean Whether model has $page
579:  */
580:     protected function _hasPage($model, $page) {
581:         $params = $this->params($model);
582:         return !empty($params) && $params[$page . 'Page'];
583:     }
584: 
585: /**
586:  * Gets the default model of the paged sets
587:  *
588:  * @return string Model name or null if the pagination isn't initialized.
589:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::defaultModel
590:  */
591:     public function defaultModel() {
592:         if ($this->_defaultModel) {
593:             return $this->_defaultModel;
594:         }
595:         if (empty($this->request->params['paging'])) {
596:             return null;
597:         }
598:         list($this->_defaultModel) = array_keys($this->request->params['paging']);
599:         return $this->_defaultModel;
600:     }
601: 
602: /**
603:  * Returns a counter string for the paged result set
604:  *
605:  * ### Options
606:  *
607:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel();
608:  * - `format` The format string you want to use, defaults to 'pages' Which generates output like '1 of 5'
609:  *    set to 'range' to generate output like '1 - 3 of 13'. Can also be set to a custom string, containing
610:  *    the following placeholders `{:page}`, `{:pages}`, `{:current}`, `{:count}`, `{:model}`, `{:start}`, `{:end}` and any
611:  *    custom content you would like.
612:  * - `separator` The separator string to use, default to ' of '
613:  *
614:  * The `%page%` style placeholders also work, but are deprecated and will be removed in a future version.
615:  * @param array $options Options for the counter string. See #options for list of keys.
616:  * @return string Counter string.
617:  * @deprecated The %page% style placeholders are deprecated.
618:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::counter
619:  */
620:     public function counter($options = array()) {
621:         if (is_string($options)) {
622:             $options = array('format' => $options);
623:         }
624: 
625:         $options = array_merge(
626:             array(
627:                 'model' => $this->defaultModel(),
628:                 'format' => 'pages',
629:                 'separator' => __d('cake', ' of ')
630:             ),
631:         $options);
632: 
633:         $paging = $this->params($options['model']);
634:         if (!$paging['pageCount']) {
635:             $paging['pageCount'] = 1;
636:         }
637:         $start = 0;
638:         if ($paging['count'] >= 1) {
639:             $start = (($paging['page'] - 1) * $paging['limit']) + 1;
640:         }
641:         $end = $start + $paging['limit'] - 1;
642:         if ($paging['count'] < $end) {
643:             $end = $paging['count'];
644:         }
645: 
646:         switch ($options['format']) {
647:             case 'range':
648:                 if (!is_array($options['separator'])) {
649:                     $options['separator'] = array(' - ', $options['separator']);
650:                 }
651:                 $out = $start . $options['separator'][0] . $end . $options['separator'][1];
652:                 $out .= $paging['count'];
653:                 break;
654:             case 'pages':
655:                 $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
656:                 break;
657:             default:
658:                 $map = array(
659:                     '%page%' => $paging['page'],
660:                     '%pages%' => $paging['pageCount'],
661:                     '%current%' => $paging['current'],
662:                     '%count%' => $paging['count'],
663:                     '%start%' => $start,
664:                     '%end%' => $end,
665:                     '%model%' => strtolower(Inflector::humanize(Inflector::tableize($options['model'])))
666:                 );
667:                 $out = str_replace(array_keys($map), array_values($map), $options['format']);
668: 
669:                 $newKeys = array(
670:                     '{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}', '{:model}'
671:                 );
672:                 $out = str_replace($newKeys, array_values($map), $out);
673:         }
674:         return $out;
675:     }
676: 
677: /**
678:  * Returns a set of numbers for the paged result set
679:  * uses a modulus to decide how many numbers to show on each side of the current page (default: 8).
680:  *
681:  * `$this->Paginator->numbers(array('first' => 2, 'last' => 2));`
682:  *
683:  * Using the first and last options you can create links to the beginning and end of the page set.
684:  *
685:  * ### Options
686:  *
687:  * - `before` Content to be inserted before the numbers
688:  * - `after` Content to be inserted after the numbers
689:  * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
690:  * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
691:  * - `separator` Separator content defaults to ' | '
692:  * - `tag` The tag to wrap links in, defaults to 'span'
693:  * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
694:  *    links to generate.
695:  * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
696:  *    links to generate.
697:  * - `ellipsis` Ellipsis content, defaults to '...'
698:  * - `class` Class for wrapper tag
699:  * - `currentClass` Class for wrapper tag on current active page, defaults to 'current'
700:  * - `currentTag` Tag to use for current page number, defaults to null
701:  *
702:  * @param array $options Options for the numbers, (before, after, model, modulus, separator)
703:  * @return string numbers string.
704:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::numbers
705:  */
706:     public function numbers($options = array()) {
707:         if ($options === true) {
708:             $options = array(
709:                 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
710:             );
711:         }
712: 
713:         $defaults = array(
714:             'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
715:             'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
716:             'currentClass' => 'current', 'currentTag' => null
717:         );
718:         $options += $defaults;
719: 
720:         $params = (array)$this->params($options['model']) + array('page' => 1);
721:         unset($options['model']);
722: 
723:         if ($params['pageCount'] <= 1) {
724:             return false;
725:         }
726: 
727:         extract($options);
728:         unset($options['tag'], $options['before'], $options['after'], $options['model'],
729:             $options['modulus'], $options['separator'], $options['first'], $options['last'],
730:             $options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
731:         );
732: 
733:         $out = '';
734: 
735:         if ($modulus && $params['pageCount'] > $modulus) {
736:             $half = intval($modulus / 2);
737:             $end = $params['page'] + $half;
738: 
739:             if ($end > $params['pageCount']) {
740:                 $end = $params['pageCount'];
741:             }
742:             $start = $params['page'] - ($modulus - ($end - $params['page']));
743:             if ($start <= 1) {
744:                 $start = 1;
745:                 $end = $params['page'] + ($modulus - $params['page']) + 1;
746:             }
747: 
748:             if ($first && $start > 1) {
749:                 $offset = ($start <= (int)$first) ? $start - 1 : $first;
750:                 if ($offset < $start - 1) {
751:                     $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
752:                 } else {
753:                     $out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
754:                 }
755:             }
756: 
757:             $out .= $before;
758: 
759:             for ($i = $start; $i < $params['page']; $i++) {
760:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
761:             }
762: 
763:             if ($class) {
764:                 $currentClass .= ' ' . $class;
765:             }
766:             if ($currentTag) {
767:                 $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass));
768:             } else {
769:                 $out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
770:             }
771:             if ($i != $params['pageCount']) {
772:                 $out .= $separator;
773:             }
774: 
775:             $start = $params['page'] + 1;
776:             for ($i = $start; $i < $end; $i++) {
777:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
778:             }
779: 
780:             if ($end != $params['page']) {
781:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
782:             }
783: 
784:             $out .= $after;
785: 
786:             if ($last && $end < $params['pageCount']) {
787:                 $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
788:                 if ($offset <= $last && $params['pageCount'] - $end > $offset) {
789:                     $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
790:                 } else {
791:                     $out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
792:                 }
793:             }
794: 
795:         } else {
796:             $out .= $before;
797: 
798:             for ($i = 1; $i <= $params['pageCount']; $i++) {
799:                 if ($i == $params['page']) {
800:                     if ($class) {
801:                         $currentClass .= ' ' . $class;
802:                     }
803:                     if ($currentTag) {
804:                         $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass));
805:                     } else {
806:                         $out .= $this->Html->tag($tag, $i, array('class' => $currentClass));
807:                     }
808:                 } else {
809:                     $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
810:                 }
811:                 if ($i != $params['pageCount']) {
812:                     $out .= $separator;
813:                 }
814:             }
815: 
816:             $out .= $after;
817:         }
818: 
819:         return $out;
820:     }
821: 
822: /**
823:  * Returns a first or set of numbers for the first pages.
824:  *
825:  * `echo $this->Paginator->first('< first');`
826:  *
827:  * Creates a single link for the first page. Will output nothing if you are on the first page.
828:  *
829:  * `echo $this->Paginator->first(3);`
830:  *
831:  * Will create links for the first 3 pages, once you get to the third or greater page. Prior to that
832:  * nothing will be output.
833:  *
834:  * ### Options:
835:  *
836:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
837:  * - `after` Content to insert after the link/tag
838:  * - `model` The model to use defaults to PaginatorHelper::defaultModel()
839:  * - `separator` Content between the generated links, defaults to ' | '
840:  * - `ellipsis` Content for ellipsis, defaults to '...'
841:  *
842:  * @param string|integer $first if string use as label for the link. If numeric, the number of page links
843:  *   you want at the beginning of the range.
844:  * @param array $options An array of options.
845:  * @return string numbers string.
846:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
847:  */
848:     public function first($first = '<< first', $options = array()) {
849:         $options = array_merge(
850:             array(
851:                 'tag' => 'span',
852:                 'after' => null,
853:                 'model' => $this->defaultModel(),
854:                 'separator' => ' | ',
855:                 'ellipsis' => '...',
856:                 'class' => null
857:             ),
858:         (array)$options);
859: 
860:         $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
861:         unset($options['model']);
862: 
863:         if ($params['pageCount'] <= 1) {
864:             return false;
865:         }
866:         extract($options);
867:         unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
868: 
869:         $out = '';
870: 
871:         if (is_int($first) && $params['page'] >= $first) {
872:             if ($after === null) {
873:                 $after = $ellipsis;
874:             }
875:             for ($i = 1; $i <= $first; $i++) {
876:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
877:                 if ($i != $first) {
878:                     $out .= $separator;
879:                 }
880:             }
881:             $out .= $after;
882:         } elseif ($params['page'] > 1 && is_string($first)) {
883:             $options += array('rel' => 'first');
884:             $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
885:         }
886:         return $out;
887:     }
888: 
889: /**
890:  * Returns a last or set of numbers for the last pages.
891:  *
892:  * `echo $this->Paginator->last('last >');`
893:  *
894:  * Creates a single link for the last page. Will output nothing if you are on the last page.
895:  *
896:  * `echo $this->Paginator->last(3);`
897:  *
898:  * Will create links for the last 3 pages. Once you enter the page range, no output will be created.
899:  *
900:  * ### Options:
901:  *
902:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
903:  * - `before` Content to insert before the link/tag
904:  * - `model` The model to use defaults to PaginatorHelper::defaultModel()
905:  * - `separator` Content between the generated links, defaults to ' | '
906:  * - `ellipsis` Content for ellipsis, defaults to '...'
907:  *
908:  * @param string|integer $last if string use as label for the link, if numeric print page numbers
909:  * @param array $options Array of options
910:  * @return string numbers string.
911:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::last
912:  */
913:     public function last($last = 'last >>', $options = array()) {
914:         $options = array_merge(
915:             array(
916:                 'tag' => 'span',
917:                 'before' => null,
918:                 'model' => $this->defaultModel(),
919:                 'separator' => ' | ',
920:                 'ellipsis' => '...',
921:                 'class' => null
922:             ),
923:         (array)$options);
924: 
925:         $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
926:         unset($options['model']);
927: 
928:         if ($params['pageCount'] <= 1) {
929:             return false;
930:         }
931: 
932:         extract($options);
933:         unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
934: 
935:         $out = '';
936:         $lower = $params['pageCount'] - $last + 1;
937: 
938:         if (is_int($last) && $params['page'] <= $lower) {
939:             if ($before === null) {
940:                 $before = $ellipsis;
941:             }
942:             for ($i = $lower; $i <= $params['pageCount']; $i++) {
943:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
944:                 if ($i != $params['pageCount']) {
945:                     $out .= $separator;
946:                 }
947:             }
948:             $out = $before . $out;
949:         } elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
950:             $options += array('rel' => 'last');
951:             $out = $before . $this->Html->tag(
952:                 $tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
953:             );
954:         }
955:         return $out;
956:     }
957: 
958: }
959: 
OpenHub
Rackspace
Rackspace
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (GitHub)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • Slack
  • Paid Support

Generated using CakePHP API Docs