PaginatorHelper.php 28 KB

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