PageRenderTime 117ms CodeModel.GetById 30ms RepoModel.GetById 12ms app.codeStats 0ms

/View/Helper/BootstrapPaginatorHelper.php

https://github.com/webacteo/Platform-Twitter-Bootstrap
PHP | 158 lines | 112 code | 26 blank | 20 comment | 31 complexity | acc1f40deeccc5930b21312badb89897 MD5 | raw file
  1. <?php
  2. App::uses('PaginatorHelper', 'View/Helper');
  3. /**
  4. * Twitter Bootstrap Paginator Helper
  5. */
  6. class BootstrapPaginatorHelper extends PaginatorHelper {
  7. /**
  8. * Make table headers sortable
  9. *
  10. * @param string $key
  11. * @param mixed $title
  12. * @param array $options
  13. *
  14. * @return string
  15. */
  16. public function sortTableHeader($key, $title = null, $options = array()) {
  17. $content = parent::sort($key, $title, $options + array('block' => 'script'));
  18. $options = array_merge(array('url' => array(), 'model' => null), $options);
  19. $class = "";
  20. $sortKey = $this->sortKey($options['model']);
  21. $defaultModel = $this->defaultModel();
  22. $isSorted = (
  23. $sortKey === $key ||
  24. $sortKey === $defaultModel . '.' . $key ||
  25. $key === $defaultModel . '.' . $sortKey
  26. );
  27. if ($isSorted) {
  28. $dir = $this->sortDir($options['model']) === 'asc' ? 'desc' : 'asc';
  29. $class = ($dir === 'asc') ? 'headerSortDown' : 'headerSortUp';
  30. }
  31. return sprintf('<th class="blue header %s">%s</th>', $class, $content);
  32. }
  33. /**
  34. * Numbers paginator as we know it
  35. * @see parent "numbers" function for docblock
  36. * @changes $currentClass & and now active, is wrapped as a link aswell to support TwitterBootstrap
  37. *
  38. * @param mixed $options Options for the numbers, (before, after, model, modulus, separator)
  39. * @return string numbers string.
  40. */
  41. public function numbers($options = array()) {
  42. if ($options === true) {
  43. $options = array(
  44. 'before' => ' | ', 'after' => ' | ', 'first' => 'first', 'last' => 'last'
  45. );
  46. }
  47. $defaults = array(
  48. 'tag' => 'span', 'before' => null, 'after' => null, 'model' => $this->defaultModel(), 'class' => null,
  49. 'modulus' => '8', 'separator' => ' | ', 'first' => null, 'last' => null, 'ellipsis' => '...',
  50. );
  51. $options += $defaults;
  52. $params = (array)$this->params($options['model']) + array('page'=> 1);
  53. unset($options['model']);
  54. if ($params['pageCount'] <= 1) {
  55. return false;
  56. }
  57. extract($options);
  58. unset($options['tag'], $options['before'], $options['after'], $options['model'],
  59. $options['modulus'], $options['separator'], $options['first'], $options['last'],
  60. $options['ellipsis'], $options['class']
  61. );
  62. $out = '';
  63. if ($modulus && $params['pageCount'] > $modulus) {
  64. $half = intval($modulus / 2);
  65. $end = $params['page'] + $half;
  66. if ($end > $params['pageCount']) {
  67. $end = $params['pageCount'];
  68. }
  69. $start = $params['page'] - ($modulus - ($end - $params['page']));
  70. if ($start <= 1) {
  71. $start = 1;
  72. $end = $params['page'] + ($modulus - $params['page']) + 1;
  73. }
  74. if ($first && $start > 1) {
  75. $offset = ($start <= (int)$first) ? $start - 1 : $first;
  76. if ($offset < $start - 1) {
  77. $out .= $this->first($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  78. } else {
  79. $out .= $this->first($offset, compact('tag', 'separator', 'class') + array('after' => $separator));
  80. }
  81. }
  82. $out .= $before;
  83. for ($i = $start; $i < $params['page']; $i++) {
  84. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'))
  85. . $separator;
  86. }
  87. $currentClass = 'current';
  88. if ($class) {
  89. $currentClass .= ' ' . $class;
  90. }
  91. $out .= $this->Html->tag($tag, $params['page'], array('class' => $currentClass));
  92. if ($i != $params['pageCount']) {
  93. $out .= $separator;
  94. }
  95. $start = $params['page'] + 1;
  96. for ($i = $start; $i < $end; $i++) {
  97. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'))
  98. . $separator;
  99. }
  100. if ($end != $params['page']) {
  101. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $end), $options), compact('class'));
  102. }
  103. $out .= $after;
  104. if ($last && $end < $params['pageCount']) {
  105. $offset = ($params['pageCount'] < $end + (int)$last) ? $params['pageCount'] - $end : $last;
  106. if ($offset <= $last && $params['pageCount'] - $end > $offset) {
  107. $out .= $this->last($offset, compact('tag', 'separator', 'ellipsis', 'class'));
  108. } else {
  109. $out .= $this->last($offset, compact('tag', 'separator', 'class') + array('before' => $separator));
  110. }
  111. }
  112. } else {
  113. $out .= $before;
  114. for ($i = 1; $i <= $params['pageCount']; $i++) {
  115. if ($i == $params['page']) {
  116. $currentClass = 'active'; // Changed to support TwitterBootstrap
  117. if ($class) {
  118. $currentClass .= ' ' . $class;
  119. }
  120. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), array('class' => $currentClass)); // Changed to support TwitterBootstrap
  121. } else {
  122. $out .= $this->Html->tag($tag, $this->link($i, array('page' => $i), $options), compact('class'));
  123. }
  124. if ($i != $params['pageCount']) {
  125. $out .= $separator;
  126. }
  127. }
  128. $out .= $after;
  129. }
  130. return $out;
  131. }
  132. }