fixed certificate
This commit is contained in:
@@ -30,10 +30,17 @@ if (
|
||||
|
||||
$file = $_FILES['fileToUpload'];
|
||||
$documentDescription = trim($_POST['documentDescription'] ?? '');
|
||||
$expiryDate = trim($_POST['expiryDate'] ?? '');
|
||||
$expiryInput = trim($_POST['expiryDate'] ?? '');
|
||||
|
||||
if ($documentDescription === '' || $expiryDate === '') {
|
||||
// Conversione GG/MM/AAAA -> AAAA-MM-GG per il database
|
||||
$expiryObj = DateTime::createFromFormat('!d/m/Y', $expiryInput);
|
||||
$expiryValid = $expiryObj && $expiryObj->format('d/m/Y') === $expiryInput;
|
||||
$expiryDate = $expiryValid ? $expiryObj->format('Y-m-d') : '';
|
||||
|
||||
if ($documentDescription === '' || $expiryInput === '') {
|
||||
$uploadFeedback = ['type' => 'error', 'text' => 'Descrizione e data di scadenza sono obbligatorie.'];
|
||||
} elseif (!$expiryValid) {
|
||||
$uploadFeedback = ['type' => 'error', 'text' => 'Data di scadenza non valida. Usa il formato GG/MM/AAAA.'];
|
||||
} elseif ($file['size'] > $maxBytes) {
|
||||
$uploadFeedback = ['type' => 'error', 'text' => 'Il file supera la dimensione massima di 16 MB.'];
|
||||
} else {
|
||||
@@ -110,12 +117,33 @@ $documents = $stmtDocs->fetchAll();
|
||||
|
||||
<script>
|
||||
$(function() {
|
||||
$.datepicker.regional['it'] = {
|
||||
closeText: 'Chiudi',
|
||||
prevText: '<Prec',
|
||||
nextText: 'Succ>',
|
||||
currentText: 'Oggi',
|
||||
monthNames: ['Gennaio', 'Febbraio', 'Marzo', 'Aprile', 'Maggio', 'Giugno', 'Luglio', 'Agosto', 'Settembre', 'Ottobre', 'Novembre', 'Dicembre'],
|
||||
monthNamesShort: ['Gen', 'Feb', 'Mar', 'Apr', 'Mag', 'Giu', 'Lug', 'Ago', 'Set', 'Ott', 'Nov', 'Dic'],
|
||||
dayNames: ['Domenica', 'Lunedì', 'Martedì', 'Mercoledì', 'Giovedì', 'Venerdì', 'Sabato'],
|
||||
dayNamesShort: ['Dom', 'Lun', 'Mar', 'Mer', 'Gio', 'Ven', 'Sab'],
|
||||
dayNamesMin: ['Do', 'Lu', 'Ma', 'Me', 'Gi', 'Ve', 'Sa'],
|
||||
weekHeader: 'Sm',
|
||||
firstDay: 1,
|
||||
isRTL: false
|
||||
};
|
||||
$.datepicker.setDefaults($.datepicker.regional['it']);
|
||||
|
||||
$("#expiryDate").datepicker({
|
||||
dateFormat: "yy-mm-dd",
|
||||
dateFormat: "dd/mm/yy",
|
||||
changeYear: true,
|
||||
changeMonth: true,
|
||||
minDate: 0
|
||||
});
|
||||
|
||||
// Punto 3: propone come scadenza oggi + 1 anno (modificabile)
|
||||
if (!$("#expiryDate").val()) {
|
||||
$("#expiryDate").datepicker("setDate", "+1y");
|
||||
}
|
||||
});
|
||||
|
||||
function confirmDeleteCertificate(id) {
|
||||
@@ -137,6 +165,9 @@ $documents = $stmtDocs->fetchAll();
|
||||
|
||||
function handleFileChange(input) {
|
||||
var label = document.getElementById('fileChosenLabel');
|
||||
var btn = document.querySelector('.btn-save');
|
||||
label.style.color = '';
|
||||
|
||||
if (!input.files.length) {
|
||||
label.textContent = 'Nessun file selezionato';
|
||||
return;
|
||||
@@ -144,6 +175,7 @@ $documents = $stmtDocs->fetchAll();
|
||||
var f = input.files[0];
|
||||
var allowed = ['application/pdf', 'image/jpeg', 'image/png'];
|
||||
var maxBytes = 16 * 1024 * 1024;
|
||||
|
||||
if (allowed.indexOf(f.type) === -1) {
|
||||
label.textContent = 'Formato non valido: usa PDF, JPG o PNG';
|
||||
label.style.color = '#dc3545';
|
||||
@@ -156,8 +188,62 @@ $documents = $stmtDocs->fetchAll();
|
||||
input.value = '';
|
||||
return;
|
||||
}
|
||||
label.textContent = f.name;
|
||||
label.style.color = '#198754';
|
||||
|
||||
// PDF o immagini già leggere: nessuna compressione
|
||||
if (f.type === 'application/pdf' || f.size < 800 * 1024) {
|
||||
label.textContent = f.name;
|
||||
label.style.color = '#198754';
|
||||
return;
|
||||
}
|
||||
|
||||
label.textContent = 'Ottimizzazione immagine in corso...';
|
||||
btn.disabled = true;
|
||||
|
||||
compressImage(f, 1800, 0.8).then(function(blob) {
|
||||
if (blob.size >= f.size) throw new Error('nessun guadagno');
|
||||
var newName = f.name.replace(/\.[^.]+$/, '') + '.jpg';
|
||||
var newFile = new File([blob], newName, {
|
||||
type: 'image/jpeg'
|
||||
});
|
||||
var dt = new DataTransfer();
|
||||
dt.items.add(newFile);
|
||||
input.files = dt.files;
|
||||
label.textContent = newName + ' (' + Math.round(blob.size / 1024) + ' KB, ottimizzata)';
|
||||
}).catch(function() {
|
||||
// Browser vecchio o errore: si carica l'originale
|
||||
label.textContent = f.name;
|
||||
}).finally(function() {
|
||||
label.style.color = '#198754';
|
||||
btn.disabled = false;
|
||||
});
|
||||
}
|
||||
|
||||
function compressImage(file, maxSide, quality) {
|
||||
return new Promise(function(resolve, reject) {
|
||||
var url = URL.createObjectURL(file);
|
||||
var img = new Image();
|
||||
img.onload = function() {
|
||||
var scale = Math.min(1, maxSide / Math.max(img.naturalWidth, img.naturalHeight));
|
||||
var w = Math.round(img.naturalWidth * scale);
|
||||
var h = Math.round(img.naturalHeight * scale);
|
||||
var canvas = document.createElement('canvas');
|
||||
canvas.width = w;
|
||||
canvas.height = h;
|
||||
var ctx = canvas.getContext('2d');
|
||||
ctx.fillStyle = '#fff'; // sfondo bianco per PNG trasparenti
|
||||
ctx.fillRect(0, 0, w, h);
|
||||
ctx.drawImage(img, 0, 0, w, h);
|
||||
URL.revokeObjectURL(url);
|
||||
canvas.toBlob(function(b) {
|
||||
b ? resolve(b) : reject();
|
||||
}, 'image/jpeg', quality);
|
||||
};
|
||||
img.onerror = function() {
|
||||
URL.revokeObjectURL(url);
|
||||
reject();
|
||||
};
|
||||
img.src = url;
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
@@ -374,7 +460,7 @@ $documents = $stmtDocs->fetchAll();
|
||||
<?php foreach ($documents as $document) : ?>
|
||||
<tr>
|
||||
<td><?php echo e($document['documentdescription']); ?></td>
|
||||
<td><?php echo e($document['expirydatedocument']); ?></td>
|
||||
<td><?php echo e(date('d/m/Y', strtotime($document['expirydatedocument']))); ?></td>
|
||||
<td>
|
||||
<a class="doc-link" href="user/document/<?php echo e(rawurlencode($document['filenamedocument'])); ?>" target="_blank" rel="noopener">
|
||||
<i class="fas fa-file-arrow-down"></i> Apri
|
||||
@@ -416,7 +502,7 @@ $documents = $stmtDocs->fetchAll();
|
||||
<div class="col-md-6">
|
||||
<div class="form-field">
|
||||
<label for="expiryDate">Data di scadenza</label>
|
||||
<input type="text" id="expiryDate" class="form-control" name="expiryDate" placeholder="AAAA-MM-GG" required>
|
||||
<input type="text" id="expiryDate" class="form-control" name="expiryDate" placeholder="GG/MM/AAAA" inputmode="none" autocomplete="off" required>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user