<?php
###############################################################
# Adrian Staniszewski
# adrian_s@toya.net.pl
# GG: 5513236
#
#    File:          Class.Pager.php5
#    Version:       1.3v.+
#    Modif:         16.10.2006r. 16:30
#    Description:   Pager for www
#
##############################################################
/*
Pager - funkcja tworzaca nowy pager (tworzona jest nowa tablica dla Pagera)
getPages - funkcja oblicza ilosc stron Pagera
setPages - funkcja zmieniajaca aktualna podstrone
buildPager - funkcja generujaca podstrony oraz linki do nich
createLink - funkcja tworzaca link z podanego szablonu
Render - funkcja
startRecord
setVar
getVar
buildLink
setFastLink
setHTML_Link

*/

class Pager {

  var $p_name;
  var $p = array();
  var $links = array();
  
   function Pager($p_name='pager', $p_count='1', $p_per='250', $p_link='20', $p_sep=' ') {
    $this -> p_name = $p_name;
    $this -> setVar('count', $p_count);
    $this -> setVar('per', $p_per);
    $this -> setVar('html_link', '<a href="index.php?site={^link}" title="{^title}" style="text-decoration: none;">{^name}</a>');
    $this -> setVar('sep', $p_sep);
    $this -> setVar('link', $p_link);
    $this -> setVar('site', '1');
    $this -> setVar('fast_link', '5');
  }

  function getPages() {
    $this -> setVar('pages', ceil(($this -> getVar('count')/$this -> getVar('per'))));
  }
  
  function setPages($page) {
    $this -> setVar('site', $page);
  }
  
  function buildPager() {
  
    
    ($this -> getVar('fast_link') >= '5' && $this -> getVar('pages') > '51' && $this -> getVar('site') > '50')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 50), 'Wstecz o 50 stron', ''):'';
    ($this -> getVar('fast_link') >= '4' && $this -> getVar('pages') > '31' && $this -> getVar('site') > '30')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 30), 'Wstecz o 30 stron', ''):'';
    ($this -> getVar('fast_link') >= '3' && $this -> getVar('pages') > '11' && $this -> getVar('site') > '10')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 10), 'Wstecz o 10 stron', ''):'';
    ($this -> getVar('fast_link') >= '2' && $this -> getVar('pages') > '6' && $this -> getVar('site') > '5')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 5), 'Wstecz o 5 stron', '&laquo;&laquo;'):'';
    ($this -> getVar('fast_link') >= '1' && $this -> getVar('pages') > '1' && $this -> getVar('site') > '1')?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') - 1), 'Wstecz o strone', '&laquo; poprzednia'):'';
    
    $start = ($this -> getVar('site') - ceil($this -> getVar('link')/2));
    if($start <= '0' or $start == '') $start = '1';
    
    $koniec = ($this -> getVar('site') + ceil($this -> getVar('link')/2));
    if($koniec > $this -> getVar('pages')) $koniec = $this -> getVar('pages');
    
    for($i=$start; $i <= $koniec; $i++) {
         ($i == $this -> getVar('site'))?$name = '<b>'.$i.'</b>':$name=$i;
       $this -> links[$this -> p_name][] = $this -> createLink($i, $i, $name);
    }
    $this -> links[$this -> p_name][] = 'z <b>'.$this -> getVar('pages').'</b>';
    ($this -> getVar('fast_link') >= '1' && $this -> getVar('pages') > $this -> getVar('site'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 1), 'Naprzód o strone', 'następna &raquo;'):'';
    ($this -> getVar('fast_link') >= '2' && ($this -> getVar('site') + 5) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 5), 'Naprzód o 5 stron', '&raquo;&raquo;'):'';
    ($this -> getVar('fast_link') >= '3' && ($this -> getVar('site') + 10) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 10), 'Naprzód o 10 stron', ''):'';
    ($this -> getVar('fast_link') >= '4' && ($this -> getVar('site') + 30) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 30), 'Naprzód o 30 stron', ''):'';
    ($this -> getVar('fast_link') >= '5' && ($this -> getVar('site') + 50) <= $this -> getVar('pages'))?$this -> links[$this -> p_name][] = $this -> createLink(($this -> getVar('site') + 50), 'Naprzód o 50 stron', ''):'';
  }

  function createLink($link, $title, $name) {
    return preg_replace('/{\^(.*?)}/sie', '$$1', $this -> getVar('html_link'));
  }
  
  function Render() {
    $this -> getPages();
    $site = ($_GET['site']<>'' and is_numeric($_GET['site']))?''.$_GET['site'].'':'1';
    $this -> setPages($site);
    $this -> buildPager();
        
    foreach($this -> links[$this -> p_name] as $link) {
      $html_out .= ''.$link.''.$this -> getVar('sep').'';
    }
    $this -> p[$this -> p_name]['html_out'] = $html_out;
    return $html_out;
  }
  
  function startRecord() {
    return (($this -> getVar('site') - 1) * $this -> getVar('per'));
  }
  
  function getVar($get) {
    return $this -> p[$this -> p_name][$get];
  }
  
  function setVar($get, $var) {
    $this -> p[$this -> p_name][$get] = $var;
  }
  
  function buildLink($link = 'index.php5', $delete = array('site')) {
  
    foreach($_GET as $pole => $war) {
      $pole = trim($pole);
        if(!in_array($pole, $delete)) {
          $a++;
          ($a==1)?$zn='?':$zn='&';
          $link .= ''.$zn.''.$pole.'='.$war.'';
        }
    }
return $link;
}

  function setFastLink($leavel = '5') {
    if(is_numeric($leavel) && $leavel >= '0' && $leavel <= '5') {
      $this -> setVar('fast_link', $leavel);
    }
  }
  
 function setHTML_Link($html_link) {
    $this -> setVar('html_link', $html_link);
  }
  
}
?>
