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

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

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