PageRenderTime 437ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/Cake/View/Helper/PaginatorHelper.php

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