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(); } } } ?>
Configurazione iniziale scuola
Crea la tua scuola per iniziare a usare il pannello proprietario.