fixed different login
This commit is contained in:
@@ -14,17 +14,21 @@ 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 = ?
|
||||
");
|
||||
// Recupera utente
|
||||
$stmt = $pdo->prepare("SELECT id, first_name, last_name, email FROM auth_users WHERE id = ? LIMIT 1");
|
||||
$stmt->execute([$iduserlogin]);
|
||||
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
if (!$user) {
|
||||
die("Errore: Utente non trovato.");
|
||||
}
|
||||
|
||||
// Recupera eventuale scuola del proprietario
|
||||
$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 = empty($school['id']);
|
||||
$is_new = !$school;
|
||||
|
||||
if ($is_new) {
|
||||
$school = [
|
||||
@@ -33,7 +37,7 @@ if ($is_new) {
|
||||
'name' => '',
|
||||
'slug' => '',
|
||||
'website' => '',
|
||||
'email' => '',
|
||||
'email' => $user['email'] ?? '', // default utile
|
||||
'phone' => '',
|
||||
'description' => '',
|
||||
'address_street' => '',
|
||||
@@ -43,16 +47,17 @@ if ($is_new) {
|
||||
'address_country' => 'Italia',
|
||||
'latitude' => null,
|
||||
'longitude' => null,
|
||||
'owner_name' => '',
|
||||
'owner_name' => trim(($user['first_name'] ?? '') . ' ' . ($user['last_name'] ?? '')),
|
||||
'vat_number' => '',
|
||||
'logo' => '',
|
||||
'status' => 'active',
|
||||
'first_name' => '',
|
||||
'last_name' => '',
|
||||
'email' => ''
|
||||
];
|
||||
} else {
|
||||
// se esiste, sincronizza school_id in sessione
|
||||
$_SESSION['school_id'] = (int)$school['id'];
|
||||
}
|
||||
|
||||
|
||||
// Generatore slug
|
||||
function generateSlug($string)
|
||||
{
|
||||
@@ -146,10 +151,27 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
$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]);
|
||||
|
||||
// 1) Prendi ID appena creato
|
||||
$newSchoolId = (int)$pdo->lastInsertId();
|
||||
|
||||
// 2) Salvalo in sessione (FONDAMENTALE per evitare redirect onboarding)
|
||||
$_SESSION['school_id'] = $newSchoolId;
|
||||
|
||||
// 3) Crea record settings base (se non esiste già)
|
||||
try {
|
||||
$stmtSet = $pdo->prepare("INSERT INTO school_settings (school_id) VALUES (?)");
|
||||
$stmtSet->execute([$newSchoolId]);
|
||||
} catch (Exception $e) {
|
||||
// se esiste già, ignoriamo
|
||||
}
|
||||
|
||||
// 4) Ricarica la scuola appena creata
|
||||
$stmt = $pdo->prepare("SELECT * FROM schools WHERE id = ? LIMIT 1");
|
||||
$stmt->execute([$newSchoolId]);
|
||||
$school = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
$success_message = "Scuola creata con successo!";
|
||||
$is_new = false;
|
||||
} else {
|
||||
$error = "Errore creazione scuola.";
|
||||
@@ -205,10 +227,13 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
font-size: 15px;
|
||||
border: 1px solid #ced4da;
|
||||
border-radius: 0.375rem;
|
||||
height: auto;
|
||||
margin-bottom: 14px;
|
||||
}
|
||||
|
||||
.ql-editor {
|
||||
min-height: 260px;
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.form-label {
|
||||
@@ -234,6 +259,24 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
display: block;
|
||||
margin: 0 auto 1rem;
|
||||
}
|
||||
|
||||
/* Placeholder logo quando non presente */
|
||||
.logo-placeholder {
|
||||
width: 100%;
|
||||
max-height: 220px;
|
||||
height: 220px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
border: 1px dashed #cfd4da;
|
||||
border-radius: 8px;
|
||||
background: #f8f9fa;
|
||||
color: #6c757d;
|
||||
font-weight: 700;
|
||||
font-size: 28px;
|
||||
letter-spacing: 1px;
|
||||
margin: 0 auto 1rem;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
@@ -268,12 +311,20 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
<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">
|
||||
<?php if (!empty($school['logo'])): ?>
|
||||
<img id="logoPreview"
|
||||
src="<?php echo htmlspecialchars($school['logo']); ?>"
|
||||
alt="Logo Scuola"
|
||||
class="school-logo mb-3">
|
||||
<?php else: ?>
|
||||
<div id="logoPreviewPlaceholder" class="logo-placeholder">NA</div>
|
||||
<img id="logoPreview" src="" alt="Logo Scuola" class="school-logo mb-3" style="display:none;">
|
||||
<?php endif; ?>
|
||||
|
||||
|
||||
<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">
|
||||
<input type="file" class="form-control" name="logo" id="logoInput" accept="image/jpeg,image/png,image/gif">
|
||||
<small class="text-muted d-block mt-1">Max 2MB – JPG, PNG, GIF</small>
|
||||
</div>
|
||||
</div>
|
||||
@@ -473,7 +524,9 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
|
||||
// Salva HTML prima del submit
|
||||
document.getElementById('schoolForm').addEventListener('submit', function(e) {
|
||||
document.getElementById('description-hidden').value = quill.root.innerHTML;
|
||||
const html = quill.root.innerHTML.trim();
|
||||
document.getElementById('description-hidden').value = (html === '<p><br></p>' ? '' : html);
|
||||
|
||||
});
|
||||
|
||||
// ====================== MAPPA (invariata, funziona già) ======================
|
||||
@@ -543,16 +596,54 @@ if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
||||
}
|
||||
});
|
||||
|
||||
// Slug automatico
|
||||
// Slug automatico (JS)
|
||||
function slugify(str) {
|
||||
return (str || '')
|
||||
.toString()
|
||||
.normalize('NFD').replace(/[\u0300-\u036f]/g, '') // rimuove accenti
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/[^a-z0-9\s-]/g, '')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/-+/g, '-');
|
||||
}
|
||||
|
||||
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);
|
||||
if (!slugTouched) slugField.value = slugify(this.value);
|
||||
});
|
||||
|
||||
slugField?.addEventListener('input', () => slugTouched = true);
|
||||
|
||||
// ====================== LOGO PREVIEW (before save) ======================
|
||||
const logoInput = document.getElementById('logoInput');
|
||||
const logoPreview = document.getElementById('logoPreview');
|
||||
const logoPlaceholder = document.getElementById('logoPreviewPlaceholder');
|
||||
|
||||
logoInput?.addEventListener('change', function() {
|
||||
const file = this.files && this.files[0];
|
||||
if (!file) return;
|
||||
|
||||
// Basic type check (client-side)
|
||||
if (!['image/jpeg', 'image/png', 'image/gif'].includes(file.type)) {
|
||||
alert('Formato non valido. Usa JPG, PNG o GIF.');
|
||||
this.value = '';
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = new FileReader();
|
||||
reader.onload = function(e) {
|
||||
if (logoPlaceholder) logoPlaceholder.style.display = 'none';
|
||||
if (logoPreview) {
|
||||
logoPreview.src = e.target.result;
|
||||
logoPreview.style.display = 'block';
|
||||
}
|
||||
};
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user