<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
include_once '../classes/Logger.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$log = new Logger(basename($_SERVER['PHP_SELF']));
$response = array();
$log->arrayLogger($_POST, "POST Req Data");
$item_per_page = 5;
//item to display per page
if (isset($_POST['page_index'])) {
    $page_start = $_POST['page_index'];
    $page_position = ($page_start - 1) * $item_per_page;
} else {
    $page_position = $utilities->replaceZero($_POST['page_start']);
}
$chef_id = $utilities->replaceDefault($_POST['chef_id']);
$dateTime = $utilities->replaceNow();
//print_r($_POST);
try {
    $query = " SELECT food_item.item_id,\n                        food_item.`name` AS food_name,\n                        food_item.description,\n                        food_item.ingredients,\n                        food_item.preparation_method,\n                        food_item.rating AS food_rating,\n                        category.category_id,\n                        category.category_name,\n                        food_item.price,\n                        currency.currency_id,\n                        currency.currency_name,\n                        currency.currency_symbol,\n                        chef.chef_id,\n                        CONCAT(chef.f_name, ' ', chef.l_name) AS chef_name,\n                        CONCAT(chef.country_code, chef.phone_number) AS chef_phone_number,\n                        chef.image_url AS chef_image,\n                        chef.rating AS chef_rating,\n                        food_item.nutrition,\n                        COALESCE(food_item.food_image_1, '') AS food_image_1,\n                        COALESCE(food_item.food_image_2, '') AS food_image_2,\n                        COALESCE(food_item.food_image_3, '') AS food_image_3,\n                        COALESCE(food_item.food_image_4, '') AS food_image_4,\n                        food_item.log_datetime\n                FROM food_item INNER JOIN category ON food_item.category_id = category.category_id\n                         INNER JOIN currency ON food_item.currency_id = currency.currency_id\n                         INNER JOIN chef ON food_item.chef_id = chef.chef_id ";
    $query .= $chef_id > 0 ? " WHERE food_item.chef_id = {$chef_id} " : " ";
    $query .= " ORDER BY food_name ASC\n                LIMIT {$page_position}, {$item_per_page};";
    //echo $query;
    $log->info("Query:" . $query);
    $statement = $dbConnection->prepare($query);
    $sql = "SELECT * FROM food_item";
    $result = $dbConnection->query($sql);
    $row = $result->fetchAll();
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$name = $utilities->clean($_POST['name']);
$area = $utilities->clean($_POST['area']);
$pincode = $utilities->clean($_POST['pincode']);
$latitude = $utilities->replaceZero($_POST['latitude']);
$longitude = $utilities->replaceZero($_POST['longitude']);
$log_datetime = $utilities->replaceNow();
$kitchenId = -1;
try {
    $insertQuery = "INSERT INTO " . " `kitchen` (name, area, pincode, latitude, longitude, log_datetime) " . " VALUES ('{$name}', '{$area}', '{$pincode}', '{$latitude}', '{$longitude}', '{$log_datetime}')";
    $statement = $dbConnection->prepare($insertQuery);
    try {
        $dbConnection->beginTransaction();
        if ($statement->execute()) {
            $kitchenId = $dbConnection->lastInsertId();
            $dbConnection->commit();
            $response = array('status' => $kitchenId, 'desc' => 'Success');
        } else {
            $status = -99;
            $dbError = $statement->errorInfo();
            $response = array('status' => $status, 'desc' => 'DB error occured' . $dbError[2]);
        }
    } catch (PDOExecption $e) {
        $status = -7;
        $error = "Exception: " . $e->getMessage();
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$user_id = $utilities->replaceZero($_POST['user_id']);
$dateTime = $utilities->replaceNow();
try {
    $query = "SELECT address.address_id,\n                    CONCAT(address.street_address, '; ', address.city, '; ',  address.state, '; ', address.country, '; ') AS address,\n                    address.landmark,\n                    address.pincode,\n                    CONCAT(address.country_code, address.phone_number) AS address_phone_number,\n                    address.is_default,\n                    `user`.user_id,\n                    CONCAT(`user`.f_name, `user`.l_name) AS user_name,\n                    `user`.image_url AS user_image\n            FROM address INNER JOIN `user` ON address.user_id = `user`.user_id\n            WHERE address.user_id  = '{$user_id}'";
    $statement = $dbConnection->prepare($query);
    try {
        if ($statement->execute($bindParams)) {
            $data = $statement->fetchAll(PDO::FETCH_ASSOC);
            $statement->closeCursor();
            $response = array('status' => 0, 'data' => $data, 'desc' => 'success');
        } else {
            $errorCode = -99;
            $dbError = $statement->errorInfo();
            $statement->closeCursor();
            $response = array('status' => $errorCode, 'data' => array(), 'desc' => 'DB error occured' . $dbError[2]);
        }
    } catch (PDOExecption $e) {
        $errorCode = -7;
        $statement->closeCursor();
        $error = "Exception: " . $e->getMessage();
        $response = array('status' => $errorCode, 'data' => array(), 'desc' => 'PDO exception occured' . $error);
    }
} catch (PDOExecption $e) {
    $errorCode = -8;
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$device_imei = $utilities->clean($_POST['deviceIMEI']);
$dateTime = $utilities->replaceNow();
$status = 0;
try {
    $delQuery = "DELETE FROM `push_reg` WHERE device_imei = '{$device_imei}'";
    $statement = $dbConnection->prepare($delQuery);
    $statement->bindParam(":device_imei", $device_imei, PDO::PARAM_STR);
    try {
        $dbConnection->beginTransaction();
        if ($statement->execute()) {
            $dbConnection->commit();
            $response = array('status' => $status, 'desc' => 'Success');
        } else {
            $status = -99;
            $dbError = $statement->errorInfo();
            $response = array('status' => $status, 'desc' => 'DB error occured: ' . $dbError[2]);
        }
    } catch (PDOExecption $e) {
        $status = -7;
        $error = "Exception: " . $e->getMessage();
        $response = array('status' => $status, 'desc' => 'PDO exception occured' . $error);
    }
    $statement->closeCursor();
} catch (PDOExecption $e) {
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$userId = $utilities->clean($_POST['userId']);
$push_reg_token = $utilities->clean($_POST['pushRegToken']);
$device_imei = $utilities->clean($_POST['deviceIMEI']);
$os_id = $utilities->clean($_POST['osId']);
//1 - Android, 2 - iOS
$os_name = $os_id == 1 ? 'Android' : 'iOS';
$dateTime = $utilities->replaceNow();
$pushRegId = -1;
try {
    $queryRegPush = "SELECT COUNT(*) AS isPushReg FROM `push_reg` WHERE device_imei = '{$device_imei}' AND push_reg_token = '{$push_reg_token}';";
    $statement = $dbConnection->prepare($queryRegPush);
    $statement->execute();
    $dataRegPushArray = $statement->fetchAll(PDO::FETCH_ASSOC);
    $statement->closeCursor();
    if ($dataRegPushArray[0]['isPushReg'] == 0) {
        $insertQuery = "INSERT INTO " . " `push_reg` (user_id, push_reg_token, device_imei, os_id, os_name, datetime) " . " VALUES (:user_id, :push_reg_token, :device_imei, :os_id, :os_name, :datetime);";
        $statement = $dbConnection->prepare($insertQuery);
        $statement->bindParam(":user_id", $userId, PDO::PARAM_INT);
        $statement->bindParam(":push_reg_token", $push_reg_token, PDO::PARAM_STR);
        $statement->bindParam(":device_imei", $device_imei, PDO::PARAM_STR);
        $statement->bindParam(":os_id", $os_id, PDO::PARAM_INT);
        $statement->bindParam(":os_name", $os_name, PDO::PARAM_STR);
        $statement->bindParam(":datetime", $dateTime, PDO::PARAM_STR);
        try {
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$item_id = $utilities->replaceZero($_POST['item_id']);
$serving_id = $utilities->replaceZero($_POST['serving_id']);
$available_for = $utilities->replaceZero($_POST['available_for']);
$order_count = $utilities->replaceZero($_POST['order_count']);
$date = $utilities->replaceZero($_POST['date']);
$log_datetime = $utilities->replaceNow();
$itemServingMappingId = -1;
try {
    $insertQuery = "INSERT INTO " . " `item_serving_mapping` (item_id, serving_id, available_for, order_count, date, log_datetime) " . " VALUES ('{$item_id}', '{$serving_id}', '{$available_for}', '{$order_count}', '{$date}', '{$log_datetime}')";
    $statement = $dbConnection->prepare($insertQuery);
    try {
        $dbConnection->beginTransaction();
        if ($statement->execute()) {
            $itemServingMappingId = $dbConnection->lastInsertId();
            $dbConnection->commit();
            $response = array('status' => $itemServingMappingId, 'desc' => 'Success');
        } else {
            $status = -99;
            $dbError = $statement->errorInfo();
            $response = array('status' => $status, 'desc' => 'DB error occured' . $dbError[2]);
        }
    } catch (PDOExecption $e) {
        $status = -7;
        $error = "Exception: " . $e->getMessage();
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$isUpdate = $utilities->replaceZero($_POST['is_update_address']);
$address_id = $utilities->clean($_POST['address_id']);
$street_address = $utilities->clean($_POST['street_address']);
$landmark = $utilities->clean($_POST['landmark']);
$city = $utilities->clean($_POST['city']);
$state = $utilities->clean($_POST['state']);
$pincode = $utilities->clean($_POST['pincode']);
$phone_number = $utilities->clean($_POST['phone_number']);
$is_default = $utilities->replaceZero($_POST['is_default']);
$user_id = $utilities->clean($_POST['user_id']);
$log_datetime = $utilities->replaceNow();
$addressId = -1;
try {
    if ($isUpdate == 0) {
        $query = "INSERT INTO " . " `address` (street_address, landmark, city, state, country, pincode, phone_number, country_code, is_default, user_id, log_datetime) " . " VALUES ('{$street_address}', '{$landmark}', '{$city}','{$state}', 'country', '{$pincode}', '{$phone_number}', '+91', '{$is_default}', '{$user_id}' ,'{$log_datetime}')";
    } else {
        $query = "UPDATE `address` SET" . " street_address = '{$street_address}', " . " landmark = '{$landmark}', " . " city = '{$city}', " . " state = '{$state}', " . " country = '{$country}', " . " pincode = '{$pincode}', " . " phone_number = '{$phone_number}', " . " country_code = '+91', " . " is_default = '{$is_default}', " . " log_datetime = '{$log_datetime}'  " . " WHERE address_id = '{$address_id}';";
    }
    $statement = $dbConnection->prepare($query);
    try {
        $dbConnection->beginTransaction();
        if ($statement->execute()) {
            $addressId = $isUpdate == 0 ? $dbConnection->lastInsertId() : $address_id;
            $dbConnection->commit();
<?php

include_once '../classes/PDOExt.php';
include_once '../classes/Utilities.php';
$dbConnection = new PDOExt();
$utilities = new Utilities();
$response = array();
$user_id = $utilities->replaceZero($_POST['user_id']);
$item_d = $utilities->replaceZero($_POST['item_d']);
$rating = $utilities->replaceZero($_POST['rating']);
$log_datetime = $utilities->replaceNow();
$user_item_rating = -1;
try {
    $insertQuery = "INSERT INTO " . " `user_item_rating` (user_id, item_d, rating, log_datetime) " . " VALUES ('{$user_id}', '{$item_d}', '{$rating}', '{$log_datetime}')";
    $statement = $dbConnection->prepare($insertQuery);
    try {
        $dbConnection->beginTransaction();
        if ($statement->execute()) {
            $user_item_rating = $dbConnection->lastInsertId();
            $dbConnection->commit();
            $response = array('status' => $user_item_rating, 'desc' => 'Success');
        } else {
            $status = -99;
            $dbError = $statement->errorInfo();
            $response = array('status' => $status, 'desc' => 'DB error occured' . $dbError[2]);
        }
    } catch (PDOExecption $e) {
        $status = -7;
        $error = "Exception: " . $e->getMessage();
        $response = array('status' => $status, 'desc' => 'PDO exception occured' . $error);
    }