From 7b83cf2f2ce19c59112aac29a67ffa02159927bb Mon Sep 17 00:00:00 2001 From: Patrick Niebeling Date: Mon, 16 Nov 2026 13:35:38 +0100 Subject: [PATCH] Browse and delete polls from a numbered list in the polls menu Paged list with pick by number (open) and "d 1,3" to delete several at once after one confirmation. List query and table moved into the shared base command. Co-Authored-By: Claude Sonnet 5.5 --- docs/deployment.md | 5 +- docs/operations.md | 3 +- src/Command/PollsCommand.php | 55 ++++++++++ src/Command/PollsListCommand.php | 31 +----- src/Command/PollsMenuCommand.php | 107 ++++++++++++++++++- tests/TestCase/Command/PollsCommandsTest.php | 40 ++++++- 6 files changed, 202 insertions(+), 39 deletions(-) diff --git a/docs/deployment.md b/docs/deployment.md index 2fa4812..004f446 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -118,8 +118,9 @@ Bei Updates: Code aktualisieren, `composer install --no-dev`, Migrationen und `c ### Umfragen verwalten und aufräumen (CLI) -Am einfachsten geht es menügeführt: `bin/cake polls` ohne Unterbefehl zeigt ein Menü (Umfragen anzeigen und suchen, -Umfrage öffnen mit Details, Admin-Link und Löschen, Erinnern, Aufräumen). Destruktive Aktionen zeigen vorher eine Vorschau +Am einfachsten geht es menügeführt: `bin/cake polls` ohne Unterbefehl zeigt ein Menü (nummerierte Liste mit Blättern: +`3` öffnet eine Umfrage, `d 2` oder `d 1,3,4` löscht direkt aus der Liste, `n`/`p` blättern; außerdem Suche nach +E-Mail, Details, Admin-Link, Erinnern, Aufräumen). Destruktive Aktionen zeigen vorher eine Vorschau und fragen nach, jede Eingabe hat eine sichere Vorgabe (Zurück/Nein). Für Skripte und Cron gibt es die einzelnen Befehle: | Befehl | Zweck | diff --git a/docs/operations.md b/docs/operations.md index 2e8de48..4224d5e 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -13,7 +13,8 @@ Aufräumen und Erinnerungen, CI/CD, Release-Pakete, Container. Aufräumen per Cron: `bin/cake polls cleanup` (erst `remind`, dann idempotent löschen über `PollsTable::delete()` samt Folien/Bildern, danach `images_cleanup`; `--dry-run`; Cron-Einrichtung für Server und Docker steht in [deployment.md](deployment.md) unter „Cronjob einrichten“, im Container läuft kein cron). `last_admin_access` fließt nicht mehr ein. `polls show --link` - ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`). Das Menü `bin/cake polls` (`PollsMenuCommand`) ruft nur diese Befehle per `executeCommand()` auf; jede Abfrage hat eine + ist die einzige Stelle, die den Admin-Link ausgibt (nie in `list`). Das Menü `bin/cake polls` (`PollsMenuCommand`) ruft nur diese Befehle per `executeCommand()` auf; die Liste ist nummeriert und seitenweise (`PAGE_SIZE`; `` öffnet, + `d 1,3` löscht mehrere nach einer Rückfrage, `n`/`p` blättern, Tastatur-Pfeile gibt es bewusst nicht: nicht portabel, v. a. unter Windows); jede Abfrage hat eine Vorgabe (`0`/leer/`n`), damit geschlossene Eingabe es beendet statt zu schleifen; Löschen/Aufräumen erst nach Vorschau (`--dry-run`) und Rückfrage. Tests: `PollsCommandsTest.php` (leert vorher alle Umfragen). diff --git a/src/Command/PollsCommand.php b/src/Command/PollsCommand.php index 6814da2..53f9c45 100644 --- a/src/Command/PollsCommand.php +++ b/src/Command/PollsCommand.php @@ -26,6 +26,61 @@ abstract class PollsCommand extends Command return $polls; } + /** + * @param bool $expiredOnly Only expired polls + * @param string $email Only polls whose e-mail address contains this text (case-insensitive) + * @return list<\App\Model\Entity\Poll> Oldest first + */ + protected function findPolls(bool $expiredOnly = false, string $email = ''): array + { + $query = $this->polls()->find() + ->select([ + 'Polls.id', 'Polls.title', 'Polls.email', 'Polls.join_code', 'Polls.current_slide_id', + 'Polls.paused_slide_id', 'Polls.last_admin_access', 'Polls.expiry_reminded_at', 'Polls.created', + ]) + ->orderBy(['Polls.id' => 'ASC']); + if ($expiredOnly) { + $query->find('expired'); + } + if ($email !== '') { + $query->where(['Polls.email LIKE' => '%' . addcslashes(strtolower($email), '%_\\') . '%']); + } + + /** @var list<\App\Model\Entity\Poll> $polls */ + $polls = $query->all()->toList(); + + return $polls; + } + + /** + * Prints polls as a table, optionally with a running number to pick from. + * + * @param list<\App\Model\Entity\Poll> $polls Polls + * @param \Cake\Console\ConsoleIo $io Console + * @param int $firstNumber Number of the first row (with $numbered) + * @param bool $numbered Add the number column + * @return void + */ + protected function printPolls(array $polls, ConsoleIo $io, int $firstNumber = 1, bool $numbered = false): void + { + $header = [__('ID'), __('Code'), __('Titel'), __('E-Mail'), __('Angelegt'), __('Letzter Admin-Besuch'), __('Läuft ab'), __('Erinnert')]; + $rows = [$numbered ? array_merge(['#'], $header) : $header]; + foreach ($polls as $i => $poll) { + $row = [ + (string)$poll->id, + $poll->join_code, + mb_strimwidth($poll->title, 0, 40, '…'), + $poll->email, + $this->formatDate($poll->created), + $this->formatDate($poll->last_admin_access), + $this->formatExpiry($poll), + $this->formatDate($poll->expiry_reminded_at), + ]; + $rows[] = $numbered ? array_merge([(string)($firstNumber + $i)], $row) : $row; + } + $io->helper('Table')->output($rows); + } + /** * Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx"). * diff --git a/src/Command/PollsListCommand.php b/src/Command/PollsListCommand.php index 4878d32..03a68e1 100644 --- a/src/Command/PollsListCommand.php +++ b/src/Command/PollsListCommand.php @@ -51,22 +51,8 @@ class PollsListCommand extends PollsCommand */ public function execute(Arguments $args, ConsoleIo $io): int { - $query = $this->polls()->find() - ->select([ - 'Polls.id', 'Polls.title', 'Polls.email', 'Polls.join_code', 'Polls.current_slide_id', - 'Polls.paused_slide_id', 'Polls.last_admin_access', 'Polls.expiry_reminded_at', 'Polls.created', - ]) - ->orderBy(['Polls.id' => 'ASC']); - if ($args->getOption('expired')) { - $query->find('expired'); - } $email = $args->getOption('email'); - if (is_string($email) && $email !== '') { - $query->where(['Polls.email LIKE' => '%' . addcslashes(strtolower($email), '%_\\') . '%']); - } - - /** @var list<\App\Model\Entity\Poll> $polls */ - $polls = $query->all()->toList(); + $polls = $this->findPolls((bool)$args->getOption('expired'), is_string($email) ? $email : ''); $sort = (string)$args->getOption('sort'); if ($sort !== 'created') { usort($polls, fn($a, $b): int => match ($sort) { @@ -81,20 +67,7 @@ class PollsListCommand extends PollsCommand return static::CODE_SUCCESS; } - $rows = [[__('ID'), __('Code'), __('Titel'), __('E-Mail'), __('Angelegt'), __('Letzter Admin-Besuch'), __('Läuft ab'), __('Erinnert')]]; - foreach ($polls as $poll) { - $rows[] = [ - (string)$poll->id, - $poll->join_code, - mb_strimwidth($poll->title, 0, 40, '…'), - $poll->email, - $this->formatDate($poll->created), - $this->formatDate($poll->last_admin_access), - $this->formatExpiry($poll), - $this->formatDate($poll->expiry_reminded_at), - ]; - } - $io->helper('Table')->output($rows); + $this->printPolls($polls, $io); $expired = count(array_filter($polls, fn($poll): bool => $poll->isExpired())); $io->out(__('{0} Umfrage(n), davon {1} abgelaufen.', count($polls), $expired)); diff --git a/src/Command/PollsMenuCommand.php b/src/Command/PollsMenuCommand.php index b6db5d4..39d386a 100644 --- a/src/Command/PollsMenuCommand.php +++ b/src/Command/PollsMenuCommand.php @@ -17,6 +17,11 @@ use Cake\Console\ConsoleOptionParser; */ class PollsMenuCommand extends PollsCommand { + /** + * Polls per page of the list. + */ + protected const PAGE_SIZE = 15; + /** * @return string */ @@ -46,7 +51,7 @@ class PollsMenuCommand extends PollsCommand $io->out(''); $io->out('' . __('PlenarIO – Umfragen verwalten') . ''); $io->out([ - ' 1) ' . __('Alle Umfragen anzeigen'), + ' 1) ' . __('Alle Umfragen (durchblättern, öffnen, löschen)'), ' 2) ' . __('Abgelaufene Umfragen anzeigen'), ' 3) ' . __('Nach E-Mail-Adresse suchen'), ' 4) ' . __('Umfrage öffnen (Details, Admin-Link, Löschen)'), @@ -57,15 +62,15 @@ class PollsMenuCommand extends PollsCommand switch ($io->askChoice(__('Was möchtest du tun?'), ['1', '2', '3', '4', '5', '6', '0'], '0')) { case '1': - $this->executeCommand(PollsListCommand::class, [], $io); + $this->browse($io); break; case '2': - $this->executeCommand(PollsListCommand::class, ['--expired'], $io); + $this->browse($io, true); break; case '3': $email = $io->ask(__('Teil der E-Mail-Adresse (leer = zurück)'), ''); if ($email !== '') { - $this->executeCommand(PollsListCommand::class, ['--email=' . $email], $io); + $this->browse($io, false, $email); } break; case '4': @@ -109,7 +114,88 @@ class PollsMenuCommand extends PollsCommand } /** - * Asks for one poll and offers the actions on it until the user goes back. + * Pageable, numbered poll list. Pick by number: open (details, admin link, delete) or delete several at once. + * + * @param \Cake\Console\ConsoleIo $io Console + * @param bool $expiredOnly Only expired polls + * @param string $email Filter by part of the e-mail address + * @return void + */ + protected function browse(ConsoleIo $io, bool $expiredOnly = false, string $email = ''): void + { + $page = 0; + while (true) { + // Reloaded each round: deleted polls disappear, numbers stay consistent with what is shown. + $polls = $this->findPolls($expiredOnly, $email); + if ($polls === []) { + $io->out(__('Keine Umfragen gefunden.')); + + return; + } + $pages = (int)ceil(count($polls) / self::PAGE_SIZE); + $page = max(0, min($page, $pages - 1)); + $offset = $page * self::PAGE_SIZE; + $shown = array_slice($polls, $offset, self::PAGE_SIZE); + + $io->out(''); + $this->printPolls($shown, $io, $offset + 1, true); + $io->out(__('{0} Umfrage(n), Seite {1} von {2}.', count($polls), $page + 1, $pages)); + $io->out(__('Nummer = öffnen · d 2 = löschen (mehrere: d 1,3,4) · n = weiter · p = zurück · leer = Menü')); + + $input = strtolower($io->ask(__('Eingabe'), '')); + if ($input === '' || $input === 'q') { + return; + } + if ($input === 'n') { + $page++; + } elseif ($input === 'p') { + $page--; + } elseif (preg_match('/^d\s*([\d\s,]+)$/', $input, $m) === 1) { + $this->deleteByNumbers($io, $polls, $m[1]); + } elseif (ctype_digit($input) && isset($polls[(int)$input - 1])) { + $this->pollActions($io, $polls[(int)$input - 1]); + } else { + $io->err(__('Das habe ich nicht verstanden.')); + } + } + } + + /** + * Deletes the polls with the given list numbers after one confirmation. + * + * @param \Cake\Console\ConsoleIo $io Console + * @param list<\App\Model\Entity\Poll> $polls The numbered list (number = index + 1) + * @param string $numbers "1,3 4" + * @return void + */ + protected function deleteByNumbers(ConsoleIo $io, array $polls, string $numbers): void + { + $selected = []; + foreach (preg_split('/[\s,]+/', trim($numbers), -1, PREG_SPLIT_NO_EMPTY) ?: [] as $number) { + if (!isset($polls[(int)$number - 1])) { + $io->err(__('Nummer {0} gibt es nicht, nichts gelöscht.', $number)); + + return; + } + $poll = $polls[(int)$number - 1]; + $selected[$poll->id] = $poll; + } + if ($selected === []) { + return; + } + + foreach ($selected as $poll) { + $io->out(__('#{0} {1} ({2}), angelegt {3}', $poll->id, $poll->title, $poll->email, $this->formatDate($poll->created))); + } + if ($io->askChoice(__('{0} Umfrage(n) endgültig löschen?', count($selected)), ['y', 'n'], 'n') === 'y') { + $this->deletePolls($selected, $io); + } else { + $io->out(__('Abgebrochen.')); + } + } + + /** + * Asks for one poll by id or code and offers the actions on it. * * @param \Cake\Console\ConsoleIo $io Console * @return void @@ -127,7 +213,18 @@ class PollsMenuCommand extends PollsCommand return; } + $this->pollActions($io, $poll); + } + /** + * Details, admin link and delete for one poll, until the user goes back (or the poll is gone). + * + * @param \Cake\Console\ConsoleIo $io Console + * @param \App\Model\Entity\Poll $poll Poll + * @return void + */ + protected function pollActions(ConsoleIo $io, Poll $poll): void + { $this->executeCommand(PollsShowCommand::class, [(string)$poll->id], $io); while (true) { $io->out([ diff --git a/tests/TestCase/Command/PollsCommandsTest.php b/tests/TestCase/Command/PollsCommandsTest.php index ced826d..e84e0db 100644 --- a/tests/TestCase/Command/PollsCommandsTest.php +++ b/tests/TestCase/Command/PollsCommandsTest.php @@ -127,9 +127,10 @@ class PollsCommandsTest extends TestCase public function testMenuListsAndOpensPollWithLink(): void { - [$poll, $token] = $this->createPoll('Menü-Umfrage', 'menu@example.org'); + [, $token] = $this->createPoll('Menü-Umfrage', 'menu@example.org'); - $this->exec('polls', ['1', '4', (string)$poll->id, '2', '0', '0']); + // Menu 1 (list), open row 1, show the link, back, leave the list, quit. + $this->exec('polls', ['1', '1', '2', '0', '', '0']); $this->assertExitSuccess(); $this->assertOutputContains('menu@example.org'); $this->assertOutputContains('https://plenario.test/admin/' . $token); @@ -150,6 +151,41 @@ class PollsCommandsTest extends TestCase $this->assertSame(0, $this->pollsTable()->find()->where(['id' => $poll->id])->count()); } + public function testMenuListDeletesByNumber(): void + { + [$a] = $this->createPoll('Eins', 'eins@example.org'); + [$b] = $this->createPoll('Zwei', 'zwei@example.org'); + [$c] = $this->createPoll('Drei', 'drei@example.org'); + + // Unknown number and cancelled confirmation delete nothing. + $this->exec('polls', ['1', 'd 9', 'd 1', 'n', '', '0']); + $this->assertErrorContains('Nummer 9 gibt es nicht'); + $this->assertSame(3, $this->pollsTable()->find()->count()); + + // Several at once with one confirmation. + $this->exec('polls', ['1', 'd 1,3', 'y', '', '0']); + $this->assertExitSuccess(); + $this->assertEquals([$b->id], $this->pollsTable()->find()->all()->extract('id')->toList()); + + // Last one gone: the empty list returns to the menu by itself. + $this->exec('polls', ['1', 'd1', 'y', '0']); + $this->assertSame(0, $this->pollsTable()->find()->count()); + $this->assertNotEmpty($a->id . $c->id); + } + + public function testMenuListPagesThroughManyPolls(): void + { + for ($i = 1; $i <= 16; $i++) { + $this->createPoll('Umfrage ' . $i, 'p' . $i . '@example.org'); + } + + $this->exec('polls', ['1', 'n', 'p', '', '0']); + $this->assertExitSuccess(); + $this->assertOutputContains('Seite 1 von 2'); + $this->assertOutputContains('Seite 2 von 2'); + $this->assertOutputContains('p16@example.org'); + } + public function testMenuCleanupAsksBeforeDeleting(): void { [$old] = $this->createPoll();