<?php

/*
   +----------------------------------------------------------------------+
   | Pagination Class                                                     |
   | Author: Adam Goldberg                                                |
   +----------------------------------------------------------------------+
   | Copyright (c) 2008-2009 Adam J Goldberg                              |
   | This file is part of Lifebox.                                        |
   |                                                                      |
   | Lifebox is free software: you can redistribute it and/or modify      |
   | it under the terms of the GNU General Public License as published by |
   | the Free Software Foundation, either version 3 of the License, or    |
   | (at your option) any later version.                                  |
   |                                                                      |
   | Lifebox is distributed in the hope that it will be useful,           |
   | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
   | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
   | GNU General Public License for more details.                         |
   |                                                                      |
   | You should have received a copy of the GNU General Public License    |
   | along with Lifebox.  If not, see <http://www.gnu.org/licenses/>.     |
   +----------------------------------------------------------------------+
   | Makes information navigable.					  |
   +----------------------------------------------------------------------+
        function Pagination($total_items,$items_per_page,$offset){
	function setURL($url){
	function getNavigation(){
	function getCurrentPage(){
        function getTotalPages(){
        function getTotalItems(){
        function getFirstResult(){
        function getLastResult(){
   +----------------------------------------------------------------------+
*/

class Pagination {

	#Default values
	var $pages_to_display = 8 ;
	
	#Default names
	var $name_offset = 'page';
	var $div_class = 'pagination';
	var $url;

	#Values set by constructor
	var $total_items;
	var $items_per_page;
	var $offset;
	var $current_page;
	var $total_pages;

	/*****************************************************************
	 * Constructor.  Initializes class variables.
         *****************************************************************/
        function Pagination($total_items,$items_per_page,$offset){

		#Divide by zero protection
		if ($items_per_page == 0){
			return false;
		}

		$this->total_items = $total_items;
		$this->items_per_page = $items_per_page;
		$this->offset = $offset;

		$this->total_pages = ceil($total_items / $items_per_page);
		$this->current_page = (($offset + $items_per_page) / $items_per_page);

	}

	function setURL($url){
		$this->url = $url;
	}

	/*****************************************************************
         * Returns html to display the pagination navigation.
	 * [ PREV ] 7 8 9 10 (11) 12 13 14 15 16 [ NEXT ]
         *****************************************************************/
	function getNavigation(){

		$NEXT='';
		$PREVious='';
		$navigation='';
		
		#Generate the range of page numbers
		$page_middle = floor($this->pages_to_display / 2);

		#Calculate the lower bound of pages.
		$page_lower_bound = abs($this->current_page - $page_middle);
		if ($this->current_page <= $page_middle){
			$page_lower_bound = 1;
		}

		if ($this->current_page - $page_lower_bound > $page_middle){
			$difference = $this->current_page - $page_middle;
			$page_lower_bound += $difference;
		}

		#Calculate the upper bound of pages
		$page_upper_bound = $this->current_page + $page_middle;
		if ($page_upper_bound > $this->total_pages){
			$page_upper_bound = $this->total_pages;
		}

		if ( ($page_upper_bound - $page_lower_bound < $this->pages_to_display) AND ( $this->pages_to_display < $this->total_pages) ){

			$difference = $page_upper_bound - $this->pages_to_display;
			$page_lower_bound = $difference;

			if ($page_lower_bound < 1) {
				$page_lower_bound = 1;
			}

			if ($page_upper_bound <= $this->pages_to_display) {
			$page_upper_bound += abs($difference);
			}
		}

		#Push page numbers into an array.
		$counter = 0;
		for ($i=$page_lower_bound; $i <= $page_upper_bound; $i++){
			if ($this->offset > 0){
                                $PREVious = "<li><a href=\"$this->url&offset=".( (($this->current_page - 2) * $this->items_per_page) )."\">PREV</a></li>\n";
                        }

			if ($i == $this->current_page){
				$navigation .= "<li class=\"active_nav\">$i</li>\n";
			} else {
				$navigation .= "<li><a href=\"$this->url&offset=".( ($i-1) * $this->items_per_page)."\">$i</a></li>\n";
			}

			if ($this->current_page != $this->total_pages AND $i == $page_upper_bound-1){
                        	$NEXT = "<li><a href=\"$this->url&offset=".( ($this->current_page * $this->items_per_page) )."\">NEXT</a></li>\n";
                	}

			$counter++;
		}

		$navigation = $PREVious.$navigation.$NEXT;
		
		if ($counter > 1) {
			return $navigation;
		} else {
			return false;
		}
	}

	/*****************************************************************
	 * Returns the current page number that the user is browsing.
         *****************************************************************/
	function getCurrentPage(){

		return $this->current_page;

	}


	/*****************************************************************
         * Returns the total number of pages.
         *****************************************************************/
        function getTotalPages(){

                return $this->total_pages;
  
        }

	/*****************************************************************
         * Returns the total number of results.
         *****************************************************************/
        function getTotalItems(){

                return $this->total_items;

        }

	/*****************************************************************
         * Returns the first result.
	 * For example, Results (15) - 25.  15 would be the first result.
         *****************************************************************/
        function getFirstResult(){

		$first_result = ($this->current_page * $this->items_per_page) - $this->items_per_page + 1;
		if ($first_result <= 0){
			$first_result = 1;
		}

		return $first_result;
        }

	/*****************************************************************
         * Returns the last result.
	 * For example, Results 15 - (25).  25 would be the last result.
         *****************************************************************/
        function getLastResult(){

		$difference = ( $this->current_page * $this->items_per_page ) - $this->total_items;
                
		$last_result = $this->current_page * $this->items_per_page;

		#Special exception for viewing the last page (example, Results 13 - 13).
		#If the pagination does not equally break down the results, this will fix
		#it.
		if ($difference > 0){
			$last_result -= $difference;
		}

		return $last_result;
        }
	
}
?>
