107 lines
4.1 KiB
PHP
107 lines
4.1 KiB
PHP
<?php
|
|
// Database connection
|
|
include('../../Connections/repnew.php');
|
|
// Starts the session to handle session variables
|
|
$conn = new mysqli($servername, $username, $password, $database);
|
|
|
|
// Check the connection
|
|
if ($conn->connect_error) {
|
|
die("Connection failed: " . $conn->connect_error);
|
|
}
|
|
|
|
// Check if POST request was received
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
// Array to collect messages about file processing
|
|
$file_messages = [];
|
|
|
|
// Receive JSON from the laboratory via a field in the form (e.g., 'json_data')
|
|
if (isset($_POST['json_data'])) {
|
|
$json_data = $_POST['json_data'];
|
|
|
|
// Decode JSON for optional validation
|
|
$decoded_data = json_decode($json_data, true);
|
|
|
|
// If the JSON is valid
|
|
if (json_last_error() === JSON_ERROR_NONE) {
|
|
// Check only for the required product_refnumber
|
|
if (!isset($decoded_data['product']) || !is_array($decoded_data['product']) || !isset($decoded_data['product'][0]['products_refnumber'])) {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Missing product reference number."
|
|
]);
|
|
exit;
|
|
}
|
|
|
|
// Generate a UUID to uniquely identify the record
|
|
$uuid = uniqid(); // Alternatively, use UUID() in MySQL
|
|
|
|
// Extract some information from JSON
|
|
$product_refnumber = $decoded_data['product'][0]['products_refnumber'];
|
|
$report_number = $decoded_data['product'][0]['reports'][0]['reportsNumberLab'] ?? null;
|
|
$rating = $decoded_data['product'][0]['reports'][0]['reportsRating'] ?? null;
|
|
$saved_at = date("Y-m-d H:i:s"); // Save date
|
|
|
|
// Query to insert data into the temp_json_queue table
|
|
$stmt = $conn->prepare("INSERT INTO temp_json_queue (uuid, lab_id, json_data) VALUES (?, ?, ?)");
|
|
$lab_id = 1; // Set lab_id to a fixed value for testing purposes
|
|
$stmt->bind_param("sss", $uuid, $lab_id, $json_data);
|
|
|
|
if ($stmt->execute()) {
|
|
// Handle file uploads if they exist
|
|
if (!empty($_FILES)) {
|
|
include('process_files.php'); // Include file processing logic here
|
|
|
|
// Retrieve any messages added in process_files.php for files
|
|
if (!empty($GLOBALS['file_messages'])) {
|
|
$file_messages = $GLOBALS['file_messages'];
|
|
}
|
|
}
|
|
|
|
// Set a session variable to notify the report import
|
|
$_SESSION['new_report'] = [
|
|
'report_number' => $report_number,
|
|
'rating' => $rating,
|
|
'timestamp' => time() // You can use a timestamp to manage the expiration of the notification
|
|
];
|
|
|
|
echo json_encode([
|
|
"status" => "success",
|
|
"message" => "Data successfully saved.",
|
|
"uuid" => $uuid,
|
|
"product_refnumber" => $product_refnumber, // Product number
|
|
"report_number" => $report_number, // Report number
|
|
"rating" => $rating, // Report rating
|
|
"saved_at" => $saved_at, // Save date
|
|
"file_messages" => $file_messages // Include file messages
|
|
]);
|
|
} else {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Failed to save data."
|
|
]);
|
|
}
|
|
|
|
$stmt->close();
|
|
} else {
|
|
// If the JSON is invalid
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Invalid JSON format."
|
|
]);
|
|
}
|
|
} else {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Missing JSON data."
|
|
]);
|
|
}
|
|
} else {
|
|
echo json_encode([
|
|
"status" => "error",
|
|
"message" => "Invalid request method."
|
|
]);
|
|
}
|
|
|
|
// Close the database connection
|
|
$conn->close();
|