<?php
##########################################
# ~ Разработка отдельных модулей ~~~~~~~ #
# ~ и написание скриптов на php (c)KoT ~ #
# ~ ICQ: 7675072 ~~~~~~~~~~~~~~~~~~~~~~~ #
# ~ Site: 4atlove.ru ~~~~~~~~~~~~~~~~~~~ #
# ~ E-mail: notefree@bk.ru ~~~~~~~~~~~~~ #
##########################################

class navigator {
	public $all = 0;
	public $page;
	public $start = 0;
	public $end = 0;
	public $limit = 'LIMIT 0';

	public $htmlForm =  '<form action="%s" method="post"><div>К странице: <input type="text" name="page" size="3"  value="%s"><input type="submit" name="" value="&gt;&gt;"></div></form>';

	public $buttonBack = '&laquo;<a href="%s&amp;page=%s">Назад</a>';

	public $buttonBackName = '&laquo;Назад';

	public $buttonForward = '<a href="%s&amp;page=%s">Вперед</a>&raquo;<br />';

	public $buttonForwardName = 'Вперед&raquo;<br />';

	public $separator = ' | ';

	public $listStr = 'Стр: %s <a href="%s&amp;page=%s" title="Вперед">&raquo;</a><br />';

	public $blocAllNavi = '%1$s  %2$s %3$s';

    function __construct($all, $pnumber, $skript = '')
    {
    	$this->all = $all;
        $this->skript = $skript == '' ? $_SERVER['SCRIPT_NAME'].'?' : rtrim($skript);
        $this->page = isset($_REQUEST['page']) && (int)$_REQUEST['page'] ? (int)$_REQUEST['page'] : 1;
        $this->num_pages = ceil($all / $pnumber);
        if(isset($_GET['last'])) $this->page = $this->num_pages;
        if($this->page > $this->num_pages || $this->page < 1) $this->page = 1;
        if($all){
	        $this->start = $this->page * $pnumber - $pnumber;
	        $this->end = ($end = $this->start + $pnumber) > $all ? $all : $end;
	        $this->limit = sprintf('LIMIT %s,%s', $this->start, $pnumber);
		}

        $this->pnumber = $pnumber;
    }
	function form(){
		return ($this->num_pages < 2) ? '' : sprintf($this->htmlForm, $this->skript, $this->page);
	}
    function button()
    {
    	$back = $this->page > 1 ? sprintf($this->buttonBack, $this->skript, $this->page-1) : $this->buttonBackName;
    	$forward = $this->page != $this->num_pages ? sprintf($this->buttonForward, $this->skript, $this->page+1) : $this->buttonForwardName;
    	return ($this->num_pages < 2) ? '' : $back.$this->separator.$forward;
    }
    function str()
    {
    	$buff = '';
        for($pr = '', $i = 1; $i <= $this->num_pages; $i++) {
            $buff .= $pr = (($i == 1 || $i == $this->num_pages || abs($i - $this->page) < 3) ? ($i == $this->page ? '['.$i.']' : sprintf(' <a href="%spage=%s">%2$s</a> ', $this->skript, $i)) : (($pr == ' ... ' || $pr == '') ? '' : ' ... '));
        }
        return ($this->num_pages < 2) ? '' :  sprintf($this->listStr, $buff, $this->skript, ($this->page != $this->num_pages ? $this->page+1 : $this->page) );
    }
    function navi($str = true, $button = false, $form = false)
    {
		$str = $str ? $this->str() : '';
    	$button = $button ? $this->button() : '';
    	$form = $form ? $this->form() : '';
        return sprintf($this->blocAllNavi, $button, $str, $form);
    }
}

?>