LuminaWebsite/includes/premade_order.php
Maaxxs 1c673a86f1 Premades: find players on the page, and a standard order the leadership sets by drag and drop
Asked for: "füge noch ein, dass man nach nutzern suchen kann" and "es soll zusätzlich eine standard sortierung
geben, da ist dann oben bei der sortierung nichts angewählt. diese standard sortierung kann von der gildenleitung
angepasst werden per drag und drop was die reihenfolge angeht".

- Search (premades.php, assets/js/premades.js): a search field above the sort chips finds players by the shown
  name, website and Discord name and the names of their characters (lead, members, reserve, open invitations)
  and premades by their name. Premades without a match are hidden; found players get a gold frame while the
  other places of the card step back (the gold lead would look like a hit otherwise). The term stays in the
  address (?q=) so it survives a reload; count and an empty note like on the raid page.
- Standard order (includes/premade_order.php): premade_options.sort_order per premade, schema version 63. The
  leadership (premades.manage) arranges it below the chips under "Standard-Reihenfolge festlegen" with the same
  list as the tag order (assets/js/tag-order.js: handle, arrows, unsaved note). premade_action.php "order" keeps
  premades founded meanwhile at the end, drops dissolved ones and logs before/after. Premades without a place
  follow in founding order. The server delivers the page in this order (own premades still first), each card
  carries data-sort-default.
- Sorting: no chip is active by default any more - then the standard order applies, and a second click on the
  active chip goes back to it.
- Live updates also watch sort_order, the data reset area mentions the order. Patch notes and README.

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

57 lines
2.2 KiB
PHP

<?php
declare(strict_types=1);
/**
* Standard-Reihenfolge der Premades (premades.php): Die Legionsleitung (premades.manage) legt sie per Drag & Drop
* fest (premade_action.php, Aktion "order"), gespeichert als premade_options.sort_order je Premade-ID. Premades
* ohne Platz (neu gegruendet) stehen dahinter, in der Reihenfolge ihrer Gruendung. Waehlt man auf der Seite eine
* Sortierung, gilt diese statt der Standard-Reihenfolge; die eigenen Premades stehen fuer jeden immer oben.
*/
/** Plaetze der Standard-Reihenfolge: [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);
}
/**
* Premades in der Standard-Reihenfolge: festgelegte 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;
}
/**
* Neue Standard-Reihenfolge speichern: jede Premade aus $premadeIds bekommt ihren Platz (ab 1) - danach hat jede
* ihren festen Platz, neu gegruendete kommen ans Ende.
*
* @param int[] $premadeIds
*/
function premade_order_save(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]);
}
$pdo->commit();
}