diff --git a/sternwarte/DB4js_all.php b/sternwarte/DB4js_all.php index c166fac..9657db5 100644 --- a/sternwarte/DB4js_all.php +++ b/sternwarte/DB4js_all.php @@ -917,6 +917,68 @@ class RepoKalender DB::exec("DELETE FROM " . self::TBL . " WHERE id=?", [$id]); return true; } + + public static function updateBeos(int $id, string $mitarbeiter): bool + { + // First, get the existing calendar entry + $existingEntry = DB::one("SELECT * FROM " . self::TBL . " WHERE id=?", [$id]); + + if (!$existingEntry) { + error_log("RepoKalender::updateBeos - Calendar entry with ID {$id} not found."); + return false; + } + + // Extract the original Sonderführung name from the existing title + // Expected format: "WK, SF [Sonderführung Name], [Old Mitarbeiter]" + $oldTitle = $existingEntry['title']; + $sofueName = ''; + if (preg_match('/^WK, SF (.*), .*$/', $oldTitle, $matches)) { + $sofueName = trim($matches[1]); + } + + $newTitle = ''; + if (!empty($sofueName)) { + $newTitle = "WK, SF {$sofueName}, {$mitarbeiter}"; + } else { + // Fallback: If we can't extract the original Sonderführung name, + // we'll try to keep the original structure if possible, or + // simply create a title indicating the BEO update. + // For now, let's just make it clear it's a BEO update. + $newTitle = "Kalender BEO: {$mitarbeiter}"; // More general fallback + error_log("RepoKalender::updateBeos - Could not parse original SF name from title '{$oldTitle}'. Using generic title fallback."); + } + + $sql = "UPDATE " . self::TBL . " SET title=? WHERE id=?"; + DB::exec($sql, [$newTitle, $id]); + error_log("Kalender-Eintrag ID {$id} BEOs aktualisiert zu: {$mitarbeiter} (Titel: '{$newTitle}')"); + return true; + } + + public static function findEntryBySofueIdAndTermin(int $sofueId, string $wtermin): ?int + { + // Fetch Sonderführung details to get its name + $sofue = RepoSoFue::getById($sofueId); + if (!$sofue) { + error_log("RepoKalender::findEntryBySofueIdAndTermin - Sofue ID {$sofueId} not found."); + return null; + } + + $sofueName = trim($sofue['name']); + $searchTitlePart = "WK, SF " . $sofueName; // We'll look for this part in the title + + // Convert wtermin to YYYY-MM-DD H:i format for comparison with kalender.start + $terminDate = new DateTime($wtermin); + $startDateStr = $terminDate->format('Y-m-d H:i'); + + // Find calendar entry that matches the start date and contains the sofue name in its title + // Use LIKE for title matching because the full title includes the BEO's name which might change. + $sql = "SELECT id FROM " . self::TBL . " WHERE start = ? AND title LIKE ?"; + $params = [$startDateStr, "%{$searchTitlePart}%"]; + + $result = DB::one($sql, $params); + + return $result ? (int)$result['id'] : null; + } } // ---- Email Service (einfach) ---- @@ -1013,6 +1075,7 @@ class Commands 'SENDMAIL2BEO' => 'Mail an Mitarbeiter', 'SENDMAIL2LISTE' => 'Mail an Verteiler', 'PUT2KALENDER' => 'Kalender-Eintrag', + 'UPDATE_KALENDER_BEO' => 'Kalender-Eintrag BEOS aktualisieren', 'GET_FDATES' => 'Führungstermine für Kalenderansicht', 'GET_CALENTRIES' => 'Kalendereinträge abrufen', 'PUT_CALENTRY' => 'Kalendereintrag erstellen', @@ -1328,6 +1391,29 @@ Spendenbescheinigung: \t" . $info['spende'] . " ]); error_log('Kalender-Eintrag erstellt: ' . $input['id'] . ' ' . $input['termin'] . ' ' . $input['mitarbeiter']); respond(['success' => true]); + + case 'UPDATE_KALENDER_BEO': + if (!isset($input['id'], $input['mitarbeiter'])) respondError('Missing fields for calendar update'); + + $sofueId = (int)$input['id']; + $mitarbeiter = $input['mitarbeiter']; + + // Fetch Sonderführung details to get wtermin + $sofue = RepoSoFue::getById($sofueId); + if (!$sofue) respondError('Sonderführung not found for calendar update', 404); + + $wtermin = $sofue['wtermin']; + + // Find the calendar entry ID based on sofueId and wtermin + $kalenderId = RepoKalender::findEntryBySofueIdAndTermin($sofueId, $wtermin); + + if (!$kalenderId) { + error_log("UPDATE_KALENDER_BEO: Could not find calendar entry for Sofue ID {$sofueId} and wtermin {$wtermin}."); + respondError('Corresponding calendar entry not found.', 404); + } + + RepoKalender::updateBeos($kalenderId, $mitarbeiter); + respond(['success' => true]); case 'GET_FDATES': // Returns führungen for calendar display if (!isset($input['start'], $input['end'])) respondError('start and end required'); diff --git a/sternwarte/intern/sofue/js/sofue.js b/sternwarte/intern/sofue/js/sofue.js index 308d6f8..0d47128 100644 --- a/sternwarte/intern/sofue/js/sofue.js +++ b/sternwarte/intern/sofue/js/sofue.js @@ -25,7 +25,7 @@ function checkFuehrung(was) { } //Übersicht über alle Stati -//Jeweils nur 5 Einträge, kein Pager +//Jeweils nur 5 Einträge, kein Pager/ function uebersicht() { for (var i=0; i< stati.length; i++) { buildGrid(''+(i+1),15,false); @@ -630,7 +630,7 @@ function sendmail2beo(id, beo, termin) { doAjaxCall_arr(ajaxURL,cmd,showajaxerg); console.log("Mail gesendet"); // und in den Kalender eintragen - cmd = {cmd: 'PUT2KALENDER', id: id, termin: termin, mitarbeiter: beo} + cmd = {cmd: 'UPDATE_KALENDER_BEO', id: id, mitarbeiter: beo} doAjaxCall_arr(ajaxURL,cmd,showajaxerg); }