PageRenderTime 62ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/cake/libs/view/helpers/paginator.php

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