114 lines
4.5 KiB
PHP
114 lines
4.5 KiB
PHP
<?php
|
||
/**
|
||
* Refugien-Planer (sanctuary.php, includes/sanctuary.php): eigenen Eintrag speichern (Zeitfenster, je Charakter
|
||
* Gearscore/Kampfkraft und die Refugien mit ihren Angaben - Raidlead, Shotcaller, Stufe, Fortschritt, Notiz), mit
|
||
* "Stimmt noch" wieder als aktuell bestaetigen oder entfernen; dazu "Heute ausblenden" fuer den Hinweis im Seitenkopf.
|
||
*/
|
||
require_once __DIR__ . '/includes/bootstrap.php';
|
||
require_once __DIR__ . '/includes/sanctuary.php';
|
||
require_login();
|
||
if (!sanctuary_can_use()) {
|
||
show_not_found();
|
||
}
|
||
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
|
||
redirect('/sanctuary.php');
|
||
}
|
||
verify_csrf();
|
||
|
||
$userId = (int) current_user()['id'];
|
||
$action = (string) ($_POST['action'] ?? '');
|
||
// "Stimmt noch" und "Heute ausblenden" kommen auch aus dem Hinweis im Seitenkopf - zurueck nur auf eigene Seiten
|
||
$back = (string) ($_POST['back'] ?? '');
|
||
$back = preg_match('~^/(?!/)[^\s]*$~', $back) ? $back : '/sanctuary.php';
|
||
|
||
if ($action === 'confirm') {
|
||
if (sanctuary_confirm($userId)) {
|
||
set_flash('success', 'Bestätigt – dein Eintrag gilt wieder ' . SANCTUARY_EXPIRE_DAYS . ' Tage als aktuell.');
|
||
}
|
||
redirect($back);
|
||
}
|
||
|
||
if ($action === 'snooze') {
|
||
$_SESSION['sanctuary_notice_snooze'] = date('Y-m-d');
|
||
redirect($back);
|
||
}
|
||
|
||
if ($action === 'remove') {
|
||
sanctuary_remove($userId);
|
||
set_flash('success', 'Dein Eintrag wurde entfernt.');
|
||
redirect('/sanctuary.php');
|
||
}
|
||
|
||
if ($action !== 'save') {
|
||
redirect('/sanctuary.php');
|
||
}
|
||
|
||
[$windows, $errors] = sanctuary_windows_from_input($_POST);
|
||
|
||
// Nur eigene Charaktere - was sonst im Formular steht, zaehlt nicht
|
||
$stmt = db()->prepare('SELECT id, name FROM member_characters WHERE user_id = ?');
|
||
$stmt->execute([$userId]);
|
||
$input = static fn (string $key): array => is_array($_POST[$key] ?? null) ? $_POST[$key] : [];
|
||
$raidsInput = $input('raids');
|
||
$scoreInput = $input('gs');
|
||
$powerInput = $input('cp');
|
||
// Je Charakter und Refugium: lead[Charakter][Refugium] usw.
|
||
$perRaid = static fn (array $values, int $id, string $raid) => is_array($values[$id] ?? null) ? ($values[$id][$raid] ?? null) : null;
|
||
$leadInput = $input('lead');
|
||
$shotInput = $input('shot');
|
||
$difficultyInput = $input('diff');
|
||
$progressInput = $input('progress');
|
||
$noteInput = $input('rnote');
|
||
$entries = [];
|
||
foreach ($stmt->fetchAll() as $character) {
|
||
$id = (int) $character['id'];
|
||
$raids = array_values(array_intersect(array_keys(SANCTUARY_RAIDS), is_array($raidsInput[$id] ?? null) ? $raidsInput[$id] : []));
|
||
if (!$raids) {
|
||
continue;
|
||
}
|
||
$score = sanctuary_number_input($scoreInput[$id] ?? '', SANCTUARY_GEARSCORE_MAX);
|
||
$power = sanctuary_number_input($powerInput[$id] ?? '', SANCTUARY_POWER_MAX);
|
||
if ($score === false) {
|
||
$errors[] = $character['name'] . ': Der Gearscore muss eine Zahl bis ' . number_format(SANCTUARY_GEARSCORE_MAX, 0, ',', '.') . ' sein.';
|
||
}
|
||
if ($power === false) {
|
||
$errors[] = $character['name'] . ': Die Kampfkraft muss eine ganze Zahl sein, z. B. 98986.';
|
||
}
|
||
$details = [];
|
||
foreach ($raids as $raid) {
|
||
$difficulty = $perRaid($difficultyInput, $id, $raid);
|
||
if (!is_string($difficulty) || !isset(SANCTUARY_DIFFICULTIES[$difficulty])) {
|
||
$errors[] = $character['name'] . ', ' . SANCTUARY_RAIDS[$raid]['short'] . ': Wähle bei „Möglich bei“ eine Stufe.';
|
||
$difficulty = '';
|
||
}
|
||
$details[$raid] = [
|
||
'lead' => !empty($perRaid($leadInput, $id, $raid)),
|
||
'shot' => !empty($perRaid($shotInput, $id, $raid)),
|
||
'difficulty' => $difficulty,
|
||
'progress' => sanctuary_text_input($perRaid($progressInput, $id, $raid), SANCTUARY_PROGRESS_MAX),
|
||
'note' => sanctuary_text_input($perRaid($noteInput, $id, $raid), SANCTUARY_NOTE_MAX),
|
||
];
|
||
}
|
||
$entries[$id] = [
|
||
'raids' => $raids,
|
||
'details' => $details,
|
||
'gearscore' => $score === false ? null : $score,
|
||
'combat_power' => $power === false ? null : $power,
|
||
];
|
||
}
|
||
|
||
if (!$entries) {
|
||
$errors[] = 'Wähle bei mindestens einem Charakter ein Refugium aus.';
|
||
}
|
||
if (!$windows && !$errors) {
|
||
$errors[] = 'Trage mindestens ein Zeitfenster ein – Wochentag, Beginn und Ende.';
|
||
}
|
||
if ($errors) {
|
||
set_flash('error', implode(' ', array_unique($errors)));
|
||
redirect('/sanctuary.php#mein-eintrag');
|
||
}
|
||
|
||
sanctuary_save($userId, $windows, $entries);
|
||
set_flash('success', 'Gespeichert – du stehst jetzt im Refugien-Planer. Der Eintrag gilt ' . SANCTUARY_EXPIRE_DAYS . ' Tage als aktuell; '
|
||
. SANCTUARY_WARN_DAYS . ' Tage vorher erinnern wir dich, ihn zu bestätigen.');
|
||
redirect('/sanctuary.php');
|