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