180) THEN 1
WHEN priority = 1 OR ((CURRENT_DATE - letzter_besuch) > 60) THEN 2
ELSE 3
END, last_name ASC";
$result = $pdo->query($sql);
$visitEntriesForJs = [];
$households = []; // Gruppierung nach Adresse
while ($row = $result->fetch()) {
$visitEntriesForJs[] = [
'besuch_id' => (int)$row['besuch_id'],
'last_name' => $row['last_name'] ?? '',
'first_name' => $row['first_name'] ?? '',
'birth_date' => $row['birth_date'] ?? '',
'address_street' => $row['address_street'] ?? '',
'address_plz' => $row['address_plz'] ?? '',
'address_link' => $row['address_link'] ?? '',
'phone' => $row['phone'] ?? '',
'comment' => $row['comment'] ?? '',
'letzter_besuch' => $row['letzter_besuch'] ?? '',
'priority' => (int)($row['priority'] ?? 0),
'pr_bezirk' => ($row['pr_bezirk'] === null ? '' : (int)$row['pr_bezirk']),
'reservation' => $row['reservation'],
];
// Gruppierung nach Adresse
$key = ($row['address_street'] ?? '') . '||' . ($row['address_plz'] ?? '');
if (!isset($households[$key])) {
$households[$key] = [
'members' => [],
'address_street' => $row['address_street'] ?? '',
'address_plz' => $row['address_plz'] ?? '',
'address_link' => $row['address_link'] ?? '',
'phone' => $row['phone'] ?? '',
'reservation' => $row['reservation'],
'last_name' => $row['last_name'] ?? '',
];
}
$households[$key]['members'][] = $row;
// Reservation: wenn mindestens eine Person reserviert ist
if ($row['reservation'] !== null) {
$households[$key]['reservation'] = $row['reservation'];
// Name und Profilbild des Reservierenden für Modal-Anzeige
$resIdModal = (int)$row['reservation'];
$stmtResModal = $pdo->prepare("SELECT first_name, last_name FROM user_general_information WHERE user_id = ?");
$stmtResModal->execute([$resIdModal]);
$resUserModal = $stmtResModal->fetch();
$households[$key]['reservation_name'] = trim(($resUserModal['first_name'] ?? '') . ' ' . ($resUserModal['last_name'] ?? ''));
$profileImgModal = glob($_SERVER['DOCUMENT_ROOT']."/intranet_repo/general_files/profile_pictures/{$resIdModal}-*.jpg");
if (!empty($profileImgModal)) {
usort($profileImgModal, function($a, $b) { return filemtime($b) - filemtime($a); });
$households[$key]['reservation_img'] = str_replace($_SERVER['DOCUMENT_ROOT'], '', $profileImgModal[0]);
} else {
$households[$key]['reservation_img'] = '/intranet_repo/general_files/profile_pictures/default.jpg';
}
}
}
?>
|
Name |
Letzter Besuch |
| Keine Einträge vorhanden. |
$household):
$members = $household['members'];
$firstMember = $members[0];
// Status bestimmen (schlimmster Status im Haushalt)
$worstStatus = 'green';
$worstMonths = 0;
$isReserved = ($household['reservation'] !== null);
foreach ($members as $m) {
$mMonths = floor((strtotime(date('Y-m-d')) - strtotime($m['letzter_besuch'])) / (60 * 60 * 24 * 30));
if ($mMonths > $worstMonths) $worstMonths = $mMonths;
if ((int)$m['priority'] == 2 || $mMonths >= 6) { $worstStatus = 'red'; }
elseif (($worstStatus !== 'red') && ((int)$m['priority'] == 1 || $mMonths >= 2)) { $worstStatus = 'orange'; }
}
if ($isReserved) $worstStatus = 'blue';
// Status-Label
$statusLabels = ['blue' => 'Geplant', 'red' => 'Dringend', 'orange' => 'Fällig', 'green' => 'Aktuell'];
$statusLabel = $statusLabels[$worstStatus] ?? '';
// Name: Nachname, Vorname1 & Vorname2 ...
$lastName = $firstMember['last_name'];
$firstNames = array_map(function($m) { return $m['first_name']; }, $members);
$nameDisplay = htmlspecialchars($lastName) . ', ' . htmlspecialchars(implode(' & ', $firstNames));
// Letzter Besuch (ältester im Haushalt)
$oldestVisit = null;
foreach ($members as $m) {
if ($oldestVisit === null || strtotime($m['letzter_besuch']) < strtotime($oldestVisit)) {
$oldestVisit = $m['letzter_besuch'];
}
}
$daysSince = floor((strtotime(date('Y-m-d')) - strtotime($oldestVisit)) / (60 * 60 * 24));
$weeksSince = floor($daysSince / 7);
$monthsSince = floor($daysSince / 30);
if ($oldestVisit === '2024-07-11') {
$visitText = 'Initialer Eintrag';
} elseif ($daysSince >= 25) {
$visitText = ($monthsSince == 1) ? 'vor einem Monat' : "vor {$monthsSince} Monaten";
} elseif ($daysSince >= 7) {
$visitText = ($weeksSince == 1) ? 'vor einer Woche' : "vor {$weeksSince} Wochen";
} else {
$visitText = ($daysSince == 1) ? 'vor einem Tag' : "vor {$daysSince} Tagen";
}
// Reservierender
$reserverHtml = '';
if ($isReserved && (int)$household['reservation'] != (int)$current_user_id) {
$resId = (int)$household['reservation'];
$profileImg = glob($_SERVER['DOCUMENT_ROOT']."/intranet_repo/general_files/profile_pictures/{$resId}-*.jpg");
if (!empty($profileImg)) {
usort($profileImg, function($a, $b) { return filemtime($b) - filemtime($a); });
$resImgPath = str_replace($_SERVER['DOCUMENT_ROOT'], '', $profileImg[0]);
} else {
$resImgPath = '/intranet_repo/general_files/profile_pictures/default.jpg';
}
$stmtRes = $pdo->prepare("SELECT first_name, last_name FROM user_general_information WHERE user_id = ?");
$stmtRes->execute([$resId]);
$resUser = $stmtRes->fetch();
$resName = trim(($resUser['first_name'] ?? '') . ' ' . ($resUser['last_name'] ?? ''));
if (empty($resName)) $resName = 'Geplant';
$reserverHtml = " . ")
Geplant von " . htmlspecialchars($resName) . "
";
} elseif ($isReserved && (int)$household['reservation'] == (int)$current_user_id) {
$reserverHtml = "";
}
// JSON data for modal
$householdJson = htmlspecialchars(json_encode([
'members' => array_map(function($m) use ($priests, $priestImages) {
$dob = new DateTime($m['birth_date']);
$age = (new DateTime())->diff($dob)->y;
$priestName = '';
$priestId = 0;
$priestImg = '/intranet_repo/general_files/profile_pictures/default.jpg';
foreach ($priests as $p) {
if ((int)$p['user_id'] === (int)$m['pr_bezirk']) {
$first = trim($p['first_name'] ?? '');
$last = trim($p['last_name'] ?? '');
$priestName = trim("$first $last");
$priestId = (int)$p['user_id'];
$priestImg = $priestImages[$priestId] ?? '/intranet_repo/general_files/profile_pictures/default.jpg';
break;
}
}
return [
'besuch_id' => (int)$m['besuch_id'],
'last_name' => $m['last_name'],
'first_name' => $m['first_name'],
'birth_date' => $m['birth_date'],
'birth_formatted' => date('d.m.Y', strtotime($m['birth_date'])),
'age' => $age,
'phone' => $m['phone'] ?? '',
'comment' => $m['comment'] ?? '',
'priority' => (int)($m['priority'] ?? 0),
'pr_bezirk' => ($m['pr_bezirk'] === null ? '' : (int)$m['pr_bezirk']),
'priest_name' => $priestName,
'priest_id' => $priestId,
'priest_img' => $priestImg,
'address_street' => $m['address_street'] ?? '',
'address_plz' => $m['address_plz'] ?? '',
'address_link' => $m['address_link'] ?? '',
'letzter_besuch' => $m['letzter_besuch'] ?? '',
'reservation' => $m['reservation'],
];
}, $members),
'address_street' => $household['address_street'],
'address_plz' => $household['address_plz'],
'address_link' => $household['address_link'],
'phone' => $household['phone'],
'reservation' => $household['reservation'],
'reservation_name' => $household['reservation_name'] ?? '',
'reservation_img' => $household['reservation_img'] ?? '',
'status' => $worstStatus,
], JSON_UNESCAPED_UNICODE | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT), ENT_QUOTES);
?>
|
|
= $nameDisplay ?>
= $statusLabel ?>
|
= $visitText ?>
= $reserverHtml ?>
|