class.pagenav.php 3.43 KB
<?php
/**
* @author:  Bunzia Alexander <nifus@mail.ru> <http://www.weblancer.net/users/nifus/>
* @copyright: Copyright (c) 2010, Bunzia Alexander
* @version: 1.0
* @license: http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @project: HiLo
*/




class page_nav {

  public $short = true; // укорачивать пагинацию
  
  
	private $count_rows;
	private $url_tmpl;
	private $page;
	private $limit;
  private $tmpl = 'admin_page_nav';
  
  
  
  public function set_tmpl($v){
		$this -> tmpl   = $v;	
	}
	
	public function __construct($count){
		$this -> count_rows   = intval($count);	
	}
	public function set_limit($limit){
		$this -> limit   = intval($limit);	
	}

	public function set_url_tmpl($tmpl){
		$this -> url_tmpl   = $tmpl;	
	}
	public function get($page){
	
		$page = !empty($page) ? intval($page) : 1;
		if ( $this->count_rows < $this->limit ) {
			return false;
		}	
		$total_pages = ceil($this->count_rows / $this->limit);
			//	если страниц больше 1
		if ( $total_pages <= 1 ) {
			return false;
		}
		$t = new PHPTAL();
		$t -> setSnippet('admin',$this -> tmpl);
		
		$ret  = '';
		$prev = $page - 1;
		$rows = array();
		if ($prev >= 1) {
		  $t -> prev = sprintf($this->url_tmpl,$prev);
		}else{
		  $t -> prev = false;
		}
		
		$counter  = 1;
		$current_page = $page ;
		//	перебираем страницы
		while ($counter <= $total_pages) 
		{
				  //  всего 10 страниц
				  //  стоим на 5 странице
				  //  1,...3,4,[5],6,7..10
			if ($counter == $current_page) 
			{
			    //	если текущая страницы, то ссылки нет
			  $rows[]=array('counter'=>$counter,'url'=> false,'now'=> true,'page'=>false);
			} 
			elseif (   ( (false===$this -> short) || ($counter > $current_page-3 && $counter < $current_page + 3) ) || $counter == 1 || $counter == $total_pages ) 
			{
			    //  если счётчик от текущей страницы ушёл больше чем на 3 позиции и счётчик  
			    //  если счётчик стоит на первой странице
			    //  если счётчик стоит на последней странице
				if ( (true===$this -> short) && ($counter == $total_pages) && ($current_page < $total_pages - 3) ) 
				{
				   //   если счётчик на последней странице
				   //   если текущая странице до конца больше 3-х страниц
				   // то пропускаем цифру  
				  $rows[]=array('counter'=>'...','url'=> false,'now'=> false,'page'=>false);
				}
				
				$rows[]=array('counter'=>$counter,'url'=> sprintf($this->url_tmpl,$counter),'page'=>$counter);
				  
				if ( (true===$this -> short) && ($counter == 1) && ($current_page > 4 ) ) 
				{
				  //  если счётчик в начале
				  //  если текущая страница ушла от начала дальше чем на 4 позици
				  //  то пропускаем
					$rows[]=array('counter'=>'...','url'=> false,'now'=> false,'page'=>false);
				}
			}
			$counter++;
		}
		$next = ($current_page+1);
		if ($next <= $total_pages) {
		  $t -> next = sprintf($this->url_tmpl,$next);
		}else{
		  $t -> next = false;
		}
		
		$t -> rows = $rows;
		$t -> all = $total_pages;
		$t -> current_page = $current_page;
		$t -> first =  sprintf($this->url_tmpl,1);
		$t -> last = sprintf($this->url_tmpl,$total_pages);
		
		return $t -> execute();
	}
	
	



} 

?>