- New permission raids.create (granted on update to every group that has raids.view): whoever creates a raid on the website becomes its raid lead (raid_settings.lead_user_id, schema v49), shown with a crown on the raid card and in the signup list. - The raid lead manages their own raid: edit, move players, invite premades, fill in the attendance list and archive it. Restoring, deleting for good and the Lumipunkte stay with the leadership (raids.manage, raids.points). Checked on the server via raid_can_manage() in every raid action. - Raids created by a member without raids.points bring no automatic points (0/0) until the leadership sets them when editing; raids of the leadership keep the fixed +1. - The attendance reminder of the bot (bot_api.php) goes to the raid lead, even if not signed up; raids without a lead (created in Discord) remind the signed-up raids.manage members as before. - Core permission descriptions are cut to the 255 characters of the column when synced - a longer one made the schema sync throw on every request (found while testing this change). - Privacy policy (raid lead is visible to all), data export, patch notes, README and the tour updated; raid_created_at() folded into raid_posted_at(). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
149 lines
7.2 KiB
PHP
149 lines
7.2 KiB
PHP
<?php
|
||
require_once __DIR__ . '/includes/bootstrap.php';
|
||
require_once __DIR__ . '/includes/raids_api.php';
|
||
require_once __DIR__ . '/includes/raid_attendance.php';
|
||
require_login();
|
||
|
||
$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.');
|
||
}
|
||
// Die Leitung jeden Raid, der Raid-Lead seinen eigenen (die Lumipunkte aber nur mit raids.points, s.u.)
|
||
if (!raid_can_manage($raidId)) {
|
||
http_response_code(403);
|
||
json_response(false, 'Diesen Raid darfst du nicht bearbeiten.');
|
||
}
|
||
|
||
// Stand vor der Aenderung: fuer unveraenderte Altwerte (siehe unten) und fuers Audit-Log (alt -> neu)
|
||
try {
|
||
$before = fetch_raid_by_id($raidId);
|
||
$beforePoints = raid_settings_get($raidId);
|
||
} catch (Throwable $e) {
|
||
$before = null;
|
||
$beforePoints = null;
|
||
}
|
||
|
||
$fields = [];
|
||
foreach (['title', 'date_raw', 'requirements', 'group_names'] as $key) {
|
||
if (isset($input[$key]) && trim((string) $input[$key]) !== '') {
|
||
$fields[$key] = trim((string) $input[$key]);
|
||
}
|
||
}
|
||
// Unveraenderte Gruppen nicht erneut schicken: Premades benennen Gruppen um, dabei kann die Liste
|
||
// laenger werden, als die API beim Speichern erlaubt - das soll andere Aenderungen nicht blockieren.
|
||
$groupNamesOf = static fn (string $names): array => array_values(array_filter(array_map('trim', explode(',', $names)), static fn (string $n): bool => $n !== ''));
|
||
if (isset($fields['group_names']) && $before !== null && $groupNamesOf($fields['group_names']) === $groupNamesOf((string) ($before['groups'] ?? ''))) {
|
||
unset($fields['group_names']);
|
||
}
|
||
// Der Gearscore ist optional - ein geleertes Feld entfernt ihn. Ein unveraenderter alter Wert
|
||
// (frueher war Freitext erlaubt) blockiert das Speichern anderer Aenderungen nicht.
|
||
if (isset($input['gearscore'])) {
|
||
$rawGearscore = trim((string) $input['gearscore']);
|
||
if ($before === null || $rawGearscore !== trim((string) ($before['gearscore'] ?? ''))) {
|
||
[$gearscore, $gearscoreError] = raid_normalize_gearscore($rawGearscore);
|
||
if ($gearscoreError !== null) {
|
||
json_response(false, $gearscoreError);
|
||
}
|
||
$fields['gearscore'] = $gearscore;
|
||
}
|
||
}
|
||
// Max. Spieler haengt an der Zahl der Gruppen (je 5 Plaetze) - geprueft wird, sobald sich eins
|
||
// von beiden aendert.
|
||
$beforeGroupCount = $before !== null ? raid_group_count((string) ($before['groups'] ?? '')) : 0;
|
||
$groupCount = isset($fields['group_names']) ? raid_group_count($fields['group_names']) : $beforeGroupCount;
|
||
if (isset($fields['group_names']) && $groupCount > RAID_MAX_GROUPS) {
|
||
json_response(false, 'Höchstens ' . RAID_MAX_GROUPS . ' Gruppen pro Raid.');
|
||
}
|
||
if (isset($input['max_slots'])) {
|
||
$rawSlots = trim((string) $input['max_slots']);
|
||
$unchanged = $before !== null && $groupCount === $beforeGroupCount
|
||
&& $rawSlots === (string) (int) ($before['max_slots'] ?? 0);
|
||
if (!$unchanged) {
|
||
$slotsError = raid_slots_error($rawSlots, $groupCount);
|
||
if ($slotsError !== null) {
|
||
json_response(false, $slotsError);
|
||
}
|
||
$fields['max_slots'] = (int) $rawSlots;
|
||
}
|
||
}
|
||
if (isset($input['locked_groups']) && is_array($input['locked_groups'])) {
|
||
$fields['locked_groups'] = array_values(array_map('intval', $input['locked_groups']));
|
||
}
|
||
|
||
// Lumipunkte aendert nur, wer raids.points hat - alle anderen lassen die Einstellung unangetastet.
|
||
$pointsPresent = null;
|
||
$pointsAbsent = null;
|
||
if (user_has_permission('raids.points') && (isset($input['points_present']) || isset($input['points_absent']))) {
|
||
$pointsPresent = raid_points_input($input['points_present'] ?? 0);
|
||
$pointsAbsent = raid_points_input($input['points_absent'] ?? 0);
|
||
$pointsError = $pointsPresent === null || $pointsAbsent === null
|
||
? 'Lumipunkte: bitte ganze Zahlen von 0 bis ' . RAID_POINTS_MAX . ' angeben.'
|
||
: raid_points_error($pointsPresent, $pointsAbsent);
|
||
if ($pointsError !== null) {
|
||
json_response(false, $pointsError);
|
||
}
|
||
}
|
||
$pointsChanged = $pointsPresent !== null
|
||
&& ($beforePoints === null || $pointsPresent !== $beforePoints['points_present'] || $pointsAbsent !== $beforePoints['points_absent']);
|
||
|
||
if (empty($fields) && !$pointsChanged) {
|
||
json_response(false, 'Keine Änderungen übermittelt.');
|
||
}
|
||
$fieldError = raid_fields_error($fields);
|
||
if ($fieldError !== null) {
|
||
json_response(false, $fieldError);
|
||
}
|
||
|
||
try {
|
||
$result = !empty($fields) ? update_raid($raidId, $fields) : ['raid_id' => $raidId];
|
||
$newRaidId = (int) ($result['raid_id'] ?? $raidId);
|
||
|
||
// Einstellungen, Anwesenheit und Vergaben haengen an der raid_id - ggf. mit umziehen.
|
||
raid_rename_id($raidId, $newRaidId);
|
||
if ($pointsChanged) {
|
||
raid_settings_save_points($newRaidId, $pointsPresent, $pointsAbsent);
|
||
}
|
||
|
||
$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 ($pointsChanged) {
|
||
$logFields['Lumipunkte'] = admin_log_change(
|
||
$beforePoints !== null ? '+' . $beforePoints['points_present'] . ' anwesend / −' . $beforePoints['points_absent'] . ' abwesend' : '',
|
||
'+' . $pointsPresent . ' anwesend / −' . $pointsAbsent . ' 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());
|
||
}
|