PageRenderTime 49ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/fouzia23chowdhury/cakephpCRUD
PHP | 1007 lines | 545 code | 83 blank | 379 comment | 123 complexity | f28ae9c80f11d6f425902ed6ef4de43f MD5 | raw file
  1. <?php
  2. /**
  3. * Pagination Helper class file.
  4. *
  5. * Generates pagination links
  6. *
  7. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  8. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  9. *
  10. * Licensed under The MIT License
  11. * For full copyright and license information, please see the LICENSE.txt
  12. * Redistributions of files must retain the above copyright notice.
  13. *
  14. * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  15. * @link http://cakephp.org CakePHP(tm) Project
  16. * @package Cake.View.Helper
  17. * @since CakePHP(tm) v 1.2.0
  18. * @license 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 View file name.
  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|null 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|null 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)$options + $defaults;
  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)$options + $defaults;
  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. * - `lock` Lock direction. Will only use the default direction then, defaults to false.
  302. *
  303. * @param string $key The name of the key that the recordset should be sorted.
  304. * @param string $title Title for the link. If $title is null $key will be used
  305. * for the title and will be generated by inflection.
  306. * @param array $options Options for sorting link. See above for list of keys.
  307. * @return string A link sorting default by 'asc'. If the resultset is sorted 'asc' by the specified
  308. * key the returned link will sort by 'desc'.
  309. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::sort
  310. */
  311. public function sort($key, $title = null, $options = array()) {
  312. $options += array('url' => array(), 'model' => null);
  313. $url = $options['url'];
  314. unset($options['url']);
  315. if (empty($title)) {
  316. $title = $key;
  317. if (strpos($title, '.') !== false) {
  318. $title = str_replace('.', ' ', $title);
  319. }
  320. $title = __(Inflector::humanize(preg_replace('/_id$/', '', $title)));
  321. }
  322. $defaultDir = isset($options['direction']) ? $options['direction'] : 'asc';
  323. unset($options['direction']);
  324. $locked = isset($options['lock']) ? $options['lock'] : false;
  325. unset($options['lock']);
  326. $sortKey = $this->sortKey($options['model']);
  327. $defaultModel = $this->defaultModel();
  328. $isSorted = (
  329. $sortKey === $key ||
  330. $sortKey === $defaultModel . '.' . $key ||
  331. $key === $defaultModel . '.' . $sortKey
  332. );
  333. $dir = $defaultDir;
  334. if ($isSorted) {
  335. $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
  336. $class = $dir === 'asc' ? 'desc' : 'asc';
  337. if (!empty($options['class'])) {
  338. $options['class'] .= ' ' . $class;
  339. } else {
  340. $options['class'] = $class;
  341. }
  342. if ($locked) {
  343. $dir = $defaultDir;
  344. $options['class'] .= ' locked';
  345. }
  346. }
  347. if (is_array($title) && array_key_exists($dir, $title)) {
  348. $title = $title[$dir];
  349. }
  350. $url = array_merge(array('sort' => $key, 'direction' => $dir), $url, array('order' => null));
  351. return $this->link($title, $url, $options);
  352. }
  353. /**
  354. * Generates a plain or Ajax link with pagination parameters
  355. *
  356. * ### Options
  357. *
  358. * - `update` The Id of the DOM element you wish to update. Creates Ajax enabled links
  359. * with the AjaxHelper.
  360. * - `escape` Whether you want the contents html entity encoded, defaults to true
  361. * - `model` The model to use, defaults to PaginatorHelper::defaultModel()
  362. *
  363. * @param string $title Title for the link.
  364. * @param string|array $url URL for the action. See Router::url()
  365. * @param array $options Options for the link. See #options for list of keys.
  366. * @return string A link with pagination parameters.
  367. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::link
  368. */
  369. public function link($title, $url = array(), $options = array()) {
  370. $options += array('model' => null, 'escape' => true);
  371. $model = $options['model'];
  372. unset($options['model']);
  373. if (!empty($this->options)) {
  374. $options += $this->options;
  375. }
  376. if (isset($options['url'])) {
  377. $url = array_merge((array)$options['url'], (array)$url);
  378. unset($options['url']);
  379. }
  380. unset($options['convertKeys']);
  381. $url = $this->url($url, true, $model);
  382. $obj = isset($options['update']) ? $this->_ajaxHelperClass : 'Html';
  383. return $this->{$obj}->link($title, $url, $options);
  384. }
  385. /**
  386. * Merges passed URL options with current pagination state to generate a pagination URL.
  387. *
  388. * @param array $options Pagination/URL options array
  389. * @param bool $asArray Return the URL as an array, or a URI string
  390. * @param string $model Which model to paginate on
  391. * @return mixed By default, returns a full pagination URL string for use in non-standard contexts (i.e. JavaScript)
  392. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::url
  393. */
  394. public function url($options = array(), $asArray = false, $model = null) {
  395. $paging = $this->params($model);
  396. $url = array_merge(array_filter($paging['options']), $options);
  397. if (isset($url['order'])) {
  398. $sort = $direction = null;
  399. if (is_array($url['order'])) {
  400. list($sort, $direction) = array($this->sortKey($model, $url), current($url['order']));
  401. }
  402. unset($url['order']);
  403. $url = array_merge($url, compact('sort', 'direction'));
  404. }
  405. $url = $this->_convertUrlKeys($url, $paging['paramType']);
  406. if (!empty($url['page']) && $url['page'] == 1) {
  407. $url['page'] = null;
  408. }
  409. if (!empty($url['?']['page']) && $url['?']['page'] == 1) {
  410. unset($url['?']['page']);
  411. }
  412. if ($asArray) {
  413. return $url;
  414. }
  415. return parent::url($url);
  416. }
  417. /**
  418. * Converts the keys being used into the format set by options.paramType
  419. *
  420. * @param array $url Array of URL params to convert
  421. * @param string $type Keys type.
  422. * @return array converted URL params.
  423. */
  424. protected function _convertUrlKeys($url, $type) {
  425. if ($type === 'named') {
  426. return $url;
  427. }
  428. if (!isset($url['?'])) {
  429. $url['?'] = array();
  430. }
  431. foreach ($this->options['convertKeys'] as $key) {
  432. if (isset($url[$key])) {
  433. $url['?'][$key] = $url[$key];
  434. unset($url[$key]);
  435. }
  436. }
  437. return $url;
  438. }
  439. /**
  440. * Protected method for generating prev/next links
  441. *
  442. * @param string $which Link type: 'Prev', 'Next'.
  443. * @param string $title Link title.
  444. * @param array $options Options list.
  445. * @param string $disabledTitle Disabled link title.
  446. * @param array $disabledOptions Disabled link options.
  447. * @return string
  448. */
  449. protected function _pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array()) {
  450. $check = 'has' . $which;
  451. $_defaults = array(
  452. 'url' => array(), 'step' => 1, 'escape' => true, 'model' => null,
  453. 'tag' => 'span', 'class' => strtolower($which), 'disabledTag' => null
  454. );
  455. $options = (array)$options + $_defaults;
  456. $paging = $this->params($options['model']);
  457. if (empty($disabledOptions)) {
  458. $disabledOptions = $options;
  459. }
  460. if (!$this->{$check}($options['model']) && (!empty($disabledTitle) || !empty($disabledOptions))) {
  461. if (!empty($disabledTitle) && $disabledTitle !== true) {
  462. $title = $disabledTitle;
  463. }
  464. $options = (array)$disabledOptions + array_intersect_key($options, $_defaults) + $_defaults;
  465. } elseif (!$this->{$check}($options['model'])) {
  466. return '';
  467. }
  468. foreach (array_keys($_defaults) as $key) {
  469. ${$key} = $options[$key];
  470. unset($options[$key]);
  471. }
  472. if ($this->{$check}($model)) {
  473. $url = array_merge(
  474. array('page' => $paging['page'] + ($which === 'Prev' ? $step * -1 : $step)),
  475. $url
  476. );
  477. if ($tag === false) {
  478. return $this->link(
  479. $title,
  480. $url,
  481. compact('escape', 'model', 'class') + $options
  482. );
  483. }
  484. $link = $this->link($title, $url, compact('escape', 'model') + $options);
  485. return $this->Html->tag($tag, $link, compact('class'));
  486. }
  487. unset($options['rel']);
  488. if (!$tag) {
  489. if ($disabledTag) {
  490. $tag = $disabledTag;
  491. $disabledTag = null;
  492. } else {
  493. $tag = $_defaults['tag'];
  494. }
  495. }
  496. if ($disabledTag) {
  497. $title = $this->Html->tag($disabledTag, $title, compact('escape') + $options);
  498. return $this->Html->tag($tag, $title, compact('class'));
  499. }
  500. return $this->Html->tag($tag, $title, compact('escape', 'class') + $options);
  501. }
  502. /**
  503. * Returns true if the given result set is not at the first page
  504. *
  505. * @param string $model Optional model name. Uses the default if none is specified.
  506. * @return bool True if the result set is not at the first page.
  507. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPrev
  508. */
  509. public function hasPrev($model = null) {
  510. return $this->_hasPage($model, 'prev');
  511. }
  512. /**
  513. * Returns true if the given result set is not at the last page
  514. *
  515. * @param string $model Optional model name. Uses the default if none is specified.
  516. * @return bool True if the result set is not at the last page.
  517. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasNext
  518. */
  519. public function hasNext($model = null) {
  520. return $this->_hasPage($model, 'next');
  521. }
  522. /**
  523. * Returns true if the given result set has the page number given by $page
  524. *
  525. * @param string $model Optional model name. Uses the default if none is specified.
  526. * @param int $page The page number - if not set defaults to 1.
  527. * @return bool True if the given result set has the specified page number.
  528. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::hasPage
  529. */
  530. public function hasPage($model = null, $page = 1) {
  531. if (is_numeric($model)) {
  532. $page = $model;
  533. $model = null;
  534. }
  535. $paging = $this->params($model);
  536. return $page <= $paging['pageCount'];
  537. }
  538. /**
  539. * Does $model have $page in its range?
  540. *
  541. * @param string $model Model name to get parameters for.
  542. * @param int $page Page number you are checking.
  543. * @return bool Whether model has $page
  544. */
  545. protected function _hasPage($model, $page) {
  546. $params = $this->params($model);
  547. return !empty($params) && $params[$page . 'Page'];
  548. }
  549. /**
  550. * Gets the default model of the paged sets
  551. *
  552. * @return string|null Model name or null if the pagination isn't initialized.
  553. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::defaultModel
  554. */
  555. public function defaultModel() {
  556. if ($this->_defaultModel) {
  557. return $this->_defaultModel;
  558. }
  559. if (empty($this->request->params['paging'])) {
  560. return null;
  561. }
  562. list($this->_defaultModel) = array_keys($this->request->params['paging']);
  563. return $this->_defaultModel;
  564. }
  565. /**
  566. * Returns a counter string for the paged result set
  567. *
  568. * ### Options
  569. *
  570. * - `model` The model to use, defaults to PaginatorHelper::defaultModel();
  571. * - `format` The format string you want to use, defaults to 'pages' Which generates output like '1 of 5'
  572. * set to 'range' to generate output like '1 - 3 of 13'. Can also be set to a custom string, containing
  573. * the following placeholders `{:page}`, `{:pages}`, `{:current}`, `{:count}`, `{:model}`, `{:start}`, `{:end}` and any
  574. * custom content you would like.
  575. * - `separator` The separator string to use, default to ' of '
  576. *
  577. * The `%page%` style placeholders also work, but are deprecated and will be removed in a future version.
  578. *
  579. * @param array $options Options for the counter string. See #options for list of keys.
  580. * @return string Counter string.
  581. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::counter
  582. */
  583. public function counter($options = array()) {
  584. if (is_string($options)) {
  585. $options = array('format' => $options);
  586. }
  587. $options += array(
  588. 'model' => $this->defaultModel(),
  589. 'format' => 'pages',
  590. 'separator' => __d('cake', ' of ')
  591. );
  592. $paging = $this->params($options['model']);
  593. if (!$paging['pageCount']) {
  594. $paging['pageCount'] = 1;
  595. }
  596. $start = 0;
  597. if ($paging['count'] >= 1) {
  598. $start = (($paging['page'] - 1) * $paging['limit']) + 1;
  599. }
  600. $end = $start + $paging['limit'] - 1;
  601. if ($paging['count'] < $end) {
  602. $end = $paging['count'];
  603. }
  604. switch ($options['format']) {
  605. case 'range':
  606. if (!is_array($options['separator'])) {
  607. $options['separator'] = array(' - ', $options['separator']);
  608. }
  609. $out = $start . $options['separator'][0] . $end . $options['separator'][1];
  610. $out .= $paging['count'];
  611. break;
  612. case 'pages':
  613. $out = $paging['page'] . $options['separator'] . $paging['pageCount'];
  614. break;
  615. default:
  616. $map = array(
  617. '%page%' => $paging['page'],
  618. '%pages%' => $paging['pageCount'],
  619. '%current%' => $paging['current'],
  620. '%count%' => $paging['count'],
  621. '%start%' => $start,
  622. '%end%' => $end,
  623. '%model%' => strtolower(Inflector::humanize(Inflector::tableize($options['model'])))
  624. );
  625. $out = str_replace(array_keys($map), array_values($map), $options['format']);
  626. $newKeys = array(
  627. '{:page}', '{:pages}', '{:current}', '{:count}', '{:start}', '{:end}', '{:model}'
  628. );
  629. $out = str_replace($newKeys, array_values($map), $out);
  630. }
  631. return $out;
  632. }
  633. /**
  634. * Returns a set of numbers for the paged result set
  635. * uses a modulus to decide how many numbers to show on each side of the current page (default: 8).
  636. *
  637. * `$this->Paginator->numbers(array('first' => 2, 'last' => 2));`
  638. *
  639. * Using the first and last options you can create links to the beginning and end of the page set.
  640. *
  641. * ### Options
  642. *
  643. * - `before` Content to be inserted before the numbers
  644. * - `after` Content to be inserted after the numbers
  645. * - `model` Model to create numbers for, defaults to PaginatorHelper::defaultModel()
  646. * - `modulus` how many numbers to include on either side of the current page, defaults to 8.
  647. * - `separator` Separator content defaults to ' | '
  648. * - `tag` The tag to wrap links in, defaults to 'span'
  649. * - `first` Whether you want first links generated, set to an integer to define the number of 'first'
  650. * links to generate.
  651. * - `last` Whether you want last links generated, set to an integer to define the number of 'last'
  652. * links to generate.
  653. * - `ellipsis` Ellipsis content, defaults to '...'
  654. * - `class` Class for wrapper tag
  655. * - `currentClass` Class for wrapper tag on current active page, defaults to 'current'
  656. * - `currentTag` Tag to use for current page number, defaults to null
  657. *
  658. * @param array|bool $options Options for the numbers, (before, after, model, modulus, separator)
  659. * @return string Numbers string.
  660. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::numbers
  661. */
  662. public function numbers($options = array()) {
  663. if ($options === true) {
  664. $options = array(
  665. 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
  666. );
  667. }
  668. $defaults = array(
  669. 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
  670. 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
  671. 'currentClass' => 'current', 'currentTag' => null
  672. );
  673. $options += $defaults;
  674. $params = (array)$this->params($options['model']) + array('page' => 1);
  675. unset($options['model']);
  676. if (empty($params['pageCount']) || $params['pageCount'] <= 1) {
  677. return '';
  678. }
  679. extract($options);
  680. unset($options['tag'], $options['before'], $options['after'], $options['model'],
  681. $options['modulus'], $options['separator'], $options['first'], $options['last'],
  682. $options['ellipsis'], $options['class'], $options['currentClass'], $options['currentTag']
  683. );
  684. $out = '';
  685. if ($modulus && $params['pageCount'] > $modulus) {
  686. $half = (int)($modulus / 2);
  687. $end = $params['page'] + $half;
  688. if ($end > $params['pageCount']) {
  689. $end = $params['pageCount'];
  690. }
  691. $start = $params['page'] - ($modulus - ($end - $params['page']));
  692. if ($start <= 1) {
  693. $start = 1;
  694. $end = $params['page'] + ($modulus - $params['page']) + 1;
  695. }
  696. if ($first && $start > 1) {
  697. $offset = ($start <= (int)$first) ? $start - 1 : $first;
  698. if ($offset < $start - 1) {
  699. $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  700. } else {
  701. $out .= $this->first($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('after' => $separator));
  702. }
  703. }
  704. $out .= $before;
  705. for ($i = $start; $i < $params['page']; $i++) {
  706. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
  707. }
  708. if ($class) {
  709. $currentClass .= ' ' . $class;
  710. }
  711. if ($currentTag) {
  712. $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $params['page']), array('class' => $currentClass));
  713. } else {
  714. $out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
  715. }
  716. if ($i != $params['pageCount']) {
  717. $out .= $separator;
  718. }
  719. $start = $params['page'] + 1;
  720. for ($i = $start; $i < $end; $i++) {
  721. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
  722. }
  723. if ($end != $params['page']) {
  724. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
  725. }
  726. $out .= $after;
  727. if ($last && $end < $params['pageCount']) {
  728. $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
  729. if ($offset <= $last && $params['pageCount'] - $end > $offset) {
  730. $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  731. } else {
  732. $out .= $this->last($offset, compact('tag', 'separator', 'class', 'ellipsis') + array('before' => $separator));
  733. }
  734. }
  735. } else {
  736. $out .= $before;
  737. for ($i = 1; $i <= $params['pageCount']; $i++) {
  738. if ($i == $params['page']) {
  739. if ($class) {
  740. $currentClass .= ' ' . $class;
  741. }
  742. if ($currentTag) {
  743. $out .= $this->Html->tag($tag, $this->Html->tag($currentTag, $i), array('class' => $currentClass));
  744. } else {
  745. $out .= $this->Html->tag($tag, $i, array('class' => $currentClass));
  746. }
  747. } else {
  748. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  749. }
  750. if ($i != $params['pageCount']) {
  751. $out .= $separator;
  752. }
  753. }
  754. $out .= $after;
  755. }
  756. return $out;
  757. }
  758. /**
  759. * Returns a first or set of numbers for the first pages.
  760. *
  761. * `echo $this->Paginator->first('< first');`
  762. *
  763. * Creates a single link for the first page. Will output nothing if you are on the first page.
  764. *
  765. * `echo $this->Paginator->first(3);`
  766. *
  767. * Will create links for the first 3 pages, once you get to the third or greater page. Prior to that
  768. * nothing will be output.
  769. *
  770. * ### Options:
  771. *
  772. * - `tag` The tag wrapping tag you want to use, defaults to 'span'
  773. * - `after` Content to insert after 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|int $first if string use as label for the link. If numeric, the number of page links
  779. * you want at the beginning of the range.
  780. * @param array $options An array of options.
  781. * @return string Numbers string.
  782. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::first
  783. */
  784. public function first($first = '<< first', $options = array()) {
  785. $options = (array)$options + array(
  786. 'tag' => 'span',
  787. 'after' => null,
  788. 'model' => $this->defaultModel(),
  789. 'separator' => ' | ',
  790. 'ellipsis' => '...',
  791. 'class' => null
  792. );
  793. $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
  794. unset($options['model']);
  795. if ($params['pageCount'] <= 1) {
  796. return '';
  797. }
  798. extract($options);
  799. unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
  800. $out = '';
  801. if (is_int($first) && $params['page'] >= $first) {
  802. if ($after === null) {
  803. $after = $ellipsis;
  804. }
  805. for ($i = 1; $i <= $first; $i++) {
  806. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  807. if ($i != $first) {
  808. $out .= $separator;
  809. }
  810. }
  811. $out .= $after;
  812. } elseif ($params['page'] > 1 && is_string($first)) {
  813. $options += array('rel' => 'first');
  814. $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
  815. }
  816. return $out;
  817. }
  818. /**
  819. * Returns a last or set of numbers for the last pages.
  820. *
  821. * `echo $this->Paginator->last('last >');`
  822. *
  823. * Creates a single link for the last page. Will output nothing if you are on the last page.
  824. *
  825. * `echo $this->Paginator->last(3);`
  826. *
  827. * Will create links for the last 3 pages. Once you enter the page range, no output will be created.
  828. *
  829. * ### Options:
  830. *
  831. * - `tag` The tag wrapping tag you want to use, defaults to 'span'
  832. * - `before` Content to insert before the link/tag
  833. * - `model` The model to use defaults to PaginatorHelper::defaultModel()
  834. * - `separator` Content between the generated links, defaults to ' | '
  835. * - `ellipsis` Content for ellipsis, defaults to '...'
  836. *
  837. * @param string|int $last if string use as label for the link, if numeric print page numbers
  838. * @param array $options Array of options
  839. * @return string Numbers string.
  840. * @link http://book.cakephp.org/2.0/en/core-libraries/helpers/paginator.html#PaginatorHelper::last
  841. */
  842. public function last($last = 'last >>', $options = array()) {
  843. $options = (array)$options + array(
  844. 'tag' => 'span',
  845. 'before' => null,
  846. 'model' => $this->defaultModel(),
  847. 'separator' => ' | ',
  848. 'ellipsis' => '...',
  849. 'class' => null
  850. );
  851. $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
  852. unset($options['model']);
  853. if ($params['pageCount'] <= 1) {
  854. return '';
  855. }
  856. extract($options);
  857. unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
  858. $out = '';
  859. $lower = $params['pageCount'] - $last + 1;
  860. if (is_int($last) && $params['page'] <= $lower) {
  861. if ($before === null) {
  862. $before = $ellipsis;
  863. }
  864. for ($i = $lower; $i <= $params['pageCount']; $i++) {
  865. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  866. if ($i != $params['pageCount']) {
  867. $out .= $separator;
  868. }
  869. }
  870. $out = $before . $out;
  871. } elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
  872. $options += array('rel' => 'last');
  873. $out = $before . $this->Html->tag(
  874. $tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
  875. );
  876. }
  877. return $out;
  878. }
  879. /**
  880. * Returns the meta-links for a paginated result set.
  881. *
  882. * `echo $this->Paginator->meta();`
  883. *
  884. * Echos the links directly, will output nothing if there is neither a previous nor next page.
  885. *
  886. * `$this->Paginator->meta(array('block' => true));`
  887. *
  888. * Will append the output of the meta function to the named block - if true is passed the "meta"
  889. * block is used.
  890. *
  891. * ### Options:
  892. *
  893. * - `model` The model to use defaults to PaginatorHelper::defaultModel()
  894. * - `block` The block name to append the output to, or false/absenst to return as a string
  895. *
  896. * @param array $options Array of options
  897. * @return string|void Meta links
  898. */
  899. public function meta($options = array()) {
  900. $model = isset($options['model']) ? $options['model'] : null;
  901. $params = $this->params($model);
  902. $links = array();
  903. if ($this->hasPrev()) {
  904. $links[] = $this->Html->meta(array(
  905. 'rel' => 'prev',
  906. 'link' => $this->url(array('page' => $params['page'] - 1), true)
  907. ));
  908. }
  909. if ($this->hasNext()) {
  910. $links[] = $this->Html->meta(array(
  911. 'rel' => 'next',
  912. 'link' => $this->url(array('page' => $params['page'] + 1), true)
  913. ));
  914. }
  915. $out = implode($links);
  916. if (empty($options['block'])) {
  917. return $out;
  918. }
  919. if ($options['block'] === true) {
  920. $options['block'] = __FUNCTION__;
  921. }
  922. $this->_View->append($options['block'], $out);
  923. }
  924. }