559 lines
25 KiB
PHP
559 lines
25 KiB
PHP
<?php
|
||
// school_profile.php
|
||
|
||
ini_set('display_errors', 1);
|
||
ini_set('display_startup_errors', 1);
|
||
error_reporting(E_ALL);
|
||
|
||
include('include/headscript.php');
|
||
|
||
$dbHandler = DBHandlerSelect::getInstance();
|
||
$pdo = $dbHandler->getConnection();
|
||
|
||
if (!isset($iduserlogin)) {
|
||
die("Errore: ID utente non definito.");
|
||
}
|
||
|
||
// Recupera scuola dell'utente loggato
|
||
$stmt = $pdo->prepare("
|
||
SELECT s.*, u.first_name, u.last_name, u.email
|
||
FROM schools s
|
||
RIGHT JOIN auth_users u ON s.owner_id = u.id
|
||
WHERE u.id = ?
|
||
");
|
||
$stmt->execute([$iduserlogin]);
|
||
$school = $stmt->fetch(PDO::FETCH_ASSOC);
|
||
|
||
$is_new = empty($school['id']);
|
||
|
||
if ($is_new) {
|
||
$school = [
|
||
'id' => null,
|
||
'owner_id' => $iduserlogin,
|
||
'name' => '',
|
||
'slug' => '',
|
||
'website' => '',
|
||
'email' => '',
|
||
'phone' => '',
|
||
'description' => '',
|
||
'address_street' => '',
|
||
'address_city' => '',
|
||
'address_postal_code' => '',
|
||
'address_province' => '',
|
||
'address_country' => 'Italia',
|
||
'latitude' => null,
|
||
'longitude' => null,
|
||
'owner_name' => '',
|
||
'vat_number' => '',
|
||
'logo' => '',
|
||
'status' => 'active',
|
||
'first_name' => '',
|
||
'last_name' => '',
|
||
'email' => ''
|
||
];
|
||
}
|
||
|
||
// Generatore slug
|
||
function generateSlug($string)
|
||
{
|
||
$slug = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
|
||
$slug = preg_replace('/[^a-z0-9 -]/i', '', $slug);
|
||
$slug = trim($slug);
|
||
$slug = preg_replace('/ +/', '-', $slug);
|
||
$slug = strtolower($slug);
|
||
return $slug;
|
||
}
|
||
|
||
// POST - Salvataggio
|
||
$success_message = $error = null;
|
||
|
||
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||
$name = trim($_POST['name'] ?? '');
|
||
$slug = generateSlug(trim($_POST['slug'] ?? $name));
|
||
$website = trim($_POST['website'] ?? '');
|
||
$email = trim($_POST['email'] ?? '');
|
||
$phone = trim($_POST['phone'] ?? '');
|
||
$description = trim($_POST['description'] ?? '');
|
||
$address_street = trim($_POST['address_street'] ?? '');
|
||
$address_city = trim($_POST['address_city'] ?? '');
|
||
$address_postal_code = trim($_POST['address_postal_code'] ?? '');
|
||
$address_province = trim($_POST['address_province'] ?? '');
|
||
$address_country = trim($_POST['address_country'] ?? 'Italia');
|
||
$latitude = !empty($_POST['latitude']) ? floatval($_POST['latitude']) : null;
|
||
$longitude = !empty($_POST['longitude']) ? floatval($_POST['longitude']) : null;
|
||
$owner_name = trim($_POST['owner_name'] ?? '');
|
||
$vat_number = trim($_POST['vat_number'] ?? '');
|
||
$status = in_array($_POST['status'] ?? 'active', ['active', 'inactive', 'suspended']) ? $_POST['status'] : 'active';
|
||
|
||
// Validazioni
|
||
if (empty($name)) $error = "Il nome della scuola è obbligatorio.";
|
||
elseif (empty($slug)) $error = "Lo slug non può essere vuoto.";
|
||
else {
|
||
$stmt = $pdo->prepare("SELECT COUNT(*) FROM schools WHERE slug = ? AND id != ?");
|
||
$stmt->execute([$slug, $school['id'] ?? 0]);
|
||
if ($stmt->fetchColumn() > 0) {
|
||
$error = "Lo slug '$slug' è già in uso.";
|
||
}
|
||
}
|
||
|
||
// Logo
|
||
$logo = $school['logo'] ?? '';
|
||
if (!empty($_FILES['logo']['name']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) {
|
||
$ext = strtolower(pathinfo($_FILES['logo']['name'], PATHINFO_EXTENSION));
|
||
if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) {
|
||
$new_name = "photoschool/{$iduserlogin}-" . time() . "-logo.$ext";
|
||
if (move_uploaded_file($_FILES['logo']['tmp_name'], $new_name)) {
|
||
if ($logo && file_exists($logo) && !$is_new) @unlink($logo);
|
||
$logo = $new_name;
|
||
} else {
|
||
$error = "Errore caricamento logo.";
|
||
}
|
||
} else {
|
||
$error = "Solo JPG, PNG, GIF ammessi.";
|
||
}
|
||
}
|
||
|
||
if (!isset($error)) {
|
||
$params = [
|
||
$name,
|
||
$slug,
|
||
$website ?: null,
|
||
$email,
|
||
$phone ?: null,
|
||
$description,
|
||
$address_street,
|
||
$address_city,
|
||
$address_postal_code,
|
||
$address_province,
|
||
$address_country,
|
||
$latitude,
|
||
$longitude,
|
||
$owner_name,
|
||
$vat_number,
|
||
$logo,
|
||
$status
|
||
];
|
||
|
||
if ($is_new) {
|
||
$stmt = $pdo->prepare("
|
||
INSERT INTO schools (
|
||
owner_id, name, slug, website, email, phone, description,
|
||
address_street, address_city, address_postal_code, address_province, address_country,
|
||
latitude, longitude, owner_name, vat_number, logo, status
|
||
) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
|
||
");
|
||
array_unshift($params, $iduserlogin);
|
||
$success = $stmt->execute($params);
|
||
|
||
if ($success) {
|
||
$success_message = "Scuola creata con successo!";
|
||
$stmt = $pdo->prepare("SELECT * FROM schools WHERE owner_id = ? ORDER BY id DESC LIMIT 1");
|
||
$stmt->execute([$iduserlogin]);
|
||
$school = $stmt->fetch(PDO::FETCH_ASSOC);
|
||
$is_new = false;
|
||
} else {
|
||
$error = "Errore creazione scuola.";
|
||
}
|
||
} else {
|
||
$params[] = $school['id'];
|
||
$stmt = $pdo->prepare("
|
||
UPDATE schools SET
|
||
name=?, slug=?, website=?, email=?, phone=?, description=?,
|
||
address_street=?, address_city=?, address_postal_code=?, address_province=?,
|
||
address_country=?, latitude=?, longitude=?, owner_name=?, vat_number=?,
|
||
logo=?, status=?
|
||
WHERE id=?
|
||
");
|
||
$success = $stmt->execute($params);
|
||
|
||
if ($success) {
|
||
$success_message = "Profilo aggiornato con successo!";
|
||
$stmt = $pdo->prepare("SELECT * FROM schools WHERE id = ?");
|
||
$stmt->execute([$school['id']]);
|
||
$school = $stmt->fetch(PDO::FETCH_ASSOC);
|
||
} else {
|
||
$error = "Errore aggiornamento.";
|
||
}
|
||
}
|
||
}
|
||
}
|
||
?>
|
||
|
||
<!doctype html>
|
||
<html lang="it">
|
||
|
||
<head>
|
||
<meta charset="utf-8">
|
||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||
<title><?php echo $is_new ? 'Crea' : 'Modifica'; ?> Profilo Scuola</title>
|
||
<?php include('cssinclude.php'); ?>
|
||
|
||
<!-- Quill.js CDN -->
|
||
<link href="https://cdn.jsdelivr.net/npm/quill@2.0.2/dist/quill.snow.css" rel="stylesheet" />
|
||
|
||
<style>
|
||
#map {
|
||
height: 380px;
|
||
border-radius: 10px;
|
||
margin-top: 12px;
|
||
box-shadow: 0 4px 15px rgba(0, 0, 0, 0.08);
|
||
}
|
||
|
||
/* Editor Quill */
|
||
.ql-container {
|
||
min-height: 260px;
|
||
font-size: 15px;
|
||
border: 1px solid #ced4da;
|
||
border-radius: 0.375rem;
|
||
}
|
||
|
||
.ql-editor {
|
||
min-height: 260px;
|
||
}
|
||
|
||
.form-label {
|
||
font-weight: 500;
|
||
}
|
||
|
||
.ql-toolbar {
|
||
border-radius: 0.375rem 0.375rem 0 0;
|
||
border-color: #ced4da;
|
||
}
|
||
|
||
/* Logo rettangolare, non arrotondato */
|
||
.school-logo {
|
||
max-width: 100%;
|
||
height: auto;
|
||
max-height: 220px;
|
||
object-fit: contain;
|
||
border: 1px solid #dee2e6;
|
||
border-radius: 8px;
|
||
box-shadow: 0 2px 10px rgba(0, 0, 0, 0.08);
|
||
background: #fff;
|
||
padding: 10px;
|
||
display: block;
|
||
margin: 0 auto 1rem;
|
||
}
|
||
</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 bg-gradient-primary text-white">
|
||
<h5 class="mb-0"><?php echo $is_new ? 'Crea il tuo profilo scuola' : 'Gestisci profilo scuola'; ?></h5>
|
||
</div>
|
||
|
||
<div class="card-body">
|
||
<?php if ($success_message): ?>
|
||
<div class="alert alert-success alert-dismissible fade show">
|
||
<?php echo htmlspecialchars($success_message); ?>
|
||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($error): ?>
|
||
<div class="alert alert-danger alert-dismissible fade show">
|
||
<?php echo htmlspecialchars($error); ?>
|
||
<button type="button" class="btn-close" data-bs-dismiss="alert"></button>
|
||
</div>
|
||
<?php endif; ?>
|
||
|
||
<form method="POST" enctype="multipart/form-data" id="schoolForm">
|
||
<div class="row g-4">
|
||
<!-- Colonna sinistra: solo logo -->
|
||
<div class="col-lg-4 text-center">
|
||
<img src="<?php echo $school['logo'] ? htmlspecialchars($school['logo']) : 'photoschool/default-school.png'; ?>"
|
||
alt="Logo Scuola" class="school-logo mb-3">
|
||
|
||
<div class="mb-3">
|
||
<label class="form-label">Cambia Logo (opzionale)</label>
|
||
<input type="file" class="form-control" name="logo" accept="image/jpeg,image/png,image/gif">
|
||
<small class="text-muted d-block mt-1">Max 2MB – JPG, PNG, GIF</small>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- Colonna destra: tutti i campi inclusa descrizione con Quill -->
|
||
<div class="col-lg-8">
|
||
<div class="row g-3">
|
||
<div class="col-md-6">
|
||
<label class="form-label">Nome Scuola <span class="text-danger">*</span></label>
|
||
<input type="text" class="form-control" name="name" required
|
||
value="<?php echo htmlspecialchars($school['name'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Slug (URL personalizzato) <span class="text-danger">*</span></label>
|
||
<div class="input-group">
|
||
<span class="input-group-text">yogiboook.com/</span>
|
||
<input type="text" class="form-control" name="slug" id="slug" required
|
||
value="<?php echo htmlspecialchars($school['slug'] ?? ''); ?>">
|
||
</div>
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Sito web</label>
|
||
<input type="url" class="form-control" name="website"
|
||
value="<?php echo htmlspecialchars($school['website'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Email scuola <span class="text-danger">*</span></label>
|
||
<input type="email" class="form-control" name="email" required
|
||
value="<?php echo htmlspecialchars($school['email'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Telefono</label>
|
||
<input type="tel" class="form-control" name="phone"
|
||
value="<?php echo htmlspecialchars($school['phone'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<!-- Editor Quill qui, nella colonna destra -->
|
||
<div class="col-12">
|
||
<label class="form-label">Descrizione scuola</label>
|
||
<div id="quill-editor"></div>
|
||
<input type="hidden" name="description" id="description-hidden">
|
||
</div>
|
||
|
||
<div class="col-12">
|
||
<hr class="my-4">
|
||
<h6 class="mb-3">Indirizzo sede</h6>
|
||
</div>
|
||
|
||
<div class="col-12">
|
||
<label class="form-label">Via e numero civico</label>
|
||
<input type="text" class="form-control" name="address_street"
|
||
value="<?php echo htmlspecialchars($school['address_street'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-4">
|
||
<label class="form-label">Città</label>
|
||
<input type="text" class="form-control" name="address_city"
|
||
value="<?php echo htmlspecialchars($school['address_city'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-4">
|
||
<label class="form-label">CAP</label>
|
||
<input type="text" class="form-control" name="address_postal_code"
|
||
value="<?php echo htmlspecialchars($school['address_postal_code'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-4">
|
||
<label class="form-label">Provincia</label>
|
||
<input type="text" class="form-control" name="address_province"
|
||
value="<?php echo htmlspecialchars($school['address_province'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Nazione</label>
|
||
<input type="text" class="form-control" name="address_country"
|
||
value="<?php echo htmlspecialchars($school['address_country'] ?? 'Italia'); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-6 mt-4 pt-2">
|
||
<button type="button" class="btn btn-outline-primary w-100" id="geocodeBtn">
|
||
<i class="bx bx-map-pin me-1"></i> Trova coordinate sulla mappa
|
||
</button>
|
||
</div>
|
||
|
||
<div class="col-12">
|
||
<div id="map"></div>
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Latitudine</label>
|
||
<input type="text" class="form-control" name="latitude" id="latitude"
|
||
value="<?php echo htmlspecialchars($school['latitude'] ?? ''); ?>" readonly>
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Longitudine</label>
|
||
<input type="text" class="form-control" name="longitude" id="longitude"
|
||
value="<?php echo htmlspecialchars($school['longitude'] ?? ''); ?>" readonly>
|
||
</div>
|
||
|
||
<div class="col-12">
|
||
<hr class="my-4">
|
||
<h6 class="mb-3">Dati amministrativi</h6>
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Nome proprietario / legale</label>
|
||
<input type="text" class="form-control" name="owner_name"
|
||
value="<?php echo htmlspecialchars($school['owner_name'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Partita IVA</label>
|
||
<input type="text" class="form-control" name="vat_number"
|
||
value="<?php echo htmlspecialchars($school['vat_number'] ?? ''); ?>">
|
||
</div>
|
||
|
||
<div class="col-md-6">
|
||
<label class="form-label">Stato</label>
|
||
<select class="form-select" name="status">
|
||
<option value="active" <?php echo ($school['status'] ?? '') === 'active' ? 'selected' : ''; ?>>Attiva</option>
|
||
<option value="inactive" <?php echo ($school['status'] ?? '') === 'inactive' ? 'selected' : ''; ?>>Inattiva</option>
|
||
<option value="suspended" <?php echo ($school['status'] ?? '') === 'suspended' ? 'selected' : ''; ?>>Sospesa</option>
|
||
</select>
|
||
</div>
|
||
|
||
<div class="col-12 mt-5">
|
||
<button type="submit" class="btn btn-primary btn-lg px-5">
|
||
<i class="bx bx-save me-2"></i>
|
||
<?php echo $is_new ? 'Crea Scuola' : 'Salva Modifiche'; ?>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</form>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<?php include('include/footer.php'); ?>
|
||
</div>
|
||
|
||
<?php include('jsinclude.php'); ?>
|
||
|
||
<!-- Quill.js -->
|
||
<script src="https://cdn.jsdelivr.net/npm/quill@2.0.2/dist/quill.js"></script>
|
||
|
||
<!-- Leaflet -->
|
||
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css" />
|
||
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
|
||
|
||
<script>
|
||
// ====================== QUILL EDITOR - colori testo e sfondo visibili ======================
|
||
const quill = new Quill('#quill-editor', {
|
||
theme: 'snow',
|
||
modules: {
|
||
toolbar: [
|
||
['bold', 'italic', 'underline', 'strike'],
|
||
['blockquote', 'code-block'],
|
||
[{
|
||
'header': [1, 2, 3, false]
|
||
}],
|
||
[{
|
||
'color': [
|
||
'#000000', '#434343', '#666666', '#999999', '#b7b7b7', '#cccccc', '#d9d9d9', '#ffffff',
|
||
'#ff0000', '#ff9900', '#ffff00', '#00ff00', '#00ffff', '#0000ff', '#9900ff', '#ff00ff',
|
||
'#ffcccc', '#ffe6cc', '#ffffcc', '#ccffcc', '#ccffff', '#ccccff', '#e6ccff', '#ffccff'
|
||
]
|
||
}, {
|
||
'background': [
|
||
'#000000', '#434343', '#666666', '#999999', '#b7b7b7', '#cccccc', '#d9d9d9', '#ffffff',
|
||
'#ff0000', '#ff9900', '#ffff00', '#00ff00', '#00ffff', '#0000ff', '#9900ff', '#ff00ff',
|
||
'#ffcccc', '#ffe6cc', '#ffffcc', '#ccffcc', '#ccffff', '#ccccff', '#e6ccff', '#ffccff'
|
||
]
|
||
}],
|
||
[{
|
||
'list': 'ordered'
|
||
}, {
|
||
'list': 'bullet'
|
||
}],
|
||
[{
|
||
'align': []
|
||
}],
|
||
['link', 'clean']
|
||
]
|
||
}
|
||
});
|
||
|
||
// Carica contenuto iniziale (HTML)
|
||
quill.root.innerHTML = `<?php echo addslashes($school['description'] ?? ''); ?>`;
|
||
|
||
// Salva HTML prima del submit
|
||
document.getElementById('schoolForm').addEventListener('submit', function(e) {
|
||
document.getElementById('description-hidden').value = quill.root.innerHTML;
|
||
});
|
||
|
||
// ====================== MAPPA (invariata, funziona già) ======================
|
||
let map, marker;
|
||
|
||
function initMap(lat = 45.4642, lng = 9.1900, zoom = 12) {
|
||
if (map) map.remove();
|
||
map = L.map('map').setView([lat, lng], zoom);
|
||
L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
|
||
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>'
|
||
}).addTo(map);
|
||
marker = L.marker([lat, lng], {
|
||
draggable: true
|
||
}).addTo(map);
|
||
marker.on('dragend', function(e) {
|
||
const pos = e.target.getLatLng();
|
||
document.getElementById('latitude').value = pos.lat.toFixed(8);
|
||
document.getElementById('longitude').value = pos.lng.toFixed(8);
|
||
});
|
||
}
|
||
|
||
function updateMap(lat, lng) {
|
||
if (!map) initMap(lat, lng, 16);
|
||
else {
|
||
map.setView([lat, lng], 16);
|
||
marker.setLatLng([lat, lng]);
|
||
}
|
||
document.getElementById('latitude').value = lat.toFixed(8);
|
||
document.getElementById('longitude').value = lng.toFixed(8);
|
||
}
|
||
|
||
document.getElementById('geocodeBtn')?.addEventListener('click', function() {
|
||
const street = document.querySelector('[name="address_street"]').value.trim();
|
||
const city = document.querySelector('[name="address_city"]').value.trim();
|
||
const cap = document.querySelector('[name="address_postal_code"]').value.trim();
|
||
const country = document.querySelector('[name="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?.length > 0) {
|
||
const loc = data[0];
|
||
updateMap(parseFloat(loc.lat), parseFloat(loc.lon));
|
||
} else {
|
||
alert('Indirizzo non trovato.');
|
||
}
|
||
})
|
||
.catch(() => alert('Errore durante la ricerca.'));
|
||
});
|
||
|
||
// Init mappa
|
||
document.addEventListener('DOMContentLoaded', function() {
|
||
const lat = parseFloat(document.getElementById('latitude').value);
|
||
const lng = parseFloat(document.getElementById('longitude').value);
|
||
if (!isNaN(lat) && !isNaN(lng)) {
|
||
initMap(lat, lng, 16);
|
||
} else {
|
||
initMap();
|
||
}
|
||
});
|
||
|
||
// Slug automatico
|
||
const nameField = document.querySelector('[name="name"]');
|
||
const slugField = document.querySelector('[name="slug"]');
|
||
let slugTouched = false;
|
||
|
||
nameField?.addEventListener('input', function() {
|
||
if (!slugTouched) slugField.value = generateSlug(this.value);
|
||
});
|
||
|
||
slugField?.addEventListener('input', () => slugTouched = true);
|
||
</script>
|
||
</body>
|
||
|
||
</html>
|