<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 7/2/2012	ELL
// Revised: 2/12/2013	ELL
// Added paging of user tables.
// 
// 
// 
	
	include ("./check_session.php");
	
	$user_table = $_POST['user_table'];
	$page = $_POST['page'];
	$max_rec = $_POST['max_rec'];
	$user_sort = $_POST['user_sort'];
	
	if (!isset($user_table) || ($user_table == ""))
	{
		echo "-no data-";
		exit;
	}
	if (!isset($page) || ($page == ""))
		$page = 1;
	if (!isset($max_rec) || ($max_rec == ""))
		$max_rec = 50;
	
	$_SESSION['last_user_table'] = $user_table;
	
	$query = "SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . $bwb_database . "' AND TABLE_NAME = '" . $user_table . "' AND COLUMN_NAME = 'UserID'";
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() < 1)
	{
		echo "-not user access table-";
		exit;
	}
	
	$start_row = $page * $max_rec;
	if (($user_sort != "undefined") && isset($user_sort) && ($user_sort != "") && ($user_sort != "-not set-"))
		$sort = $user_sort;
	else
		$sort = "UserID";
	$query = "SELECT * FROM " . $user_table . " ORDER BY " . $sort . " LIMIT " . $start_row . "," . $max_rec;
//	echo $query;
//	exit;
	$result = $bwb_dbh->query($query);
	if ($result->rowCount() < 1)
	{
		echo "-no data-";
		exit;
	}

	$row_delim = "";
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$table .= $row_delim . json_encode($row);
		$row_delim = "@@@@@";
	}
	
	print $table;
?>