LuminaWebsite/includes/premade_order.php
Maaxxs 42e3fede77 Premades: overview grouped by lead, collapsed by default; leads and their premades in the leadership's order
Asked for: "baue ein, dass die Premade-Übersicht in Web bei der Standardsortierung gestaffelt aufgereiht wird nach
nutzer. standartmäßig ist alles eingeklappt ... reihenfolge der benutzer und den premaids innerhalb des benutzers
können wir frei konfigurieren".

- premades.php: without a chosen sort, one collapsible row per lead (<details class="premade-group">: avatar, name,
  number of premades and how many have a free slot, "Du bist dabei" for own premades, open applications for those who
  decide on them), premades without a lead in their own group "Ohne Lead". With a sort chip the cards stand flat in
  #premade-list as before.
- includes/premade_order.php: premade_order_groups() groups by lead; new table premade_lead_order (Discord ID of the
  lead, 0 = without lead; schema version 65) next to premade_options.sort_order. Leads and premades without a place
  stand at the end. premade_action.php "order" saves lead_order[] and order[] (log entry lead by lead).
- Editor "Standard-Reihenfolge festlegen": nested list, leads with their premades below; assets/js/tag-order.js now
  orders every list of a form on its own (arrows, drag & drop), the roles page works as before.
- assets/js/premades.js: open groups are remembered in sessionStorage (premades-open) only for reloads (live update,
  after an action on the page, back/forward) - a new visit shows everything collapsed. The search opens the groups with
  hits and hides the others, afterwards the own state returns. Links to a premade (#premade-5, also the bot's
  "Bewerbung ansehen") open its group. assets/js/live.js ignores details with data-live-restore, so an open group
  does not block the automatic reload.
- Live fingerprint, data reset and account deletion include premade_lead_order; privacy page lists premades-open;
  patch note and README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-09-27 02:37:17 +02:00

104 lines
4.4 KiB
PHP

<?php
declare(strict_types=1);
/**
* Standard-Reihenfolge der Premades (premades.php): Die Seite zeigt die Premades nach Lead gruppiert - jeder Lead eine
* aufklappbare Zeile mit seinen Premades, Premades ohne Lead in einer eigenen Gruppe. Die Reihenfolge der Leads und die
* der Premades innerhalb eines Leads legt die Legionsleitung (premades.manage) per Drag & Drop fest (premade_action.php,
* Aktion "order"): gespeichert in premade_lead_order (Discord-ID des Leads, 0 = ohne Lead) und premade_options.sort_order
* je Premade-ID. Wer (noch) keinen Platz hat, steht dahinter - Premades in der Reihenfolge ihrer Gruendung, Leads in der
* ihrer ersten Premade. Waehlt man oben eine Sortierung, zeigt die Seite fuer diesen Besuch eine flache Liste.
*/
/** Plaetze der Premades: [premade_id => Platz ab 1]. */
function premade_order_positions(): array
{
try {
$rows = db()->query('SELECT premade_id, sort_order FROM premade_options WHERE sort_order IS NOT NULL')->fetchAll(PDO::FETCH_KEY_PAIR);
} catch (PDOException $e) {
return []; // Spalte kommt erst mit dem naechsten Schema-Abgleich
}
return array_map('intval', $rows);
}
/** Plaetze der Leads: [Discord-ID (0 = ohne Lead) => Platz ab 1]. */
function premade_lead_positions(): array
{
try {
$rows = db()->query('SELECT discord_id, sort_order FROM premade_lead_order')->fetchAll(PDO::FETCH_KEY_PAIR);
} catch (PDOException $e) {
return []; // Tabelle kommt erst mit dem naechsten Schema-Abgleich
}
$out = [];
foreach ($rows as $discordId => $position) {
$out[(int) $discordId] = (int) $position;
}
return $out;
}
/**
* Premades nach ihrem Platz, danach die uebrigen nach der Gruendung (ID).
*
* @param array<int, array<string, mixed>> $premades
* @param array<int, int> $positions premade_order_positions()
* @return array<int, array<string, mixed>>
*/
function premade_order_sort(array $premades, array $positions): array
{
usort($premades, static function (array $a, array $b) use ($positions): int {
$idA = (int) ($a['id'] ?? 0);
$idB = (int) ($b['id'] ?? 0);
return [$positions[$idA] ?? PHP_INT_MAX, $idA] <=> [$positions[$idB] ?? PHP_INT_MAX, $idB];
});
return $premades;
}
/**
* Die Premades nach Lead gruppiert, in der Standard-Reihenfolge: [['lead_id' => Discord-ID (0 = ohne Lead),
* 'premades' => [...]], …]. Leads ohne festen Platz stehen dahinter, in der Reihenfolge ihrer ersten Premade.
*
* @param array<int, array<string, mixed>> $premades
* @return array<int, array{lead_id: int, premades: array<int, array<string, mixed>>}>
*/
function premade_order_groups(array $premades, array $premadePositions, array $leadPositions): array
{
$groups = [];
foreach (premade_order_sort($premades, $premadePositions) as $premade) {
$leadId = (int) ($premade['owner_id'] ?? 0);
$groups[$leadId] ??= ['lead_id' => $leadId, 'premades' => []];
$groups[$leadId]['premades'][] = $premade;
}
$rank = [];
foreach (array_keys($groups) as $index => $leadId) {
$rank[$leadId] = [$leadPositions[$leadId] ?? PHP_INT_MAX, $index];
}
uksort($groups, static fn (int $a, int $b): int => $rank[$a] <=> $rank[$b]);
return array_values($groups);
}
/**
* Neue Standard-Reihenfolge speichern: die Leads (Discord-IDs, 0 = ohne Lead) und die Premades in der Reihenfolge, in
* der sie untereinander stehen (Lead fuer Lead) - danach hat jeder seinen festen Platz, Neue kommen ans Ende.
*
* @param int[] $leadIds
* @param int[] $premadeIds
*/
function premade_order_save(array $leadIds, array $premadeIds, int $userId): void
{
$pdo = db();
$pdo->beginTransaction();
$stmt = $pdo->prepare(
'INSERT INTO premade_options (premade_id, sort_order, updated_by) VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE sort_order = VALUES(sort_order), updated_by = VALUES(updated_by)'
);
foreach (array_values($premadeIds) as $index => $premadeId) {
$stmt->execute([(int) $premadeId, $index + 1, $userId]);
}
// Die Leads ganz neu - wer keine Premade mehr leitet, faellt dabei heraus
$pdo->exec('DELETE FROM premade_lead_order');
$stmt = $pdo->prepare('INSERT INTO premade_lead_order (discord_id, sort_order, updated_by) VALUES (?, ?, ?)');
foreach (array_values($leadIds) as $index => $leadId) {
$stmt->execute([(int) $leadId, $index + 1, $userId]);
}
$pdo->commit();
}