PageRenderTime 85ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 1ms

/code/ryzom/tools/server/www/webtt/cake/libs/view/helpers/paginator.php

https://bitbucket.org/mattraykowski/ryzomcore_demoshard
PHP | 816 lines | 430 code | 80 blank | 306 comment | 97 complexity | ad6f6ab7cf6e76329e628657c80c1b9c MD5 | raw file
Possible License(s): AGPL-3.0, GPL-3.0, LGPL-2.1
  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
  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. // var_dump($paging['count']);
  494. if ($paging['pageCount'] == 0) {
  495. $paging['pageCount'] = 1;
  496. }
  497. $start = 0;
  498. if ($paging['count'] >= 1) {
  499. $start = (($paging['page'] - 1) * $paging['options']['limit']) + 1;
  500. }
  501. $end = $start + $paging['options']['limit'] - 1;
  502. if ($paging['count'] < $end) {
  503. $end = $paging['count'];
  504. }
  505. switch ($options['format']) {
  506. case 'range':
  507. if (!is_array($options['separator'])) {
  508. $options['separator'] = array(' - ', $options['separator']);
  509. }
  510. $out = $start . $options['separator'][0] . $end . $options['separator'][1];
  511. $out .= $paging['count'];
  512. break;
  513. case 'pages':
  514. $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
  515. break;
  516. default:
  517. $map = array(
  518. '%page%' => $paging['page'],
  519. '%pages%' => $paging['pageCount'],
  520. '%current%' => $paging['current'],
  521. '%count%' => $paging['count'],
  522. '%start%' => $start,
  523. '%end%' => $end
  524. );
  525. $out = str_replace(array_keys($map), array_values($map), $options['format']);
  526. $newKeys = array(
  527. '{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}'
  528. );
  529. $out = str_replace($newKeys, array_values($map), $out);
  530. break;
  531. }
  532. return $out;
  533. }
  534. /**
  535. * Returns a set of numbers for the paged result set
  536. * uses a modulus to decide how many numbers to show on each side of the current page (default: 8)
  537. *
  538. * ### Options
  539. *
  540. * - `before` Content to be inserted before the numbers
  541. * - `after` Content to be inserted after the numbers
  542. * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
  543. * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
  544. * - `separator` Separator content defaults to ' | '
  545. * - `tag` The tag to wrap links in, defaults to 'span'
  546. * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
  547. * links to generate
  548. * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
  549. * links to generate
  550. *
  551. * @param mixed $options Options for the numbers, (before, after, model, modulus, separator)
  552. * @return string numbers string.
  553. * @access public
  554. */
  555. function numbers($options = array()) {
  556. if ($options === true) {
  557. $options = array(
  558. 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
  559. );
  560. }
  561. $defaults = array(
  562. 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(),
  563. 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null,
  564. );
  565. $options += $defaults;
  566. $params = (array)$this->params($options['model']) + array('page'=> 1);
  567. unset($options['model']);
  568. if ($params['pageCount'] <= 1) {
  569. return false;
  570. }
  571. extract($options);
  572. unset($options['tag'], $options['before'], $options['after'], $options['model'],
  573. $options['modulus'], $options['separator'], $options['first'], $options['last']);
  574. $out = '';
  575. if ($modulus && $params['pageCount'] > $modulus) {
  576. $half = intval($modulus / 2);
  577. $end = $params['page'] + $half;
  578. if ($end > $params['pageCount']) {
  579. $end = $params['pageCount'];
  580. }
  581. $start = $params['page'] - ($modulus - ($end - $params['page']));
  582. if ($start <= 1) {
  583. $start = 1;
  584. $end = $params['page'] + ($modulus - $params['page']) + 1;
  585. }
  586. if ($first && $start > 1) {
  587. $offset = ($start <= (int)$first) ? $start - 1 : $first;
  588. if ($offset < $start - 1) {
  589. $out .= $this->first($offset, array('tag' => $tag, 'separator' => $separator));
  590. } else {
  591. $out .= $this->first($offset, array('tag' => $tag, 'after' => $separator, 'separator' => $separator));
  592. }
  593. }
  594. $out .= $before;
  595. for ($i = $start; $i < $params['page']; $i++) {
  596. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options))
  597. . $separator;
  598. }
  599. $out .= $this->Html->tag($tag, $params['page'], array('class' => 'current'));
  600. if ($i != $params['pageCount']) {
  601. $out .= $separator;
  602. }
  603. $start = $params['page'] + 1;
  604. for ($i = $start; $i < $end; $i++) {
  605. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options))
  606. . $separator;
  607. }
  608. if ($end != $params['page']) {
  609. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options));
  610. }
  611. $out .= $after;
  612. if ($last && $end < $params['pageCount']) {
  613. $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
  614. if ($offset <= $last && $params['pageCount'] - $end > $offset) {
  615. $out .= $this->last($offset, array('tag' => $tag, 'separator' => $separator));
  616. } else {
  617. $out .= $this->last($offset, array('tag' => $tag, 'before' => $separator, 'separator' => $separator));
  618. }
  619. }
  620. } else {
  621. $out .= $before;
  622. for ($i = 1; $i <= $params['pageCount']; $i++) {
  623. if ($i == $params['page']) {
  624. $out .= $this->Html->tag($tag, $i, array('class' => 'current'));
  625. } else {
  626. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
  627. }
  628. if ($i != $params['pageCount']) {
  629. $out .= $separator;
  630. }
  631. }
  632. $out .= $after;
  633. }
  634. return $out;
  635. }
  636. /**
  637. * Returns a first or set of numbers for the first pages
  638. *
  639. * ### Options:
  640. *
  641. * - `tag` The tag wrapping tag you want to use, defaults to 'span'
  642. * - `after` Content to insert after the link/tag
  643. * - `model` The model to use defaults to PaginatorHelper::defaultModel()
  644. * - `separator` Content between the generated links, defaults to ' | '
  645. *
  646. * @param mixed $first if string use as label for the link, if numeric print page numbers
  647. * @param mixed $options
  648. * @return string numbers string.
  649. * @access public
  650. */
  651. function first($first = '<< first', $options = array()) {
  652. $options = array_merge(
  653. array(
  654. 'tag' => 'span',
  655. 'after'=> null,
  656. 'model' => $this->defaultModel(),
  657. 'separator' => ' | ',
  658. ),
  659. (array)$options);
  660. $params = array_merge(array('page'=> 1), (array)$this->params($options['model']));
  661. unset($options['model']);
  662. if ($params['pageCount'] <= 1) {
  663. return false;
  664. }
  665. extract($options);
  666. unset($options['tag'], $options['after'], $options['model'], $options['separator']);
  667. $out = '';
  668. if (is_int($first) && $params['page'] > $first) {
  669. if ($after === null) {
  670. $after = '...';
  671. }
  672. for ($i = 1; $i <= $first; $i++) {
  673. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
  674. if ($i != $first) {
  675. $out .= $separator;
  676. }
  677. }
  678. $out .= $after;
  679. } elseif ($params['page'] > 1) {
  680. $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options))
  681. . $after;
  682. }
  683. return $out;
  684. }
  685. /**
  686. * Returns a last or set of numbers for the last pages
  687. *
  688. * ### Options:
  689. *
  690. * - `tag` The tag wrapping tag you want to use, defaults to 'span'
  691. * - `before` Content to insert before the link/tag
  692. * - `model` The model to use defaults to PaginatorHelper::defaultModel()
  693. * - `separator` Content between the generated links, defaults to ' | '
  694. *
  695. * @param mixed $last if string use as label for the link, if numeric print page numbers
  696. * @param mixed $options Array of options
  697. * @return string numbers string.
  698. * @access public
  699. */
  700. function last($last = 'last >>', $options = array()) {
  701. $options = array_merge(
  702. array(
  703. 'tag' => 'span',
  704. 'before'=> null,
  705. 'model' => $this->defaultModel(),
  706. 'separator' => ' | ',
  707. ),
  708. (array)$options);
  709. $params = array_merge(array('page'=> 1), (array)$this->params($options['model']));
  710. unset($options['model']);
  711. if ($params['pageCount'] <= 1) {
  712. return false;
  713. }
  714. extract($options);
  715. unset($options['tag'], $options['before'], $options['model'], $options['separator']);
  716. $out = '';
  717. $lower = $params['pageCount'] - $last + 1;
  718. if (is_int($last) && $params['page'] < $lower) {
  719. if ($before === null) {
  720. $before = '...';
  721. }
  722. for ($i = $lower; $i <= $params['pageCount']; $i++) {
  723. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options));
  724. if ($i != $params['pageCount']) {
  725. $out .= $separator;
  726. }
  727. }
  728. $out = $before . $out;
  729. } elseif ($params['page'] < $params['pageCount']) {
  730. $out = $before . $this->Html->tag(
  731. $tag, $this->link($last, array('page' => $params['pageCount']), $options
  732. ));
  733. }
  734. return $out;
  735. }
  736. }