83 lines
3.1 KiB
PHP
83 lines
3.1 KiB
PHP
<?php
|
||
/**
|
||
* Refugien-Planer (sanctuary.php, includes/sanctuary.php): eigenen Eintrag speichern (Zeitfenster, je Charakter die
|
||
* Refugien, Werte von Hand und Notiz), mit "Stimmt noch" bestaetigen oder entfernen.
|
||
*/
|
||
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'] ?? '');
|
||
|
||
if ($action === 'confirm') {
|
||
if (sanctuary_confirm($userId)) {
|
||
set_flash('success', 'Bestätigt – dein Eintrag gilt wieder ' . SANCTUARY_EXPIRE_DAYS . ' Tage.');
|
||
}
|
||
redirect('/sanctuary.php');
|
||
}
|
||
|
||
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, aion_char_id FROM member_characters WHERE user_id = ?');
|
||
$stmt->execute([$userId]);
|
||
$raidsInput = is_array($_POST['raids'] ?? null) ? $_POST['raids'] : [];
|
||
$scoreInput = is_array($_POST['gs'] ?? null) ? $_POST['gs'] : [];
|
||
$powerInput = is_array($_POST['cp'] ?? null) ? $_POST['cp'] : [];
|
||
$noteInput = is_array($_POST['note'] ?? null) ? $_POST['note'] : [];
|
||
$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.';
|
||
}
|
||
$note = is_string($noteInput[$id] ?? null) ? trim((string) preg_replace('/\s+/u', ' ', $noteInput[$id])) : '';
|
||
$entries[$id] = [
|
||
'raids' => $raids,
|
||
'gearscore' => $score === false ? null : $score,
|
||
'combat_power' => $power === false ? null : $power,
|
||
'note' => mb_substr($note, 0, SANCTUARY_NOTE_MAX),
|
||
];
|
||
}
|
||
|
||
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, danach bitte kurz bestätigen.');
|
||
redirect('/sanctuary.php');
|