<?php
/*
 * DataTables example server-side processing script.
 *
 * Please note that this script is intentionally extremely simply to show how
 * server-side processing can be implemented, and probably shouldn't be used as
 * the basis for a large complex system. It is suitable for simple use cases as
 * for learning.
 *
 * See http://datatables.net/usage/server-side for full details on the server-
 * side processing requirements of DataTables.
 *
 * @license MIT - http://datatables.net/license_mit
 */
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * Easy set variables
 */
 
// DB table to use
$table =  $_COOKIE['phxcrmclient_id'] . "_prodserv_view";
// $table =  "65_prodserv_view";
// Table's primary key
$primaryKey = 'uid';
 
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes


$columns = array(
    array( 'db' => 'uid', 'dt' => 0 ),
    array( 'db' => 'uid_link', 'dt' => 1 ),
    array( 'db' => 'prod_code', 'dt' => 2 ),
    array( 'db' => 'prod_type',  'dt' => 3 ),
    array( 'db' => 'prod_title',   'dt' => 4 ),
    array( 'db' => 'prod_retail',     'dt' => 5 ),
    array( 'db' => 'prod_cost_price',     'dt' => 6 ),
    array( 'db' => 'prod_status',     'dt' => 7 ),
	    array( 'db' => 'add_link',     'dt' => 8 )
 );

 
// SQL server connection information
$sql_details = array(
    'user' => 'wealthhe_mybzu',
    'pass' => 'IuGaYVI@b%tD',
    'db'   => 'wealthhe_mybizdb',
    'host' => 'localhost'
);
 
// $search = "comp_name like '%a%' and comp_name like '%v%'";
 $search = "";
 
// $search = "search like '%a%' and comp_name like '%v%'";
 $search = "";

//dave

$search = "";

if(!isset($_COOKIE['search_text'])) {
	$search = "";

//exit;
}else{

$search_text = $_COOKIE['search_text'];
$search_option = $_COOKIE['search_option'];

 
 $pieces = explode(" ",$search_text);

$arraycount = count($pieces);


if ($search_option == 'All') {

//All

		$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " search like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and search like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
}else if ($search_option == 'Title') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_title like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_title like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
	}else if ($search_option == 'Title begins with') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_title like '" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_title like '" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
		}else if ($search_option == 'Description') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_descr like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_descr like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
	
	
	}else if ($search_option == 'PROD_CODE') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_code like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_code like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
		}else if ($search_option == 'Category') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_catname like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_catname like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
			}else if ($search_option == 'Supplier') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_suppname like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_suppname like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
				}else if ($search_option == 'Brand') {
	
			$ac = 0;
while ($ac < $arraycount) {

	if ($ac == 0) {
	$search =  $search . " prod_brandname like '%" . $pieces[0] . "%' ";
	}
	
	if ($ac > 0) {
	$search =  $search . " and prod_brandname like '%" . $pieces[$ac] . "%' ";
	}
	
  $ac = $ac + 1;

	}
	
}
}
 
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */
 
require( 'ssp.class_search.php' );
 
 	if ($search == ""){
		echo json_encode(
		SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
		);
	}else{
		echo json_encode(
		SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns, stripslashes($search) )
		);		
	}
	