PageRenderTime 87ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/external/pagination.php

https://github.com/uwiuw/myphpclass
PHP | 255 lines | 182 code | 37 blank | 36 comment | 48 complexity | 1758236c71abcb48df021fc21d54efb7 MD5 | raw file
  1. <?php
  2. class Pagination{
  3. /*
  4. Script Name: *Digg Style Paginator Class
  5. Script URI: http://www.mis-algoritmos.com/2007/05/27/digg-style-pagination-class/
  6. Description: Class in PHP that allows to use a pagination like a digg or sabrosus style.
  7. Script Version: 0.4
  8. Author: Victor De la Rocha
  9. Author URI: http://www.mis-algoritmos.com
  10. */
  11. /*Default values*/
  12. var $total_pages = -1;//items
  13. var $limit = null;
  14. var $target = "";
  15. var $page = 1;
  16. var $adjacents = 2;
  17. var $showCounter = false;
  18. var $showNumber = true;
  19. var $className = "pagination";
  20. var $classPreviousName = 'prev';
  21. var $classNextName = 'next';
  22. var $classCurrentName = 'current';
  23. var $parameterName = "paged";
  24. var $urlF = false;//urlFriendly
  25. /*Buttons next and previous*/
  26. var $nextT = "Next";
  27. var $nextI = "&#187;"; //&#9658;
  28. var $prevT = "Previous";
  29. var $prevI = "&#171;"; //&#9668;
  30. var $calculate = false;
  31. #Total items
  32. function items($value){$this->total_pages = (int) $value;}
  33. #how many items to show per page
  34. function limit($value){$this->limit = (int) $value;}
  35. #Page to sent the page value
  36. function target($value){$this->target = $value;}
  37. #Current page
  38. function currentPage($value){$this->page = (int) $value;}
  39. #How many adjacent pages should be shown on each side of the current page?
  40. function adjacents($value){$this->adjacents = (int) $value;}
  41. #show counter?
  42. function showCounter($value=""){$this->showCounter=($value===true)?true:false;}
  43. #show pages num?
  44. function showNumber($value=""){$this->showNumber=($value===true)?true:false;}
  45. #to change the class name of the pagination div
  46. function changeClass($value=""){$this->className=$value;}
  47. #to change the class name of the previous link
  48. function changePreviousClass($value=""){$this->classPreviousName=$value;}
  49. #to change the class name of the next link
  50. function changeNextClass($value=""){$this->classNextName=$value;}
  51. #to change the class name of the current link
  52. function changeCurrentClass($value=""){$this->classCurrentName=$value;}
  53. function nextLabel($value){$this->nextT = $value;}
  54. function nextIcon($value){$this->nextI = $value;}
  55. function prevLabel($value){$this->prevT = $value;}
  56. function prevIcon($value){$this->prevI = $value;}
  57. #to change the class name of the pagination div
  58. function parameterName($value=""){$this->parameterName=$value;}
  59. #to change urlFriendly
  60. function urlFriendly($value="%"){
  61. if(preg_match('/^ *$/',$value)){
  62. $this->urlF=false;
  63. return false;
  64. }
  65. $this->urlF=$value;
  66. }
  67. var $pagination;
  68. function Pagination(){}
  69. function show(){
  70. if(!$this->calculate) {
  71. if($this->calculate()) {
  72. echo "<div class=\"$this->className\">$this->pagination</div>\n";
  73. }
  74. }
  75. }
  76. function getOutput(){
  77. if(!$this->calculate) {
  78. if($this->calculate()) {
  79. return "<div class=\"$this->className\">$this->pagination</div>\n";
  80. }
  81. }
  82. }
  83. function get_pagenum_link($id){
  84. if(strpos($this->target,'?')===false) {
  85. if($this->urlF) {
  86. return str_replace($this->urlF,$id,$this->target);
  87. } else {
  88. return "$this->target?$this->parameterName=$id";
  89. }
  90. } else {
  91. return "$this->target&$this->parameterName=$id";
  92. }
  93. }
  94. function calculate(){
  95. $this->pagination = "";
  96. $this->calculate == true;
  97. $error = false;
  98. if($this->urlF and $this->urlF != '%' and strpos($this->target,$this->urlF)===false){
  99. //Es necesario especificar el comodin para sustituir
  100. echo "Especificaste un wildcard para sustituir, pero no existe en el target<br />";
  101. $error = true;
  102. }elseif($this->urlF and $this->urlF == '%' and strpos($this->target,$this->urlF)===false){
  103. echo "Es necesario especificar en el target el comodin % para sustituir el n�mero de p�gina<br />";
  104. $error = true;
  105. }
  106. if($this->total_pages < 0){
  107. echo "It is necessary to specify the <strong>number of pages</strong> (\$class->items(1000))<br />";
  108. $error = true;
  109. }
  110. if($this->limit == null){
  111. echo "It is necessary to specify the <strong>limit of items</strong> to show per page (\$class->limit(10))<br />";
  112. $error = true;
  113. }
  114. if($error)return false;
  115. $n = trim($this->nextT.' '.$this->nextI);
  116. $p = trim($this->prevI.' '.$this->prevT);
  117. /* Setup vars for query. */
  118. if($this->page)
  119. $start = ($this->page - 1) * $this->limit; //first item to display on this page
  120. else
  121. $start = 0; //if no page var is given, set start to 0
  122. /* Setup page vars for display. */
  123. $prev = $this->page - 1; //previous page is page - 1
  124. $next = $this->page + 1; //next page is page + 1
  125. $lastpage = ceil($this->total_pages/$this->limit); //lastpage is = total pages / items per page, rounded up.
  126. $lpm1 = $lastpage - 1; //last page minus 1
  127. /*
  128. Now we apply our rules and draw the pagination object.
  129. We're actually saving the code to a variable in case we want to draw it more than once.
  130. */
  131. if($lastpage > 1){
  132. if($this->page){
  133. //anterior button
  134. if($this->page > 1) {
  135. $this->pagination .= "<a href=\"".$this->get_pagenum_link($prev)."\" class=\"".$this->classPreviousName."\">$p</a>";
  136. } else {
  137. $this->pagination .= "<span class=\"".$this->classPreviousName."\">$p</span>";
  138. }
  139. }
  140. //pages
  141. if ($lastpage < 7 + ($this->adjacents * 2)){//not enough pages to bother breaking it up
  142. for ($counter = 1; $counter <= $lastpage; $counter++){
  143. if ($this->showNumber) {
  144. if ($counter == $this->page)
  145. $this->pagination .= "<span class=\"".$this->classCurrentName."\">$counter</span>";
  146. else
  147. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  148. }
  149. }
  150. }
  151. elseif($lastpage > 5 + ($this->adjacents * 2)){//enough pages to hide some
  152. //close to beginning; only hide later pages
  153. if($this->page < 1 + ($this->adjacents * 2)){
  154. for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++){
  155. if ($this->showNumber) {
  156. if ($counter == $this->page)
  157. $this->pagination .= "<span class=\"current\">$counter</span>";
  158. else
  159. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  160. }
  161. }
  162. if ($this->showNumber) {
  163. $this->pagination .= "...";
  164. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
  165. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
  166. }
  167. }
  168. //in middle; hide some front and some back
  169. elseif($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)){
  170. if ($this->showNumber) {
  171. $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
  172. $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
  173. $this->pagination .= "...";
  174. }
  175. for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++) {
  176. if ($this->showNumber) {
  177. if ($counter == $this->page)
  178. $this->pagination .= "<span class=\"".$this->classCurrentName."\">$counter</span>";
  179. else
  180. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  181. }
  182. }
  183. if ($this->showNumber) {
  184. $this->pagination .= "...";
  185. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
  186. $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
  187. }
  188. }
  189. //close to end; only hide early pages
  190. else{
  191. if ($this->showNumber) {
  192. $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
  193. $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
  194. $this->pagination .= "...";
  195. }
  196. for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++) {
  197. if ($this->showNumber) {
  198. if ($counter == $this->page)
  199. $this->pagination .= "<span class=\"".$this->classCurrentName."\">$counter</span>";
  200. else
  201. $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
  202. }
  203. }
  204. }
  205. }
  206. if($this->page){
  207. //siguiente button
  208. if ($this->page < $counter - 1)
  209. $this->pagination .= "<a href=\"".$this->get_pagenum_link($next)."\" class=\"".$this->classNextName."\">$n</a>";
  210. else
  211. $this->pagination .= "<span class=\"".$this->classNextName."\">$n</span>";
  212. if($this->showCounter)$this->pagination .= "<div class=\"pagination_data\">($this->total_pages Pages)</div>";
  213. }
  214. }
  215. return true;
  216. }
  217. }
  218. ?>