PageRenderTime 53ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

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

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