prepare("INSERT INTO home (iduser) VALUES (?)");
$insertQuery->bind_param("i", $iduserlogin);
$insertQuery->execute();
$idhome = $conn->insert_id; // Ottieni il nuovo idhome generato
// Ricarica la pagina con il nuovo idhome
header("Location: manage-home.php?idhome=$idhome");
exit(); // Assicurati di uscire per evitare che il codice continui
}
// Determina l'iduser corretto per l'immobile, controllando sia il proprietario che le condivisioni
$ownerId = null;
$queryOwner = $conn->prepare("SELECT iduser FROM home WHERE idhome = ?");
$queryOwner->bind_param("i", $idhome);
$queryOwner->execute();
$resultOwner = $queryOwner->get_result();
if ($resultOwner->num_rows > 0) {
$ownerData = $resultOwner->fetch_assoc();
$ownerId = $ownerData['iduser'];
}
// Verifica se l'utente ha accesso diretto (è il proprietario) o tramite condivisione
$hasAccess = false;
if ($ownerId == $iduserlogin) {
$hasAccess = true; // Utente è il proprietario
} else {
// Controlla se l'utente ha accesso tramite home_sharing
$querySharing = $conn->prepare("
SELECT * FROM home_sharing
WHERE idhome = ?
AND (idshareduser = ? OR shared_email = ?)
AND status = 'accepted'
");
$querySharing->bind_param("iis", $idhome, $iduserlogin, $emailuser);
$querySharing->execute();
$resultSharing = $querySharing->get_result();
$hasAccess = ($resultSharing->num_rows > 0);
}
// Se l'utente non ha accesso, reindirizza o mostra un errore
if (!$hasAccess) {
header("Location: access-denied.php"); // O una pagina di errore personalizzata
exit();
}
// Carica i dati della casa per l'utente con accesso
$query = $conn->prepare("SELECT * FROM home WHERE idhome = ?");
$query->bind_param("i", $idhome);
$query->execute();
$result = $query->get_result();
$homeData = $result->fetch_assoc();
// Assegna i valori esistenti ai campi
$namedb = $homeData['name'];
$addressdb = $homeData['address'];
$countrydb = $homeData['country'];
$citydb = $homeData['city'];
$zipdb = $homeData['zip'];
$commentdb = $homeData['comment'];
$latitudedb = $homeData['latitude'];
$longitudedb = $homeData['longitude'];
$fulladdressdb = $homeData['fulladdress'];
// Campi catastali
$cadastral_municipalitydb = $homeData['cadastral_municipality'];
$cadastral_sectiondb = $homeData['cadastral_section'];
$cadastral_sheetdb = $homeData['cadastral_sheet'];
$cadastral_particledb = $homeData['cadastral_particle'];
$cadastral_subdb = $homeData['cadastral_sub'];
$cadastral_categorydb = $homeData['cadastral_category'];
$cadastral_classdb = $homeData['cadastral_class'];
$cadastral_surfacedb = $homeData['cadastral_surface'];
$cadastral_renditadbs = $homeData['cadastral_rendita'];
$cadastral_notesdb = $homeData['cadastral_notes'];
?>
prepare("
SELECT
po.owner_id,
po.first_name,
po.last_name,
po.company_name,
po.tax_code,
po.email,
ho.ownership_percentage,
ho.notes
FROM
home_owners AS ho
INNER JOIN
property_owners AS po ON ho.owner_id = po.owner_id
WHERE
ho.home_id = ?
");
$queryOwners->bind_param('i', $idhome);
$queryOwners->execute();
$resultOwners = $queryOwners->get_result();
// Recupera tutti i proprietari disponibili dell'utente per la selezione
$queryAvailableOwners = $conn->prepare("
SELECT
owner_id,
first_name,
last_name,
company_name,
tax_code
FROM
property_owners
WHERE
user_id = ?
AND owner_id NOT IN (
SELECT owner_id FROM home_owners WHERE home_id = ?
)
");
$queryAvailableOwners->bind_param('ii', $iduserlogin, $idhome);
$queryAvailableOwners->execute();
$resultAvailableOwners = $queryAvailableOwners->get_result();
?>
num_rows > 0) { ?>