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.3 API

  • Overview
  • Tree
  • Deprecated
  • Version:
    • 2.3
      • 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(sprintf(
 99:                 __d('cake_dev', '%s does not implement a link() method, it is incompatible with PaginatorHelper'), $classname
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:  * Sets default options for all pagination links
138:  *
139:  * @param array|string $options Default options for pagination links. If a string is supplied - it
140:  *   is used as the DOM id element to update. See PaginatorHelper::$options for list of keys.
141:  * @return void
142:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::options
143:  */
144:     public function options($options = array()) {
145:         if (is_string($options)) {
146:             $options = array('update' => $options);
147:         }
148: 
149:         if (!empty($options['paging'])) {
150:             if (!isset($this->request->params['paging'])) {
151:                 $this->request->params['paging'] = array();
152:             }
153:             $this->request->params['paging'] = array_merge($this->request->params['paging'], $options['paging']);
154:             unset($options['paging']);
155:         }
156:         $model = $this->defaultModel();
157: 
158:         if (!empty($options[$model])) {
159:             if (!isset($this->request->params['paging'][$model])) {
160:                 $this->request->params['paging'][$model] = array();
161:             }
162:             $this->request->params['paging'][$model] = array_merge(
163:                 $this->request->params['paging'][$model], $options[$model]
164:             );
165:             unset($options[$model]);
166:         }
167:         if (!empty($options['convertKeys'])) {
168:             $options['convertKeys'] = array_merge($this->options['convertKeys'], $options['convertKeys']);
169:         }
170:         $this->options = array_filter(array_merge($this->options, $options));
171:     }
172: 
173: /**
174:  * Gets the current page of the recordset for the given model
175:  *
176:  * @param string $model Optional model name. Uses the default if none is specified.
177:  * @return string The current page number of the recordset.
178:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::current
179:  */
180:     public function current($model = null) {
181:         $params = $this->params($model);
182: 
183:         if (isset($params['page'])) {
184:             return $params['page'];
185:         }
186:         return 1;
187:     }
188: 
189: /**
190:  * Gets the current key by which the recordset is sorted
191:  *
192:  * @param string $model Optional model name. Uses the default if none is specified.
193:  * @param array $options Options for pagination links. See #options for list of keys.
194:  * @return string The name of the key by which the recordset is being sorted, or
195:  *  null if the results are not currently sorted.
196:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortKey
197:  */
198:     public function sortKey($model = null, $options = array()) {
199:         if (empty($options)) {
200:             $params = $this->params($model);
201:             $options = $params['options'];
202:         }
203:         if (isset($options['sort']) && !empty($options['sort'])) {
204:             return $options['sort'];
205:         }
206:         if (isset($options['order'])) {
207:             return is_array($options['order']) ? key($options['order']) : $options['order'];
208:         }
209:         if (isset($params['order'])) {
210:             return is_array($params['order']) ? key($params['order']) : $params['order'];
211:         }
212:         return null;
213:     }
214: 
215: /**
216:  * Gets the current direction the recordset is sorted
217:  *
218:  * @param string $model Optional model name. Uses the default if none is specified.
219:  * @param array $options Options for pagination links. See #options for list of keys.
220:  * @return string The direction by which the recordset is being sorted, or
221:  *  null if the results are not currently sorted.
222:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortDir
223:  */
224:     public function sortDir($model = null, $options = array()) {
225:         $dir = null;
226: 
227:         if (empty($options)) {
228:             $params = $this->params($model);
229:             $options = $params['options'];
230:         }
231: 
232:         if (isset($options['direction'])) {
233:             $dir = strtolower($options['direction']);
234:         } elseif (isset($options['order']) && is_array($options['order'])) {
235:             $dir = strtolower(current($options['order']));
236:         } elseif (isset($params['order']) && is_array($params['order'])) {
237:             $dir = strtolower(current($params['order']));
238:         }
239: 
240:         if ($dir === 'desc') {
241:             return 'desc';
242:         }
243:         return 'asc';
244:     }
245: 
246: /**
247:  * Generates a "previous" link for a set of paged records
248:  *
249:  * ### Options:
250:  *
251:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'. Set this to false to disable this option
252:  * - `escape` Whether you want the contents html entity encoded, defaults to true
253:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
254:  * - `disabledTag` Tag to use instead of A tag when there is no previous page
255:  *
256:  * @param string $title Title for the link. Defaults to '<< Previous'.
257:  * @param array $options Options for pagination link. See #options for list of keys.
258:  * @param string $disabledTitle Title when the link is disabled.
259:  * @param array $disabledOptions Options for the disabled pagination link. See #options for list of keys.
260:  * @return string A "previous" link or $disabledTitle text if the link is disabled.
261:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::prev
262:  */
263:     public function prev($title = '<< Previous', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
264:         $defaults = array(
265:             'rel' => 'prev'
266:         );
267:         $options = array_merge($defaults, (array)$options);
268:         return $this->_pagingLink('Prev', $title, $options, $disabledTitle, $disabledOptions);
269:     }
270: 
271: /**
272:  * Generates a "next" link for a set of paged records
273:  *
274:  * ### Options:
275:  *
276:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'. Set this to false to disable this option
277:  * - `escape` Whether you want the contents html entity encoded, defaults to true
278:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
279:  * - `disabledTag` Tag to use instead of A tag when there is no next page
280:  *
281:  * @param string $title Title for the link. Defaults to 'Next >>'.
282:  * @param array $options Options for pagination link. See above for list of keys.
283:  * @param string $disabledTitle Title when the link is disabled.
284:  * @param array $disabledOptions Options for the disabled pagination link. See above for list of keys.
285:  * @return string A "next" link or $disabledTitle text if the link is disabled.
286:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::next
287:  */
288:     public function next($title = 'Next >>', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
289:         $defaults = array(
290:             'rel' => 'next'
291:         );
292:         $options = array_merge($defaults, (array)$options);
293:         return $this->_pagingLink('Next', $title, $options, $disabledTitle, $disabledOptions);
294:     }
295: 
296: /**
297:  * Generates a sorting link. Sets named parameters for the sort and direction. Handles
298:  * direction switching automatically.
299:  *
300:  * ### Options:
301:  *
302:  * - `escape` Whether you want the contents html entity encoded, defaults to true
303:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
304:  * - `direction` The default direction to use when this link isn't active.
305:  *
306:  * @param string $key The name of the key that the recordset should be sorted.
307:  * @param string $title Title for the link. If $title is null $key will be used
308:  *      for the title and will be generated by inflection.
309:  * @param array $options Options for sorting link. See above for list of keys.
310:  * @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
311:  *  key the returned link will sort by 'desc'.
312:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sort
313:  */
314:     public function sort($key, $title = null, $options = array()) {
315:         $options = array_merge(array('url' => array(), 'model' => null), $options);
316:         $url = $options['url'];
317:         unset($options['url']);
318: 
319:         if (empty($title)) {
320:             $title = $key;
321: 
322:             if (strpos($title, '.') !== false) {
323:                 $title = str_replace('.', ' ', $title);
324:             }
325: 
326:             $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
327:         }
328:         $dir = isset($options['direction']) ? $options['direction'] : 'asc';
329:         unset($options['direction']);
330: 
331:         $sortKey = $this->sortKey($options['model']);
332:         $defaultModel = $this->defaultModel();
333:         $isSorted = (
334:             $sortKey === $key ||
335:             $sortKey === $defaultModel . '.' . $key ||
336:             $key === $defaultModel . '.' . $sortKey
337:         );
338: 
339:         if ($isSorted) {
340:             $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
341:             $class = $dir === 'asc' ? 'desc' : 'asc';
342:             if (!empty($options['class'])) {
343:                 $options['class'] .= ' ' . $class;
344:             } else {
345:                 $options['class'] = $class;
346:             }
347:         }
348:         if (is_array($title) && array_key_exists($dir, $title)) {
349:             $title = $title[$dir];
350:         }
351: 
352:         $url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
353:         return $this->link($title, $url, $options);
354:     }
355: 
356: /**
357:  * Generates a plain or Ajax link with pagination parameters
358:  *
359:  * ### Options
360:  *
361:  * - `update` The Id of the DOM element you wish to update. Creates Ajax enabled links
362:  *    with the AjaxHelper.
363:  * - `escape` Whether you want the contents html entity encoded, defaults to true
364:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
365:  *
366:  * @param string $title Title for the link.
367:  * @param string|array $url Url for the action. See Router::url()
368:  * @param array $options Options for the link. See #options for list of keys.
369:  * @return string A link with pagination parameters.
370:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::link
371:  */
372:     public function link($title, $url = array(), $options = array()) {
373:         $options = array_merge(array('model' => null, 'escape' => true), $options);
374:         $model = $options['model'];
375:         unset($options['model']);
376: 
377:         if (!empty($this->options)) {
378:             $options = array_merge($this->options, $options);
379:         }
380:         if (isset($options['url'])) {
381:             $url = array_merge((array)$options['url'], (array)$url);
382:             unset($options['url']);
383:         }
384:         unset($options['convertKeys']);
385: 
386:         $url = $this->url($url, true, $model);
387: 
388:         $obj = isset($options['update']) ? $this->_ajaxHelperClass : 'Html';
389:         return $this->{$obj}->link($title, $url, $options);
390:     }
391: 
392: /**
393:  * Merges passed URL options with current pagination state to generate a pagination URL.
394:  *
395:  * @param array $options Pagination/URL options array
396:  * @param boolean $asArray Return the url as an array, or a URI string
397:  * @param string $model Which model to paginate on
398:  * @return mixed By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
399:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::url
400:  */
401:     public function url($options = array(), $asArray = false, $model = null) {
402:         $paging = $this->params($model);
403:         $url = array_merge(array_filter($paging['options']), $options);
404: 
405:         if (isset($url['order'])) {
406:             $sort = $direction = null;
407:             if (is_array($url['order'])) {
408:                 list($sort, $direction) = array($this->sortKey($model, $url), current($url['order']));
409:             }
410:             unset($url['order']);
411:             $url = array_merge($url, compact('sort', 'direction'));
412:         }
413:         $url = $this->_convertUrlKeys($url, $paging['paramType']);
414: 
415:         if ($asArray) {
416:             return $url;
417:         }
418:         return parent::url($url);
419:     }
420: 
421: /**
422:  * Converts the keys being used into the format set by options.paramType
423:  *
424:  * @param array $url Array of url params to convert
425:  * @param string $type
426:  * @return array converted url params.
427:  */
428:     protected function _convertUrlKeys($url, $type) {
429:         if ($type === 'named') {
430:             return $url;
431:         }
432:         if (!isset($url['?'])) {
433:             $url['?'] = array();
434:         }
435:         foreach ($this->options['convertKeys'] as $key) {
436:             if (isset($url[$key])) {
437:                 $url['?'][$key] = $url[$key];
438:                 unset($url[$key]);
439:             }
440:         }
441:         return $url;
442:     }
443: 
444: /**
445:  * Protected method for generating prev/next links
446:  *
447:  * @param string $which
448:  * @param string $title
449:  * @param array $options
450:  * @param string $disabledTitle
451:  * @param array $disabledOptions
452:  * @return string
453:  */
454:     protected function _pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array()) {
455:         $check = 'has' . $which;
456:         $_defaults = array(
457:             'url' => array(), 'step' => 1, 'escape' => true, 'model' => null,
458:             'tag' => 'span', 'class' => strtolower($which), 'disabledTag' => null
459:         );
460:         $options = array_merge($_defaults, (array)$options);
461:         $paging = $this->params($options['model']);
462:         if (empty($disabledOptions)) {
463:             $disabledOptions = $options;
464:         }
465: 
466:         if (!$this->{$check}($options['model']) && (!empty($disabledTitle) || !empty($disabledOptions))) {
467:             if (!empty($disabledTitle) && $disabledTitle !== true) {
468:                 $title = $disabledTitle;
469:             }
470:             $options = array_merge($_defaults, (array)$disabledOptions);
471:         } elseif (!$this->{$check}($options['model'])) {
472:             return null;
473:         }
474: 
475:         foreach (array_keys($_defaults) as $key) {
476:             ${$key} = $options[$key];
477:             unset($options[$key]);
478:         }
479: 
480:         if ($this->{$check}($model)) {
481:             $url = array_merge(
482:                 array('page' => $paging['page'] + ($which === 'Prev' ? $step * -1 : $step)),
483:                 $url
484:             );
485:             if ($tag === false) {
486:                 return $this->link(
487:                     $title,
488:                     $url,
489:                     compact('escape', 'model', 'class') + $options
490:                 );
491:             }
492:             $link = $this->link($title, $url, compact('escape', 'model') + $options);
493:             return $this->Html->tag($tag, $link, compact('class'));
494:         }
495:         unset($options['rel']);
496:         if (!$tag) {
497:             if ($disabledTag) {
498:                 $tag = $disabledTag;
499:                 $disabledTag = null;
500:             } else {
501:                 $tag = $_defaults['tag'];
502:             }
503:         }
504:         if ($disabledTag) {
505:             $title = $this->Html->tag($disabledTag, $title, compact('escape') + $options);
506:             return $this->Html->tag($tag, $title, compact('class'));
507:         }
508:         return $this->Html->tag($tag, $title, compact('escape', 'class') + $options);
509:     }
510: 
511: /**
512:  * Returns true if the given result set is not at the first page
513:  *
514:  * @param string $model Optional model name. Uses the default if none is specified.
515:  * @return boolean True if the result set is not at the first page.
516:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPrev
517:  */
518:     public function hasPrev($model = null) {
519:         return $this->_hasPage($model, 'prev');
520:     }
521: 
522: /**
523:  * Returns true if the given result set is not at the last page
524:  *
525:  * @param string $model Optional model name. Uses the default if none is specified.
526:  * @return boolean True if the result set is not at the last page.
527:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasNext
528:  */
529:     public function hasNext($model = null) {
530:         return $this->_hasPage($model, 'next');
531:     }
532: 
533: /**
534:  * Returns true if the given result set has the page number given by $page
535:  *
536:  * @param string $model Optional model name. Uses the default if none is specified.
537:  * @param integer $page The page number - if not set defaults to 1.
538:  * @return boolean True if the given result set has the specified page number.
539:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPage
540:  */
541:     public function hasPage($model = null, $page = 1) {
542:         if (is_numeric($model)) {
543:             $page = $model;
544:             $model = null;
545:         }
546:         $paging = $this->params($model);
547:         return $page <= $paging['pageCount'];
548:     }
549: 
550: /**
551:  * Does $model have $page in its range?
552:  *
553:  * @param string $model Model name to get parameters for.
554:  * @param integer $page Page number you are checking.
555:  * @return boolean Whether model has $page
556:  */
557:     protected function _hasPage($model, $page) {
558:         $params = $this->params($model);
559:         return !empty($params) && $params[$page . 'Page'];
560:     }
561: 
562: /**
563:  * Gets the default model of the paged sets
564:  *
565:  * @return string Model name or null if the pagination isn't initialized.
566:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::defaultModel
567:  */
568:     public function defaultModel() {
569:         if ($this->_defaultModel) {
570:             return $this->_defaultModel;
571:         }
572:         if (empty($this->request->params['paging'])) {
573:             return null;
574:         }
575:         list($this->_defaultModel) = array_keys($this->request->params['paging']);
576:         return $this->_defaultModel;
577:     }
578: 
579: /**
580:  * Returns a counter string for the paged result set
581:  *
582:  * ### Options
583:  *
584:  * - `model` The model to use, defaults to PaginatorHelper::defaultModel();
585:  * - `format` The format string you want to use, defaults to 'pages' Which generates output like '1 of 5'
586:  *    set to 'range' to generate output like '1 - 3 of 13'. Can also be set to a custom string, containing
587:  *    the following placeholders `{:page}`, `{:pages}`, `{:current}`, `{:count}`, `{:model}`, `{:start}`, `{:end}` and any
588:  *    custom content you would like.
589:  * - `separator` The separator string to use, default to ' of '
590:  *
591:  * The `%page%` style placeholders also work, but are deprecated and will be removed in a future version.
592:  * @param array $options Options for the counter string. See #options for list of keys.
593:  * @return string Counter string.
594:  * @deprecated The %page% style placeholders are deprecated.
595:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::counter
596:  */
597:     public function counter($options = array()) {
598:         if (is_string($options)) {
599:             $options = array('format' => $options);
600:         }
601: 
602:         $options = array_merge(
603:             array(
604:                 'model' => $this->defaultModel(),
605:                 'format' => 'pages',
606:                 'separator' => __d('cake', ' of ')
607:             ),
608:         $options);
609: 
610:         $paging = $this->params($options['model']);
611:         if (!$paging['pageCount']) {
612:             $paging['pageCount'] = 1;
613:         }
614:         $start = 0;
615:         if ($paging['count'] >= 1) {
616:             $start = (($paging['page'] - 1) * $paging['limit']) + 1;
617:         }
618:         $end = $start + $paging['limit'] - 1;
619:         if ($paging['count'] < $end) {
620:             $end = $paging['count'];
621:         }
622: 
623:         switch ($options['format']) {
624:             case 'range':
625:                 if (!is_array($options['separator'])) {
626:                     $options['separator'] = array(' - ', $options['separator']);
627:                 }
628:                 $out = $start . $options['separator'][0] . $end . $options['separator'][1];
629:                 $out .= $paging['count'];
630:                 break;
631:             case 'pages':
632:                 $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
633:                 break;
634:             default:
635:                 $map = array(
636:                     '%page%' => $paging['page'],
637:                     '%pages%' => $paging['pageCount'],
638:                     '%current%' => $paging['current'],
639:                     '%count%' => $paging['count'],
640:                     '%start%' => $start,
641:                     '%end%' => $end,
642:                     '%model%' => strtolower(Inflector::humanize(Inflector::tableize($options['model'])))
643:                 );
644:                 $out = str_replace(array_keys($map), array_values($map), $options['format']);
645: 
646:                 $newKeys = array(
647:                     '{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}', '{:model}'
648:                 );
649:                 $out = str_replace($newKeys, array_values($map), $out);
650:         }
651:         return $out;
652:     }
653: 
654: /**
655:  * Returns a set of numbers for the paged result set
656:  * uses a modulus to decide how many numbers to show on each side of the current page (default: 8).
657:  *
658:  * `$this->Paginator->numbers(array('first' => 2, 'last' => 2));`
659:  *
660:  * Using the first and last options you can create links to the beginning and end of the page set.
661:  *
662:  * ### Options
663:  *
664:  * - `before` Content to be inserted before the numbers
665:  * - `after` Content to be inserted after the numbers
666:  * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
667:  * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
668:  * - `separator` Separator content defaults to ' | '
669:  * - `tag` The tag to wrap links in, defaults to 'span'
670:  * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
671:  *    links to generate.
672:  * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
673:  *    links to generate.
674:  * - `ellipsis` Ellipsis content, defaults to '...'
675:  * - `class` Class for wrapper tag
676:  * - `currentClass` Class for wrapper tag on current active page, defaults to 'current'
677:  * - `currentTag` Tag to use for current page number, defaults to null
678:  *
679:  * @param array $options Options for the numbers, (before, after, model, modulus, separator)
680:  * @return string numbers string.
681:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::numbers
682:  */
683:     public function numbers($options = array()) {
684:         if ($options === true) {
685:             $options = array(
686:                 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
687:             );
688:         }
689: 
690:         $defaults = array(
691:             'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
692:             'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
693:             'currentClass' => 'current', 'currentTag' => null
694:         );
695:         $options += $defaults;
696: 
697:         $params = (array)$this->params($options['model']) + array('page' => 1);
698:         unset($options['model']);
699: 
700:         if ($params['pageCount'] <= 1) {
701:             return false;
702:         }
703: 
704:         extract($options);
705:         unset($options['tag'], $options['before'], $options['after'], $options['model'],
706:             $options['modulus'], $options['separator'], $options['first'], $options['last'],
707:             $options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
708:         );
709: 
710:         $out = '';
711: 
712:         if ($modulus && $params['pageCount'] > $modulus) {
713:             $half = intval($modulus / 2);
714:             $end = $params['page'] + $half;
715: 
716:             if ($end > $params['pageCount']) {
717:                 $end = $params['pageCount'];
718:             }
719:             $start = $params['page'] - ($modulus - ($end - $params['page']));
720:             if ($start <= 1) {
721:                 $start = 1;
722:                 $end = $params['page'] + ($modulus - $params['page']) + 1;
723:             }
724: 
725:             if ($first && $start > 1) {
726:                 $offset = ($start <= (int)$first) ? $start - 1 : $first;
727:                 if ($offset < $start - 1) {
728:                     $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
729:                 } else {
730:                     $out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
731:                 }
732:             }
733: 
734:             $out .= $before;
735: 
736:             for ($i = $start; $i < $params['page']; $i++) {
737:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
738:             }
739: 
740:             if ($class) {
741:                 $currentClass .= ' ' . $class;
742:             }
743:             if ($currentTag) {
744:                 $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass));
745:             } else {
746:                 $out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
747:             }
748:             if ($i != $params['pageCount']) {
749:                 $out .= $separator;
750:             }
751: 
752:             $start = $params['page'] + 1;
753:             for ($i = $start; $i < $end; $i++) {
754:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
755:             }
756: 
757:             if ($end != $params['page']) {
758:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
759:             }
760: 
761:             $out .= $after;
762: 
763:             if ($last && $end < $params['pageCount']) {
764:                 $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
765:                 if ($offset <= $last && $params['pageCount'] - $end > $offset) {
766:                     $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
767:                 } else {
768:                     $out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
769:                 }
770:             }
771: 
772:         } else {
773:             $out .= $before;
774: 
775:             for ($i = 1; $i <= $params['pageCount']; $i++) {
776:                 if ($i == $params['page']) {
777:                     if ($class) {
778:                         $currentClass .= ' ' . $class;
779:                     }
780:                     if ($currentTag) {
781:                         $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass));
782:                     } else {
783:                         $out .= $this->Html->tag($tag, $i, array('class' => $currentClass));
784:                     }
785:                 } else {
786:                     $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
787:                 }
788:                 if ($i != $params['pageCount']) {
789:                     $out .= $separator;
790:                 }
791:             }
792: 
793:             $out .= $after;
794:         }
795: 
796:         return $out;
797:     }
798: 
799: /**
800:  * Returns a first or set of numbers for the first pages.
801:  *
802:  * `echo $this->Paginator->first('< first');`
803:  *
804:  * Creates a single link for the first page. Will output nothing if you are on the first page.
805:  *
806:  * `echo $this->Paginator->first(3);`
807:  *
808:  * Will create links for the first 3 pages, once you get to the third or greater page. Prior to that
809:  * nothing will be output.
810:  *
811:  * ### Options:
812:  *
813:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
814:  * - `after` Content to insert after the link/tag
815:  * - `model` The model to use defaults to PaginatorHelper::defaultModel()
816:  * - `separator` Content between the generated links, defaults to ' | '
817:  * - `ellipsis` Content for ellipsis, defaults to '...'
818:  *
819:  * @param string|integer $first if string use as label for the link. If numeric, the number of page links
820:  *   you want at the beginning of the range.
821:  * @param array $options An array of options.
822:  * @return string numbers string.
823:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
824:  */
825:     public function first($first = '<< first', $options = array()) {
826:         $options = array_merge(
827:             array(
828:                 'tag' => 'span',
829:                 'after' => null,
830:                 'model' => $this->defaultModel(),
831:                 'separator' => ' | ',
832:                 'ellipsis' => '...',
833:                 'class' => null
834:             ),
835:         (array)$options);
836: 
837:         $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
838:         unset($options['model']);
839: 
840:         if ($params['pageCount'] <= 1) {
841:             return false;
842:         }
843:         extract($options);
844:         unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
845: 
846:         $out = '';
847: 
848:         if (is_int($first) && $params['page'] >= $first) {
849:             if ($after === null) {
850:                 $after = $ellipsis;
851:             }
852:             for ($i = 1; $i <= $first; $i++) {
853:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
854:                 if ($i != $first) {
855:                     $out .= $separator;
856:                 }
857:             }
858:             $out .= $after;
859:         } elseif ($params['page'] > 1 && is_string($first)) {
860:             $options += array('rel' => 'first');
861:             $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
862:         }
863:         return $out;
864:     }
865: 
866: /**
867:  * Returns a last or set of numbers for the last pages.
868:  *
869:  * `echo $this->Paginator->last('last >');`
870:  *
871:  * Creates a single link for the last page. Will output nothing if you are on the last page.
872:  *
873:  * `echo $this->Paginator->last(3);`
874:  *
875:  * Will create links for the last 3 pages. Once you enter the page range, no output will be created.
876:  *
877:  * ### Options:
878:  *
879:  * - `tag` The tag wrapping tag you want to use, defaults to 'span'
880:  * - `before` Content to insert before the link/tag
881:  * - `model` The model to use defaults to PaginatorHelper::defaultModel()
882:  * - `separator` Content between the generated links, defaults to ' | '
883:  * - `ellipsis` Content for ellipsis, defaults to '...'
884:  *
885:  * @param string|integer $last if string use as label for the link, if numeric print page numbers
886:  * @param array $options Array of options
887:  * @return string numbers string.
888:  * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::last
889:  */
890:     public function last($last = 'last >>', $options = array()) {
891:         $options = array_merge(
892:             array(
893:                 'tag' => 'span',
894:                 'before' => null,
895:                 'model' => $this->defaultModel(),
896:                 'separator' => ' | ',
897:                 'ellipsis' => '...',
898:                 'class' => null
899:             ),
900:         (array)$options);
901: 
902:         $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
903:         unset($options['model']);
904: 
905:         if ($params['pageCount'] <= 1) {
906:             return false;
907:         }
908: 
909:         extract($options);
910:         unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
911: 
912:         $out = '';
913:         $lower = $params['pageCount'] - $last + 1;
914: 
915:         if (is_int($last) && $params['page'] <= $lower) {
916:             if ($before === null) {
917:                 $before = $ellipsis;
918:             }
919:             for ($i = $lower; $i <= $params['pageCount']; $i++) {
920:                 $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
921:                 if ($i != $params['pageCount']) {
922:                     $out .= $separator;
923:                 }
924:             }
925:             $out = $before . $out;
926:         } elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
927:             $options += array('rel' => 'last');
928:             $out = $before . $this->Html->tag(
929:                 $tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
930:             );
931:         }
932:         return $out;
933:     }
934: 
935: }
936: 
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