PageRenderTime 222ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/Vendor/pear-pear.cakephp.org/CakePHP/Cake/View/Helper/PaginatorHelper.php

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