538 lines
24 KiB
PHP
538 lines
24 KiB
PHP
<?php
|
||
// Forza la visualizzazione degli errori
|
||
ini_set('display_errors', 1);
|
||
ini_set('display_startup_errors', 1);
|
||
error_reporting(E_ALL);
|
||
|
||
include('include/headscript.php');
|
||
|
||
// Connessione al database
|
||
$dbHandler = DBHandlerSelect::getInstance();
|
||
$pdo = $dbHandler->getConnection();
|
||
|
||
// ID dell'utente loggato (assumiamo sia definito)
|
||
if (!isset($iduserlogin)) {
|
||
die("Errore: ID utente non definito.");
|
||
}
|
||
|
||
// Recupera i dati della scuola associata all'utente
|
||
$stmt = $pdo->prepare("
|
||
SELECT s.*, u.first_name, u.last_name, u.email
|
||
FROM auth_users u
|
||
LEFT JOIN schools s ON s.owner_id = u.id
|
||
WHERE u.id = ?
|
||
");
|
||
$stmt->execute([$iduserlogin]);
|
||
$school = $stmt->fetch();
|
||
|
||
if (!$school) {
|
||
die("Errore: Utente non trovato.");
|
||
}
|
||
|
||
// Determina se è una nuova scuola
|
||
$is_new = !isset($school['id']);
|
||
if ($is_new) {
|
||
$school = [
|
||
'id' => null,
|
||
'owner_id' => $iduserlogin,
|
||
'name' => '',
|
||
'website' => '',
|
||
'email' => '',
|
||
'phone' => '',
|
||
'description' => '',
|
||
'address_street' => '',
|
||
'address_city' => '',
|
||
'address_postal_code' => '',
|
||
'address_province' => '',
|
||
'address_country' => '',
|
||
'latitude' => '',
|
||
'longitude' => '',
|
||
'owner_name' => '',
|
||
'vat_number' => '',
|
||
'logo' => '',
|
||
'status' => 'active',
|
||
'created_at' => '',
|
||
'updated_at' => '',
|
||
'slug' => '',
|
||
'first_name' => $school['first_name'],
|
||
'last_name' => $school['last_name'],
|
||
'email' => $school['email']
|
||
];
|
||
}
|
||
|
||
// Funzione per generare uno slug valido
|
||
function generateSlug($string)
|
||
{
|
||
$slug = strtolower($string); // Converti in minuscolo
|
||
$slug = preg_replace('/[^a-z0-9-]+/', '-', $slug); // Sostituisci caratteri non validi con trattini
|
||
$slug = preg_replace('/-+/', '-', $slug); // Rimuovi trattini multipli
|
||
$slug = trim($slug, '-'); // Rimuovi trattini all'inizio e alla fine
|
||
return $slug;
|
||
}
|
||
|
||
// Gestione del form
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$name = $_POST['name'] ?? '';
|
||
$website = $_POST['website'] ?? null;
|
||
$email = $_POST['email'] ?? '';
|
||
$phone = $_POST['phone'] ?? null;
|
||
$description = $_POST['description'] ?? null;
|
||
$address_street = $_POST['address_street'] ?? '';
|
||
$address_city = $_POST['address_city'] ?? '';
|
||
$address_postal_code = $_POST['address_postal_code'] ?? '';
|
||
$address_province = $_POST['address_province'] ?? null;
|
||
$address_country = $_POST['address_country'] ?? '';
|
||
$latitude = $_POST['latitude'] ? floatval($_POST['latitude']) : null;
|
||
$longitude = $_POST['longitude'] ? floatval($_POST['longitude']) : null;
|
||
$owner_name = $_POST['owner_name'] ?? '';
|
||
$vat_number = $_POST['vat_number'] ?? '';
|
||
$status = in_array($_POST['status'], ['active', 'inactive', 'suspended']) ? $_POST['status'] : 'active';
|
||
$slug = isset($_POST['slug']) ? generateSlug($_POST['slug']) : '';
|
||
|
||
// Validazione dello slug
|
||
if (empty($slug)) {
|
||
$error = "Errore: Lo slug non può essere vuoto.";
|
||
} else {
|
||
// Controlla se lo slug è univoco
|
||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM schools WHERE slug = ? AND id != ?");
|
||
$stmt->execute([$slug, $school['id'] ?? 0]);
|
||
$slug_exists = $stmt->fetchColumn();
|
||
|
||
if ($slug_exists) {
|
||
$error = "Errore: Lo slug '$slug' è già in uso. Scegli un altro slug.";
|
||
}
|
||
}
|
||
|
||
// Gestione del caricamento del logo
|
||
$logo = $school['logo'];
|
||
if (isset($_FILES['logo']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
|
||
$file = $_FILES['logo'];
|
||
$timestamp = time();
|
||
$original_name = basename($file['name']);
|
||
$extension = strtolower(pathinfo($original_name, PATHINFO_EXTENSION));
|
||
$allowed_extensions = ['jpg', 'jpeg', 'png', 'gif'];
|
||
|
||
if (in_array($extension, $allowed_extensions)) {
|
||
$new_filename = "photoschool/{$iduserlogin}-{$timestamp}-{$original_name}";
|
||
if (move_uploaded_file($file['tmp_name'], $new_filename)) {
|
||
$logo = $new_filename;
|
||
if ($school['logo'] && file_exists($school['logo']) && !$is_new) {
|
||
unlink($school['logo']);
|
||
}
|
||
} else {
|
||
$error = "Errore durante il caricamento del logo.";
|
||
}
|
||
} else {
|
||
$error = "Estensione del file non consentita. Usa JPG, JPEG, PNG o GIF.";
|
||
}
|
||
}
|
||
|
||
// Se non ci sono errori, procedi con il salvataggio
|
||
if (!isset($error)) {
|
||
// Aggiorna auth_users (opzionale, se vuoi aggiornare first_name e last_name)
|
||
$stmt = $pdo->prepare("UPDATE auth_users SET first_name = ?, last_name = ? WHERE id = ?");
|
||
$stmt->execute([$school['first_name'], $school['last_name'], $iduserlogin]);
|
||
|
||
if ($is_new) {
|
||
$stmt = $pdo->prepare("
|
||
INSERT INTO schools (owner_id, name, website, email, phone, description, address_street, address_city, address_postal_code, address_province, address_country, latitude, longitude, owner_name, vat_number, logo, status, slug)
|
||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||
");
|
||
$success = $stmt->execute([
|
||
$iduserlogin,
|
||
$name,
|
||
$website,
|
||
$email,
|
||
$phone,
|
||
$description,
|
||
$address_street,
|
||
$address_city,
|
||
$address_postal_code,
|
||
$address_province,
|
||
$address_country,
|
||
$latitude,
|
||
$longitude,
|
||
$owner_name,
|
||
$vat_number,
|
||
$logo,
|
||
$status,
|
||
$slug
|
||
]);
|
||
|
||
if ($success) {
|
||
$success_message = "Scuola creata con successo!";
|
||
$stmt = $pdo->prepare("
|
||
SELECT s.*, u.first_name, u.last_name, u.email
|
||
FROM auth_users u
|
||
LEFT JOIN schools s ON s.owner_id = u.id
|
||
WHERE u.id = ?
|
||
");
|
||
$stmt->execute([$iduserlogin]);
|
||
$school = $stmt->fetch();
|
||
$is_new = false;
|
||
} else {
|
||
$error = "Errore durante la creazione della scuola.";
|
||
}
|
||
} else {
|
||
$stmt = $pdo->prepare("
|
||
UPDATE schools
|
||
SET name = ?, website = ?, email = ?, phone = ?, description = ?, address_street = ?, address_city = ?,
|
||
address_postal_code = ?, address_province = ?, address_country = ?, latitude = ?, longitude = ?,
|
||
owner_name = ?, vat_number = ?, logo = ?, status = ?, slug = ?
|
||
WHERE owner_id = ?
|
||
");
|
||
$success = $stmt->execute([
|
||
$name,
|
||
$website,
|
||
$email,
|
||
$phone,
|
||
$description,
|
||
$address_street,
|
||
$address_city,
|
||
$address_postal_code,
|
||
$address_province,
|
||
$address_country,
|
||
$latitude,
|
||
$longitude,
|
||
$owner_name,
|
||
$vat_number,
|
||
$logo,
|
||
$status,
|
||
$slug,
|
||
$iduserlogin
|
||
]);
|
||
|
||
if ($success) {
|
||
$success_message = "Dati aggiornati con successo!";
|
||
$stmt = $pdo->prepare("
|
||
SELECT s.*, u.first_name, u.last_name, u.email
|
||
FROM auth_users u
|
||
LEFT JOIN schools s ON s.owner_id = u.id
|
||
WHERE u.id = ?
|
||
");
|
||
$stmt->execute([$iduserlogin]);
|
||
$school = $stmt->fetch();
|
||
} else {
|
||
$error = "Errore durante l'aggiornamento dei dati.";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
?>
|
||
|
||
<!doctype html>
|
||
<html lang="it">
|
||
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<link rel="icon" href="assets/images/favicon-32x32.png" type="image/png" />
|
||
<?php include('cssinclude.php'); ?>
|
||
<?php include('siteinfo.php'); ?>
|
||
|
||
<!-- Leaflet CSS -->
|
||
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
|
||
integrity="sha256-sA+Zcx6cNpCzIvJczQqny0Sg0r7GDL2wMpN4k1kJ0fPQ=" crossorigin="" />
|
||
<style>
|
||
#map {
|
||
height: 350px;
|
||
border-radius: 10px;
|
||
margin-top: 15px;
|
||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
|
||
}
|
||
|
||
.geocode-btn {
|
||
margin-top: 10px;
|
||
}
|
||
</style>
|
||
</head>
|
||
|
||
<body>
|
||
<div class="wrapper">
|
||
<?php include('include/navbar.php'); ?>
|
||
<?php include('include/topbar.php'); ?>
|
||
|
||
<div class="page-wrapper">
|
||
<div class="page-content">
|
||
<div class="card radius-10">
|
||
<div class="card-header">
|
||
<h6 class="mb-0"><?php echo $is_new ? 'Crea Profilo Scuola' : 'Profilo Scuola'; ?></h6>
|
||
</div>
|
||
<div class="card-body">
|
||
|
||
<?php if (isset($success_message)): ?>
|
||
<div class="alert alert-success"><?php echo $success_message; ?></div>
|
||
<?php endif; ?>
|
||
<?php if (isset($error)): ?>
|
||
<div class="alert alert-danger"><?php echo $error; ?></div>
|
||
<?php endif; ?>
|
||
|
||
<form method="POST" enctype="multipart/form-data">
|
||
<div class="row">
|
||
<!-- Colonna sinistra: logo -->
|
||
<div class="col-md-4 text-center">
|
||
<div class="mb-3">
|
||
<img src="<?php echo $school['logo'] ? htmlspecialchars($school['logo']) : 'photoschool/ndphoto.png'; ?>"
|
||
alt="Logo" class="img-fluid rounded-circle"
|
||
style="width:150px;height:150px;object-fit:cover;">
|
||
</div>
|
||
<div class="mb-3">
|
||
<label for="logo" class="form-label">Carica nuovo logo</label>
|
||
<input type="file" class="form-control" id="logo" name="logo" accept="image/*">
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Colonna destra: tutti i campi -->
|
||
<div class="col-md-8">
|
||
|
||
<!-- Nome, slug, ecc... (tutto invariato fino all'indirizzo) -->
|
||
<div class="mb-3">
|
||
<label for="name" class="form-label">Nome Scuola</label>
|
||
<input type="text" class="form-control" id="name" name="name"
|
||
value="<?php echo htmlspecialchars($school['name'] ?? ''); ?>" required>
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="slug" class="form-label">Slug (URL personalizzato)</label>
|
||
<input type="text" class="form-control" id="slug" name="slug"
|
||
value="<?php echo htmlspecialchars($school['slug'] ?? ''); ?>" required>
|
||
<small class="text-muted">es. yoga-milano</small>
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="website" class="form-label">Sito Web</label>
|
||
<input type="url" class="form-control" id="website" name="website"
|
||
value="<?php echo htmlspecialchars($school['website'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="email" class="form-label">Email Scuola</label>
|
||
<input type="email" class="form-control" id="email" name="email"
|
||
value="<?php echo htmlspecialchars($school['email'] ?? ''); ?>" required>
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="phone" class="form-label">Telefono</label>
|
||
<input type="text" class="form-control" id="phone" name="phone"
|
||
value="<?php echo htmlspecialchars($school['phone'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="description" class="form-label">Descrizione</label>
|
||
<textarea class="form-control" id="description" name="description" rows="3"><?php echo htmlspecialchars($school['description'] ?? ''); ?></textarea>
|
||
</div>
|
||
|
||
<!-- ==================== INIZIO INDIRIZZO + MAPPA ==================== -->
|
||
<div class="mb-3">
|
||
<label for="address_street" class="form-label">Via / Numero civico</label>
|
||
<input type="text" class="form-control" id="address_street" name="address_street"
|
||
value="<?php echo htmlspecialchars($school['address_street'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="row">
|
||
<div class="col-md-6 mb-3">
|
||
<label for="address_city" class="form-label">Città</label>
|
||
<input type="text" class="form-control" id="address_city" name="address_city"
|
||
value="<?php echo htmlspecialchars($school['address_city'] ?? ''); ?>">
|
||
</div>
|
||
<div class="col-md-6 mb-3">
|
||
<label for="address_postal_code" class="form-label">CAP</label>
|
||
<input type="text" class="form-control" id="address_postal_code" name="address_postal_code"
|
||
value="<?php echo htmlspecialchars($school['address_postal_code'] ?? ''); ?>">
|
||
</div>
|
||
</div>
|
||
|
||
<div class="row">
|
||
<div class="col-md-6 mb-3">
|
||
<label for="address_province" class="form-label">Provincia</label>
|
||
<input type="text" class="form-control" id="address_province" name="address_province"
|
||
value="<?php echo htmlspecialchars($school['address_province'] ?? ''); ?>">
|
||
</div>
|
||
<div class="col-md-6 mb-3">
|
||
<label for="address_country" class="form-label">Nazione</label>
|
||
<input type="text" class="form-control" id="address_country" name="address_country"
|
||
value="<?php echo htmlspecialchars($school['address_country'] ?? 'Italia'); ?>">
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Pulsante geocoding -->
|
||
<div class="mb-3">
|
||
<button type="button" class="btn btn-outline-primary geocode-btn" id="geocode-btn">
|
||
Cerca sulla mappa
|
||
</button>
|
||
<small class="text-muted d-block">Compila via + città + CAP → clicca qui</small>
|
||
</div>
|
||
|
||
<!-- Mappa -->
|
||
<div id="map"></div>
|
||
|
||
<!-- Latitudine e Longitudine (readonly ma modificabili manualmente se serve) -->
|
||
<div class="row mt-3">
|
||
<div class="col-md-6">
|
||
<label for="latitude" class="form-label">Latitudine</label>
|
||
<input type="text" class="form-control" id="latitude" name="latitude"
|
||
value="<?php echo htmlspecialchars($school['latitude'] ?? ''); ?>" readonly>
|
||
</div>
|
||
<div class="col-md-6">
|
||
<label for="longitude" class="form-label">Longitudine</label>
|
||
<input type="text" class="form-control" id="longitude" name="longitude"
|
||
value="<?php echo htmlspecialchars($school['longitude'] ?? ''); ?>" readonly>
|
||
</div>
|
||
</div>
|
||
<!-- ==================== FINE INDIRIZZO + MAPPA ==================== -->
|
||
|
||
<hr class="my-4">
|
||
|
||
<div class="mb-3">
|
||
<label for="owner_name" class="form-label">Nome Proprietario</label>
|
||
<input type="text" class="form-control" id="owner_name" name="owner_name"
|
||
value="<?php echo htmlspecialchars($school['owner_name'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="vat_number" class="form-label">Partita IVA</label>
|
||
<input type="text" class="form-control" id="vat_number" name="vat_number"
|
||
value="<?php echo htmlspecialchars($school['vat_number'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="mb-3">
|
||
<label for="status" class="form-label">Stato</label>
|
||
<select class="form-control" id="status" name="status">
|
||
<option value="active" <?php echo ($school['status'] ?? '') === 'active' ? 'selected' : ''; ?>>Attivo</option>
|
||
<option value="inactive" <?php echo ($school['status'] ?? '') === 'inactive' ? 'selected' : ''; ?>>Inattivo</option>
|
||
<option value="suspended" <?php echo ($school['status'] ?? '') === 'suspended' ? 'selected' : ''; ?>>Sospeso</option>
|
||
</select>
|
||
</div>
|
||
|
||
<?php if (!$is_new): ?>
|
||
<div class="mb-3">
|
||
<label class="form-label">Creato il</label>
|
||
<input type="text" class="form-control" value="<?php echo htmlspecialchars($school['created_at'] ?? ''); ?>" readonly>
|
||
</div>
|
||
<div class="mb-3">
|
||
<label class="form-label">Ultimo aggiornamento</label>
|
||
<input type="text" class="form-control" value="<?php echo htmlspecialchars($school['updated_at'] ?? ''); ?>" readonly>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<button type="submit" class="btn btn-primary btn-lg">
|
||
<?php echo $is_new ? 'Crea Profilo' : 'Salva Modifiche'; ?>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="overlay toggle-icon"></div>
|
||
<a href="javaScript:;" class="back-to-top"><i class='bx bxs-up-arrow-alt'></i></a>
|
||
<?php include('include/footer.php'); ?>
|
||
</div>
|
||
|
||
<?php include('jsinclude.php'); ?>
|
||
|
||
<!-- Leaflet JS -->
|
||
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"
|
||
integrity="sha256-o9N1j3Z3B9n0nN2V3H7N7X8z0z1k6p3c1F0L5g0B6p8=" crossorigin=""></script>
|
||
|
||
<script>
|
||
let map, marker;
|
||
|
||
// Inizializza la mappa
|
||
function initMap(lat = 41.9028, lng = 12.4964, zoom = 5) {
|
||
if (map) map.remove();
|
||
|
||
map = L.map('map').setView([lat, lng], zoom);
|
||
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||
attribution: '© OpenStreetMap contributors'
|
||
}).addTo(map);
|
||
|
||
marker = L.marker([lat, lng], {
|
||
draggable: true
|
||
}).addTo(map);
|
||
|
||
marker.on('dragend', function() {
|
||
const pos = marker.getLatLng();
|
||
document.getElementById('latitude').value = pos.lat.toFixed(8);
|
||
document.getElementById('longitude').value = pos.lng.toFixed(8);
|
||
});
|
||
}
|
||
|
||
// Aggiorna mappa e campi
|
||
function updateMap(lat, lng) {
|
||
document.getElementById('latitude').value = lat.toFixed(8);
|
||
document.getElementById('longitude').value = lng.toFixed(8);
|
||
map.setView([lat, lng], 16);
|
||
if (marker) marker.setLatLng([lat, lng]);
|
||
}
|
||
|
||
// Geocoding con Nominatim
|
||
document.getElementById('geocode-btn').addEventListener('click', function() {
|
||
const street = document.getElementById('address_street').value.trim();
|
||
const city = document.getElementById('address_city').value.trim();
|
||
const cap = document.getElementById('address_postal_code').value.trim();
|
||
const country = document.getElementById('address_country').value.trim() || 'Italia';
|
||
|
||
if (!street || !city) {
|
||
alert('Inserisci almeno Via e Città');
|
||
return;
|
||
}
|
||
|
||
const query = `${street}, ${city}${cap ? ' ' + cap : ''}, ${country}`;
|
||
const url = `https://nominatim.openstreetmap.org/search?format=json&q=${encodeURIComponent(query)}&limit=1`;
|
||
|
||
fetch(url)
|
||
.then(r => r.json())
|
||
.then(data => {
|
||
if (data && data.length > 0) {
|
||
const r = data[0];
|
||
updateMap(parseFloat(r.lat), parseFloat(r.lon));
|
||
} else {
|
||
alert('Indirizzo non trovato. Controlla i dati.');
|
||
}
|
||
})
|
||
.catch(() => alert('Errore di rete. Riprova.'));
|
||
});
|
||
|
||
// Al caricamento della pagina
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
const lat = document.getElementById('latitude').value;
|
||
const lng = document.getElementById('longitude').value;
|
||
|
||
if (lat && lng) {
|
||
initMap(parseFloat(lat), parseFloat(lng), 16);
|
||
} else {
|
||
initMap(); // Italia centrata
|
||
}
|
||
});
|
||
|
||
/* Slug automatico (il tuo codice originale – invariato) */
|
||
const nameInput = document.getElementById('name');
|
||
const slugInput = document.getElementById('slug');
|
||
let isUserTypingSlug = false;
|
||
|
||
function generateSlug(str) {
|
||
return str.toLowerCase()
|
||
.replace(/[^a-z0-9-]+/g, '-')
|
||
.replace(/-+/g, '-')
|
||
.replace(/^-|-$/g, '');
|
||
}
|
||
|
||
nameInput?.addEventListener('input', function() {
|
||
if (!isUserTypingSlug) {
|
||
slugInput.value = generateSlug(this.value);
|
||
}
|
||
});
|
||
|
||
slugInput?.addEventListener('input', function(e) {
|
||
isUserTypingSlug = true;
|
||
this.value = generateSlug(this.value);
|
||
});
|
||
|
||
slugInput?.addEventListener('blur', () => isUserTypingSlug = false);
|
||
</script>
|
||
</body>
|
||
|
||
</html>
|