<?php
/*********************/
/*                   */
/*  Version : 5.1.0  */
/*  Author  : RM     */
/*  Comment : 071223 */
/*                   */
/*********************/

class paginator
{

    var $previous;
    var $current;
    var $next;
    var $page;
    var $total_pages;
    var $link_arr;
    var $range1;
    var $range2;
    var $num_rows;
    var $first;
    var $last;
    var $first_of;
    var $second_of;
    var $limit;
    var $prev_next;
    var $base_page_num;
    var $extra_page_num;
    var $total_items;
    var $pagename;

    function paginator( $page, $num_rows = 0 )
    {
        if ( !$page )
        {
            $this->page = 1;
        }
        else
        {
            $this->page = $page;
        }
        if ( $num_rows )
        {
            $this->num_rows = $num_rows;
            $this->total_items = $this->num_rows;
        }
    }

    function set_limit( $limit = 5 )
    {
        $this->limit = $limit;
        $this->setbasepage( );
        $this->setextrapage( );
    }

    function setbasepage( )
    {
        $div = $this->num_rows / $this->limit;
        $this->base_page_num = floor( $div );
    }

    function setextrapage( )
    {
        $this->extra_page_num = $this->num_rows - $this->base_page_num * $this->limit;
    }

    function set_links( $prev_next = 5 )
    {
        $this->prev_next = $prev_next;
    }

    function gettotalitems( )
    {
        $this->total_items = $this->num_rows;
        return $this->total_items;
    }

    function getrange1( )
    {
        $this->range1 = $this->limit * $this->page - $this->limit;
        return $this->range1;
    }

    function getrange2( )
    {
        if ( $this->page == $this->base_page_num + 1 )
        {
            $this->range2 = $this->extra_page_num;
        }
        else
        {
            $this->range2 = $this->limit;
        }
        return $this->range2;
    }

    function getfirstof( )
    {
        $this->first_of = $this->range1 + 1;
        return $this->first_of;
    }

    function getsecondof( )
    {
        if ( $this->page == $this->base_page_num + 1 )
        {
            $this->second_of = $this->range1 + $this->extra_page_num;
        }
        else
        {
            $this->second_of = $this->range1 + $this->limit;
        }
        return $this->second_of;
    }

    function gettotalpages( )
    {
        if ( $this->extra_page_num )
        {
            $this->total_pages = $this->base_page_num + 1;
        }
        else
        {
            $this->total_pages = $this->base_page_num;
        }
        return $this->total_pages;
    }

    function getfirst( )
    {
        $this->first = 1;
        return $this->first;
    }

    function getlast( )
    {
        if ( $this->page == $this->total_pages )
        {
            $this->last = 0;
        }
        else
        {
            $this->last = $this->total_pages;
        }
        return $this->last;
    }

    function getprevious( )
    {
        if ( 1 < $this->page )
        {
            $this->previous = $this->page - 1;
        }
        return $this->previous;
    }

    function getcurrent( )
    {
        $this->current = $this->page;
        return $this->current;
    }

    function getpagename( )
    {
        $this->pagename = $_SERVER['PHP_SELF'];
        return $this->pagename;
    }

    function getnext( )
    {
        $this->gettotalpages( );
        if ( $this->total_pages != $this->page )
        {
            $this->next = $this->page + 1;
        }
        return $this->next;
    }

    function getlinkarr( )
    {
        $top = $this->gettotalpages( ) - $this->getcurrent( );
        if ( $top <= $this->prev_next )
        {
            $top = $top;
            $top_range = $this->getcurrent( ) + $top;
        }
        else
        {
            $top = $this->prev_next;
            $top_range = $this->getcurrent( ) + $top;
        }
        $bottom = $this->getcurrent( ) - 1;
        if ( $bottom <= $this->prev_next )
        {
            $bottom = $bottom;
            $bottom_range = $this->getcurrent( ) - $bottom;
        }
        else
        {
            $bottom = $this->prev_next;
            $bottom_range = $this->getcurrent( ) - $bottom;
        }
        $j = 0;
        foreach ( range( $bottom_range, $top_range ) as $i )
        {
            $this->link_arr[$j] = $i;
            ++$j;
        }
        return $this->link_arr;
    }

}

echo "\t";
?>
