<?php
/*~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!
!		!!! Разработка сайтов и отдельных модулей на PHP !!!
!	* - Автор скрипата   :  lugaro
!	* - E-mail           :  lugaro@bk.ru
!	* - Сайт             :  wapNEW.ru
!	* - ICQ              :  2000882
~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!~!*/
class navigator {
	public $form;   //Показ формы
	public $rewrit; //С ревврейтом
	public $no_button; //Без кнопок вперед назад
    // Всего записей, сколько выводить на стр, путь к скрипту
    function __construct($all, $pnumber, $skript = '?')
    {
        $this->all = $all;
        $this->pnumber = $pnumber;
        $this->skript = $skript;
        $this->page = (int)$_REQUEST['page'] ? (int)$_REQUEST['page'] : 1;
    }

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

    function end()
    {
        $this->end = ($this->start() + $this->pnumber);
        $this->end = $this->end > $this->all ? $this->all : $this->end;
        return $this->end;
    }

    function navi()
    {
        if ($this->num_pages < 2) return '';
        $buff = '';
        if (!$this->no_button) {
            if ($this->page > 1) {
                $n = $this->page;
                $n--;
                $buff .= '&laquo;<a href="' . $this->skript . ($this->rewrit ? 'page-' . $n . '.html' : 'page=' . $n) . '">Назад</a>';
            } else {
                $buff .= '&laquo;Назад';
            }
            $buff .= ' | ';
            if ($this->page != $this->num_pages) {
                $p = $this->page;
                $p++;
                $buff .= ' <a href="' . $this->skript . ($this->rewrit ? 'page-' . $p . '.html' : 'page=' . $p) . '">Далее</a>&raquo;<br />';
            } else {
                $buff .= 'Далее&raquo;<br />';
            }
        }
        $buff .= 'Стр: ';
        for($pr = '', $i = 1; $i <= $this->num_pages; $i++) {
            $buff .= $pr = (($i == 1 || $i == $this->num_pages || abs($i - $this->page) < 5) ? ($i == $this->page ? " [$i] " : ' <a href="' . $this->skript . ($this->rewrite ? 'page-' . $i . '.html' : 'page=' . $i) . '">' . $i . '</a> ') : (($pr == ' ... ' || $pr == '')? '' : ' ... '));
        }
        if ($this->form) {
            $buff .= '
<form action="' . $this->skript . '" method="POST">
  <div>
    <input type="text" name="page" size="3"  value="' . $this->page . '">
    <input type="submit" name="" value="&gt;&gt;">
  </div>
</form>';
        }
        return $buff . '<br />';
    }
    /* Список страниц 1 2 3 .. */
    function list_str(){
        $num_pages = ceil($this->all / $this->pnumber);
    	if($num_pages > 1){
    		$buff = 'Стр: ';
    		for($pr = '', $i = 1; $i <= $num_pages; $i++) {
            	$buff .= $pr = (($i == 1 || $i == $num_pages || abs($i < 5)) ? ' <a href="' . $this->skript . ($this->rewrite ? 'page-' . $i . '.html' : 'page=' . $i) . '">' . $i . '</a> ' : (($pr == ' ... ' || $pr == '') ? '' : ' ... '));
    		}
    		$buff .= '<br />';
    	}
		return $buff;
	}
}

?>