PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

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

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