LuminaWebsite/raid_template_action.php
2026-10-13 21:16:54 +02:00

43 lines
1.8 KiB
PHP

<?php
/** Raid-Vorlagen speichern und loeschen (Formular "Neuen Raid erstellen" auf raids.php, includes/raid_templates.php). */
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/raid_templates.php';
require_login();
$input = read_json_body();
verify_csrf_token($input['csrf_token'] ?? '');
$area = (string) ($input['bereich'] ?? 'legion');
// Vorlagen sind Sache der Raid-Leitung (raids.manage bzw. consilium.raids.manage)
if (!in_array($area, ['legion', 'consilium'], true) || !raid_templates_allowed($area)) {
http_response_code(403);
json_response(false, 'Raid-Vorlagen kann nur die Raid-Leitung verwalten.');
}
$action = (string) ($input['action'] ?? '');
try {
if ($action === 'save') {
$name = trim((string) preg_replace('/\s+/u', ' ', (string) ($input['name'] ?? '')));
if ($name === '' || mb_strlen($name, 'UTF-8') > RAID_TEMPLATE_NAME_MAX) {
json_response(false, 'Der Name der Vorlage muss 1 bis ' . RAID_TEMPLATE_NAME_MAX . ' Zeichen lang sein.');
}
[$data, $error] = raid_template_clean((array) ($input['data'] ?? []));
$error ??= raid_template_save($area, $name, $data);
if ($error !== null) {
json_response(false, $error);
}
json_response(true, 'Vorlage „' . $name . '“ gespeichert.', ['templates' => raid_templates_for_area($area)]);
}
if ($action === 'delete') {
$error = raid_template_delete($area, (int) ($input['id'] ?? 0));
if ($error !== null) {
json_response(false, $error);
}
json_response(true, 'Vorlage gelöscht.', ['templates' => raid_templates_for_area($area)]);
}
} catch (PDOException $e) {
json_response(false, 'Die Vorlage konnte nicht gespeichert werden (Datenbank).');
}
http_response_code(400);
json_response(false, 'Unbekannte Aktion.');