<?php
////////////////////////////////////////////////////////////////////////
// Breeze Website Builder
// Copyright Silver Dolphin Solutions, LLC, Edward Lemmers, P.E.
// For licensing, see license.txt.
// Created: 11/12/2015	ELL
// Modified: 11/21/2018	ELL
// 
// 
// 
	include ("./check_session.php");
	
	$table = $_REQUEST['table'];
	$fields = $_REQUEST['fields'];
	if (!isset($fields) || ($fields == ""))
		$fields = "*";
	$where = $_REQUEST['where'];
	$order_by = $_REQUEST['order_by'];
	$no_prefix = $_REQUEST['no_prefix'];
	if (isset($no_prefix) && ($no_prefix == 1))
		$prefix = "";
	else
		$prefix = BWB_TABLE_PREFIX;
	$page = $_REQUEST['page'];
	$max_rec = $_REQUEST['max_rec'];
	
	$query = "SELECT " . $fields . " FROM " . $prefix . $table;
	if (isset($where) && ($where != ""))
		$query .= " WHERE " . $where;
	if (isset($order_by) && ($order_by != ""))
		$query .= " ORDER BY " . $order_by;
	if (isset($page) && isset($max_rec) && is_numeric ($page) && is_numeric ($max_rec))	{
		$start_row = $page * $max_rec;
		$result = $bwb_dbh->query($query);
		if ($result->rowCount() < 1)	{
			echo "-no data-";
			exit;
		}
		$total_rec = $result->rowCount();
		$query .= " LIMIT " . $start_row . "," . $max_rec;
	}

	$result = $bwb_dbh->query($query);
	
	if ($result->rowCount() < 1)	{
		echo "-no data-";
		exit;
	}

	$row_delim = "";
	while ($row = $result->fetch(PDO::FETCH_ASSOC))
	{
		$rows .= $row_delim . json_encode($row);
		$row_delim = "@@@@@";
	}
	if (isset($total_rec) && is_numeric($total_rec))
		$rows .= "&&&&&" . $total_rec;
	
	echo $rows;
?>