PageRenderTime 56ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/browsewire/symfony-project-code
PHP | 936 lines | 508 code | 81 blank | 347 comment | 114 complexity | 8d7f9cd7867f455d3f5e147deaedb207 MD5 | raw file
  1. <?php
  2. /**
  3. * Pagination Helper class file.
  4. *
  5. * Generates pagination links
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright 2005-2012, 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-2012, 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 (!class_exists($classname) || !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. if (!empty($this->request->query)) {
  106. $this->options['url']['?'] = $this->request->query;
  107. }
  108. parent::beforeRender($viewFile);
  109. }
  110. /**
  111. * Gets the current paging parameters from the resultset for the given model
  112. *
  113. * @param string $model Optional model name. Uses the default if none is specified.
  114. * @return array The array of paging parameters for the paginated resultset.
  115. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::params
  116. */
  117. public function params($model = null) {
  118. if (empty($model)) {
  119. $model = $this->defaultModel();
  120. }
  121. if (!isset($this->request->params['paging']) || empty($this->request->params['paging'][$model])) {
  122. return null;
  123. }
  124. return $this->request->params['paging'][$model];
  125. }
  126. /**
  127. * Sets default options for all pagination links
  128. *
  129. * @param array|string $options Default options for pagination links. If a string is supplied - it
  130. * is used as the DOM id element to update. See PaginatorHelper::$options for list of keys.
  131. * @return void
  132. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::options
  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. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::current
  166. */
  167. public function current($model = null) {
  168. $params = $this->params($model);
  169. if (isset($params['page'])) {
  170. return $params['page'];
  171. }
  172. return 1;
  173. }
  174. /**
  175. * Gets the current key by which the recordset is sorted
  176. *
  177. * @param string $model Optional model name. Uses the default if none is specified.
  178. * @param array $options Options for pagination links. See #options for list of keys.
  179. * @return string The name of the key by which the recordset is being sorted, or
  180. * null if the results are not currently sorted.
  181. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortKey
  182. */
  183. public function sortKey($model = null, $options = array()) {
  184. if (empty($options)) {
  185. $params = $this->params($model);
  186. $options = $params['options'];
  187. }
  188. if (isset($options['sort']) && !empty($options['sort'])) {
  189. return $options['sort'];
  190. }
  191. if (isset($options['order'])) {
  192. return is_array($options['order']) ? key($options['order']) : $options['order'];
  193. }
  194. if (isset($params['order'])) {
  195. return is_array($params['order']) ? key($params['order']) : $params['order'];
  196. }
  197. return null;
  198. }
  199. /**
  200. * Gets the current direction the recordset is sorted
  201. *
  202. * @param string $model Optional model name. Uses the default if none is specified.
  203. * @param array $options Options for pagination links. See #options for list of keys.
  204. * @return string The direction by which the recordset is being sorted, or
  205. * null if the results are not currently sorted.
  206. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sortDir
  207. */
  208. public function sortDir($model = null, $options = array()) {
  209. $dir = null;
  210. if (empty($options)) {
  211. $params = $this->params($model);
  212. $options = $params['options'];
  213. }
  214. if (isset($options['direction'])) {
  215. $dir = strtolower($options['direction']);
  216. } elseif (isset($options['order']) && is_array($options['order'])) {
  217. $dir = strtolower(current($options['order']));
  218. } elseif (isset($params['order']) && is_array($params['order'])) {
  219. $dir = strtolower(current($params['order']));
  220. }
  221. if ($dir == 'desc') {
  222. return 'desc';
  223. }
  224. return 'asc';
  225. }
  226. /**
  227. * Generates a "previous" link for a set of paged records
  228. *
  229. * ### Options:
  230. *
  231. * - `tag` The tag wrapping tag you want to use, defaults to 'span'. Set this to false to disable this option
  232. * - `escape` Whether you want the contents html entity encoded, defaults to true
  233. * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
  234. * - `disabledTag` Tag to use instead of A tag when there is no previous page
  235. *
  236. * @param string $title Title for the link. Defaults to '<< Previous'.
  237. * @param array $options Options for pagination link. See #options for list of keys.
  238. * @param string $disabledTitle Title when the link is disabled.
  239. * @param array $disabledOptions Options for the disabled pagination link. See #options for list of keys.
  240. * @return string A "previous" link or $disabledTitle text if the link is disabled.
  241. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::prev
  242. */
  243. public function prev($title = '<< Previous', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
  244. $defaults = array(
  245. 'rel' => 'prev'
  246. );
  247. $options = array_merge($defaults, (array)$options);
  248. return $this->_pagingLink('Prev', $title, $options, $disabledTitle, $disabledOptions);
  249. }
  250. /**
  251. * Generates a "next" link for a set of paged records
  252. *
  253. * ### Options:
  254. *
  255. * - `tag` The tag wrapping tag you want to use, defaults to 'span'. Set this to false to disable this option
  256. * - `escape` Whether you want the contents html entity encoded, defaults to true
  257. * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
  258. * - `disabledTag` Tag to use instead of A tag when there is no next page
  259. *
  260. * @param string $title Title for the link. Defaults to 'Next >>'.
  261. * @param array $options Options for pagination link. See above for list of keys.
  262. * @param string $disabledTitle Title when the link is disabled.
  263. * @param array $disabledOptions Options for the disabled pagination link. See above for list of keys.
  264. * @return string A "next" link or $disabledTitle text if the link is disabled.
  265. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::next
  266. */
  267. public function next($title = 'Next >>', $options = array(), $disabledTitle = null, $disabledOptions = array()) {
  268. $defaults = array(
  269. 'rel' => 'next'
  270. );
  271. $options = array_merge($defaults, (array)$options);
  272. return $this->_pagingLink('Next', $title, $options, $disabledTitle, $disabledOptions);
  273. }
  274. /**
  275. * Generates a sorting link. Sets named parameters for the sort and direction. Handles
  276. * direction switching automatically.
  277. *
  278. * ### Options:
  279. *
  280. * - `escape` Whether you want the contents html entity encoded, defaults to true
  281. * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
  282. * - `direction` The default direction to use when this link isn't active.
  283. *
  284. * @param string $key The name of the key that the recordset should be sorted.
  285. * @param string $title Title for the link. If $title is null $key will be used
  286. * for the title and will be generated by inflection.
  287. * @param array $options Options for sorting link. See above for list of keys.
  288. * @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
  289. * key the returned link will sort by 'desc'.
  290. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sort
  291. */
  292. public function sort($key, $title = null, $options = array()) {
  293. $options = array_merge(array('url' => array(), 'model' => null), $options);
  294. $url = $options['url'];
  295. unset($options['url']);
  296. if (empty($title)) {
  297. $title = $key;
  298. if (strpos($title, '.') !== false) {
  299. $title = str_replace('.', ' ', $title);
  300. }
  301. $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
  302. }
  303. $dir = isset($options['direction']) ? $options['direction'] : 'asc';
  304. unset($options['direction']);
  305. $sortKey = $this->sortKey($options['model']);
  306. $defaultModel = $this->defaultModel();
  307. $isSorted = (
  308. $sortKey === $key ||
  309. $sortKey === $defaultModel . '.' . $key ||
  310. $key === $defaultModel . '.' . $sortKey
  311. );
  312. if ($isSorted) {
  313. $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
  314. $class = $dir === 'asc' ? 'desc' : 'asc';
  315. if (!empty($options['class'])) {
  316. $options['class'] .= ' ' . $class;
  317. } else {
  318. $options['class'] = $class;
  319. }
  320. }
  321. if (is_array($title) && array_key_exists($dir, $title)) {
  322. $title = $title[$dir];
  323. }
  324. $url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
  325. return $this->link($title, $url, $options);
  326. }
  327. /**
  328. * Generates a plain or Ajax link with pagination parameters
  329. *
  330. * ### Options
  331. *
  332. * - `update` The Id of the DOM element you wish to update. Creates Ajax enabled links
  333. * with the AjaxHelper.
  334. * - `escape` Whether you want the contents html entity encoded, defaults to true
  335. * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
  336. *
  337. * @param string $title Title for the link.
  338. * @param string|array $url Url for the action. See Router::url()
  339. * @param array $options Options for the link. See #options for list of keys.
  340. * @return string A link with pagination parameters.
  341. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::link
  342. */
  343. public function link($title, $url = array(), $options = array()) {
  344. $options = array_merge(array('model' => null, 'escape' => true), $options);
  345. $model = $options['model'];
  346. unset($options['model']);
  347. if (!empty($this->options)) {
  348. $options = array_merge($this->options, $options);
  349. }
  350. if (isset($options['url'])) {
  351. $url = array_merge((array)$options['url'], (array)$url);
  352. unset($options['url']);
  353. }
  354. unset($options['convertKeys']);
  355. $url = $this->url($url, true, $model);
  356. $obj = isset($options['update']) ? $this->_ajaxHelperClass : 'Html';
  357. return $this->{$obj}->link($title, $url, $options);
  358. }
  359. /**
  360. * Merges passed URL options with current pagination state to generate a pagination URL.
  361. *
  362. * @param array $options Pagination/URL options array
  363. * @param boolean $asArray Return the url as an array, or a URI string
  364. * @param string $model Which model to paginate on
  365. * @return mixed By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
  366. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::url
  367. */
  368. public function url($options = array(), $asArray = false, $model = null) {
  369. $paging = $this->params($model);
  370. $url = array_merge(array_filter($paging['options']), $options);
  371. if (isset($url['order'])) {
  372. $sort = $direction = null;
  373. if (is_array($url['order'])) {
  374. list($sort, $direction) = array($this->sortKey($model, $url), current($url['order']));
  375. }
  376. unset($url['order']);
  377. $url = array_merge($url, compact('sort', 'direction'));
  378. }
  379. $url = $this->_convertUrlKeys($url, $paging['paramType']);
  380. if ($asArray) {
  381. return $url;
  382. }
  383. return parent::url($url);
  384. }
  385. /**
  386. * Converts the keys being used into the format set by options.paramType
  387. *
  388. * @param array $url Array of url params to convert
  389. * @param string $type
  390. * @return array converted url params.
  391. */
  392. protected function _convertUrlKeys($url, $type) {
  393. if ($type == 'named') {
  394. return $url;
  395. }
  396. if (!isset($url['?'])) {
  397. $url['?'] = array();
  398. }
  399. foreach ($this->options['convertKeys'] as $key) {
  400. if (isset($url[$key])) {
  401. $url['?'][$key] = $url[$key];
  402. unset($url[$key]);
  403. }
  404. }
  405. return $url;
  406. }
  407. /**
  408. * Protected method for generating prev/next links
  409. *
  410. * @param string $which
  411. * @param string $title
  412. * @param array $options
  413. * @param string $disabledTitle
  414. * @param array $disabledOptions
  415. * @return string
  416. */
  417. protected function _pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array()) {
  418. $check = 'has' . $which;
  419. $_defaults = array(
  420. 'url' => array(), 'step' => 1, 'escape' => true, 'model' => null,
  421. 'tag' => 'span', 'class' => strtolower($which), 'disabledTag' => null
  422. );
  423. $options = array_merge($_defaults, (array)$options);
  424. $paging = $this->params($options['model']);
  425. if (empty($disabledOptions)) {
  426. $disabledOptions = $options;
  427. }
  428. if (!$this->{$check}($options['model']) && (!empty($disabledTitle) || !empty($disabledOptions))) {
  429. if (!empty($disabledTitle) && $disabledTitle !== true) {
  430. $title = $disabledTitle;
  431. }
  432. $options = array_merge($_defaults, (array)$disabledOptions);
  433. } elseif (!$this->{$check}($options['model'])) {
  434. return null;
  435. }
  436. foreach (array_keys($_defaults) as $key) {
  437. ${$key} = $options[$key];
  438. unset($options[$key]);
  439. }
  440. if ($this->{$check}($model)) {
  441. $url = array_merge(
  442. array('page' => $paging['page'] + ($which == 'Prev' ? $step * -1 : $step)),
  443. $url
  444. );
  445. if ($tag === false) {
  446. return $this->link(
  447. $title,
  448. $url,
  449. compact('escape', 'model', 'class') + $options
  450. );
  451. }
  452. $link = $this->link($title, $url, compact('escape', 'model') + $options);
  453. return $this->Html->tag($tag, $link, compact('class'));
  454. } else {
  455. unset($options['rel']);
  456. if (!$tag) {
  457. if ($disabledTag) {
  458. $tag = $disabledTag;
  459. $disabledTag = null;
  460. } else {
  461. $tag = $_defaults['tag'];
  462. }
  463. }
  464. if ($disabledTag) {
  465. $title = $this->Html->tag($disabledTag, $title, compact('escape') + $options);
  466. return $this->Html->tag($tag, $title, compact('class'));
  467. }
  468. return $this->Html->tag($tag, $title, compact('escape', 'class') + $options);
  469. }
  470. }
  471. /**
  472. * Returns true if the given result set is not at the first page
  473. *
  474. * @param string $model Optional model name. Uses the default if none is specified.
  475. * @return boolean True if the result set is not at the first page.
  476. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPrev
  477. */
  478. public function hasPrev($model = null) {
  479. return $this->_hasPage($model, 'prev');
  480. }
  481. /**
  482. * Returns true if the given result set is not at the last page
  483. *
  484. * @param string $model Optional model name. Uses the default if none is specified.
  485. * @return boolean True if the result set is not at the last page.
  486. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasNext
  487. */
  488. public function hasNext($model = null) {
  489. return $this->_hasPage($model, 'next');
  490. }
  491. /**
  492. * Returns true if the given result set has the page number given by $page
  493. *
  494. * @param string $model Optional model name. Uses the default if none is specified.
  495. * @param integer $page The page number - if not set defaults to 1.
  496. * @return boolean True if the given result set has the specified page number.
  497. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPage
  498. */
  499. public function hasPage($model = null, $page = 1) {
  500. if (is_numeric($model)) {
  501. $page = $model;
  502. $model = null;
  503. }
  504. $paging = $this->params($model);
  505. return $page <= $paging['pageCount'];
  506. }
  507. /**
  508. * Does $model have $page in its range?
  509. *
  510. * @param string $model Model name to get parameters for.
  511. * @param integer $page Page number you are checking.
  512. * @return boolean Whether model has $page
  513. */
  514. protected function _hasPage($model, $page) {
  515. $params = $this->params($model);
  516. return !empty($params) && $params[$page . 'Page'];
  517. }
  518. /**
  519. * Gets the default model of the paged sets
  520. *
  521. * @return string Model name or null if the pagination isn't initialized.
  522. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::defaultModel
  523. */
  524. public function defaultModel() {
  525. if ($this->_defaultModel) {
  526. return $this->_defaultModel;
  527. }
  528. if (empty($this->request->params['paging'])) {
  529. return null;
  530. }
  531. list($this->_defaultModel) = array_keys($this->request->params['paging']);
  532. return $this->_defaultModel;
  533. }
  534. /**
  535. * Returns a counter string for the paged result set
  536. *
  537. * ### Options
  538. *
  539. * - `model` The model to use, defaults to PaginatorHelper::defaultModel();
  540. * - `format` The format string you want to use, defaults to 'pages' Which generates output like '1 of 5'
  541. * set to 'range' to generate output like '1 - 3 of 13'. Can also be set to a custom string, containing
  542. * the following placeholders `{:page}`, `{:pages}`, `{:current}`, `{:count}`, `{:model}`, `{:start}`, `{:end}` and any
  543. * custom content you would like.
  544. * - `separator` The separator string to use, default to ' of '
  545. *
  546. * The `%page%` style placeholders also work, but are deprecated and will be removed in a future version.
  547. * @param array $options Options for the counter string. See #options for list of keys.
  548. * @return string Counter string.
  549. * @deprecated The %page% style placeholders are deprecated.
  550. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::counter
  551. */
  552. public function counter($options = array()) {
  553. if (is_string($options)) {
  554. $options = array('format' => $options);
  555. }
  556. $options = array_merge(
  557. array(
  558. 'model' => $this->defaultModel(),
  559. 'format' => 'pages',
  560. 'separator' => __d('cake', ' of ')
  561. ),
  562. $options);
  563. $paging = $this->params($options['model']);
  564. if (!$paging['pageCount']) {
  565. $paging['pageCount'] = 1;
  566. }
  567. $start = 0;
  568. if ($paging['count'] >= 1) {
  569. $start = (($paging['page'] - 1) * $paging['limit']) + 1;
  570. }
  571. $end = $start + $paging['limit'] - 1;
  572. if ($paging['count'] < $end) {
  573. $end = $paging['count'];
  574. }
  575. switch ($options['format']) {
  576. case 'range':
  577. if (!is_array($options['separator'])) {
  578. $options['separator'] = array(' - ', $options['separator']);
  579. }
  580. $out = $start . $options['separator'][0] . $end . $options['separator'][1];
  581. $out .= $paging['count'];
  582. break;
  583. case 'pages':
  584. $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
  585. break;
  586. default:
  587. $map = array(
  588. '%page%' => $paging['page'],
  589. '%pages%' => $paging['pageCount'],
  590. '%current%' => $paging['current'],
  591. '%count%' => $paging['count'],
  592. '%start%' => $start,
  593. '%end%' => $end,
  594. '%model%' => strtolower(Inflector::humanize(Inflector::tableize($options['model'])))
  595. );
  596. $out = str_replace(array_keys($map), array_values($map), $options['format']);
  597. $newKeys = array(
  598. '{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}', '{:model}'
  599. );
  600. $out = str_replace($newKeys, array_values($map), $out);
  601. break;
  602. }
  603. return $out;
  604. }
  605. /**
  606. * Returns a set of numbers for the paged result set
  607. * uses a modulus to decide how many numbers to show on each side of the current page (default: 8).
  608. *
  609. * `$this->Paginator->numbers(array('first' => 2, 'last' => 2));`
  610. *
  611. * Using the first and last options you can create links to the beginning and end of the page set.
  612. *
  613. * ### Options
  614. *
  615. * - `before` Content to be inserted before the numbers
  616. * - `after` Content to be inserted after the numbers
  617. * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
  618. * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
  619. * - `separator` Separator content defaults to ' | '
  620. * - `tag` The tag to wrap links in, defaults to 'span'
  621. * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
  622. * links to generate.
  623. * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
  624. * links to generate.
  625. * - `ellipsis` Ellipsis content, defaults to '...'
  626. * - `class` Class for wrapper tag
  627. * - `currentClass` Class for wrapper tag on current active page, defaults to 'current'
  628. * - `currentTag` Tag to use for current page number, defaults to null
  629. *
  630. * @param array $options Options for the numbers, (before, after, model, modulus, separator)
  631. * @return string numbers string.
  632. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::numbers
  633. */
  634. public function numbers($options = array()) {
  635. if ($options === true) {
  636. $options = array(
  637. 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
  638. );
  639. }
  640. $defaults = array(
  641. 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
  642. 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
  643. 'currentClass' => 'current', 'currentTag' => null
  644. );
  645. $options += $defaults;
  646. $params = (array)$this->params($options['model']) + array('page' => 1);
  647. unset($options['model']);
  648. if ($params['pageCount'] <= 1) {
  649. return false;
  650. }
  651. extract($options);
  652. unset($options['tag'], $options['before'], $options['after'], $options['model'],
  653. $options['modulus'], $options['separator'], $options['first'], $options['last'],
  654. $options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
  655. );
  656. $out = '';
  657. if ($modulus && $params['pageCount'] > $modulus) {
  658. $half = intval($modulus / 2);
  659. $end = $params['page'] + $half;
  660. if ($end > $params['pageCount']) {
  661. $end = $params['pageCount'];
  662. }
  663. $start = $params['page'] - ($modulus - ($end - $params['page']));
  664. if ($start <= 1) {
  665. $start = 1;
  666. $end = $params['page'] + ($modulus - $params['page']) + 1;
  667. }
  668. if ($first && $start > 1) {
  669. $offset = ($start <= (int)$first) ? $start - 1 : $first;
  670. if ($offset < $start - 1) {
  671. $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  672. } else {
  673. $out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
  674. }
  675. }
  676. $out .= $before;
  677. for ($i = $start; $i < $params['page']; $i++) {
  678. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
  679. }
  680. if ($class) {
  681. $currentClass .= ' ' . $class;
  682. }
  683. if ($currentTag) {
  684. $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass));
  685. } else {
  686. $out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
  687. }
  688. if ($i != $params['pageCount']) {
  689. $out .= $separator;
  690. }
  691. $start = $params['page'] + 1;
  692. for ($i = $start; $i < $end; $i++) {
  693. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
  694. }
  695. if ($end != $params['page']) {
  696. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
  697. }
  698. $out .= $after;
  699. if ($last && $end < $params['pageCount']) {
  700. $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
  701. if ($offset <= $last && $params['pageCount'] - $end > $offset) {
  702. $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  703. } else {
  704. $out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
  705. }
  706. }
  707. } else {
  708. $out .= $before;
  709. for ($i = 1; $i <= $params['pageCount']; $i++) {
  710. if ($i == $params['page']) {
  711. if ($class) {
  712. $currentClass .= ' ' . $class;
  713. }
  714. if ($currentTag) {
  715. $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass));
  716. } else {
  717. $out .= $this->Html->tag($tag, $i, array('class' => $currentClass));
  718. }
  719. } else {
  720. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  721. }
  722. if ($i != $params['pageCount']) {
  723. $out .= $separator;
  724. }
  725. }
  726. $out .= $after;
  727. }
  728. return $out;
  729. }
  730. /**
  731. * Returns a first or set of numbers for the first pages.
  732. *
  733. * `echo $this->Paginator->first('< first');`
  734. *
  735. * Creates a single link for the first page. Will output nothing if you are on the first page.
  736. *
  737. * `echo $this->Paginator->first(3);`
  738. *
  739. * Will create links for the first 3 pages, once you get to the third or greater page. Prior to that
  740. * nothing will be output.
  741. *
  742. * ### Options:
  743. *
  744. * - `tag` The tag wrapping tag you want to use, defaults to 'span'
  745. * - `after` Content to insert after the link/tag
  746. * - `model` The model to use defaults to PaginatorHelper::defaultModel()
  747. * - `separator` Content between the generated links, defaults to ' | '
  748. * - `ellipsis` Content for ellipsis, defaults to '...'
  749. *
  750. * @param string|integer $first if string use as label for the link. If numeric, the number of page links
  751. * you want at the beginning of the range.
  752. * @param array $options An array of options.
  753. * @return string numbers string.
  754. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
  755. */
  756. public function first($first = '<< first', $options = array()) {
  757. $options = array_merge(
  758. array(
  759. 'tag' => 'span',
  760. 'after' => null,
  761. 'model' => $this->defaultModel(),
  762. 'separator' => ' | ',
  763. 'ellipsis' => '...',
  764. 'class' => null
  765. ),
  766. (array)$options);
  767. $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
  768. unset($options['model']);
  769. if ($params['pageCount'] <= 1) {
  770. return false;
  771. }
  772. extract($options);
  773. unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
  774. $out = '';
  775. if (is_int($first) && $params['page'] >= $first) {
  776. if ($after === null) {
  777. $after = $ellipsis;
  778. }
  779. for ($i = 1; $i <= $first; $i++) {
  780. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  781. if ($i != $first) {
  782. $out .= $separator;
  783. }
  784. }
  785. $out .= $after;
  786. } elseif ($params['page'] > 1 && is_string($first)) {
  787. $options += array('rel' => 'first');
  788. $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
  789. }
  790. return $out;
  791. }
  792. /**
  793. * Returns a last or set of numbers for the last pages.
  794. *
  795. * `echo $this->Paginator->last('last >');`
  796. *
  797. * Creates a single link for the last page. Will output nothing if you are on the last page.
  798. *
  799. * `echo $this->Paginator->last(3);`
  800. *
  801. * Will create links for the last 3 pages. Once you enter the page range, no output will be created.
  802. *
  803. * ### Options:
  804. *
  805. * - `tag` The tag wrapping tag you want to use, defaults to 'span'
  806. * - `before` Content to insert before the link/tag
  807. * - `model` The model to use defaults to PaginatorHelper::defaultModel()
  808. * - `separator` Content between the generated links, defaults to ' | '
  809. * - `ellipsis` Content for ellipsis, defaults to '...'
  810. *
  811. * @param string|integer $last if string use as label for the link, if numeric print page numbers
  812. * @param array $options Array of options
  813. * @return string numbers string.
  814. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::last
  815. */
  816. public function last($last = 'last >>', $options = array()) {
  817. $options = array_merge(
  818. array(
  819. 'tag' => 'span',
  820. 'before' => null,
  821. 'model' => $this->defaultModel(),
  822. 'separator' => ' | ',
  823. 'ellipsis' => '...',
  824. 'class' => null
  825. ),
  826. (array)$options);
  827. $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
  828. unset($options['model']);
  829. if ($params['pageCount'] <= 1) {
  830. return false;
  831. }
  832. extract($options);
  833. unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
  834. $out = '';
  835. $lower = $params['pageCount'] - $last + 1;
  836. if (is_int($last) && $params['page'] <= $lower) {
  837. if ($before === null) {
  838. $before = $ellipsis;
  839. }
  840. for ($i = $lower; $i <= $params['pageCount']; $i++) {
  841. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  842. if ($i != $params['pageCount']) {
  843. $out .= $separator;
  844. }
  845. }
  846. $out = $before . $out;
  847. } elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
  848. $options += array('rel' => 'last');
  849. $out = $before . $this->Html->tag(
  850. $tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
  851. );
  852. }
  853. return $out;
  854. }
  855. }