<?php
error_reporting(0);
include 'connect.php';
$json = file_get_contents('php://input');
$objdata = json_decode( $json );
header('Content-Type: application/json; charset=utf-8');
header("Access-Control-Allow-Origin: *");

$function = $objdata->{'function'};

try {

    switch ($function) {
    case 'getGroceriesTypes':
    $response['result'] = getGroceriesTypes($db_connection, $objdata);
    break;
    case 'addGroceriesTypes':
    $response['result'] = addGroceriesTypes($db_connection, $objdata, $projectname, $API_URL, $user_key);
    break;
    case 'deleteGroceriesTypes':
    $response['result'] = deleteGroceriesTypes($db_connection, $objdata);
    break;
    case 'updateGroceriesTypes':
    $response['result'] = updateGroceriesTypes($db_connection, $objdata);
    break;
    case 'addHomeBanners':
    $response['result'] = addHomeBanners($db_connection, $objdata);
    break;
    case 'getHomeBanners':
    $response['result'] = getHomeBanners($db_connection, $objdata);
    break;
    default:
    # code...
    break;
    }

    $response['message'] = 'Success';
	$response['error'] = false;

} catch (UnexpectedValueException $e) {
	$response['message'] = "Invalid data.";
	$response['error'] = true;
} catch (Exception $e) {
	$response['message'] = "Error";
	$response['error'] = true;
}
echo json_encode($response);

function updateGroceriesTypes($db_connection, $objdata) {
    $id = $objdata->{'id'};
	$name = $objdata->{'name'};

    $image = $objdata->{'image'};
    if(isset($objdata->{'image'})){
    $imagesour = $objdata->{'image'};
    $api_key = '273683164257111';
    $api_secret = '0_ELN2DJLY0lPvf0pBxrNPow68E';
    $cloud_name = 'dmw8kjmxe';

    require '../CloudinaryUpload/src/Cloudinary.php';
	require '../CloudinaryUpload/src/Uploader.php';
	require '../CloudinaryUpload/src/Api.php';
	\Cloudinary::config(array(
	  "cloud_name" => $cloud_name,//"ordering",
	  "api_key" => $api_key,//"286845943699458",
	  "api_secret" => $api_secret//"gNr8jY6sCYyihc7mrcoJHq4AaAw"
	));
	$result_cloud_image = \Cloudinary\Uploader::upload($imagesour);
    $image = $result_cloud_image['secure_url'];

    pg_prepare($db_connection, 'sqluppd', 'UPDATE groceries_types SET image = $1 WHERE id = $2');
	pg_execute($db_connection, 'sqluppd', array($image, $id));
    }
    if(isset($objdata->{'name'})){
    pg_prepare($db_connection, 'sqlupp', 'UPDATE groceries_types SET name = $1 WHERE id = $2');
	pg_execute($db_connection, 'sqlupp', array($name, $id));
    }

    pg_prepare($db_connection, 'sqlfetch', 'SELECT * FROM groceries_types ORDER BY id ASC');
	$result = pg_execute($db_connection, 'sqlfetch', array());
	$groceries = array();
	while($row = pg_fetch_array($result)){
    $obj = new stdClass();
    $obj->id = (int)$row['id'];
    $obj->name = $row['name'];
    $obj->image = $row['image'];
    $obj->enabled = $row['enabled'];

    array_push($groceries,$obj);
    }
    return $groceries;

}

function deleteGroceriesTypes($db_connection, $objdata) {
	$id = $objdata->{'id'};

	pg_prepare($db_connection,'sqldelete','DELETE FROM groceries_types WHERE id=$1');
 	pg_execute($db_connection,'sqldelete',array($id));

    pg_prepare($db_connection, 'sqlfetch', 'SELECT * FROM groceries_types ORDER BY id ASC');
	$result = pg_execute($db_connection, 'sqlfetch', array());
	$groceries = array();
	while($row = pg_fetch_array($result)){
    $obj = new stdClass();
    $obj->id = (int)$row['id'];
    $obj->name = $row['name'];
    $obj->image = $row['image'];
    $obj->enabled = $row['enabled'];

    array_push($groceries,$obj);
    }
    return $groceries;

}

function addGroceriesTypes($db_connection, $objdata, $projectname, $API_URL, $user_key) {
	$name = $objdata->{'name'};
    $image = $objdata->{'image'};
    if($image){
    $imagesour = $objdata->{'image'};
    $api_key = '273683164257111';
    $api_secret = '0_ELN2DJLY0lPvf0pBxrNPow68E';
    $cloud_name = 'dmw8kjmxe';

    require '../CloudinaryUpload/src/Cloudinary.php';
	require '../CloudinaryUpload/src/Uploader.php';
	require '../CloudinaryUpload/src/Api.php';
	\Cloudinary::config(array(
	  "cloud_name" => $cloud_name,//"ordering",
	  "api_key" => $api_key,//"286845943699458",
	  "api_secret" => $api_secret//"gNr8jY6sCYyihc7mrcoJHq4AaAw"
	));
	$result_cloud_image = \Cloudinary\Uploader::upload($imagesour);
    $image = $result_cloud_image['secure_url'];
    }
    pg_prepare($db_connection,'sqlseq',"SELECT * from groceries_types ORDER BY id DESC LIMIT 1");
	 $resultkey = pg_execute($db_connection,'sqlseq',array());

	if (pg_num_rows($resultkey)==1){
		$rowsq = pg_fetch_array($resultkey);
		$seqid = $rowsq['id'] + 1;
	}else{
		$seqid = 1;
	}

    pg_prepare($db_connection,'sqlins','INSERT INTO groceries_types(id,name,image) VALUES($1,$2,$3)');
	pg_execute($db_connection,'sqlins',array($seqid,$name,$image));

    pg_prepare($db_connection, 'sqlfetch', 'SELECT * FROM groceries_types ORDER BY id ASC');
	$result = pg_execute($db_connection, 'sqlfetch', array());
	$groceries = array();
	while($row = pg_fetch_array($result)){
    $obj = new stdClass();
    $obj->id = (int)$row['id'];
    $obj->name = $row['name'];
    $obj->image = $row['image'];
    $obj->enabled = $row['enabled'];

    array_push($groceries,$obj);
    }
    return $groceries;

}

function getGroceriesTypes($db_connection, $objdata) {
	$id = $objdata->{'id'};
	pg_prepare($db_connection, 'sqlfetch', 'SELECT * FROM groceries_types ORDER BY id ASC');
	$result = pg_execute($db_connection, 'sqlfetch', array());
	$groceries = array();
	while($row = pg_fetch_array($result)){
    $obj = new stdClass();
    $obj->id = (int)$row['id'];
    $obj->name = $row['name'];
    $obj->image = $row['image'];
    $obj->enabled = $row['enabled'];

    array_push($groceries,$obj);
    }
    return $groceries;

}
function addHomeBanners($db_connection, $objdata) {

    $bannerone = $objdata->{'bannerone'};
    $bannertwo = $objdata->{'bannertwo'};
    $bannerthree = $objdata->{'bannerthree'};

    $api_key = '273683164257111';
    $api_secret = '0_ELN2DJLY0lPvf0pBxrNPow68E';
    $cloud_name = 'dmw8kjmxe';

    require '../CloudinaryUpload/src/Cloudinary.php';
	require '../CloudinaryUpload/src/Uploader.php';
	require '../CloudinaryUpload/src/Api.php';
	\Cloudinary::config(array(
	  "cloud_name" => $cloud_name,//"ordering",
	  "api_key" => $api_key,//"286845943699458",
	  "api_secret" => $api_secret//"gNr8jY6sCYyihc7mrcoJHq4AaAw"
	));

    if($bannerone){

	$result_cloud_image = \Cloudinary\Uploader::upload($bannerone);
    $banner1 = $result_cloud_image['secure_url'];

    pg_prepare($db_connection, 'sqluppdb', 'UPDATE home_banners SET banner1 = $1 WHERE id = $2');
	pg_execute($db_connection, 'sqluppdb', array($banner1, 1));
    }

    if($bannertwo){
	$result_cloud_image2 = \Cloudinary\Uploader::upload($bannertwo);
    $banner2 = $result_cloud_image2['secure_url'];

    pg_prepare($db_connection, 'sqluppdbf', 'UPDATE home_banners SET banner2 = $1 WHERE id = $2');
	pg_execute($db_connection, 'sqluppdbf', array($banner2, 1));
    }

    if($bannerthree){
	$result_cloud_image3 = \Cloudinary\Uploader::upload($bannerthree);
    $banner3 = $result_cloud_image3['secure_url'];

    pg_prepare($db_connection, 'sqluppdbe', 'UPDATE home_banners SET banner3 = $1 WHERE id = $2');
	pg_execute($db_connection, 'sqluppdbe', array($banner3, 1));
    }

    pg_prepare($db_connection, 'sqlfetch', 'SELECT * FROM home_banners ORDER BY id ASC');
	$result = pg_execute($db_connection, 'sqlfetch', array());

	$row = pg_fetch_array($result);
    $obj = new stdClass();
    $obj->id = (int)$row['id'];
    $obj->banner1 = $row['banner1'];
    $obj->banner2 = $row['banner2'];
    $obj->banner3 = $row['banner3'];
    return $obj;

}
function getHomeBanners($db_connection, $objdata) {
    pg_prepare($db_connection, 'sqlfetch', 'SELECT * FROM home_banners ORDER BY id ASC');
	$result = pg_execute($db_connection, 'sqlfetch', array());
	$row = pg_fetch_array($result);
    $obj = new stdClass();
    $obj->id = (int)$row['id'];
    $obj->banner1 = $row['banner1'];
    $obj->banner2 = $row['banner2'];
    $obj->banner3 = $row['banner3'];
    return $obj;
}


?>
