PageRenderTime 54ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

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

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