LuminaWebsite/contributions_action.php
Maaxxs 5aaf44eaec Consilium Elyseum: allied legions get their own view - own raids, calendar entries, member tab and shared live map sessions
Asked for: alliances ("Consilium Elyseum") with their own view, a member-list tab with the allied legions and their
members, an own raid area, a calendar that shows them only alliance entries, and live map sessions the legion can
share with them - watching only, or drawing only with an extra permission. There is one Discord role "Consilium
Elyseum" for all allies, so which legion each ally belongs to is stored on the website.

- Permissions consilium.view / consilium.draw / consilium.manage. An account with consilium.view but without
  dashboard.view is a guest of the Consilium (consilium_only(), sql_is_consilium_only()) and sees only the alliance
  area; raids, calendar and map stay restricted even with extra rights like raids.view or map.view.
- includes/consilium.php; consilium.php is the allies' start page instead of the dashboard (next raids and entries,
  legions, running shared map sessions); admin/consilium.php: setup hints, allied legions, member assignment.
  users.consilium_legion_id, table consilium_legions (schema 69); allies pick their legion under Mein Profil.
- Members: new tab "Consilium Elyseum" (legions with their members), the only tab allies see. Legion-only lists
  (dues, Lumipunkte, member pickers, raid and activity points) leave allies out.
- Raids: two areas, legion and Consilium, told apart by the Discord channel ("Consilium-Raids" under Verwaltung ->
  Discord-Bot, settings.consilium_raid_channel; the bot API returns channel_id). Allies only see and sign up for
  Consilium raids, which give no Lumipunkte; raids created in the Consilium area are posted to its channel.
- Calendar: audience "Consilium Elyseum". Allies see only those entries and the Consilium raids (also in their iCal
  subscription), their own entries are always Consilium entries; the public calendar shows no Consilium raids; the
  legion sees them marked with 🤝.
- Map: whoever may edit a public tactic shares its running live session with the Consilium (consilium_share, logged).
  The share lives in the bot's live relay and only for that session: it ends with "Freigabe beenden" or 60 s after
  the last legion member left; making the tactic private or deleting it ends it as well. Allies see only running
  shared sessions under Karte (and on their start page), join and watch; with consilium.draw they add strokes,
  areas, points and text - no figures, nothing moved, erased or undone, no tactics, steps, copies or history.
  Enforced in tactics_api.php (guest branch: shared tactics only, only live_token and shape_create) and in the relay
  (guest tickets, event whitelist, guests are disconnected when the share ends). Needs the bot update (api.py,
  database/db.py, live_tactics.py).
- Navigation, privacy page, patch note, README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-28 01:36:49 +02:00

166 lines
7.8 KiB
PHP

<?php
require_once __DIR__ . '/includes/bootstrap.php';
require_once __DIR__ . '/includes/contributions.php';
require_permission('contributions.manage');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
redirect('/contributions.php');
}
verify_csrf();
$action = (string) ($_POST['action'] ?? '');
$pdo = db();
/** Bricht ab, wenn die Vergabe vom Lumishop stammt - die wird dort storniert, sonst stimmen Bestellung und Punkte nicht mehr ueberein. */
function contribution_guard_system(int $awardId): void
{
$stmt = db()->prepare('SELECT category FROM contribution_awards WHERE id = ?');
$stmt->execute([$awardId]);
$category = (string) $stmt->fetchColumn();
if (isset(CONTRIBUTION_SYSTEM_CATEGORIES[$category])) {
set_flash('error', 'Diese Buchung stammt aus dem Lumishop. Bitte dort die Bestellung stornieren.');
redirect('/contributions.php#verlauf');
}
}
/** Zurueck ins Formular, mit Fehlermeldung und den bisherigen Eingaben. */
function contribution_fail(string $message): void
{
$_SESSION['contribution_form'] = [
'category' => (string) ($_POST['category'] ?? ''),
'raid_id' => (string) ($_POST['raid_id'] ?? ''),
'title' => (string) ($_POST['title'] ?? ''),
'awarded_at' => (string) ($_POST['awarded_at'] ?? ''),
'points' => (string) ($_POST['points'] ?? ''),
'note' => (string) ($_POST['note'] ?? ''),
'users' => array_map('intval', (array) ($_POST['users'] ?? [])),
];
set_flash('error', $message);
redirect('/contributions_award.php');
}
switch ($action) {
// ---- Punkte vergeben ----
case 'create':
$category = (string) ($_POST['category'] ?? '');
if (!array_key_exists($category, CONTRIBUTION_CATEGORIES)) {
contribution_fail('Bitte einen Anlass auswählen.');
}
$title = trim((string) ($_POST['title'] ?? ''));
if ($title === '' || mb_strlen($title) > 150) {
contribution_fail('Bitte einen Grund mit höchstens 150 Zeichen angeben.');
}
$note = trim((string) ($_POST['note'] ?? ''));
if (mb_strlen($note) > 255) {
contribution_fail('Die Bemerkung darf höchstens 255 Zeichen lang sein.');
}
$points = filter_var($_POST['points'] ?? '', FILTER_VALIDATE_INT);
if ($points === false || $points === 0 || abs($points) > CONTRIBUTION_MAX_POINTS) {
contribution_fail('Bitte eine Punktzahl zwischen 1 und ' . number_format(CONTRIBUTION_MAX_POINTS, 0, ',', '.') . ' angeben (negativ für einen Abzug).');
}
$awardedAt = contribution_parse_time((string) ($_POST['awarded_at'] ?? ''));
if ($awardedAt === null) {
contribution_fail('Bitte ein gültiges Datum angeben (nicht in der Zukunft).');
}
$raidId = $category === 'raid' ? (int) ($_POST['raid_id'] ?? 0) : 0;
// Nur aktive Konten der Legion - doppelte, unbekannte und Verbuendete aus dem Consilium fallen hier heraus.
$userIds = array_values(array_unique(array_filter(array_map('intval', (array) ($_POST['users'] ?? [])))));
if (!empty($userIds)) {
$placeholders = implode(',', array_fill(0, count($userIds), '?'));
$stmt = $pdo->prepare("SELECT u.id FROM users u WHERE u.status = 'active' AND " . sql_is_legion_member('u') . " AND u.id IN ($placeholders)");
$stmt->execute($userIds);
$userIds = array_map('intval', $stmt->fetchAll(PDO::FETCH_COLUMN));
}
if (empty($userIds)) {
contribution_fail('Bitte mindestens ein Mitglied auswählen.');
}
$pdo->beginTransaction();
$pdo->prepare(
'INSERT INTO contribution_awards (category, title, note, raid_id, awarded_at, created_by) VALUES (?, ?, ?, ?, ?, ?)'
)->execute([$category, $title, $note !== '' ? $note : null, $raidId > 0 ? $raidId : null, $awardedAt, (int) current_user()['id']]);
$awardId = (int) $pdo->lastInsertId();
$insert = $pdo->prepare('INSERT INTO contribution_entries (award_id, user_id, points) VALUES (?, ?, ?)');
foreach ($userIds as $userId) {
$insert->execute([$awardId, $userId, $points]);
}
$pdo->commit();
$count = count($userIds);
$who = $count === 1 ? '1 Mitglied' : $count . ' Mitglieder';
admin_log($points > 0 ? '💛 Lumipunkte vergeben' : '💛 Lumipunkte abgezogen', [
'Punkte' => ($points > 0 ? '+' : '') . contribution_format($points) . ' pro Mitglied',
'Anlass' => CONTRIBUTION_CATEGORIES[$category] ?? $category,
'Grund' => $title,
'Bemerkung' => $note,
'Mitglieder (' . $count . ')' => implode(', ', array_map('admin_log_user', $userIds)),
], $points > 0 ? 'success' : 'warning');
set_flash('success', $points > 0
? contribution_format($points) . ' Lumipunkte an ' . $who . ' vergeben.'
: contribution_format(-$points) . ' Lumipunkte bei ' . $who . ' abgezogen.');
redirect('/contributions.php');
// ---- Ganze Vergabe loeschen ----
case 'delete_award':
contribution_guard_system((int) ($_POST['award_id'] ?? 0));
$info = $pdo->prepare(
'SELECT a.title, COUNT(e.id) AS members, MAX(e.points) AS points FROM contribution_awards a
LEFT JOIN contribution_entries e ON e.award_id = a.id WHERE a.id = ? GROUP BY a.id'
);
$info->execute([(int) ($_POST['award_id'] ?? 0)]);
$award = $info->fetch();
$stmt = $pdo->prepare('DELETE FROM contribution_awards WHERE id = ?');
$stmt->execute([(int) ($_POST['award_id'] ?? 0)]);
if ($stmt->rowCount() > 0 && $award) {
admin_log('🗑️ Lumipunkte-Vergabe gelöscht', [
'Grund' => $award['title'],
'Punkte' => contribution_format((int) $award['points']) . ' pro Mitglied',
'Mitglieder' => $award['members'],
], 'danger');
}
set_flash($stmt->rowCount() > 0 ? 'success' : 'error', $stmt->rowCount() > 0 ? 'Vergabe gelöscht.' : 'Diese Vergabe gibt es nicht mehr.');
redirect('/contributions.php#verlauf');
// ---- Ein Mitglied aus einer Vergabe entfernen ----
case 'remove_entry':
$entryId = (int) ($_POST['entry_id'] ?? 0);
$stmt = $pdo->prepare('SELECT award_id FROM contribution_entries WHERE id = ?');
$stmt->execute([$entryId]);
$awardId = (int) $stmt->fetchColumn();
if ($awardId === 0) {
set_flash('error', 'Diesen Eintrag gibt es nicht mehr.');
redirect('/contributions.php#verlauf');
}
contribution_guard_system($awardId);
$info = $pdo->prepare(
'SELECT a.title, e.user_id, e.points FROM contribution_entries e JOIN contribution_awards a ON a.id = e.award_id WHERE e.id = ?'
);
$info->execute([$entryId]);
$entry = $info->fetch();
if ($entry) {
admin_log('🗑️ Mitglied aus Lumipunkte-Vergabe entfernt', [
'Mitglied' => admin_log_user((int) $entry['user_id']),
'Grund' => $entry['title'],
'Punkte' => contribution_format((int) $entry['points']),
], 'danger');
}
$pdo->beginTransaction();
$pdo->prepare('DELETE FROM contribution_entries WHERE id = ?')->execute([$entryId]);
// War es das letzte Mitglied, bliebe eine leere Vergabe uebrig - die faellt mit weg.
$left = $pdo->prepare('SELECT COUNT(*) FROM contribution_entries WHERE award_id = ?');
$left->execute([$awardId]);
if ((int) $left->fetchColumn() === 0) {
$pdo->prepare('DELETE FROM contribution_awards WHERE id = ?')->execute([$awardId]);
}
$pdo->commit();
set_flash('success', 'Mitglied aus der Vergabe entfernt.');
redirect('/contributions.php#verlauf');
default:
redirect('/contributions.php');
}