getConnection(); // Verifica utente loggato if (!isset($iduserlogin)) { header("Location: login.php"); exit; } // Controlla se esiste almeno un salone $stmt = $pdo->prepare("SELECT COUNT(*) FROM shops WHERE owner_id = ?"); $stmt->execute([$iduserlogin]); if ((int)$stmt->fetchColumn() === 0) { header("Location: onboarding_salon.php"); exit; } // Prendi il salone $stmt = $pdo->prepare(" SELECT * FROM shops WHERE owner_id = ? ORDER BY created_at ASC LIMIT 1 "); $stmt->execute([$iduserlogin]); $shop = $stmt->fetch(PDO::FETCH_ASSOC); if (!$shop) { die("Errore: salone non trovato."); } $shop_id = (int)$shop['id']; // =========================== // Helper: Flash messages // =========================== function setFlash(string $type, string $text): void { $_SESSION['flash'] = ['type' => $type, 'text' => $text]; } function getFlash(): ?array { if (!isset($_SESSION['flash'])) return null; $f = $_SESSION['flash']; unset($_SESSION['flash']); return $f; } // =========================== // Helper: Ridimensiona immagine (come nel tuo template) // =========================== function resizeImage($source_path, $dest_path, $max_width = 800) { list($width, $height, $type) = getimagesize($source_path); if ($width <= $max_width) { copy($source_path, $dest_path); return; } $new_width = $max_width; $new_height = (int)(($height * $new_width) / $width); switch ($type) { case IMAGETYPE_JPEG: $source = imagecreatefromjpeg($source_path); break; case IMAGETYPE_PNG: $source = imagecreatefrompng($source_path); break; case IMAGETYPE_GIF: $source = imagecreatefromgif($source_path); break; default: throw new Exception("Formato non supportato."); } $dest = imagecreatetruecolor($new_width, $new_height); if ($type == IMAGETYPE_PNG) { imagealphablending($dest, false); imagesavealpha($dest, true); } imagecopyresampled($dest, $source, 0, 0, 0, 0, $new_width, $new_height, $width, $height); switch ($type) { case IMAGETYPE_JPEG: imagejpeg($dest, $dest_path, 90); break; case IMAGETYPE_PNG: imagepng($dest, $dest_path); break; case IMAGETYPE_GIF: imagegif($dest, $dest_path); break; } imagedestroy($source); imagedestroy($dest); } // =========================== // Helper: Geolocalizzazione con Nominatim // =========================== function geocodeAddress(string $address): array { $url = 'https://nominatim.openstreetmap.org/search'; $params = http_build_query([ 'q' => $address, 'format' => 'json', 'limit' => 1, 'addressdetails' => 1, 'countrycodes' => 'it', ]); $ch = curl_init($url . '?' . $params); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_USERAGENT, 'HairBook Salon Manager/1.0 (contact: info@hairbook.it)'); curl_setopt($ch, CURLOPT_TIMEOUT, 10); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode !== 200 || !$response) { return ['success' => false, 'message' => 'Errore connessione Nominatim']; } $data = json_decode($response, true); if (empty($data) || !isset($data[0])) { return ['success' => false, 'message' => 'Indirizzo non trovato']; } $result = $data[0]; return [ 'success' => true, 'lat' => (float)$result['lat'], 'lon' => (float)$result['lon'], 'display' => $result['display_name'] ?? '' ]; } // =========================== // POST - Salva profilo + geocode se necessario // =========================== $flash = null; if ($_SERVER['REQUEST_METHOD'] === 'POST' && !isset($_POST['action'])) { // normale submit form try { $name = trim($_POST['name'] ?? ''); $description = trim($_POST['description'] ?? ''); $address = trim($_POST['address'] ?? ''); $address_extra = trim($_POST['address_extra'] ?? ''); $city = trim($_POST['city'] ?? ''); $province = strtoupper(trim($_POST['province'] ?? '')); $zip_code = trim($_POST['zip_code'] ?? ''); $phone = trim($_POST['phone'] ?? ''); $mobile = trim($_POST['mobile'] ?? ''); $email = trim($_POST['email'] ?? ''); $website = trim($_POST['website'] ?? ''); $instagram = trim(ltrim($_POST['instagram'] ?? '', '@')); $facebook = trim($_POST['facebook'] ?? ''); $google_maps_url = trim($_POST['google_maps_url'] ?? ''); $latitude = !empty($_POST['latitude']) ? (float)$_POST['latitude'] : null; $longitude = !empty($_POST['longitude']) ? (float)$_POST['longitude'] : null; $active = isset($_POST['active']) ? 1 : 0; // Validazioni if ($name === '') { throw new Exception("Nome salone obbligatorio."); } if ($city === '') { throw new Exception("Città obbligatoria."); } // Tenta geocode automatico se lat/lon vuoti if ($latitude === null || $longitude === null) { $full_address = implode(', ', array_filter([ $address, $address_extra, $zip_code, $city, $province ? $province : '', 'Italia' ])); $geocode = geocodeAddress($full_address); if ($geocode['success']) { $latitude = $geocode['lat']; $longitude = $geocode['lon']; setFlash('info', "Coordinate calcolate automaticamente!"); } } // Upload logo $logo = $shop['logo']; if (isset($_FILES['logo']) && $_FILES['logo']['error'] === UPLOAD_ERR_OK) { $file = $_FILES['logo']; $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) { $new_filename = "logos/{$shop_id}-" . time() . "." . $ext; resizeImage($file['tmp_name'], $new_filename, 400); if ($logo && file_exists($logo)) @unlink($logo); $logo = $new_filename; } } // Upload cover $cover = $shop['cover_image']; if (isset($_FILES['cover_image']) && $_FILES['cover_image']['error'] === UPLOAD_ERR_OK) { $file = $_FILES['cover_image']; $ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION)); if (in_array($ext, ['jpg', 'jpeg', 'png', 'gif'])) { $new_filename = "covers/{$shop_id}-" . time() . "." . $ext; resizeImage($file['tmp_name'], $new_filename, 1200); if ($cover && file_exists($cover)) @unlink($cover); $cover = $new_filename; } } // Update DB $stmt = $pdo->prepare(" UPDATE shops SET name = ?, description = ?, address = ?, address_extra = ?, city = ?, province = ?, zip_code = ?, phone = ?, mobile = ?, email = ?, website = ?, instagram = ?, facebook = ?, google_maps_url = ?, latitude = ?, longitude = ?, logo = ?, cover_image = ?, active = ?, updated_at = NOW() WHERE id = ? AND owner_id = ? "); $ok = $stmt->execute([ $name, $description ?: null, $address, $address_extra ?: null, $city, $province, $zip_code, $phone ?: null, $mobile ?: null, $email ?: null, $website ?: null, $instagram ?: null, $facebook ?: null, $google_maps_url ?: null, $latitude, $longitude, $logo, $cover, $active, $shop_id, $iduserlogin ]); setFlash($ok ? 'success' : 'danger', $ok ? "Profilo aggiornato!" : "Errore salvataggio."); header("Location: salon_profile.php"); exit; } catch (Exception $e) { setFlash('danger', $e->getMessage()); header("Location: salon_profile.php"); exit; } } // Flash $flash = getFlash(); ?> Profilo Salone - <?= htmlspecialchars($shop['name']) ?>
Modifica Profilo Salone
Dashboard
@
Logo
Cover
Clicca per calcolare lat/lon da indirizzo (usa OpenStreetMap)
>
false, 'message' => 'Indirizzo vuoto']); exit; } $result = geocodeAddress($addr); header('Content-Type: application/json'); echo json_encode($result); exit; } ?>