407 lines
20 KiB
PHP
407 lines
20 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="en">
|
|
|
|
<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'); ?>
|
|
</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" role="alert">
|
|
<?php echo $success_message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if (isset($error)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo $error; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form method="POST" enctype="multipart/form-data">
|
|
<div class="row">
|
|
<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 Scuola" 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>
|
|
<div class="col-md-8">
|
|
<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, es. yogiboook.com/slug)</label>
|
|
<input type="text" class="form-control" id="slug" name="slug" value="<?php echo htmlspecialchars($school['slug'] ?? ''); ?>" required>
|
|
<small class="form-text text-muted">Usa solo lettere minuscole, numeri e trattini (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>
|
|
<div class="mb-3">
|
|
<label for="address_street" class="form-label">Via</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'] ?? ''); ?>">
|
|
</div>
|
|
</div>
|
|
<div class="row">
|
|
<div class="col-md-6 mb-3">
|
|
<label for="latitude" class="form-label">Latitudine</label>
|
|
<input type="number" step="any" class="form-control" id="latitude" name="latitude" value="<?php echo htmlspecialchars($school['latitude'] ?? ''); ?>">
|
|
</div>
|
|
<div class="col-md-6 mb-3">
|
|
<label for="longitude" class="form-label">Longitudine</label>
|
|
<input type="number" step="any" class="form-control" id="longitude" name="longitude" value="<?php echo htmlspecialchars($school['longitude'] ?? ''); ?>">
|
|
</div>
|
|
</div>
|
|
<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">Data Creazione</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"><?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'); ?>
|
|
|
|
<!-- Script per validazione e suggerimento dello slug -->
|
|
<script>
|
|
// Funzione per generare uno slug valido
|
|
function generateSlug(string) {
|
|
return string
|
|
.toLowerCase() // Converti in minuscolo
|
|
.replace(/[^a-z0-9-]+/g, '-') // Sostituisci caratteri non validi con trattini
|
|
.replace(/-+/g, '-') // Rimuovi trattini multipli
|
|
.replace(/^-|-$/g, ''); // Rimuovi trattini all'inizio e alla fine
|
|
}
|
|
|
|
// Riferimenti ai campi
|
|
const nameInput = document.getElementById('name');
|
|
const slugInput = document.getElementById('slug');
|
|
let isUserTypingSlug = false; // Flag per tracciare se l'utente sta modificando lo slug manualmente
|
|
|
|
// Suggerimento dello slug basato sul nome
|
|
nameInput.addEventListener('input', function(e) {
|
|
if (!isUserTypingSlug) { // Aggiorna lo slug solo se l'utente non lo sta modificando manualmente
|
|
const name = e.target.value;
|
|
const generatedSlug = generateSlug(name);
|
|
slugInput.value = generatedSlug;
|
|
}
|
|
});
|
|
|
|
// Validazione dello slug in tempo reale
|
|
slugInput.addEventListener('input', function(e) {
|
|
isUserTypingSlug = true; // L'utente sta modificando lo slug manualmente
|
|
let value = e.target.value;
|
|
value = generateSlug(value); // Applica le regole di validazione
|
|
e.target.value = value; // Aggiorna il campo con il valore validato
|
|
});
|
|
|
|
// Ripristina il flag quando l'utente smette di modificare lo slug
|
|
slugInput.addEventListener('blur', function() {
|
|
isUserTypingSlug = false; // L'utente ha finito di modificare lo slug
|
|
});
|
|
</script>
|
|
</body>
|
|
|
|
</html>
|