yogiboook_new/public/userarea/school_onboarding.php
2026-01-28 20:14:49 +01:00

235 lines
8.8 KiB
PHP

<?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();
$user_id = (int)($iduserlogin ?? $_SESSION['iduserlogin'] ?? 0);
if ($user_id <= 0) {
header('Location: login.php');
exit;
}
// solo school_owner (admin ok)
if (!(Auth::user()->hasRole('school_owner') || Auth::user()->hasRole('Admin'))) {
die("Access denied");
}
// Se ha già una scuola, setta session e vai in dashboard
$stmt = $pdo->prepare("SELECT id FROM schools WHERE owner_id = ? ORDER BY id DESC LIMIT 1");
$stmt->execute([$user_id]);
$existing = $stmt->fetch(PDO::FETCH_ASSOC);
if ($existing && !empty($existing['id'])) {
$_SESSION['school_id'] = (int)$existing['id'];
header('Location: school_dashboard.php');
exit;
}
function makeSlug($str)
{
$str = trim(mb_strtolower($str));
$str = preg_replace('/[^a-z0-9]+/i', '-', $str);
$str = trim($str, '-');
return $str ?: 'school';
}
$success_message = null;
$error = null;
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$name = trim($_POST['name'] ?? '');
$email = trim($_POST['email'] ?? '');
$phone = trim($_POST['phone'] ?? '');
$website = trim($_POST['website'] ?? '');
$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'] ?? 'Italy');
$owner_name = trim($_POST['owner_name'] ?? '');
$vat_number = trim($_POST['vat_number'] ?? '');
if ($name === '' || $email === '' || $address_street === '' || $address_city === '' || $address_postal_code === '' || $address_country === '' || $owner_name === '' || $vat_number === '') {
$error = "Compila tutti i campi obbligatori.";
} else {
$slugBase = makeSlug($name);
$slug = $slugBase;
// slug unico
$check = $pdo->prepare("SELECT COUNT(*) FROM schools WHERE slug = ?");
$i = 1;
while (true) {
$check->execute([$slug]);
if ((int)$check->fetchColumn() === 0) break;
$i++;
$slug = $slugBase . '-' . $i;
}
try {
$pdo->beginTransaction();
$stmtIns = $pdo->prepare("
INSERT INTO schools
(owner_id, name, website, email, phone, description,
address_street, address_city, address_postal_code, address_province, address_country,
owner_name, vat_number, status, slug)
VALUES
(?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?,
?, ?, 'active', ?)
");
$stmtIns->execute([
$user_id,
$name,
($website ?: null),
$email,
($phone ?: null),
($description ?: null),
$address_street,
$address_city,
$address_postal_code,
($address_province ?: null),
$address_country,
$owner_name,
$vat_number,
$slug
]);
$newSchoolId = (int)$pdo->lastInsertId();
// school_settings default
$stmtSet = $pdo->prepare("INSERT INTO school_settings (school_id) VALUES (?)");
$stmtSet->execute([$newSchoolId]);
$pdo->commit();
$_SESSION['school_id'] = $newSchoolId;
header('Location: school_dashboard.php');
exit;
} catch (Throwable $e) {
if ($pdo->inTransaction()) $pdo->rollBack();
$error = "Errore creazione scuola: " . $e->getMessage();
}
}
}
?>
<!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'); ?>
</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">
<h5 class="mb-0">Configurazione iniziale scuola</h5>
<small class="text-muted">Crea la tua scuola per iniziare a usare il pannello proprietario.</small>
</div>
<div class="card-body">
<?php if ($error): ?>
<div class="alert alert-danger"><?= htmlspecialchars($error) ?></div>
<?php endif; ?>
<form method="POST" class="row g-3">
<div class="col-md-8">
<label class="form-label">Nome scuola *</label>
<input type="text" name="name" class="form-control" required>
</div>
<div class="col-md-4">
<label class="form-label">Email scuola *</label>
<input type="email" name="email" class="form-control" required>
</div>
<div class="col-md-4">
<label class="form-label">Telefono</label>
<input type="text" name="phone" class="form-control">
</div>
<div class="col-md-8">
<label class="form-label">Sito web</label>
<input type="text" name="website" class="form-control" placeholder="https://...">
</div>
<div class="col-12">
<label class="form-label">Descrizione</label>
<textarea name="description" class="form-control" rows="3"></textarea>
</div>
<hr class="my-2">
<div class="col-md-6">
<label class="form-label">Indirizzo *</label>
<input type="text" name="address_street" class="form-control" required>
</div>
<div class="col-md-3">
<label class="form-label">Città *</label>
<input type="text" name="address_city" class="form-control" required>
</div>
<div class="col-md-3">
<label class="form-label">CAP *</label>
<input type="text" name="address_postal_code" class="form-control" required>
</div>
<div class="col-md-4">
<label class="form-label">Provincia</label>
<input type="text" name="address_province" class="form-control">
</div>
<div class="col-md-8">
<label class="form-label">Nazione *</label>
<input type="text" name="address_country" class="form-control" value="Italy" required>
</div>
<hr class="my-2">
<div class="col-md-8">
<label class="form-label">Nome intestatario/Proprietario *</label>
<input type="text" name="owner_name" class="form-control" required>
</div>
<div class="col-md-4">
<label class="form-label">P.IVA / VAT *</label>
<input type="text" name="vat_number" class="form-control" required>
</div>
<div class="col-12 d-flex justify-content-end gap-2">
<button type="submit" class="btn btn-primary">
Crea scuola
</button>
</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'); ?>
</body>
</html>