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 View file name.
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|null 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|null 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)$options + $defaults;
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)$options + $defaults;
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: * - `lock` Lock direction. Will only use the default direction then, defaults to false.
324: *
325: * @param string $key The name of the key that the recordset should be sorted.
326: * @param string $title Title for the link. If $title is null $key will be used
327: * for the title and will be generated by inflection.
328: * @param array $options Options for sorting link. See above for list of keys.
329: * @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
330: * key the returned link will sort by 'desc'.
331: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sort
332: */
333: public function sort($key, $title = null, $options = array()) {
334: $options += array('url' => array(), 'model' => null);
335: $url = $options['url'];
336: unset($options['url']);
337:
338: if (empty($title)) {
339: $title = $key;
340:
341: if (strpos($title, '.') !== false) {
342: $title = str_replace('.', ' ', $title);
343: }
344:
345: $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
346: }
347: $defaultDir = isset($options['direction']) ? $options['direction'] : 'asc';
348: unset($options['direction']);
349:
350: $locked = isset($options['lock']) ? $options['lock'] : false;
351: unset($options['lock']);
352:
353: $sortKey = $this->sortKey($options['model']);
354: $defaultModel = $this->defaultModel();
355: $isSorted = (
356: $sortKey === $key ||
357: $sortKey === $defaultModel . '.' . $key ||
358: $key === $defaultModel . '.' . $sortKey
359: );
360:
361: $dir = $defaultDir;
362: if ($isSorted) {
363: $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
364: $class = $dir === 'asc' ? 'desc' : 'asc';
365: if (!empty($options['class'])) {
366: $options['class'] .= ' ' . $class;
367: } else {
368: $options['class'] = $class;
369: }
370: if ($locked) {
371: $dir = $defaultDir;
372: $options['class'] .= ' locked';
373: }
374: }
375: if (is_array($title) && array_key_exists($dir, $title)) {
376: $title = $title[$dir];
377: }
378:
379: $url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
380: return $this->link($title, $url, $options);
381: }
382:
383: /**
384: * Generates a plain or Ajax link with pagination parameters
385: *
386: * ### Options
387: *
388: * - `update` The Id of the DOM element you wish to update. Creates Ajax enabled links
389: * with the AjaxHelper.
390: * - `escape` Whether you want the contents html entity encoded, defaults to true
391: * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
392: *
393: * @param string $title Title for the link.
394: * @param string|array $url URL for the action. See Router::url()
395: * @param array $options Options for the link. See #options for list of keys.
396: * @return string A link with pagination parameters.
397: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::link
398: */
399: public function link($title, $url = array(), $options = array()) {
400: $options += array('model' => null, 'escape' => true);
401: $model = $options['model'];
402: unset($options['model']);
403:
404: if (!empty($this->options)) {
405: $options += $this->options;
406: }
407: if (isset($options['url'])) {
408: $url = array_merge((array)$options['url'], (array)$url);
409: unset($options['url']);
410: }
411: unset($options['convertKeys']);
412:
413: $url = $this->url($url, true, $model);
414:
415: $obj = isset($options['update']) ? $this->_ajaxHelperClass : 'Html';
416: return $this->{$obj}->link($title, $url, $options);
417: }
418:
419: /**
420: * Merges passed URL options with current pagination state to generate a pagination URL.
421: *
422: * @param array $options Pagination/URL options array
423: * @param bool $asArray Return the URL as an array, or a URI string
424: * @param string $model Which model to paginate on
425: * @return mixed By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
426: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::url
427: */
428: public function url($options = array(), $asArray = false, $model = null) {
429: $paging = $this->params($model);
430: $url = array_merge(array_filter($paging['options']), $options);
431:
432: if (isset($url['order'])) {
433: $sort = $direction = null;
434: if (is_array($url['order'])) {
435: list($sort, $direction) = array($this->sortKey($model, $url), current($url['order']));
436: }
437: unset($url['order']);
438: $url = array_merge($url, compact('sort', 'direction'));
439: }
440: $url = $this->_convertUrlKeys($url, $paging['paramType']);
441: if (!empty($url['page']) && $url['page'] == 1) {
442: $url['page'] = null;
443: }
444: if (!empty($url['?']['page']) && $url['?']['page'] == 1) {
445: unset($url['?']['page']);
446: }
447: if ($asArray) {
448: return $url;
449: }
450: return parent::url($url);
451: }
452:
453: /**
454: * Converts the keys being used into the format set by options.paramType
455: *
456: * @param array $url Array of URL params to convert
457: * @param string $type Keys type.
458: * @return array converted URL params.
459: */
460: protected function _convertUrlKeys($url, $type) {
461: if ($type === 'named') {
462: return $url;
463: }
464: if (!isset($url['?'])) {
465: $url['?'] = array();
466: }
467: foreach ($this->options['convertKeys'] as $key) {
468: if (isset($url[$key])) {
469: $url['?'][$key] = $url[$key];
470: unset($url[$key]);
471: }
472: }
473: return $url;
474: }
475:
476: /**
477: * Protected method for generating prev/next links
478: *
479: * @param string $which Link type: 'Prev', 'Next'.
480: * @param string $title Link title.
481: * @param array $options Options list.
482: * @param string $disabledTitle Disabled link title.
483: * @param array $disabledOptions Disabled link options.
484: * @return string|null
485: */
486: protected function _pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array()) {
487: $check = 'has' . $which;
488: $_defaults = array(
489: 'url' => array(), 'step' => 1, 'escape' => true, 'model' => null,
490: 'tag' => 'span', 'class' => strtolower($which), 'disabledTag' => null
491: );
492: $options = (array)$options + $_defaults;
493: $paging = $this->params($options['model']);
494: if (empty($disabledOptions)) {
495: $disabledOptions = $options;
496: }
497:
498: if (!$this->{$check}($options['model']) && (!empty($disabledTitle) || !empty($disabledOptions))) {
499: if (!empty($disabledTitle) && $disabledTitle !== true) {
500: $title = $disabledTitle;
501: }
502: $options = (array)$disabledOptions + $_defaults;
503: } elseif (!$this->{$check}($options['model'])) {
504: return null;
505: }
506:
507: foreach (array_keys($_defaults) as $key) {
508: ${$key} = $options[$key];
509: unset($options[$key]);
510: }
511:
512: if ($this->{$check}($model)) {
513: $url = array_merge(
514: array('page' => $paging['page'] + ($which === 'Prev' ? $step * -1 : $step)),
515: $url
516: );
517: if ($tag === false) {
518: return $this->link(
519: $title,
520: $url,
521: compact('escape', 'model', 'class') + $options
522: );
523: }
524: $link = $this->link($title, $url, compact('escape', 'model') + $options);
525: return $this->Html->tag($tag, $link, compact('class'));
526: }
527: unset($options['rel']);
528: if (!$tag) {
529: if ($disabledTag) {
530: $tag = $disabledTag;
531: $disabledTag = null;
532: } else {
533: $tag = $_defaults['tag'];
534: }
535: }
536: if ($disabledTag) {
537: $title = $this->Html->tag($disabledTag, $title, compact('escape') + $options);
538: return $this->Html->tag($tag, $title, compact('class'));
539: }
540: return $this->Html->tag($tag, $title, compact('escape', 'class') + $options);
541: }
542:
543: /**
544: * Returns true if the given result set is not at the first page
545: *
546: * @param string $model Optional model name. Uses the default if none is specified.
547: * @return bool True if the result set is not at the first page.
548: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPrev
549: */
550: public function hasPrev($model = null) {
551: return $this->_hasPage($model, 'prev');
552: }
553:
554: /**
555: * Returns true if the given result set is not at the last page
556: *
557: * @param string $model Optional model name. Uses the default if none is specified.
558: * @return bool True if the result set is not at the last page.
559: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasNext
560: */
561: public function hasNext($model = null) {
562: return $this->_hasPage($model, 'next');
563: }
564:
565: /**
566: * Returns true if the given result set has the page number given by $page
567: *
568: * @param string $model Optional model name. Uses the default if none is specified.
569: * @param int $page The page number - if not set defaults to 1.
570: * @return bool True if the given result set has the specified page number.
571: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPage
572: */
573: public function hasPage($model = null, $page = 1) {
574: if (is_numeric($model)) {
575: $page = $model;
576: $model = null;
577: }
578: $paging = $this->params($model);
579: return $page <= $paging['pageCount'];
580: }
581:
582: /**
583: * Does $model have $page in its range?
584: *
585: * @param string $model Model name to get parameters for.
586: * @param int $page Page number you are checking.
587: * @return bool Whether model has $page
588: */
589: protected function _hasPage($model, $page) {
590: $params = $this->params($model);
591: return !empty($params) && $params[$page . 'Page'];
592: }
593:
594: /**
595: * Gets the default model of the paged sets
596: *
597: * @return string|null Model name or null if the pagination isn't initialized.
598: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::defaultModel
599: */
600: public function defaultModel() {
601: if ($this->_defaultModel) {
602: return $this->_defaultModel;
603: }
604: if (empty($this->request->params['paging'])) {
605: return null;
606: }
607: list($this->_defaultModel) = array_keys($this->request->params['paging']);
608: return $this->_defaultModel;
609: }
610:
611: /**
612: * Returns a counter string for the paged result set
613: *
614: * ### Options
615: *
616: * - `model` The model to use, defaults to PaginatorHelper::defaultModel();
617: * - `format` The format string you want to use, defaults to 'pages' Which generates output like '1 of 5'
618: * set to 'range' to generate output like '1 - 3 of 13'. Can also be set to a custom string, containing
619: * the following placeholders `{:page}`, `{:pages}`, `{:current}`, `{:count}`, `{:model}`, `{:start}`, `{:end}` and any
620: * custom content you would like.
621: * - `separator` The separator string to use, default to ' of '
622: *
623: * The `%page%` style placeholders also work, but are deprecated and will be removed in a future version.
624: *
625: * @param array $options Options for the counter string. See #options for list of keys.
626: * @return string Counter string.
627: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::counter
628: */
629: public function counter($options = array()) {
630: if (is_string($options)) {
631: $options = array('format' => $options);
632: }
633:
634: $options += array(
635: 'model' => $this->defaultModel(),
636: 'format' => 'pages',
637: 'separator' => __d('cake', ' of ')
638: );
639:
640: $paging = $this->params($options['model']);
641: if (!$paging['pageCount']) {
642: $paging['pageCount'] = 1;
643: }
644: $start = 0;
645: if ($paging['count'] >= 1) {
646: $start = (($paging['page'] - 1) * $paging['limit']) + 1;
647: }
648: $end = $start + $paging['limit'] - 1;
649: if ($paging['count'] < $end) {
650: $end = $paging['count'];
651: }
652:
653: switch ($options['format']) {
654: case 'range':
655: if (!is_array($options['separator'])) {
656: $options['separator'] = array(' - ', $options['separator']);
657: }
658: $out = $start . $options['separator'][0] . $end . $options['separator'][1];
659: $out .= $paging['count'];
660: break;
661: case 'pages':
662: $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
663: break;
664: default:
665: $map = array(
666: '%page%' => $paging['page'],
667: '%pages%' => $paging['pageCount'],
668: '%current%' => $paging['current'],
669: '%count%' => $paging['count'],
670: '%start%' => $start,
671: '%end%' => $end,
672: '%model%' => strtolower(Inflector::humanize(Inflector::tableize($options['model'])))
673: );
674: $out = str_replace(array_keys($map), array_values($map), $options['format']);
675:
676: $newKeys = array(
677: '{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}', '{:model}'
678: );
679: $out = str_replace($newKeys, array_values($map), $out);
680: }
681: return $out;
682: }
683:
684: /**
685: * Returns a set of numbers for the paged result set
686: * uses a modulus to decide how many numbers to show on each side of the current page (default: 8).
687: *
688: * `$this->Paginator->numbers(array('first' => 2, 'last' => 2));`
689: *
690: * Using the first and last options you can create links to the beginning and end of the page set.
691: *
692: * ### Options
693: *
694: * - `before` Content to be inserted before the numbers
695: * - `after` Content to be inserted after the numbers
696: * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
697: * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
698: * - `separator` Separator content defaults to ' | '
699: * - `tag` The tag to wrap links in, defaults to 'span'
700: * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
701: * links to generate.
702: * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
703: * links to generate.
704: * - `ellipsis` Ellipsis content, defaults to '...'
705: * - `class` Class for wrapper tag
706: * - `currentClass` Class for wrapper tag on current active page, defaults to 'current'
707: * - `currentTag` Tag to use for current page number, defaults to null
708: *
709: * @param array $options Options for the numbers, (before, after, model, modulus, separator)
710: * @return string|bool numbers string.
711: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::numbers
712: */
713: public function numbers($options = array()) {
714: if ($options === true) {
715: $options = array(
716: 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
717: );
718: }
719:
720: $defaults = array(
721: 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
722: 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
723: 'currentClass' => 'current', 'currentTag' => null
724: );
725: $options += $defaults;
726:
727: $params = (array)$this->params($options['model']) + array('page' => 1);
728: unset($options['model']);
729:
730: if ($params['pageCount'] <= 1) {
731: return false;
732: }
733:
734: extract($options);
735: unset($options['tag'], $options['before'], $options['after'], $options['model'],
736: $options['modulus'], $options['separator'], $options['first'], $options['last'],
737: $options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
738: );
739:
740: $out = '';
741:
742: if ($modulus && $params['pageCount'] > $modulus) {
743: $half = (int)($modulus / 2);
744: $end = $params['page'] + $half;
745:
746: if ($end > $params['pageCount']) {
747: $end = $params['pageCount'];
748: }
749: $start = $params['page'] - ($modulus - ($end - $params['page']));
750: if ($start <= 1) {
751: $start = 1;
752: $end = $params['page'] + ($modulus - $params['page']) + 1;
753: }
754:
755: if ($first && $start > 1) {
756: $offset = ($start <= (int)$first) ? $start - 1 : $first;
757: if ($offset < $start - 1) {
758: $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
759: } else {
760: $out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
761: }
762: }
763:
764: $out .= $before;
765:
766: for ($i = $start; $i < $params['page']; $i++) {
767: $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
768: }
769:
770: if ($class) {
771: $currentClass .= ' ' . $class;
772: }
773: if ($currentTag) {
774: $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass));
775: } else {
776: $out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
777: }
778: if ($i != $params['pageCount']) {
779: $out .= $separator;
780: }
781:
782: $start = $params['page'] + 1;
783: for ($i = $start; $i < $end; $i++) {
784: $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
785: }
786:
787: if ($end != $params['page']) {
788: $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
789: }
790:
791: $out .= $after;
792:
793: if ($last && $end < $params['pageCount']) {
794: $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
795: if ($offset <= $last && $params['pageCount'] - $end > $offset) {
796: $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
797: } else {
798: $out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
799: }
800: }
801:
802: } else {
803: $out .= $before;
804:
805: for ($i = 1; $i <= $params['pageCount']; $i++) {
806: if ($i == $params['page']) {
807: if ($class) {
808: $currentClass .= ' ' . $class;
809: }
810: if ($currentTag) {
811: $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass));
812: } else {
813: $out .= $this->Html->tag($tag, $i, array('class' => $currentClass));
814: }
815: } else {
816: $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
817: }
818: if ($i != $params['pageCount']) {
819: $out .= $separator;
820: }
821: }
822:
823: $out .= $after;
824: }
825:
826: return $out;
827: }
828:
829: /**
830: * Returns a first or set of numbers for the first pages.
831: *
832: * `echo $this->Paginator->first('< first');`
833: *
834: * Creates a single link for the first page. Will output nothing if you are on the first page.
835: *
836: * `echo $this->Paginator->first(3);`
837: *
838: * Will create links for the first 3 pages, once you get to the third or greater page. Prior to that
839: * nothing will be output.
840: *
841: * ### Options:
842: *
843: * - `tag` The tag wrapping tag you want to use, defaults to 'span'
844: * - `after` Content to insert after the link/tag
845: * - `model` The model to use defaults to PaginatorHelper::defaultModel()
846: * - `separator` Content between the generated links, defaults to ' | '
847: * - `ellipsis` Content for ellipsis, defaults to '...'
848: *
849: * @param string|int $first if string use as label for the link. If numeric, the number of page links
850: * you want at the beginning of the range.
851: * @param array $options An array of options.
852: * @return string|bool numbers string.
853: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
854: */
855: public function first($first = '<< first', $options = array()) {
856: $options = (array)$options + array(
857: 'tag' => 'span',
858: 'after' => null,
859: 'model' => $this->defaultModel(),
860: 'separator' => ' | ',
861: 'ellipsis' => '...',
862: 'class' => null
863: );
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: extract($options);
872: unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
873:
874: $out = '';
875:
876: if (is_int($first) && $params['page'] >= $first) {
877: if ($after === null) {
878: $after = $ellipsis;
879: }
880: for ($i = 1; $i <= $first; $i++) {
881: $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
882: if ($i != $first) {
883: $out .= $separator;
884: }
885: }
886: $out .= $after;
887: } elseif ($params['page'] > 1 && is_string($first)) {
888: $options += array('rel' => 'first');
889: $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
890: }
891: return $out;
892: }
893:
894: /**
895: * Returns a last or set of numbers for the last pages.
896: *
897: * `echo $this->Paginator->last('last >');`
898: *
899: * Creates a single link for the last page. Will output nothing if you are on the last page.
900: *
901: * `echo $this->Paginator->last(3);`
902: *
903: * Will create links for the last 3 pages. Once you enter the page range, no output will be created.
904: *
905: * ### Options:
906: *
907: * - `tag` The tag wrapping tag you want to use, defaults to 'span'
908: * - `before` Content to insert before the link/tag
909: * - `model` The model to use defaults to PaginatorHelper::defaultModel()
910: * - `separator` Content between the generated links, defaults to ' | '
911: * - `ellipsis` Content for ellipsis, defaults to '...'
912: *
913: * @param string|int $last if string use as label for the link, if numeric print page numbers
914: * @param array $options Array of options
915: * @return string|bool numbers string.
916: * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::last
917: */
918: public function last($last = 'last >>', $options = array()) {
919: $options = (array)$options + array(
920: 'tag' => 'span',
921: 'before' => null,
922: 'model' => $this->defaultModel(),
923: 'separator' => ' | ',
924: 'ellipsis' => '...',
925: 'class' => null
926: );
927:
928: $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
929: unset($options['model']);
930:
931: if ($params['pageCount'] <= 1) {
932: return false;
933: }
934:
935: extract($options);
936: unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
937:
938: $out = '';
939: $lower = $params['pageCount'] - $last + 1;
940:
941: if (is_int($last) && $params['page'] <= $lower) {
942: if ($before === null) {
943: $before = $ellipsis;
944: }
945: for ($i = $lower; $i <= $params['pageCount']; $i++) {
946: $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
947: if ($i != $params['pageCount']) {
948: $out .= $separator;
949: }
950: }
951: $out = $before . $out;
952: } elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
953: $options += array('rel' => 'last');
954: $out = $before . $this->Html->tag(
955: $tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
956: );
957: }
958: return $out;
959: }
960:
961: }
962: