<?php
defined('_CONSTANT_') or die('Error. You don`t have permision to access.');

class Navigator
{

function __construct ($all, $pnumber, $query = '') 
{
    $this->all = $all;
    $this->pnumber = $pnumber;
    $this->query = $query; 
    $this->p = isset($_GET['p']) ? (int)$_GET['p'] : 1; 
    if (isset($_POST['p'])) 
    {
        $this->p = (int)$_POST['p'];
    }
}

function start() 
{
    $this->num_ps = ceil($this->all / $this->pnumber);
    if (isset($_GET['last'])) 
    {
        $this->p = $this->num_ps;
    } 
    $this->start = $this->p * $this->pnumber - $this->pnumber;
    if ($this->p > $this->num_ps || $this->p < 1) 
    {
        $this->p = 1;
        $this->start = 0;
    } 
    return $this->start;
}

function navi($uri = null) 
{
    global $Filter;

    if ($uri == null) 
    {
        $uri = $_SERVER['SCRIPT_NAME'];
    } 
    else 
    {
        $uri = $uri;
    }

    if ($this->num_ps < 2) 
    {
        return '';
    }

    $buff = '<div class="block">';

    if ($this->p > 1) 
    {
        $n = $this->p;
        $n--;
        $buff .= '<a href="'.$uri.'?p='.$n.'&amp;'.$this->query.'" class="link-touch">&laquo; Назад</a>';
    }

    if (($this->p > 1) && ($this->p != $this->num_ps)) 
    {
        $buff .= '';
    }

    if ($this->p != $this->num_ps) 
    {
        $p = $this->p; 
        $p++;
        $buff .= '<a href="'.$Filter->output($uri).'?p='.$p.'&amp;'.$this->query.'" class="link-touch">Далее &raquo;</a>';
    }

    $buff .= '';

    for ($pr = '', $i = 1; $i <= $this->num_ps; $i++) 
    {
        $buff .= $pr = (($i == 1 || $i == $this->num_ps || abs($i-$this->p) < 5) ? ($i == $this->p ? "  <span class='nav'>$i</span> " : ' <a href="'.$uri.'?p='.$i.'&amp;'.$this->query.'" class="nav">'.$i.'</a> ') : (($pr == ' .. ' || $pr == '')? '' : ' .. '));
    } 

    if ($this->num_ps > 9) 
    {
        $buff = '<form action="'.$uri.'?'.$this->query.'" method="POST">
        <input type="text" size="3" name="p" value="'.$this->p.'">
        <input type="submit" name="" value="&gt;&gt;"><br></form>';
    }

    $buff .= '';
    
    return $buff;
}
}
?>