<?php
class post_pager
{ 
	
	var $page_limit	=	"";
	var $range_limit	=	"";
	var $current_page	=	"";
	
	var $page_total	=	"";
	var $recresult	=	"";
	var	$total_record	=	"";
	
	
	function post_pager($page_size_limit=USER_MAXRAW, $range_size_limit=USER_MAXPAGE) {
		// Page no
		if (isset($_POST['page4_'.($_POST["curr_pg"]+1)."_x"]))
			$this->current_page = ($_POST["curr_pg"]+1);
		else if (isset($_POST['page5_'.$_POST["total_pg"]."_x"]))
			$this->current_page = $_POST["total_pg"];
		else if (isset($_POST['page1_1_x']))
			$this->current_page = 1;
		else if (isset($_POST['page2_'.($_POST["curr_pg"]-1)."_x"]))
			$this->current_page = ($_POST["curr_pg"]-1);
		else if (isset($_POST['page3']))
			$this->current_page = $_POST['page3'];		
		else if (isset($_POST['page']))
			$this->current_page = $_POST['page'];				
		else if (isset($_GET['page']))
			$this->current_page = $_GET['page'];				
		else
			$this->current_page = 1;
		
		
		$this->page_limit	=	$page_size_limit;
		$this->range_limit	=	$range_size_limit;
		
	}
	
	/*********************************************************************************** 
   	* int findStartPost (int int_limit, int int_page) 
   	* Returns the start of pages on a limit and current page
   	***********************************************************************************/ 
	function findStartPost() 
	{ 
		
		if ($this->current_page	== "1")
		{ 
			$int_start = 0; 
		} 	 
		else 
		{ 
			$int_start = ($this->current_page-1) * $this->page_limit; 
		} 
		return $int_start; 
	} 
	
	/*********************************************************************************** 
	* int findPages (int count, int limit) 
	* Returns the number of pages needed based on a count and a limit 
	***********************************************************************************/ 
	function findPages() 
	{ 
		$pages = (($this->total_record % $this->page_limit) == 0) ? $this->total_record / $this->page_limit : floor($this->total_record / $this->page_limit) + 1; 
		return $pages; 
	} 
	
	function query($str_query1, $sorting, $conn) {
		
		$start 			= 	$this->findStartPost(); 
		
		if(strlen($sorting)==0) {
			$str_query 		= 	$str_query1." LIMIT ".$start.", ".$this->page_limit."";
		} else {
			$str_query 		= 	$str_query1." ORDER BY ".$sorting." LIMIT ".$start.", ".$this->page_limit."";		
		}
		
		
		$this->recresult 		= 	tep_db_query($str_query,$conn);
		$this->page_total		=	tep_db_num_rows($this->recresult);
		
		$recresult_count 		= 	tep_db_query($str_query1,$conn);	
		$this->total_record		= 	tep_db_num_rows($recresult_count); 	
		
	}
	
	function get_counter() {
		return (($this->current_page * $this->page_limit) - $this->page_limit)+1;
	}
	
	/*********************************************************************************** 
	* string pageList (int curpage, int pages) 
	* Returns a list of pages in the format of "« < [pages] > »" 
	* strsort for sorting the page.Add by Ranjana
	***********************************************************************************/ 
	function pageList($frm="frmpagelist") 
	{ 
		$total_page	=	$this->findPages();
		
		$page_list  = 	""; 
		$combo_page	=	ceil($total_page/$this->range_limit);
		$start_page	=	ceil($this->current_page/$this->range_limit)*$this->range_limit - ($this->range_limit-1);
		$current_group	=	ceil($this->current_page/$this->range_limit);

		$prevlink="";
		$nextlink="";
	
		$firstlink	=	$this->firstlink($total_page);
		$prevlink	=	$this->prevlink($total_page);
		$nextlink	=	$this->nextlink($total_page);
		$lastlink	=	$this->lastlink($total_page);			
		
	 	$page_list="<table border='0' cellpadding='1' cellspacing='1' class='PgPage'>
	 				<tr>
						<td>
							<table border='0' cellpadding='0' cellspacing='0'>
								<tr>
									<td class='blacktext'>".$firstlink."&nbsp;&nbsp;</td>
								</tr>
							</table>
						</td>";	
		$page_list.=	"<td>
							<table border='0' cellpadding='0' cellspacing='0'>
								<tr>
									<td class='blacktext'> ".$prevlink."&nbsp;</td>
								</tr>
							</table>
						</td>";
		$page_list.=	"<td>";
						if ($current_group==$combo_page) {
							$end_page	=	$total_page+1;
						} else {
							$end_page	=	($start_page + $this->range_limit);
						}
						
						for ($i=$start_page; $i<$end_page; $i++) 
						 { 
						 	if ($this->current_page==$i) {
								$page_list.= "<strong>".$i."</strong>";
							} else {
								$page_list.= " <a href='' class='blacktext' onclick=\"return formsubmit(".$i.",'".$frm."')\">".$i."</a> ";
							}
						 }
						$page_list.="</td>";						
		$page_list.=	"<td>
							<table border='0' cellpadding='0' cellspacing='0'>
								<tr>
									<td class='blacktext'>&nbsp;".$nextlink."&nbsp;&nbsp;</td>
								</tr>
							</table>
						</td>
						<td>
							<table border='0' cellpadding='0' cellspacing='0'>
								<tr>
									<td class='blacktext'>".$lastlink."&nbsp;&nbsp;</td>
								</tr>
							</table>
						</td>";
						if ($combo_page!=1) {
		$page_list.=	"
							<td  align='center' colspan=5> 
							<table border='0' cellpadding='0' cellspacing='0'>
								<tr class='blacktext'>
									<td>
										<select class='comboboxmedium' name='page_combo'>";
										 for ($i=1; $i<=$combo_page; $i++) 
										 { 
												$page_list .="<option value='".(($i*$this->range_limit)-$this->range_limit+1)."'";
												if ($current_group==$i)
												{
													$page_list .=" selected";
												}
												if ($i==$combo_page) {
													$page_list .=	">".(($i*$this->range_limit)-$this->range_limit+1)." - ".$total_page."</option>";
												} else {
													$page_list .=	">".(($i*$this->range_limit)-$this->range_limit+1)." - ".($i*$this->range_limit)."</option>";
												}													
										 }
										 
										 $page_list .="</select>			
										<a href='' class='blacktext' onclick=\"return formsubmit(document.frmpagelist.page_combo.value,'".$frm."');\">Go</a>										
										&nbsp;&nbsp;
									 </td>
								</tr>
							</table>
							</td>		
						</tr>";
						}
		$page_list.=	"</tr></table>";
   
     return $page_list; 
    } 


	/*********************************************************************************** 
		Generate the link for "MOVE TO LAST"	
	***********************************************************************************/ 
	
	function lastlink($pages11) 
	{ 
		$last_link  = ""; 
		if (($this->current_page+1) > $pages11 || $pages11<=1) 
		{ 
			$last_link .= "<img src='images/arlast.gif' alt='Last'  border='0' />"; 
		} 
		else 
		{ 
			$last_link .= "<input name='page5_".$pages11."' type=\"image\" src='images/arlast.gif' alt='Last' border='0'  value='".$pages11."' />"; 
		} 
		return $last_link; 
	} 

	/*********************************************************************************** 
		Generate the link for "MOVE TO FIRST"	
	***********************************************************************************/ 
	
	function firstlink($pages) 
	{ 
	  	$first_link  = "";
		if (($this->current_page-1) <= 0 || $pages<=1) 
		{ 
			$first_link .= "<img src='images/arfirst.gif' alt='First' border='0' />"; 
		} 
		else 
		{ 
			$first_link .= "<input type='image' src='images/arfirst.gif' alt='First' border='0' name='page1_1' value='1' />"; 
		} 
		return $first_link; 
	} 
	
	/*********************************************************************************** 
		Generate the link for "MOVE TO NEXT"	
	***********************************************************************************/ 
 
	function nextlink($pages) 
	{ 
		$next_prev  = ""; 
		if (($this->current_page+1) > $pages) 
		{ 
			$next_prev .= "<img src='images/ar2.gif' alt='Next' border='0' />"; 
		} 
		else 
		{ 
			$next_prev .= "<input type='image' src='images/ar2.gif' alt='Next' border='0' name='page4_".($this->current_page+1)."' value='".($this->current_page+1)."' />"; 
		} 
		
		return $next_prev; 
	} 

	/*********************************************************************************** 
		Generate the link for "MOVE TO PREV"	
	***********************************************************************************/ 
	
	function prevlink($pages) 
	{ 
		$next_prev  = ""; 
		
		if (($this->current_page-1) <= 0) 
		{ 
			$next_prev .= "<img src='images/ar1.gif' alt='Previous'  border='0' />"; 
		} 
		else 
		{ 
			$next_prev .= "<input type='image' src='images/ar1.gif' alt='Previous'  border='0' name='page2_".($this->current_page-1)."' value='".($this->current_page-1)."' />"; 
		} 
		
		return $next_prev; 
	} 
} 

?>
