<?php
/*
***************************************************************************
* Function sql_navigation(), used to create a link-index of page numbers  *
* for paging through the records which come from your MySQL-database.     *
* Copyright (C) 2004 The Celestial Celebi.                                *
* This program 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 2 of the License, or (at your  *
* option) any later version.                                              *
* This program 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 this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.               *
***************************************************************************
*/

/*
***************************************************************************
* @function: sql_navigation(): Returns an array for creating a            *
* pagination.                                                             *
* @param $rDatabaseconnection (resource): Your database connection, we    *
*  need it to get the total number of records from the database.  This    *
*  parameter must be given by reference.                                  *
* @param $aTable (string / array): The table where we need to take out    *
*  the records for the navigation, can be an array of two elements: 1st   *
*  is the table and the 2nd is a piece of query after the FROM.           *
* @param $sUrl (string): The name of the file in your URL, plus (if there *
*  is any) the query string, needed for making correct navigation links.  *
* @param $sAlias (string): The alias you want to give the navigation you  *
*  are making now, it becomes part of the URL: index.php?MYALIAS=40.      *
* @param $iPerpage (int): The number of records you want to display per   *
*  page.                                                                  *
* @param $iType (int): The type of navigation, you can use either the     *
*  constants or integer numbers, see constants for more information.      *
* @return array: All information you need to display your navigation.     *
*-------------------------------------------------------------------------*
* @started on: 12 May 2004 at 20:25:56 by The Celestial Celebi.           *
* @last edited on: 20 June 2004 at 11:36:38 by The Celestial Celebi.      *
* @constructs used: array, else, elseif, if, for, return.                 *
* @functions used: ceil, intval, is_array, isset, mysql_error,            *
* mysql_query, mysql_result, trigger_error, str_replace, strstr, trim.    *
***************************************************************************
*/

function sql_navigation(&$rDatabaseconnection, $aTable, $sUrl, $sAlias, $iPerpage, $iType)
{
	if(is_array($aTable))
	{
		$sQuerybit = ' ' . $aTable[1];
		$sTable = $aTable[0];
	}
	else
	{
		$sTable = $aTable;
		$sQuerybit = '';
	}
	$sSqlgetnumberofrecordsstring = "SELECT COUNT(1) AS totalrecords FROM " . $sTable . $sQuerybit;
	if(!$rSqlgetnumberofrecordsquery = mysql_query($sSqlgetnumberofrecordsstring, $rDatabaseconnection))
	{
		trigger_error('sql_navigation(): Could not query the total number of records from database, ' . mysql_error(), E_USER_WARNING);
		return;
	}
	else
	{
		$iTotalrecords = mysql_result($rSqlgetnumberofrecordsquery, 0, 'totalrecords');
		$iTotalpages = ($iTotalrecords / $iPerpage);
		$iTotalpages = ceil($iTotalpages);
		if(!isset($_GET[$sAlias]) || $_GET[$sAlias] > $iTotalpages || $_GET[$sAlias] < 1 || intval($_GET[$sAlias]) != $_GET[$sAlias]) // a whole block.. it's here to validate the pagenumber as some users might f*ck it up
		{
			$iCurrent = 1;
		}
		else
		{
			$iCurrent = intval($_GET[$sAlias]);
		}
		$iStartpoint = (($iCurrent - 1) * $iPerpage);
		if($iCurrent == $iTotalpages && ($iTotalrecords % $iPerpage) != 0)
		{
			$iEndpoint = ($iTotalrecords % $iPerpage);
			if($iEndpoint == 0)
			{
				$iEndpoint += 1;
			}
		}
		elseif($iTotalrecords == 0)
		{
			$iEndpoint = 0;
		}
		else
		{
			$iEndpoint = $iPerpage;
		}
		$sNavigation = '';
		if($iTotalpages != 1 && $iTotalrecords != 0)
		{
			if(strstr($sUrl, '?'))
			{
				$sUrlseperator = '&';
			}
			else
			{
				$sUrlseperator = '?';
			}
			if($iType == NAV_BOTH || $iType == NAV_PAGENUMBERS)
			{
				for($i = 1; $i <= $iTotalpages; $i++)
				{
					if($iCurrent == $i)
					{
						$sNavigation .= ' <b>' . $i . '</b> ';
					}
					else
					{
						$sNavigation .= ' <a class="navigation" href="' . $sUrl . $sUrlseperator . $sAlias . '=' . $i . '">' . $i . '</a> ';
					}
				}
			}
			if($iType == NAV_BOTH || $iType == NAV_PREVIOUSNEXT)
			{
				if($iCurrent != 1)
				{
					$sNavigation = '<a class="navigation" href="' . $sUrl . $sUrlseperator . $sAlias . '=' . ($iCurrent - 1) . '">vorige</a> ' . $sNavigation;
				}
				else
				{
					$sNavigation = 'vorige ' . $sNavigation;
				}
				if($iCurrent < $iTotalpages)
				{
					$sNavigation .= ' <a class="navigation" href="' . $sUrl . $sUrlseperator . $sAlias . '=' . ($iCurrent + 1) . '">volgende</a>';
				}
				else
				{
					$sNavigation .= ' volgende';
				}
			}
			$sNavigation = str_replace('  ', ' ', $sNavigation);
			$sNavigation = trim($sNavigation);
		}
		return array(
			'current' => $iCurrent,
			'navigation' => $sNavigation,
			'startpoint' => $iStartpoint,
			'endpoint' => $iEndpoint,
			'totalrecords' => $iTotalrecords,
			'totalpages' => $iTotalpages
		);
	}
}
?>