123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- <?php
- if(!defined('entry') || !entry) die('Not a valid page');
-
- class pagination{
-
-
- var $total_pages = null;
- var $limit = null;
- var $target = "";
- var $page = 1;
- var $adjacents = 2;
- var $showCounter = false;
- var $className = "pagination";
- var $parameterName = "page";
- var $urlF = false;
-
-
- var $nextT = "Next";
- var $nextI = "»";
- var $prevT = "Previous";
- var $prevI = "«";
-
-
- var $calculate = false;
-
-
- function items($value){$this->total_pages = intval($value);}
-
-
- function limit($value){$this->limit = intval($value);}
-
-
- function target($value){$this->target = $value;}
-
-
- function currentPage($value){$this->page = intval($value);}
-
-
- function adjacents($value){$this->adjacents = intval($value);}
-
-
- function showCounter($value=""){$this->showCounter=($value===true)?true:false;}
-
-
- function changeClass($value=""){$this->className=$value;}
-
- function nextLabel($value){$this->nextT = $value;}
- function nextIcon($value){$this->nextI = $value;}
- function prevLabel($value){$this->prevT = $value;}
- function prevIcon($value){$this->prevI = $value;}
-
-
- function parameterName($value=""){$this->parameterName=$value;}
-
-
- function urlFriendly($value="%"){
- if(eregi('^ *$',$value)){
- $this->urlF=false;
- return false;
- }
- $this->urlF=$value;
- }
-
- var $pagination;
-
- function pagination(){}
- function show(){
- if(!$this->calculate)
- if($this->calculate())
- echo "<div class=\"$this->className\">$this->pagination</div>";
- }
- function getPagination(){
- if(!$this->calculate)
- if($this->calculate())
- return "<div class=\"$this->className\">$this->pagination</div>";
- }
- function get_pagenum_link($id){
-
- if($this->urlF)
- return str_replace($this->urlF,$id,$this->target);
-
- else
- return "$this->target?$this->parameterName=$id";
-
-
-
- }
-
- function calculate(){
- $this->pagination = "";
- $this->calculate == true;
- $error = false;
- if($this->urlF and $this->urlF != '%' and strpos($this->target,$this->urlF)===false){
-
- echo "Especificaste un wildcard para sustituir, pero no existe en el target<br />";
- $error = true;
- }elseif($this->urlF and $this->urlF == '%' and strpos($this->target,$this->urlF)===false){
- echo "Es necesario especificar en el target el comodin % para sustituir el n�mero de p�gina<br />";
- $error = true;
- }
- if($this->total_pages == null){
- echo "It is necessary to specify the <strong>number of pages</strong> (\$class->items(1000))<br />";
- $error = true;
- }
- if($this->limit == null){
- echo "It is necessary to specify the <strong>limit of items</strong> to show per page (\$class->limit(10))<br />";
- $error = true;
- }
- if($error)return false;
-
- $n = trim($this->nextT.' '.$this->nextI);
- $p = trim($this->prevI.' '.$this->prevT);
-
-
- if($this->page)
- $start = ($this->page - 1) * $this->limit;
- else
- $start = 0;
-
-
- if ($this->page == 0) $this->page = 1;
- $prev = $this->page - 1;
- $next = $this->page + 1;
- $lastpage = ceil($this->total_pages/$this->limit);
- $lpm1 = $lastpage - 1;
-
-
-
-
- if($lastpage > 1){
-
- if($this->page > 1)
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($prev)."\">$p</a>";
- else
- $this->pagination .= "<span class=\"disabled\">$p</span>";
-
- if ($lastpage < 7 + ($this->adjacents * 2)){
- for ($counter = 1; $counter <= $lastpage; $counter++){
- if ($counter == $this->page)
- $this->pagination .= "<span class=\"current\">$counter</span>";
- else
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
- }
- }
- elseif($lastpage > 5 + ($this->adjacents * 2)){
-
- if($this->page < 1 + ($this->adjacents * 2)){
- for ($counter = 1; $counter < 4 + ($this->adjacents * 2); $counter++){
- if ($counter == $this->page)
- $this->pagination .= "<span class=\"current\">$counter</span>";
- else
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
- }
- $this->pagination .= "...";
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
- }
-
- elseif($lastpage - ($this->adjacents * 2) > $this->page && $this->page > ($this->adjacents * 2)){
- $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
- $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
- $this->pagination .= "...";
- for ($counter = $this->page - $this->adjacents; $counter <= $this->page + $this->adjacents; $counter++)
- if ($counter == $this->page)
- $this->pagination .= "<span class=\"current\">$counter</span>";
- else
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
- $this->pagination .= "...";
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($lpm1)."\">$lpm1</a>";
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($lastpage)."\">$lastpage</a>";
- }
-
- else{
- $this->pagination .= "<a href=\"".$this->get_pagenum_link(1)."\">1</a>";
- $this->pagination .= "<a href=\"".$this->get_pagenum_link(2)."\">2</a>";
- $this->pagination .= "...";
- for ($counter = $lastpage - (2 + ($this->adjacents * 2)); $counter <= $lastpage; $counter++)
- if ($counter == $this->page)
- $this->pagination .= "<span class=\"current\">$counter</span>";
- else
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($counter)."\">$counter</a>";
- }
- }
-
- if ($this->page < $counter - 1)
- $this->pagination .= "<a href=\"".$this->get_pagenum_link($next)."\">$n</a>";
- else
- $this->pagination .= "<span class=\"disabled\">$n</span>";
- if($this->showCounter)$this->pagination .= "<div class=\"pagination_data\">($this->total_pages Pages)</div>";
- }
-
- return true;
- }
- }
- ?>
|