Files
zibo-dashboard/public/userarea/save_matrice.php
T
2026-10-29 17:22:27 +01:00

112 lines
3.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
ob_start();
include('include/headscript.php');
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
function jsonOut($success, $message = '', $extra = [])
{
if (ob_get_length()) ob_clean();
header('Content-Type: application/json; charset=utf-8');
echo json_encode(array_merge(['success' => $success, 'message' => $message], $extra));
exit;
}
$id = (int)($_POST['id'] ?? 0);
$nome = trim($_POST['nome'] ?? '');
$cliente = trim($_POST['cliente'] ?? '');
$data = trim($_POST['data'] ?? '');
$difficolta = trim($_POST['difficolta'] ?? '');
$removePhoto = ($_POST['remove_photo'] ?? '0') === '1';
if ($nome === '') {
jsonOut(false, 'Il nome matrice è obbligatorio');
}
$data = ($data !== '' && preg_match('/^\d{4}-\d{2}-\d{2}$/', $data)) ? $data : null;
$cliente = $cliente !== '' ? $cliente : null;
// difficoltà: vuota = non indicata, altrimenti intero 1..10
if ($difficolta === '') {
$difficolta = null;
} elseif (ctype_digit($difficolta) && (int)$difficolta >= 1 && (int)$difficolta <= 10) {
$difficolta = (int)$difficolta;
} else {
jsonOut(false, 'Difficoltà non valida (valori ammessi: 1–10)');
}
$photoDir = __DIR__ . '/photos/matrici/';
if (!is_dir($photoDir)) {
mkdir($photoDir, 0775, true);
}
// Validazione file (se presente)
$hasUpload = isset($_FILES['photo']) && $_FILES['photo']['error'] !== UPLOAD_ERR_NO_FILE;
$ext = '';
if ($hasUpload) {
if ($_FILES['photo']['error'] !== UPLOAD_ERR_OK) {
jsonOut(false, 'Errore upload foto (codice ' . $_FILES['photo']['error'] . ')');
}
if ($_FILES['photo']['size'] > 10 * 1024 * 1024) {
jsonOut(false, 'La foto supera i 10 MB');
}
$ext = strtolower(pathinfo($_FILES['photo']['name'], PATHINFO_EXTENSION));
if (!in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp'], true)) {
jsonOut(false, 'Formato foto non consentito (jpg, jpeg, png, gif, webp)');
}
if (@getimagesize($_FILES['photo']['tmp_name']) === false) {
jsonOut(false, 'Il file caricato non è un\'immagine valida');
}
}
try {
$pdo->beginTransaction();
$oldPhoto = null;
if ($id > 0) {
$st = $pdo->prepare("SELECT photo FROM matrice WHERE id = ?");
$st->execute([$id]);
$row = $st->fetch(PDO::FETCH_ASSOC);
if (!$row) {
$pdo->rollBack();
jsonOut(false, 'Matrice non trovata');
}
$oldPhoto = $row['photo'];
$st = $pdo->prepare("UPDATE matrice SET nome = ?, cliente = ?, data_produzione = ?, difficolta = ? WHERE id = ?");
$st->execute([$nome, $cliente, $data, $difficolta, $id]);
} else {
$st = $pdo->prepare("INSERT INTO matrice (nome, cliente, data_produzione, difficolta) VALUES (?, ?, ?, ?)");
$st->execute([$nome, $cliente, $data, $difficolta]);
$id = (int)$pdo->lastInsertId();
}
// Nuova foto
if ($hasUpload) {
$newName = 'matrice_' . $id . '_' . time() . '.' . $ext;
if (!move_uploaded_file($_FILES['photo']['tmp_name'], $photoDir . $newName)) {
$pdo->rollBack();
jsonOut(false, 'Impossibile salvare la foto sul server');
}
$pdo->prepare("UPDATE matrice SET photo = ? WHERE id = ?")->execute([$newName, $id]);
if ($oldPhoto && is_file($photoDir . basename($oldPhoto))) {
@unlink($photoDir . basename($oldPhoto));
}
} elseif ($removePhoto && $oldPhoto) {
// Rimozione foto
$pdo->prepare("UPDATE matrice SET photo = NULL WHERE id = ?")->execute([$id]);
if (is_file($photoDir . basename($oldPhoto))) {
@unlink($photoDir . basename($oldPhoto));
}
}
$pdo->commit();
jsonOut(true, 'Matrice salvata correttamente', ['id' => $id]);
} catch (Throwable $e) {
if ($pdo->inTransaction()) $pdo->rollBack();
jsonOut(false, 'Errore: ' . $e->getMessage());
}