added Files JSON import

This commit is contained in:
2024-11-05 16:18:55 +01:00
parent c6f01119de
commit 8ab9f5f948
25 changed files with 314 additions and 95 deletions
+19 -8
View File
@@ -9,10 +9,11 @@ if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); die("Connection failed: " . $conn->connect_error);
} }
// Check if JSON was received via POST // Check if POST request was received
if ($_SERVER['REQUEST_METHOD'] === 'POST') { if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Receive JSON from the laboratory // Receive JSON from the laboratory via a field in the form (e.g., 'json_data')
$json_data = file_get_contents('php://input'); if (isset($_POST['json_data'])) {
$json_data = $_POST['json_data'];
// Decode JSON for optional validation // Decode JSON for optional validation
$decoded_data = json_decode($json_data, true); $decoded_data = json_decode($json_data, true);
@@ -38,11 +39,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$stmt->execute(); $stmt->execute();
$result = $stmt->get_result(); $result = $stmt->get_result();
// Controllo se un laboratorio valido è stato trovato con `reflab` e `api_key` // Check if a valid laboratory was found with `reflab` and `api_key`
if ($result->num_rows > 0) { if ($result->num_rows > 0) {
$row = $result->fetch_assoc(); $row = $result->fetch_assoc();
// Verifica lo stato del laboratorio // Verify the status of the laboratory
if ($row['status'] !== 'active') { if ($row['status'] !== 'active') {
echo json_encode([ echo json_encode([
"status" => "error", "status" => "error",
@@ -51,7 +52,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
exit; exit;
} }
// Verifica la chiave segreta utilizzando `password_verify` // Verify the secret key using `password_verify`
if (!password_verify($secret_key, $row['api_secret'])) { if (!password_verify($secret_key, $row['api_secret'])) {
echo json_encode([ echo json_encode([
"status" => "error", "status" => "error",
@@ -60,7 +61,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
exit; exit;
} }
} else { } else {
// Verifica se il `reflab` è valido, ma l'`api_key` non corrisponde // Check if the `reflab` is valid, but the `api_key` doesn't match
$query = "SELECT * FROM laboratories WHERE reflab = ?"; $query = "SELECT * FROM laboratories WHERE reflab = ?";
$stmt = $conn->prepare($query); $stmt = $conn->prepare($query);
$stmt->bind_param("s", $reflab); $stmt->bind_param("s", $reflab);
@@ -81,7 +82,6 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
exit; exit;
} }
// Generate a UUID to uniquely identify the record // Generate a UUID to uniquely identify the record
$uuid = uniqid(); // Alternatively, use UUID() in MySQL $uuid = uniqid(); // Alternatively, use UUID() in MySQL
@@ -105,6 +105,11 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$stmt->bind_param("sss", $uuid, $lab_id, $json_data); $stmt->bind_param("sss", $uuid, $lab_id, $json_data);
if ($stmt->execute()) { if ($stmt->execute()) {
// Handle file uploads if they exist
if (!empty($_FILES)) {
include('process_files.php'); // Include file processing logic here
}
// Set a session variable to notify the report import // Set a session variable to notify the report import
$_SESSION['new_report'] = [ $_SESSION['new_report'] = [
'report_number' => $report_number, 'report_number' => $report_number,
@@ -136,6 +141,12 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
"message" => "Invalid JSON format." "message" => "Invalid JSON format."
]); ]);
} }
} else {
echo json_encode([
"status" => "error",
"message" => "Missing JSON data."
]);
}
} else { } else {
echo json_encode([ echo json_encode([
"status" => "error", "status" => "error",
@@ -0,0 +1,56 @@
<?php
// Check if there are any files uploaded
if (!empty($_FILES)) {
// Define the directory where files will be stored
$upload_dir = '../report_files/';
// Ensure the upload directory exists
if (!is_dir($upload_dir)) {
mkdir($upload_dir, 0755, true);
}
foreach ($_FILES as $key => $file) {
if ($file['error'] === UPLOAD_ERR_OK) {
// Get original filename and generate a stored filename with UUID as a prefix
$original_filename = $file['name'];
$stored_filename = $uuid . '_' . $original_filename; // Add UUID as prefix
// Define the full path where the file will be saved
$filepath = $upload_dir . $stored_filename;
// Move the uploaded file to the specified directory
if (move_uploaded_file($file['tmp_name'], $filepath)) {
// Get the associated comment for the file if it exists
$comment_key = str_replace('file', 'comment', $key);
$file_comment = $_POST[$comment_key] ?? null;
// Insert file information into the database
$stmt = $conn->prepare("INSERT INTO report_files (uuid, original_filename, stored_filename, filepath, file_comment) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $uuid, $original_filename, $stored_filename, $filepath, $file_comment);
if (!$stmt->execute()) {
echo json_encode([
"status" => "error",
"message" => "Failed to save file information for $original_filename."
]);
continue;
}
echo json_encode([
"status" => "success",
"message" => "File $original_filename uploaded and information saved."
]);
} else {
echo json_encode([
"status" => "error",
"message" => "Failed to move file $original_filename."
]);
}
} else {
echo json_encode([
"status" => "error",
"message" => "Error uploading file $original_filename. Error code: " . $file['error']
]);
}
}
}
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 MiB

+133
View File
@@ -41,6 +41,23 @@ $stmtParts = $conn->prepare($queryPartsAndResults);
$stmtParts->bind_param("i", $idreports); $stmtParts->bind_param("i", $idreports);
$stmtParts->execute(); $stmtParts->execute();
$partsAndResults = $stmtParts->get_result(); $partsAndResults = $stmtParts->get_result();
// Query per ottenere i file associati al report
$queryFiles = "
SELECT original_filename, stored_filename, file_comment, filepath
FROM report_files
WHERE uuid = ?
AND (file_comment = 'report' OR file_comment = 'main_product_image')";
$stmtFiles = $conn->prepare($queryFiles);
$stmtFiles->bind_param("s", $reportDetails['importcode']);
$stmtFiles->execute();
$filesResult = $stmtFiles->get_result();
$files = [];
while ($fileRow = $filesResult->fetch_assoc()) {
$files[$fileRow['file_comment']] = $fileRow;
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
@@ -176,12 +193,128 @@ $partsAndResults = $stmtParts->get_result();
<td><strong>Color:</strong></td> <td><strong>Color:</strong></td>
<td><?php echo $reportDetails['products_color']; ?></td> <td><?php echo $reportDetails['products_color']; ?></td>
</tr> </tr>
<tr>
<td><strong>PDF Report:</strong></td>
<td>
<?php if (isset($files['report'])): ?>
<a href="<?php echo htmlspecialchars('../report_files/' . $files['report']['stored_filename']); ?>" target="_blank">
<i class="bx bxs-file-pdf" style="font-size: 24px; color: #d9534f;"></i> View Report
</a>
<?php else: ?>
<span>No PDF available</span>
<?php endif; ?>
</td>
</tr>
<tr>
<td><strong>Product Image:</strong></td>
<td>
<?php if (isset($files['main_product_image'])): ?>
<a href="<?php echo htmlspecialchars('../report_files/' . $files['main_product_image']['stored_filename']); ?>"
target="_blank" onclick="openModal(event, this); return false;">
<img src="<?php echo htmlspecialchars('../report_files/' . $files['main_product_image']['stored_filename']); ?>"
alt="Product Image" style="max-width: 150px; max-height: 150px;">
</a>
<?php else: ?>
<span>No image available</span>
<?php endif; ?>
</td>
</tr>
<!-- Modal container for enlarged image -->
<div id="imageModal" style="display:none; position:fixed; z-index:1000; left:0; top:0; width:100%; height:100%; background-color:rgba(0, 0, 0, 0.8); justify-content:center; align-items:center;" onclick="closeModal(event)">
<img id="modalImage" style="max-width:90%; max-height:90%;">
</div>
</table> </table>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<script>
function openModal(event, link) {
event.preventDefault();
var modal = document.getElementById('imageModal');
var modalImage = document.getElementById('modalImage');
modalImage.src = link.href;
modal.style.display = 'flex';
}
function closeModal(event) {
var modal = document.getElementById('imageModal');
// Chiudi il modal solo se il clic è al di fuori dell'immagine
if (event.target.id === 'imageModal') {
modal.style.display = 'none';
}
}
</script>
<?php
// Query per ottenere i file aggiuntivi non etichettati come 'report' o 'main_product_image'
$queryAdditionalFiles = "SELECT * FROM report_files WHERE uuid = ? AND file_comment NOT IN ('report', 'main_product_image')";
$stmtAdditionalFiles = $conn->prepare($queryAdditionalFiles);
$stmtAdditionalFiles->bind_param("s", $reportDetails['importcode']);
$stmtAdditionalFiles->execute();
$additionalFiles = $stmtAdditionalFiles->get_result();
?>
<?php if ($additionalFiles->num_rows > 0): ?>
<div class="card mt-4">
<div class="card-body">
<h5 class="header-title pb-3 mt-0">Additional Files</h5>
<!-- Button to toggle collapsible area -->
<button class="btn btn-primary mb-3" type="button" data-toggle="collapse" data-target="#additionalFilesSection">
Show/Hide Additional Files
</button>
<!-- Collapsible area for additional files -->
<div id="additionalFilesSection" class="collapse">
<table class="table table-bordered">
<thead class="thead-light">
<tr>
<th>File Preview</th>
<th>Comment</th>
</tr>
</thead>
<tbody>
<?php while ($file = $additionalFiles->fetch_assoc()): ?>
<?php
$filePath = "../report_files/" . $file['stored_filename'];
$isImage = preg_match('/\.(jpg|jpeg|png|gif)$/i', $file['stored_filename']);
$isPDF = preg_match('/\.pdf$/i', $file['stored_filename']);
?>
<tr>
<td>
<?php if ($isImage): ?>
<!-- Thumbnail with click to enlarge -->
<a href="<?php echo htmlspecialchars($filePath); ?>" onclick="openModal(event, this)">
<img src="<?php echo htmlspecialchars($filePath); ?>" alt="Additional File" style="width:50px; height:auto;">
</a>
<?php elseif ($isPDF): ?>
<!-- PDF icon with link -->
<a href="<?php echo htmlspecialchars($filePath); ?>" target="_blank">
<i class="fas fa-file-pdf" style="font-size: 24px; color: #d9534f;"></i>
</a>
<?php else: ?>
<!-- Generic file icon with link -->
<a href="<?php echo htmlspecialchars($filePath); ?>" target="_blank">
<i class="fas fa-file-alt" style="font-size: 24px; color: #5a5a5a;"></i>
</a>
<?php endif; ?>
</td>
<td><?php echo htmlspecialchars($file['file_comment'], ENT_QUOTES, 'UTF-8'); ?></td>
</tr>
<?php endwhile; ?>
</tbody>
</table>
</div>
</div>
</div>
<?php endif; ?>
<!-- Filtri --> <!-- Filtri -->
<!-- Aggiungi questo filtro sopra la tabella --> <!-- Aggiungi questo filtro sopra la tabella -->
Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 56 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 162 KiB

@@ -347,6 +347,8 @@ $productBySupplierQuery = "
LIMIT 30 LIMIT 30
"; ";
$productBySupplierResult = $conn->query($productBySupplierQuery); $productBySupplierResult = $conn->query($productBySupplierQuery);
$productBySupplier = []; $productBySupplier = [];
while ($row = $productBySupplierResult->fetch_assoc()) { while ($row = $productBySupplierResult->fetch_assoc()) {
@@ -355,6 +357,21 @@ while ($row = $productBySupplierResult->fetch_assoc()) {
'totalProducts' => $row['totalProducts'] 'totalProducts' => $row['totalProducts']
]; ];
} }
$productDropdownQuery = "
SELECT DISTINCT p.namesupplier AS supplier
FROM products p
WHERE p.namesupplier IS NOT NULL
ORDER BY p.namesupplier ASC
";
$productDropdownResult = $conn->query($productDropdownQuery);
$productDropdown = [];
while ($row = $productDropdownResult->fetch_assoc()) {
$productDropdown[] = [
'supplier' => $row['supplier']
];
}
// refNumbers // refNumbers
$refNumbersQuery = " $refNumbersQuery = "
SELECT p.products_refnumber AS refNumber SELECT p.products_refnumber AS refNumber
@@ -514,6 +531,7 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
'productsSeasons' => $productsSeasons, 'productsSeasons' => $productsSeasons,
'ageRange' => $ageRange, 'ageRange' => $ageRange,
'labName' => $labName, 'labName' => $labName,
'productDropdown' => $productDropdown,
'tesType' => $tesType, 'tesType' => $tesType,
'numberLabs' => $numberLabs, 'numberLabs' => $numberLabs,
'failedAnalytes' => $failedAnalytes, 'failedAnalytes' => $failedAnalytes,
+2 -1
View File
@@ -452,11 +452,12 @@ include('parsedatachart.php');
<div class="col-md-4"> <div class="col-md-4">
<label for="supplierFilter">Supplier</label> <label for="supplierFilter">Supplier</label>
<select id="supplierFilter" class="form-control select2" multiple> <select id="supplierFilter" class="form-control select2" multiple>
<?php foreach ($productBySupplier as $supplier): ?> <?php foreach ($productDropdown as $supplier): ?>
<option value="<?php echo $supplier['supplier']; ?>"><?php echo $supplier['supplier']; ?></option> <option value="<?php echo $supplier['supplier']; ?>"><?php echo $supplier['supplier']; ?></option>
<?php endforeach; ?> <?php endforeach; ?>
</select> </select>
</div> </div>
</div> </div>
<hr> <hr>
<!-- Active Filters Display --> <!-- Active Filters Display -->