<?
	require_once('../include/user.php');
	require_once('../include/authenticate.php');
	require_once('../include/mysql.php');
	require_once('../include/functions.php');
	require_once('../include/fb.php');
	
	ob_start();
	session_start();
	
	// SETUP MYSQL HANDLERS
	$mysqldb1 = new mysql($host, $user, $pswd, $db); $mysqldb1->connect(); $mysqldb1->select();
	$mysqldb2 = new mysql($host, $user, $pswd, $db); $mysqldb2->connect(); $mysqldb2->select();
	
	// SETUP GENERAL VARIABLES
	$url = $_SERVER['REQUEST_URI'];
	$sort = $_GET['sort'];
	$id = $_GET['id']; if ($id == '') { $id = $_POST['id']; }
	$date = date('Y-m-d H:i:s', time());
	$error = $_GET['error'];
	$log = '';
	$_SESSION['panel_view_url'] = $_SERVER['REQUEST_URI'];
	
	// SETUP RECORD HANDLER
	$table = $_GET['table'];
	
	// RECORD STATUS HANDLER
	if ($_GET['status'] != '') {
		$query = "UPDATE ".$table." SET status='".$_GET['status']."' WHERE id=".$_GET['id'];
		$status_set = $mysqldb1->query($query);
		if ($status_set) {
			$log .= '<div class="success">[RECORD: ID-'.$_GET['id'].' '.strtoupper($_GET['status']).']</div>';
		} else {
			$log .= '<div class="error">ERROR! [RECORD: ID-'.$_GET['id'].' FAILED] (status not changed)</div>';
		}
	} 
	
	$page_size = $records_per_page;
	$record_start = $_GET['record_start'];
	if (strlen($record_start) == 0) {
		$record_start = 0;
	}
	
	$url_variables = '';
	
	require_once('../table/query.php');
	
	$count_query = "SELECT COUNT(t1.id) AS total FROM ".$table." AS t1 ".$filter_sql." ".$search_sql;
	$mysqldb2->query($count_query);
	$row = $mysqldb2->fetchArray();
	$total_rows = $row['total'];
	$query = "SELECT ".$columns." FROM ".$table." AS t1 ".$search_sql." ORDER BY ".$sort." LIMIT ".$record_start.", ".$page_size;
	
	$variables = "&table=".$table."&sort=".$sort.$url_variables;
	
	// Create the previous link
	if ($record_start > 0) {
		$prev = $record_start - $page_size;
		$url = $_SERVER['PHP_SELF'].'?record_start='.$prev.$variables;
		$prev_link = ' <a href="'.$url.'"><<< previous</a>';
	} else {
		$prev_link = '';
	}
	
	// Create the next link
	if ($total_rows > ($record_start + $page_size)) {
		$next = $record_start + $page_size;
		$url = $_SERVER['PHP_SELF'].'?record_start='.$next.$variables;
		$next_link = '<a href="'.$url.'">next >>></a> ';
	} else {
		$next_link = '';
	}
	
	$total_pages = ceil($total_rows / $page_size);
	$current_page = ($record_start / $page_size) + 1;
	
	// Create the page links
	if ($total_rows > $page_size) {
		$page_links = pageLinks($total_pages, $current_page, $page_size, 'record_start', $variables);
	}
	
	fb($query);
	$mysqldb1->query($query);
	$rows = $mysqldb1->affectedRows();
	
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Control Panel View</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<!--STYLES-->
<link type="text/css" rel="stylesheet" href="../styles/default/view.css"/>
<!--/STYLES-->
<!--JQUERY-->
<link type="text/css" rel="stylesheet" href="../styles/smoothness/jquery.ui.css"/>
<script type="text/javascript" src="../scripts/jquery.js"></script>
<script type="text/javascript" src="../scripts/jquery.ui.js"></script>
<script type="text/javascript" src="../scripts/jquery.tablehover.js"></script>
<script type="text/javascript" src="../scripts/jquery.tablesorter.js"></script>
<!--/JQUERY-->
<!--JGROWL-->
<link rel="stylesheet" href="../uploadify/css/uploadify.jGrowl.css" type="text/css" />
<script type="text/javascript" src="../uploadify/js/jquery.jgrowl_minimized.js"></script>
<!--/JGROWL-->
<!--CUSTOM-->
<script type="text/javascript" src="../scripts/custom.js"></script>
<script type="text/javascript">
	$(document).ready(function() {
	
		$('#table').tableHover(); 
		//$("#table").tablesorter();

        $(".dialog").hide();
		
		imageHover('img');
		
	});
</script>
<!--/CUSTOM-->
</head>
<body>
<div id="header"><? $plural = 's'; if ($rows == 1) { $plural = ''; } print 'The '.$table.' table has '.$rows.' record'.$plural.' sorted by "'.$sort.'".'; ?></div>
<div id="feedback"><? print $log; ?></div>
<div class="page_links"><? print $prev_link.' '.$page_links.' '.$next_link; ?></div>
<? print $mysqldb1->displayRecords($table, $grid_width, $sort, $record_start, $url_variables, $_SERVER['REQUEST_URI']); ?>
<div class="page_links"><? print $prev_link.' '.$page_links.' '.$next_link; ?></div>
<? require_once("dialog.php"); ?>
</body>
</html>
<?
	$mysqldb1->close();
	$mysqldb2->close();
?>