Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
12345 anikendra 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 The array of paging parameters for the paginated resultset.
124
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::params
125
 */
126
	public function params($model = null) {
127
		if (empty($model)) {
128
			$model = $this->defaultModel();
129
		}
130
		if (!isset($this->request->params['paging']) || empty($this->request->params['paging'][$model])) {
131
			return null;
132
		}
133
		return $this->request->params['paging'][$model];
134
	}
135
 
136
/**
137
 * Convenience access to any of the paginator params.
138
 *
139
 * @param string $key Key of the paginator params array to retrieve.
140
 * @param string $model Optional model name. Uses the default if none is specified.
141
 * @return mixed Content of the requested param.
142
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::params
143
 */
144
	public function param($key, $model = null) {
145
		$params = $this->params($model);
146
		if (!isset($params[$key])) {
147
			return null;
148
		}
149
		return $params[$key];
150
	}
151
 
152
/**
153
 * Sets default options for all pagination links
154
 *
155
 * @param array|string $options Default options for pagination links. If a string is supplied - it
156
 *   is used as the DOM id element to update. See PaginatorHelper::$options for list of keys.
157
 * @return void
158
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::options
159
 */
160
	public function options($options = array()) {
161
		if (is_string($options)) {
162
			$options = array('update' => $options);
163
		}
164
 
165
		if (!empty($options['paging'])) {
166
			if (!isset($this->request->params['paging'])) {
167
				$this->request->params['paging'] = array();
168
			}
169
			$this->request->params['paging'] = array_merge($this->request->params['paging'], $options['paging']);
170
			unset($options['paging']);
171
		}
172
		$model = $this->defaultModel();
173
 
174
		if (!empty($options[$model])) {
175
			if (!isset($this->request->params['paging'][$model])) {
176
				$this->request->params['paging'][$model] = array();
177
			}
178
			$this->request->params['paging'][$model] = array_merge(
179
				$this->request->params['paging'][$model], $options[$model]
180
			);
181
			unset($options[$model]);
182
		}
183
		if (!empty($options['convertKeys'])) {
184
			$options['convertKeys'] = array_merge($this->options['convertKeys'], $options['convertKeys']);
185
		}
186
		$this->options = array_filter(array_merge($this->options, $options));
187
	}
188
 
189
/**
190
 * Gets the current page of the recordset for the given model
191
 *
192
 * @param string $model Optional model name. Uses the default if none is specified.
193
 * @return string The current page number of the recordset.
194
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::current
195
 */
196
	public function current($model = null) {
197
		$params = $this->params($model);
198
 
199
		if (isset($params['page'])) {
200
			return $params['page'];
201
		}
202
		return 1;
203
	}
204
 
205
/**
206
 * Gets the current key by which the recordset is sorted
207
 *
208
 * @param string $model Optional model name. Uses the default if none is specified.
209
 * @param array $options Options for pagination links. See #options for list of keys.
210
 * @return string The name of the key by which the recordset is being sorted, or
211
 *  null if the results are not currently sorted.
212
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortKey
213
 */
214
	public function sortKey($model = null, $options = array()) {
215
		if (empty($options)) {
216
			$params = $this->params($model);
217
			$options = $params['options'];
218
		}
219
		if (isset($options['sort']) && !empty($options['sort'])) {
220
			return $options['sort'];
221
		}
222
		if (isset($options['order'])) {
223
			return is_array($options['order']) ? key($options['order']) : $options['order'];
224
		}
225
		if (isset($params['order'])) {
226
			return is_array($params['order']) ? key($params['order']) : $params['order'];
227
		}
228
		return null;
229
	}
230
 
231
/**
232
 * Gets the current direction the recordset is sorted
233
 *
234
 * @param string $model Optional model name. Uses the default if none is specified.
235
 * @param array $options Options for pagination links. See #options for list of keys.
236
 * @return string The direction by which the recordset is being sorted, or
237
 *  null if the results are not currently sorted.
238
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortDir
239
 */
240
	public function sortDir($model = null, $options = array()) {
241
		$dir = null;
242
 
243
		if (empty($options)) {
244
			$params = $this->params($model);
245
			$options = $params['options'];
246
		}
247
 
248
		if (isset($options['direction'])) {
249
			$dir = strtolower($options['direction']);
250
		} elseif (isset($options['order']) && is_array($options['order'])) {
251
			$dir = strtolower(current($options['order']));
252
		} elseif (isset($params['order']) && is_array($params['order'])) {
253
			$dir = strtolower(current($params['order']));
254
		}
255
 
256
		if ($dir === 'desc') {
257
			return 'desc';
258
		}
259
		return 'asc';
260
	}
261
 
262
/**
263
 * Generates a "previous" link for a set of paged records
264
 *
265
 * ### Options:
266
 *
267
 * - `url` Allows sending routing parameters such as controllers, actions or passed arguments.
268
 * - `tag` The tag wrapping tag you want to use, defaults to 'span'. Set this to false to disable this option
269
 * - `escape` Whether you want the contents html entity encoded, defaults to true
270
 * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
271
 * - `disabledTag` Tag to use instead of A tag when there is no previous page
272
 *
273
 * @param string $title Title for the link. Defaults to '<< Previous'.
274
 * @param array $options Options for pagination link. See #options for list of keys.
275
 * @param string $disabledTitle Title when the link is disabled.
276
 * @param array $disabledOptions Options for the disabled pagination link. See #options for list of keys.
277
 * @return string A "previous" link or $disabledTitle text if the link is disabled.
278
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::prev
279
 */
280
	public function prev($title = '<< Previous', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
281
		$defaults = array(
282
			'rel' => 'prev'
283
		);
284
		$options = (array)$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
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 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
 * @deprecated The %page% style placeholders are deprecated.
628
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::counter
629
 */
630
	public function counter($options = array()) {
631
		if (is_string($options)) {
632
			$options = array('format' => $options);
633
		}
634
 
635
		$options += array(
636
			'model' => $this->defaultModel(),
637
			'format' => 'pages',
638
			'separator' => __d('cake', ' of ')
639
		);
640
 
641
		$paging = $this->params($options['model']);
642
		if (!$paging['pageCount']) {
643
			$paging['pageCount'] = 1;
644
		}
645
		$start = 0;
646
		if ($paging['count'] >= 1) {
647
			$start = (($paging['page'] - 1) * $paging['limit']) + 1;
648
		}
649
		$end = $start + $paging['limit'] - 1;
650
		if ($paging['count'] < $end) {
651
			$end = $paging['count'];
652
		}
653
 
654
		switch ($options['format']) {
655
			case 'range':
656
				if (!is_array($options['separator'])) {
657
					$options['separator'] = array(' - ', $options['separator']);
658
				}
659
				$out = $start . $options['separator'][0] . $end . $options['separator'][1];
660
				$out .= $paging['count'];
661
				break;
662
			case 'pages':
663
				$out = $paging['page'] . $options['separator'] . $paging['pageCount'];
664
				break;
665
			default:
666
				$map = array(
667
					'%page%' => $paging['page'],
668
					'%pages%' => $paging['pageCount'],
669
					'%current%' => $paging['current'],
670
					'%count%' => $paging['count'],
671
					'%start%' => $start,
672
					'%end%' => $end,
673
					'%model%' => strtolower(Inflector::humanize(Inflector::tableize($options['model'])))
674
				);
675
				$out = str_replace(array_keys($map), array_values($map), $options['format']);
676
 
677
				$newKeys = array(
678
					'{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}', '{:model}'
679
				);
680
				$out = str_replace($newKeys, array_values($map), $out);
681
		}
682
		return $out;
683
	}
684
 
685
/**
686
 * Returns a set of numbers for the paged result set
687
 * uses a modulus to decide how many numbers to show on each side of the current page (default: 8).
688
 *
689
 * `$this->Paginator->numbers(array('first' => 2, 'last' => 2));`
690
 *
691
 * Using the first and last options you can create links to the beginning and end of the page set.
692
 *
693
 * ### Options
694
 *
695
 * - `before` Content to be inserted before the numbers
696
 * - `after` Content to be inserted after the numbers
697
 * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
698
 * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
699
 * - `separator` Separator content defaults to ' | '
700
 * - `tag` The tag to wrap links in, defaults to 'span'
701
 * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
702
 *    links to generate.
703
 * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
704
 *    links to generate.
705
 * - `ellipsis` Ellipsis content, defaults to '...'
706
 * - `class` Class for wrapper tag
707
 * - `currentClass` Class for wrapper tag on current active page, defaults to 'current'
708
 * - `currentTag` Tag to use for current page number, defaults to null
709
 *
710
 * @param array $options Options for the numbers, (before, after, model, modulus, separator)
711
 * @return string numbers string.
712
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::numbers
713
 */
714
	public function numbers($options = array()) {
715
		if ($options === true) {
716
			$options = array(
717
				'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
718
			);
719
		}
720
 
721
		$defaults = array(
722
			'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
723
			'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
724
			'currentClass' => 'current', 'currentTag' => null
725
		);
726
		$options += $defaults;
727
 
728
		$params = (array)$this->params($options['model']) + array('page' => 1);
729
		unset($options['model']);
730
 
731
		if ($params['pageCount'] <= 1) {
732
			return false;
733
		}
734
 
735
		extract($options);
736
		unset($options['tag'], $options['before'], $options['after'], $options['model'],
737
			$options['modulus'], $options['separator'], $options['first'], $options['last'],
738
			$options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
739
		);
740
 
741
		$out = '';
742
 
743
		if ($modulus && $params['pageCount'] > $modulus) {
744
			$half = intval($modulus / 2);
745
			$end = $params['page'] + $half;
746
 
747
			if ($end > $params['pageCount']) {
748
				$end = $params['pageCount'];
749
			}
750
			$start = $params['page'] - ($modulus - ($end - $params['page']));
751
			if ($start <= 1) {
752
				$start = 1;
753
				$end = $params['page'] + ($modulus - $params['page']) + 1;
754
			}
755
 
756
			if ($first && $start > 1) {
757
				$offset = ($start <= (int)$first) ? $start - 1 : $first;
758
				if ($offset < $start - 1) {
759
					$out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
760
				} else {
761
					$out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
762
				}
763
			}
764
 
765
			$out .= $before;
766
 
767
			for ($i = $start; $i < $params['page']; $i++) {
768
				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
769
			}
770
 
771
			if ($class) {
772
				$currentClass .= ' ' . $class;
773
			}
774
			if ($currentTag) {
775
				$out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass));
776
			} else {
777
				$out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
778
			}
779
			if ($i != $params['pageCount']) {
780
				$out .= $separator;
781
			}
782
 
783
			$start = $params['page'] + 1;
784
			for ($i = $start; $i < $end; $i++) {
785
				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
786
			}
787
 
788
			if ($end != $params['page']) {
789
				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
790
			}
791
 
792
			$out .= $after;
793
 
794
			if ($last && $end < $params['pageCount']) {
795
				$offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
796
				if ($offset <= $last && $params['pageCount'] - $end > $offset) {
797
					$out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
798
				} else {
799
					$out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
800
				}
801
			}
802
 
803
		} else {
804
			$out .= $before;
805
 
806
			for ($i = 1; $i <= $params['pageCount']; $i++) {
807
				if ($i == $params['page']) {
808
					if ($class) {
809
						$currentClass .= ' ' . $class;
810
					}
811
					if ($currentTag) {
812
						$out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass));
813
					} else {
814
						$out .= $this->Html->tag($tag, $i, array('class' => $currentClass));
815
					}
816
				} else {
817
					$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
818
				}
819
				if ($i != $params['pageCount']) {
820
					$out .= $separator;
821
				}
822
			}
823
 
824
			$out .= $after;
825
		}
826
 
827
		return $out;
828
	}
829
 
830
/**
831
 * Returns a first or set of numbers for the first pages.
832
 *
833
 * `echo $this->Paginator->first('< first');`
834
 *
835
 * Creates a single link for the first page. Will output nothing if you are on the first page.
836
 *
837
 * `echo $this->Paginator->first(3);`
838
 *
839
 * Will create links for the first 3 pages, once you get to the third or greater page. Prior to that
840
 * nothing will be output.
841
 *
842
 * ### Options:
843
 *
844
 * - `tag` The tag wrapping tag you want to use, defaults to 'span'
845
 * - `after` Content to insert after the link/tag
846
 * - `model` The model to use defaults to PaginatorHelper::defaultModel()
847
 * - `separator` Content between the generated links, defaults to ' | '
848
 * - `ellipsis` Content for ellipsis, defaults to '...'
849
 *
850
 * @param string|int $first if string use as label for the link. If numeric, the number of page links
851
 *   you want at the beginning of the range.
852
 * @param array $options An array of options.
853
 * @return string numbers string.
854
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
855
 */
856
	public function first($first = '<< first', $options = array()) {
857
		$options = (array)$options + array(
858
			'tag' => 'span',
859
			'after' => null,
860
			'model' => $this->defaultModel(),
861
			'separator' => ' | ',
862
			'ellipsis' => '...',
863
			'class' => null
864
		);
865
 
866
		$params = array_merge(array('page' => 1), (array)$this->params($options['model']));
867
		unset($options['model']);
868
 
869
		if ($params['pageCount'] <= 1) {
870
			return false;
871
		}
872
		extract($options);
873
		unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
874
 
875
		$out = '';
876
 
877
		if (is_int($first) && $params['page'] >= $first) {
878
			if ($after === null) {
879
				$after = $ellipsis;
880
			}
881
			for ($i = 1; $i <= $first; $i++) {
882
				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
883
				if ($i != $first) {
884
					$out .= $separator;
885
				}
886
			}
887
			$out .= $after;
888
		} elseif ($params['page'] > 1 && is_string($first)) {
889
			$options += array('rel' => 'first');
890
			$out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
891
		}
892
		return $out;
893
	}
894
 
895
/**
896
 * Returns a last or set of numbers for the last pages.
897
 *
898
 * `echo $this->Paginator->last('last >');`
899
 *
900
 * Creates a single link for the last page. Will output nothing if you are on the last page.
901
 *
902
 * `echo $this->Paginator->last(3);`
903
 *
904
 * Will create links for the last 3 pages. Once you enter the page range, no output will be created.
905
 *
906
 * ### Options:
907
 *
908
 * - `tag` The tag wrapping tag you want to use, defaults to 'span'
909
 * - `before` Content to insert before the link/tag
910
 * - `model` The model to use defaults to PaginatorHelper::defaultModel()
911
 * - `separator` Content between the generated links, defaults to ' | '
912
 * - `ellipsis` Content for ellipsis, defaults to '...'
913
 *
914
 * @param string|int $last if string use as label for the link, if numeric print page numbers
915
 * @param array $options Array of options
916
 * @return string numbers string.
917
 * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::last
918
 */
919
	public function last($last = 'last >>', $options = array()) {
920
		$options = (array)$options + array(
921
			'tag' => 'span',
922
			'before' => null,
923
			'model' => $this->defaultModel(),
924
			'separator' => ' | ',
925
			'ellipsis' => '...',
926
			'class' => null
927
		);
928
 
929
		$params = array_merge(array('page' => 1), (array)$this->params($options['model']));
930
		unset($options['model']);
931
 
932
		if ($params['pageCount'] <= 1) {
933
			return false;
934
		}
935
 
936
		extract($options);
937
		unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
938
 
939
		$out = '';
940
		$lower = $params['pageCount'] - $last + 1;
941
 
942
		if (is_int($last) && $params['page'] <= $lower) {
943
			if ($before === null) {
944
				$before = $ellipsis;
945
			}
946
			for ($i = $lower; $i <= $params['pageCount']; $i++) {
947
				$out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
948
				if ($i != $params['pageCount']) {
949
					$out .= $separator;
950
				}
951
			}
952
			$out = $before . $out;
953
		} elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
954
			$options += array('rel' => 'last');
955
			$out = $before . $this->Html->tag(
956
				$tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
957
			);
958
		}
959
		return $out;
960
	}
961
 
962
}