trf_certest/public/userarea/upload_photos_mobile.php
2025-09-20 22:00:16 +02:00

286 lines
9.8 KiB
PHP

<?php
// upload_photos_mobile.php
include('include/headscript.php');
$db = DBHandlerSelect::getInstance();
$pdo = $db->getConnection();
// Verifica che almeno uno degli ID sia passato
$iddatadb = isset($_GET['iddatadb']) && !empty($_GET['iddatadb']) ? intval($_GET['iddatadb']) : null;
$idquotations = isset($_GET['idquotations']) && !empty($_GET['idquotations']) ? intval($_GET['idquotations']) : null;
if (!$iddatadb && !$idquotations) {
die('ID riga o ID quotations non fornito');
}
if ($iddatadb && $idquotations) {
die('Non è possibile specificare sia iddatadb che idquotations');
}
// Verifica che l'utente loggato esista
$stmt = $pdo->prepare("SELECT id FROM auth_users WHERE id = ?");
$stmt->execute([$iduserlogin]);
if (!$stmt->fetch(PDO::FETCH_ASSOC)) {
die('Utente non valido');
}
// Determina quale ID usare e verifica l'esistenza
$paramName = $iddatadb ? 'iddatadb' : 'idquotations';
$paramValue = $iddatadb ?: $idquotations;
$table = $iddatadb ? 'datadb' : 'quotations';
$field = $iddatadb ? 'sample_code' : 'quotation_code';
$stmt = $pdo->prepare("SELECT {$paramName}, {$field} FROM {$table} WHERE {$paramName} = ?");
$stmt->execute([$paramValue]);
$row = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$row) {
die('Riga non trovata');
}
$id = $row[$paramName];
$code = $row[$field] ?? 'Non disponibile';
?>
<!DOCTYPE html>
<html lang="it">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Carica Foto da Mobile</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
<style>
body {
font-family: Arial, sans-serif;
padding: 20px;
text-align: center;
background: #f4f4f4;
}
.upload-area {
border: 2px dashed #ccc;
padding: 20px;
margin: 20px auto;
max-width: 500px;
background: white;
border-radius: 8px;
cursor: pointer;
}
.upload-area.highlight {
border-color: #28a745;
background-color: #e9ecef;
}
.photo-item {
display: flex;
align-items: center;
margin-bottom: 10px;
border-bottom: 1px solid #eee;
padding-bottom: 10px;
max-width: 500px;
margin-left: auto;
margin-right: auto;
}
.photo-item img {
max-width: 100px;
max-height: 100px;
margin-right: 10px;
border-radius: 4px;
}
.loader {
display: none;
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(0, 0, 0, 0.5);
z-index: 1000;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.loader i {
font-size: 40px;
margin-bottom: 10px;
}
.error-message {
color: red;
margin: 10px 0;
display: none;
}
button.delete-photo-btn {
background: none;
border: none;
color: #dc3545;
cursor: pointer;
font-size: 18px;
}
</style>
</head>
<body>
<h2>Carica Foto per ID: <?= htmlspecialchars($id) ?></h2>
<p><strong>Codice:</strong> <?= htmlspecialchars($code) ?></p>
<div class="loader" id="loader">
<div>
<i class="fas fa-spinner fa-spin"></i>
<p>Caricamento in corso...</p>
</div>
</div>
<div class="error-message" id="errorMessage"></div>
<div class="upload-area" id="uploadArea">
<p>Scatta una foto o seleziona immagini</p>
<input type="file" id="photoInput" accept="image/*" capture="camera" multiple style="display: none;">
</div>
<div id="photosList"></div>
<script>
const uploadArea = document.getElementById('uploadArea');
const photoInput = document.getElementById('photoInput');
const photosList = document.getElementById('photosList');
const loader = document.getElementById('loader');
const errorMessage = document.getElementById('errorMessage');
const iddatadb = '<?= $iddatadb ?>';
const idquotations = '<?= $idquotations ?>';
const endpoint = idquotations ? 'load_photo_quotation.php' : 'load_photo.php';
const dataParam = idquotations ? {
idquotations: idquotations
} : {
iddatadb: iddatadb
};
// Carica le foto esistenti all'avvio
loadPhotos();
// Gestione drag-and-drop
uploadArea.addEventListener('dragover', (e) => {
e.preventDefault();
uploadArea.classList.add('highlight');
});
uploadArea.addEventListener('dragleave', () => {
uploadArea.classList.remove('highlight');
});
uploadArea.addEventListener('drop', (e) => {
e.preventDefault();
uploadArea.classList.remove('highlight');
handleFiles(e.dataTransfer.files);
});
// Gestione click sull'area di upload
uploadArea.addEventListener('click', () => photoInput.click());
// Gestione caricamento foto
photoInput.addEventListener('change', () => handleFiles(photoInput.files));
async function handleFiles(files) {
loader.style.display = 'flex';
errorMessage.style.display = 'none';
for (const file of files) {
if (!file.type.startsWith('image/')) {
showError('Per favore, carica solo immagini!');
continue;
}
const formData = new FormData();
formData.append('photo', file);
if (iddatadb) formData.append('iddatadb', iddatadb);
if (idquotations) formData.append('idquotations', idquotations);
try {
const response = await fetch('upload_photo.php', {
method: 'POST',
body: formData
});
const result = await response.json();
if (!result.success) {
showError('Errore durante il caricamento: ' + result.message);
}
} catch (error) {
showError('Errore di rete: ' + error.message);
}
}
loadPhotos();
loader.style.display = 'none';
}
async function loadPhotos() {
loader.style.display = 'flex';
errorMessage.style.display = 'none';
try {
const response = await fetch(`${endpoint}?${new URLSearchParams(dataParam)}`);
const result = await response.json();
photosList.innerHTML = '';
if (result.success && result.photos && result.photos.length > 0) {
for (const photo of result.photos) {
const photoName = photo.split('/').pop();
const photoItem = document.createElement('div');
photoItem.className = 'photo-item';
photoItem.innerHTML = `
<img src="${photo}" alt="${photoName}">
<div style="flex: 1; text-align: left;">
<strong>Nome:</strong> ${photoName}<br>
<strong>Caricata il:</strong> Non disponibile
</div>
<button class="delete-photo-btn" data-photo-path="${photo}">
<i class="fas fa-trash"></i>
</button>`;
photosList.appendChild(photoItem);
}
} else {
photosList.innerHTML = '<p>Nessuna foto presente.</p>';
}
} catch (error) {
showError('Errore durante il caricamento delle foto: ' + error.message);
}
loader.style.display = 'none';
}
function showError(message) {
errorMessage.textContent = message;
errorMessage.style.display = 'block';
setTimeout(() => errorMessage.style.display = 'none', 5000);
}
// Gestione eliminazione foto
photosList.addEventListener('click', async (e) => {
if (e.target.closest('.delete-photo-btn')) {
const button = e.target.closest('.delete-photo-btn');
const photoPath = button.dataset.photoPath;
if (confirm('Sei sicuro di voler eliminare questa foto?')) {
loader.style.display = 'flex';
try {
const response = await fetch('delete_photo.php', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
file_path: photoPath
})
});
const result = await response.json();
if (result.success) {
loadPhotos();
} else {
showError('Errore durante l\'eliminazione: ' + result.message);
}
} catch (error) {
showError('Errore durante l\'eliminazione: ' + error.message);
}
loader.style.display = 'none';
}
}
});
</script>
</body>
</html>