Jeff PHP framework  0.99
Modular, extensible, OOP, MVC, lightweight php framework designed to ease the programmers in the development of web applications.
pagination.class.php
Go to the documentation of this file.
00001 <?php
00021 class pagination {
00022 
00026         private $_view;
00027 
00031         private $_range;
00032 
00036         private $_urlp;
00037 
00041         private $_actual;
00042         
00046         private $_last;
00047 
00051         private $_start;
00052 
00056         private $_end;
00057         
00061         private $_tot;
00062         
00066         private $_npages;
00067         
00080         function __construct($ifp, $tot, $opts=null) {
00081 
00082                 $this->_view = new view();
00083 
00084                 $this->_urlp = gOpt($opts, 'urlp', 'p');
00085                 $this->_npages = gOpt($opts, 'npages', 2);
00086                 $this->_permalink = gOpt($opts, 'permalink', true);
00087 
00088                 $this->_range = $ifp;
00089                 $this->_tot = $tot;
00090                 $this->_last = ceil($this->_tot/$this->_range);
00091                 
00092                 $this->_actual = $this->actual();
00093                 $this->_start = $this->start()+1;
00094                 $this->_end = ($this->_start + $this->_range - 1) > $this->_tot ? $this->_tot : $this->_start + $this->_range - 1;
00095                 
00096 
00097         }
00098         
00104         public function actual() {
00105         
00106                 $actual = isset($_REQUEST[$this->_urlp]) ? cleanVar($_REQUEST[$this->_urlp], 'int') : 1;
00107 
00108                 return $actual < 1 ? 1 : ($actual > $this->_last ? $this->_last : $actual);
00109         }
00110         
00116         public function start() {
00117 
00118                 $start = ($this->_actual - 1) * $this->_range;
00119                 
00120                 return $start;
00121         }
00122         
00128         public function total() {
00129 
00130                 return $this->_tot;
00131 
00132         }
00133         
00139         public function limit() {
00140 
00141                 return ($this->start() + $this->_range) > $this->_tot ? $this->_tot : $this->start() + $this->_range;
00142 
00143         }
00144         
00153         public function summary() {
00154 
00155                 if(!$this->_last) return null;
00156 
00157                 $this->_view->setTpl('pagination_summary');
00158                 $this->_view->assign('start', $this->_start);
00159                 $this->_view->assign('end', $this->_end);
00160                 $this->_view->assign('total', $this->_tot);
00161 
00162                 return $this->_view->render();
00163         }
00164 
00171         public function navigation() {
00172                 
00173                 if($this->_last == 1) return "";
00174                 
00175                 $clean_uri = $this->_permalink 
00176                         ? preg_replace("#".$this->_urlp."/[0-9]*/#", "", $_SERVER['REQUEST_URI'])
00177                         : preg_replace("#(&|\?)".$this->_urlp."=[0-9]*#", "", $_SERVER['REQUEST_URI']);
00178                 preg_match("#(.*?)(/)([^/]*)$#", $clean_uri, $matches);
00179 
00180                 if($this->_permalink) {
00181                         $base_link = $matches[1]."/".$this->_urlp."/";
00182                         $params = isset($matches[3]) ? $matches[3] : '';
00183                 }
00184                 else {
00185                         $base_link = $matches[1]."/";
00186                         $params = (isset($matches[3]) && $matches[3]) ? $matches[3]."&".$this->_urlp."=" : "?".$this->_urlp."=";
00187                 }
00188 
00189                 $pages = array();
00190                 for($i=1; $i<$this->_last+1; $i++) {
00191                         if($i == 1 || abs($this->_actual - $i) < ($this->_npages+1) || $i == $this->_last) {
00192                                 $page = array('number'=>$i);
00193                                 if($this->_actual != $i) 
00194                                         $page['link'] = $this->_permalink ? $base_link.$i."/".$params : $base_link.$params.$i;
00195                                 if($this->_actual == $i) $page['selected'] = true;
00196                                 $pages[] = $page;
00197                         }
00198                         elseif(abs($this->_actual - $i) == $this->_npages + 1) {
00199                                 $page = array('number'=>'GAP');
00200                                 $pages[] = $page;
00201                         }
00202                 }
00203 
00204                 $this->_view->setTpl('pagination_navigation');
00205                 $this->_view->assign('pages', $pages);
00206                 $prev_p = $this->_actual == 1 
00207                         ? null 
00208                         : anchor(($this->_permalink ? $base_link.($this->_actual-1)."/".$params : $base_link.$params.($this->_actual-1)), "<--");
00209                 $next_p = $this->_actual == $this->_last 
00210                         ? null : 
00211                         anchor(($this->_permalink ? $base_link.($this->_actual+1)."/".$params : $base_link.$params.($this->_actual+1)), "-->");
00212                 $this->_view->assign('prev', $prev_p);
00213                 $this->_view->assign('next', $next_p);
00214 
00215                 return $this->_view->render();
00216 
00217         }
00218 
00219 }
00220 
00221 ?>