Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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