471 lines
20 KiB
PHP
471 lines
20 KiB
PHP
<?php
|
|
require_once('include/headscript.php');
|
|
|
|
/**
|
|
* Modulo Eventi - gestione Sedi (event_locations): elenco, aggiungi, modifica,
|
|
* elimina e "imposta come predefinita".
|
|
* - event_locations.php -> elenco + form nuovo
|
|
* - event_locations.php?edit=ID -> form in modifica
|
|
* - event_locations.php?delete=ID -> elimina (con conferma via modale)
|
|
* - event_locations.php?makedefault=ID -> imposta predefinita
|
|
* Il salvataggio (insert/update) passa da event_locations_save.php.
|
|
*/
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
function e($value): string
|
|
{
|
|
return htmlspecialchars((string) $value, ENT_QUOTES, 'UTF-8');
|
|
}
|
|
|
|
// Azioni inline (prima di ogni output)
|
|
if (isset($_GET['delete'])) {
|
|
$delId = (int) $_GET['delete'];
|
|
$pdo->prepare("DELETE FROM event_locations WHERE id = ?")->execute([$delId]);
|
|
header('Location: event_locations.php?message=deleted');
|
|
exit();
|
|
}
|
|
if (isset($_GET['makedefault'])) {
|
|
$defId = (int) $_GET['makedefault'];
|
|
// Una sola predefinita: Y su questa, N su tutte le altre
|
|
$pdo->prepare("UPDATE event_locations SET is_default = IF(id = ?, 'Y', 'N'), updated_at = NOW()")->execute([$defId]);
|
|
header('Location: event_locations.php?message=default');
|
|
exit();
|
|
}
|
|
|
|
$editId = isset($_GET['edit']) ? (int) $_GET['edit'] : 0;
|
|
$edit = null;
|
|
if ($editId > 0) {
|
|
$st = $pdo->prepare("SELECT * FROM event_locations WHERE id = ?");
|
|
$st->execute([$editId]);
|
|
$edit = $st->fetch(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
$tableMissing = false;
|
|
$locations = [];
|
|
try {
|
|
$locations = $pdo->query("SELECT * FROM event_locations ORDER BY is_default DESC, name ASC")->fetchAll(PDO::FETCH_ASSOC);
|
|
} catch (Throwable $ex) {
|
|
$tableMissing = true;
|
|
}
|
|
|
|
$message = $_GET['message'] ?? '';
|
|
|
|
$val = [
|
|
'id' => $edit['id'] ?? 0,
|
|
'name' => $edit['name'] ?? '',
|
|
'address' => $edit['address'] ?? '',
|
|
'lat' => $edit['lat'] ?? '',
|
|
'lng' => $edit['lng'] ?? '',
|
|
'is_default' => ($edit['is_default'] ?? 'N') === 'Y',
|
|
];
|
|
$isEdit = $editId > 0 && $edit;
|
|
?>
|
|
<!doctype html>
|
|
<html lang="it">
|
|
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<title>YogiBook - Sedi</title>
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<link rel="shortcut icon" href="assets/images/favicon.ico">
|
|
<link href="assets/css/bootstrap.min.css" id="bootstrap-style" rel="stylesheet" type="text/css" />
|
|
<link href="assets/css/icons.min.css" rel="stylesheet" type="text/css" />
|
|
<link href="assets/css/app.min.css" id="app-style" rel="stylesheet" type="text/css" />
|
|
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
|
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
|
|
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
|
|
|
|
<style>
|
|
.admin-card {
|
|
border: none;
|
|
border-radius: 14px;
|
|
box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
|
|
}
|
|
|
|
.admin-card .card-body {
|
|
padding: 26px 30px;
|
|
}
|
|
|
|
.section-title {
|
|
font-size: 15px;
|
|
font-weight: 700;
|
|
letter-spacing: 0.04em;
|
|
text-transform: uppercase;
|
|
color: #6b7280;
|
|
margin-bottom: 4px;
|
|
}
|
|
|
|
.section-lead {
|
|
color: #9ca3af;
|
|
font-size: 14px;
|
|
margin-bottom: 22px;
|
|
}
|
|
|
|
.form-label {
|
|
font-weight: 600;
|
|
font-size: 14px;
|
|
color: #374151;
|
|
}
|
|
|
|
.form-control {
|
|
border-radius: 8px;
|
|
border: 1px solid #e2e4e9;
|
|
padding: 10px 12px;
|
|
}
|
|
|
|
.form-control:focus {
|
|
border-color: #1ebf73;
|
|
box-shadow: 0 0 0 3px rgba(30, 191, 115, 0.12);
|
|
}
|
|
|
|
.hint {
|
|
color: #9ca3af;
|
|
font-size: 12px;
|
|
margin-top: 4px;
|
|
}
|
|
|
|
.btn-geocode {
|
|
border: 1px solid #e2e4e9;
|
|
background: #fff;
|
|
border-radius: 8px;
|
|
padding: 10px 14px;
|
|
font-weight: 600;
|
|
font-size: 13px;
|
|
color: #2f7be7;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.btn-geocode:hover {
|
|
background: #f3f4f6;
|
|
}
|
|
|
|
.coord-flash {
|
|
border-color: #1ebf73 !important;
|
|
box-shadow: 0 0 0 3px rgba(30, 191, 115, 0.18) !important;
|
|
transition: box-shadow .2s;
|
|
}
|
|
|
|
.loc-table thead th {
|
|
font-size: 12px;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.03em;
|
|
color: #6b7280;
|
|
border-bottom: 2px solid #eef0f3;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.loc-table td {
|
|
vertical-align: middle;
|
|
}
|
|
|
|
.loc-name {
|
|
font-weight: 600;
|
|
color: #1f2937;
|
|
}
|
|
|
|
.default-badge {
|
|
display: inline-block;
|
|
font-size: 12px;
|
|
font-weight: 700;
|
|
padding: 3px 10px;
|
|
border-radius: 20px;
|
|
background: #e7f7ee;
|
|
color: #1a7f52;
|
|
}
|
|
|
|
.btn-act {
|
|
border: 1px solid #e2e4e9;
|
|
background: #fff;
|
|
border-radius: 8px;
|
|
padding: 6px 12px;
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
color: #374151;
|
|
cursor: pointer;
|
|
text-decoration: none;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.btn-act:hover {
|
|
background: #f3f4f6;
|
|
color: #111827;
|
|
}
|
|
|
|
.btn-act.danger {
|
|
color: #c0392b;
|
|
border-color: #f2c9c4;
|
|
}
|
|
|
|
.btn-act.danger:hover {
|
|
background: #fdecea;
|
|
}
|
|
|
|
.empty-state {
|
|
text-align: center;
|
|
padding: 40px 10px;
|
|
color: #9ca3af;
|
|
}
|
|
|
|
.empty-state i {
|
|
font-size: 34px;
|
|
margin-bottom: 10px;
|
|
display: block;
|
|
color: #d5d9e0;
|
|
}
|
|
|
|
.btn-save {
|
|
border-radius: 8px;
|
|
padding: 10px 26px;
|
|
font-weight: 600;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<div id="layout-wrapper">
|
|
<header id="page-topbar" class="isvertical-topbar">
|
|
<div class="navbar-header">
|
|
<div class="d-flex">
|
|
<?php include('include/logoarea.php'); ?>
|
|
<button type="button" class="btn btn-sm px-3 font-size-24 header-item waves-effect vertical-menu-btn">
|
|
<i class="bx bx-menu align-middle"></i>
|
|
</button>
|
|
<div class="page-title-box align-self-center d-none d-md-block">
|
|
<h4 class="page-title mb-0">Sedi</h4>
|
|
</div>
|
|
</div>
|
|
<div class="d-flex">
|
|
<?php include('include/languageselection.php'); ?>
|
|
<?php include('include/profiletopbar.php'); ?>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
<?php include('include/sidebar.php'); ?>
|
|
<header class="ishorizontal-topbar">
|
|
<div class="navbar-header">
|
|
<div class="d-flex"></div>
|
|
</div>
|
|
<div class="topnav">
|
|
<div class="container-fluid">
|
|
<nav class="navbar navbar-light navbar-expand-lg topnav-menu"></nav>
|
|
</div>
|
|
</div>
|
|
</header>
|
|
|
|
<div class="main-content">
|
|
<div class="page-content">
|
|
<div class="container-fluid">
|
|
|
|
<?php if ($message === 'success') : ?>
|
|
<div class="alert alert-success" role="alert">Sede salvata.</div>
|
|
<?php elseif ($message === 'deleted') : ?>
|
|
<div class="alert alert-warning" role="alert">Sede eliminata.</div>
|
|
<?php elseif ($message === 'default') : ?>
|
|
<div class="alert alert-success" role="alert">Sede predefinita aggiornata.</div>
|
|
<?php elseif ($message === 'error') : ?>
|
|
<div class="alert alert-danger" role="alert">Errore: nome e indirizzo sono obbligatori.</div>
|
|
<?php endif; ?>
|
|
|
|
<?php if ($tableMissing) : ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
La tabella <code>event_locations</code> non esiste ancora. Esegui la migration Phinx <code>CreateEventLocationsTable</code> e ricarica.
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<div class="row">
|
|
<!-- Form -->
|
|
<div class="col-lg-5">
|
|
<div class="card admin-card mb-4">
|
|
<div class="card-body">
|
|
<div class="section-title"><?php echo $isEdit ? 'Modifica sede' : 'Nuova sede'; ?></div>
|
|
<p class="section-lead">Le sedi salvate compaiono nella tendina "Sede" del form evento.</p>
|
|
|
|
<form action="event_locations_save.php" method="POST" id="loc-form">
|
|
<input type="hidden" name="id" value="<?php echo (int) $val['id']; ?>">
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Nome sede *</label>
|
|
<input type="text" class="form-control" name="name" required
|
|
value="<?php echo e($val['name']); ?>" placeholder="Es. Studio YogaSoul">
|
|
</div>
|
|
|
|
<div class="mb-3">
|
|
<label class="form-label">Indirizzo *</label>
|
|
<div class="d-flex gap-2">
|
|
<input type="text" class="form-control" id="address-input" name="address" required
|
|
value="<?php echo e($val['address']); ?>" placeholder="Via, numero, città">
|
|
<button type="button" class="btn-geocode" id="btn-geocode"><i class="bx bx-map-pin"></i> Calcola</button>
|
|
</div>
|
|
<div class="hint" id="geo-hint">Premi "Calcola" per ottenere le coordinate dall'indirizzo.</div>
|
|
</div>
|
|
|
|
<div class="row">
|
|
<div class="col-6 mb-3">
|
|
<label class="form-label">Latitudine</label>
|
|
<input type="text" class="form-control" id="lat-input" name="lat" value="<?php echo e($val['lat']); ?>" placeholder="45.6600">
|
|
</div>
|
|
<div class="col-6 mb-3">
|
|
<label class="form-label">Longitudine</label>
|
|
<input type="text" class="form-control" id="lng-input" name="lng" value="<?php echo e($val['lng']); ?>" placeholder="8.7900">
|
|
</div>
|
|
</div>
|
|
|
|
<div class="form-check mb-3">
|
|
<input class="form-check-input" type="checkbox" id="is_default" name="is_default" value="Y" <?php echo $val['is_default'] ? 'checked' : ''; ?>>
|
|
<label class="form-check-label" for="is_default">Imposta come sede predefinita</label>
|
|
</div>
|
|
|
|
<div class="d-flex gap-2">
|
|
<button type="submit" class="btn btn-primary btn-save"><i class="bx bx-save"></i> <?php echo $isEdit ? 'Salva' : 'Aggiungi'; ?></button>
|
|
<?php if ($isEdit) : ?>
|
|
<a href="event_locations.php" class="btn btn-light btn-save">Annulla</a>
|
|
<?php endif; ?>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Elenco -->
|
|
<div class="col-lg-7">
|
|
<div class="card admin-card">
|
|
<div class="card-body">
|
|
<div class="section-title">Sedi salvate</div>
|
|
<p class="section-lead">Rubrica delle sedi riutilizzabili.</p>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table loc-table align-middle">
|
|
<thead>
|
|
<tr>
|
|
<th>Sede</th>
|
|
<th>Indirizzo</th>
|
|
<th>Coordinate</th>
|
|
<th class="text-end">Azioni</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
<?php if (empty($locations)) : ?>
|
|
<tr>
|
|
<td colspan="4">
|
|
<div class="empty-state">
|
|
<i class="fas fa-map-marker-alt"></i>
|
|
Nessuna sede salvata. Aggiungine una dal form a sinistra.
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
<?php else :
|
|
foreach ($locations as $loc) :
|
|
$hasCoords = $loc['lat'] !== null && $loc['lng'] !== null && $loc['lat'] !== '' && $loc['lng'] !== '';
|
|
?>
|
|
<tr>
|
|
<td>
|
|
<span class="loc-name"><?php echo e($loc['name']); ?></span>
|
|
<?php if ($loc['is_default'] === 'Y') : ?>
|
|
<span class="default-badge"><i class="bx bxs-star"></i> Predefinita</span>
|
|
<?php endif; ?>
|
|
</td>
|
|
<td><?php echo e($loc['address']); ?></td>
|
|
<td><?php echo $hasCoords ? e($loc['lat']) . ', ' . e($loc['lng']) : '<span style="color:#9ca3af;">—</span>'; ?></td>
|
|
<td class="text-end">
|
|
<a href="event_locations.php?edit=<?php echo (int) $loc['id']; ?>" class="btn-act"><i class="bx bx-edit"></i> Modifica</a>
|
|
<?php if ($loc['is_default'] !== 'Y') : ?>
|
|
<a href="event_locations.php?makedefault=<?php echo (int) $loc['id']; ?>" class="btn-act"><i class="bx bx-star"></i> Predefinita</a>
|
|
<?php endif; ?>
|
|
<button type="button" class="btn-act danger btn-del"
|
|
data-id="<?php echo (int) $loc['id']; ?>"
|
|
data-name="<?php echo e($loc['name']); ?>">
|
|
<i class="bx bx-trash"></i> Elimina
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
<?php endforeach; ?>
|
|
<?php endif; ?>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
</div>
|
|
<?php include('include/footer.php'); ?>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="assets/libs/bootstrap/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/libs/metismenujs/metismenujs.min.js"></script>
|
|
<script src="assets/libs/simplebar/simplebar.min.js"></script>
|
|
<script src="assets/libs/eva-icons/eva.min.js"></script>
|
|
<script src="assets/js/app.js"></script>
|
|
|
|
<script>
|
|
var addressInput = document.getElementById('address-input');
|
|
var latInput = document.getElementById('lat-input');
|
|
var lngInput = document.getElementById('lng-input');
|
|
var geoHint = document.getElementById('geo-hint');
|
|
|
|
// Modifica indirizzo -> le vecchie coordinate non valgono più
|
|
addressInput.addEventListener('input', function() {
|
|
latInput.value = '';
|
|
lngInput.value = '';
|
|
geoHint.textContent = 'Indirizzo modificato: premi "Calcola" per aggiornare le coordinate.';
|
|
});
|
|
|
|
document.getElementById('btn-geocode').addEventListener('click', function() {
|
|
var q = addressInput.value.trim();
|
|
if (q === '') {
|
|
addressInput.focus();
|
|
return;
|
|
}
|
|
geoHint.textContent = 'Ricerca coordinate…';
|
|
fetch('event_geocode.php?q=' + encodeURIComponent(q))
|
|
.then(function(r) {
|
|
return r.json();
|
|
})
|
|
.then(function(data) {
|
|
if (data.ok) {
|
|
latInput.value = data.lat;
|
|
lngInput.value = data.lng;
|
|
latInput.classList.add('coord-flash');
|
|
lngInput.classList.add('coord-flash');
|
|
setTimeout(function() {
|
|
latInput.classList.remove('coord-flash');
|
|
lngInput.classList.remove('coord-flash');
|
|
}, 900);
|
|
geoHint.innerHTML = '<span style="color:#1a7f52;font-weight:600;">✓ Coordinate aggiornate:</span> ' +
|
|
data.lat + ', ' + data.lng + (data.display ? ' — ' + data.display : '');
|
|
} else {
|
|
geoHint.textContent = data.error || 'Indirizzo non trovato.';
|
|
}
|
|
})
|
|
.catch(function() {
|
|
geoHint.textContent = 'Errore durante il geocoding.';
|
|
});
|
|
});
|
|
|
|
$(document).ready(function() {
|
|
$('.btn-del').on('click', function() {
|
|
var id = $(this).data('id');
|
|
var name = $(this).data('name');
|
|
Swal.fire({
|
|
title: 'Eliminare la sede?',
|
|
html: 'Stai per eliminare <strong>' + $('<div>').text(name).html() + '</strong>.<br><small>Gli eventi che la usavano mantengono il loro indirizzo.</small>',
|
|
icon: 'warning',
|
|
showCancelButton: true,
|
|
confirmButtonText: 'Elimina',
|
|
cancelButtonText: 'Chiudi',
|
|
confirmButtonColor: '#e74c3c',
|
|
reverseButtons: true
|
|
}).then(function(result) {
|
|
if (result.isConfirmed) {
|
|
window.location.href = 'event_locations.php?delete=' + id;
|
|
}
|
|
});
|
|
});
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|