PageRenderTime 47ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Plugin/Admin/View/Helper/BootstrapPaginatorHelper.php

https://bitbucket.org/reyx/p0001festivalcachorroquente
PHP | 254 lines | 205 code | 5 blank | 44 comment | 51 complexity | 2e53171445c1c42b9fb696afe6896913 MD5 | raw file
  1. <?php
  2. /**
  3. * Bootstrap Paginator Helper
  4. *
  5. * PHP 5
  6. *
  7. * Licensed under The MIT License
  8. * Redistributions of files must retain the below copyright notice.
  9. *
  10. * @author Yusuf Abdulla Shunan <shunan@maldicore.com>
  11. * @copyright Copyright 2012, Maldicore Group Pvt Ltd. (http://maldicore.com)
  12. * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
  13. * @since CakePHP(tm) v 2.1.1
  14. */
  15. App::uses('PaginatorHelper', 'View/Helper');
  16. class BootstrapPaginatorHelper extends PaginatorHelper
  17. {
  18. /**
  19. * Paging Link
  20. *
  21. * @param $which string
  22. * @param $title string
  23. * @param $options array
  24. * @param $disabledTitle string
  25. * @param $disabledOptions array
  26. * @return string
  27. */
  28. protected function _pagingLink($which, $title = null, $options = array(), $disabledTitle = null, $disabledOptions = array())
  29. {
  30. $check = 'has' . $which;
  31. $_defaults = array(
  32. 'url' => array(),
  33. 'step' => 1,
  34. 'escape' => false,
  35. 'model' => null,
  36. 'tag' => 'li',
  37. 'class' => null
  38. );
  39. $options = array_merge($_defaults, (array)$options);
  40. $paging = $this->params($options['model']);
  41. if (empty($disabledOptions)) {
  42. $disabledOptions = $options;
  43. }
  44. if (!$this->{$check}($options['model']) && (!empty($disabledTitle) || !empty($disabledOptions))) {
  45. if (!empty($disabledTitle) && $disabledTitle !== true) {
  46. $title = $disabledTitle;
  47. }
  48. $options = array_merge($_defaults, (array)$disabledOptions);
  49. } elseif (!$this->{$check}($options['model'])) {
  50. return null;
  51. }
  52. foreach (array_keys($_defaults) as $key) {
  53. ${$key} = $options[$key];
  54. unset($options[$key]);
  55. }
  56. $url = array_merge(array('page' => $paging['page'] + ($which == 'Prev' ? $step * -1 : $step)), $url);
  57. $title = ($which == 'Prev') ? '&lsaquo;' : '&rsaquo;';
  58. unset($options['rel']);
  59. return $this->Html->tag($tag, $this->link($title, $url, array_merge($options, compact('escape'))), compact('class'));
  60. }
  61. /**
  62. * First
  63. *
  64. * @param $first string
  65. * @param $options array
  66. * @return string
  67. */
  68. public function first($first = '&laquo;', $options = array())
  69. {
  70. $defaults = array(
  71. 'tag' => 'li',
  72. 'after' => null,
  73. 'model' => $this->defaultModel(),
  74. 'separator' => null,
  75. 'ellipsis' => '...',
  76. 'class' => null,
  77. 'escape' => false
  78. );
  79. $options += $defaults;
  80. $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
  81. unset($options['model']);
  82. if ($params['pageCount'] <= 1) {
  83. return false;
  84. }
  85. extract($options);
  86. unset($options['tag'], $options['after'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
  87. $out = '';
  88. if (is_int($first) && $params['page'] >= $first) {
  89. if ($after === null) {
  90. $after = $ellipsis;
  91. }
  92. for ($i = 1; $i <= $first; $i++) {
  93. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  94. if ($i != $first) {
  95. $out .= $separator;
  96. }
  97. }
  98. $out .= $after;
  99. } elseif ($params['page'] > 1 && is_string($first)) {
  100. $out = $this->Html->tag($tag, $this->link($first, array('page' => 1), $options), compact('class')) . $after;
  101. }
  102. return $out;
  103. }
  104. /**
  105. * Last
  106. *
  107. * @param $last string
  108. * @param $options array
  109. * @return string
  110. */
  111. public function last($last = '&raquo;', $options = array())
  112. {
  113. $options = array_merge(
  114. array(
  115. 'tag' => 'li',
  116. 'before' => null,
  117. 'model' => $this->defaultModel(),
  118. 'separator' => ' | ',
  119. 'ellipsis' => '...',
  120. 'class' => null,
  121. 'escape' => false
  122. ),
  123. (array)$options);
  124. $params = array_merge(array('page' => 1), (array)$this->params($options['model']));
  125. unset($options['model']);
  126. if ($params['pageCount'] <= 1) {
  127. return false;
  128. }
  129. extract($options);
  130. unset($options['tag'], $options['before'], $options['model'], $options['separator'], $options['ellipsis'], $options['class']);
  131. $out = '';
  132. $lower = $params['pageCount'] - $last + 1;
  133. if (is_int($last) && $params['page'] <= $lower) {
  134. if ($before === null) {
  135. $before = $ellipsis;
  136. }
  137. for ($i = $lower; $i <= $params['pageCount']; $i++) {
  138. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  139. if ($i != $params['pageCount']) {
  140. $out .= $separator;
  141. }
  142. }
  143. $out = $before . $out;
  144. } elseif ($params['page'] < $params['pageCount'] && is_string($last)) {
  145. $options += array('rel' => 'last');
  146. $out = $before . $this->Html->tag(
  147. $tag, $this->link($last, array('page' => $params['pageCount']), $options), compact('class')
  148. );
  149. }
  150. return $out;
  151. }
  152. /**
  153. * Numbers
  154. *
  155. * @param $options array
  156. * @return string
  157. */
  158. public function numbers($options = array())
  159. {
  160. $defaults = array(
  161. 'tag' => 'li',
  162. 'before' => null,
  163. 'after' => null,
  164. 'model' => $this->defaultModel(),
  165. 'class' => null,
  166. 'modulus' => '8',
  167. 'separator' => null,
  168. 'first' => null,
  169. 'last' => null,
  170. 'ellipsis' => '...',
  171. );
  172. $options += $defaults;
  173. $params = (array) $this->params($options['model']) + array('page' => 1);
  174. unset($options['model']);
  175. if ($params['pageCount'] <= 1) {
  176. return false;
  177. }
  178. extract($options);
  179. unset($options['tag'], $options['before'], $options['after'], $options['model'],
  180. $options['modulus'], $options['separator'], $options['first'], $options['last'],
  181. $options['ellipsis'], $options['class']
  182. );
  183. $out = '';
  184. if ($modulus && $params['pageCount'] > $modulus) {
  185. $half = intval($modulus / 2);
  186. $end = $params['page'] + $half;
  187. if ($end > $params['pageCount']) {
  188. $end = $params['pageCount'];
  189. }
  190. $start = $params['page'] - ($modulus - ($end - $params['page']));
  191. if ($start <= 1) {
  192. $start = 1;
  193. $end = $params['page'] + ($modulus - $params['page']) + 1;
  194. }
  195. if ($first && $start > 1) {
  196. $offset = ($start <= (int)$first) ? $start - 1 : $first;
  197. if ($offset < $start - 1) {
  198. $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  199. } else {
  200. $out .= $this->first($offset, compact('tag', 'separator', 'class') + array('after' => $separator));
  201. }
  202. }
  203. $out .= $before;
  204. for ($i = $start; $i < $params['page']; $i++) {
  205. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'))
  206. . $separator;
  207. }
  208. $currentClass = 'active';
  209. if ($class) {
  210. $currentClass .= ' ' . $class;
  211. }
  212. //$out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
  213. $out .= $this->Html->tag($tag, $this->link($params['page']), array('class' => $currentClass));
  214. if ($i != $params['pageCount']) {
  215. $out .= $separator;
  216. }
  217. $start = $params['page'] + 1;
  218. for ($i = $start; $i < $end; $i++) {
  219. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class')) . $separator;
  220. }
  221. if ($end != $params['page']) {
  222. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
  223. }
  224. $out .= $after;
  225. if ($last && $end < $params['pageCount']) {
  226. $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
  227. if ($offset <= $last && $params['pageCount'] - $end > $offset) {
  228. $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  229. } else {
  230. $out .= $this->last($offset, compact('tag', 'separator', 'class') + array('before' => $separator));
  231. }
  232. }
  233. } else {
  234. $out .= $before;
  235. for ($i = 1; $i <= $params['pageCount']; $i++) {
  236. $cls = empty($class) ? null : $class;
  237. if ($i == $params['page']) {
  238. $cls .= ' active';
  239. }
  240. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), array('class' => $cls));
  241. if ($i != $params['pageCount']) {
  242. $out .= $separator;
  243. }
  244. }
  245. $out .= $after;
  246. }
  247. return $out;
  248. }
  249. }