LuminaWebsite/raid_edit_action.php

103 lines
4.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raids_api.php';
require_once __DIR__ . '/includes/raid_attendance.php';
require_permission('raids.manage');
$input = read_json_body();
verify_csrf_token($input['csrf_token'] ?? '');
$raidId = (int) ($input['raid_id'] ?? 0);
if ($raidId <= 0) {
json_response(false, 'Ungültige Anfrage.');
}
$fields = [];
foreach (['title', 'date_raw', 'requirements', 'gearscore', 'group_names'] as $key) {
if (isset($input[$key]) && trim((string) $input[$key]) !== '') {
$fields[$key] = trim((string) $input[$key]);
}
}
// Der Gearscore ist optional - ein geleertes Feld entfernt ihn.
if (isset($input['gearscore']) && trim((string) $input['gearscore']) === '') {
$fields['gearscore'] = '';
}
if (isset($input['max_slots']) && is_numeric($input['max_slots'])) {
$fields['max_slots'] = (int) $input['max_slots'];
}
if (isset($input['locked_groups']) && is_array($input['locked_groups'])) {
$fields['locked_groups'] = array_values(array_map('intval', $input['locked_groups']));
}
if (empty($fields)) {
json_response(false, 'Keine Änderungen übermittelt.');
}
$fieldError = raid_fields_error($fields);
if ($fieldError !== null) {
json_response(false, $fieldError);
}
// Stand vor der Aenderung fuers Audit-Log (alt -> neu)
try {
$before = fetch_raid_by_id($raidId);
$beforePoints = raid_settings_get($raidId);
} catch (Throwable $e) {
$before = null;
$beforePoints = null;
}
try {
$result = update_raid($raidId, $fields);
$newRaidId = (int) ($result['raid_id'] ?? $raidId);
// Einstellungen, Anwesenheit und Vergaben haengen an der raid_id - ggf. mit umziehen.
raid_rename_id($raidId, $newRaidId);
if (isset($input['points_present']) || isset($input['points_absent'])) {
raid_settings_save_points(
$newRaidId,
max(0, min(CONTRIBUTION_MAX_POINTS, (int) ($input['points_present'] ?? 0))),
max(0, min(CONTRIBUTION_MAX_POINTS, (int) ($input['points_absent'] ?? 0)))
);
}
$groupList = static fn ($groups): string => $groups ? implode(', ', array_map('intval', (array) $groups)) : 'keine';
$logFields = ['Raid' => admin_log_raid($newRaidId)];
if ($before !== null) {
$logFields += [
'Titel' => isset($fields['title']) ? admin_log_change($before['title'] ?? '', $fields['title']) : '',
'Termin' => isset($fields['date_raw']) ? admin_log_change($before['date'] ?? '', $fields['date_raw']) : '',
'Anforderungen' => isset($fields['requirements']) ? admin_log_change($before['requirements'] ?? '', $fields['requirements']) : '',
'Gearscore' => isset($fields['gearscore']) ? admin_log_change($before['gearscore'] ?? '', $fields['gearscore']) : '',
'Gruppen' => isset($fields['group_names']) ? admin_log_change($before['groups'] ?? '', $fields['group_names'], 300) : '',
'Plätze' => isset($fields['max_slots']) ? admin_log_change($before['max_slots'] ?? '', $fields['max_slots']) : '',
'Gesperrte Gruppen' => isset($fields['locked_groups'])
? admin_log_change($groupList($before['locked_groups'] ?? []), $groupList($fields['locked_groups'])) : '',
];
} else {
$logFields['Geändert'] = implode(', ', array_keys($fields));
}
if ($beforePoints !== null && (isset($input['points_present']) || isset($input['points_absent']))) {
$logFields['Lumipunkte'] = admin_log_change(
'+' . $beforePoints['points_present'] . ' anwesend / −' . $beforePoints['points_absent'] . ' abwesend',
'+' . max(0, min(CONTRIBUTION_MAX_POINTS, (int) ($input['points_present'] ?? 0))) . ' anwesend / −'
. max(0, min(CONTRIBUTION_MAX_POINTS, (int) ($input['points_absent'] ?? 0))) . ' abwesend'
);
}
if ($newRaidId !== $raidId) {
$logFields['Hinweis'] = 'Discord-Nachricht war gelöscht und wurde neu gepostet';
}
if (count(array_filter($logFields, static fn ($v) => $v !== '')) <= 1) {
$logFields['Änderung'] = 'keine inhaltliche Änderung';
}
admin_log('⚔️ Raid bearbeitet', $logFields);
// Die API postet die Discord-Nachricht automatisch neu, falls sie zwischenzeitlich
// manuell geloescht wurde - die raid_id (= Discord-Message-ID) aendert sich dann.
if ($newRaidId !== $raidId) {
json_response(true, 'Raid wurde aktualisiert. Die Discord-Nachricht war nicht mehr auffindbar und wurde neu gepostet.', ['raid_id' => $newRaidId]);
} else {
json_response(true, 'Raid wurde aktualisiert.', ['raid_id' => $newRaidId]);
}
} catch (Throwable $e) {
json_response(false, 'Speichern fehlgeschlagen: ' . $e->getMessage());
}