PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

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

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