Browse and delete polls from a numbered list in the polls menu
CI / Deploy / PHP 8.5 (push) Successful in 1m7s
CI / Deploy / PHP 8.4 (push) Successful in 1m12s
CI / Deploy / test (push) Successful in 1s
CI / Deploy / deploy (push) Skipped

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 <noreply@anthropic.com>
This commit is contained in:
gnilebeinandClaude Sonnet 5.5 committed 2026-11-16 13:35:38 +01:00
1 parent 6e8fef022b
commit 7b83cf2f2c
6 files changed
+202 -39

No files matched your search

+3 -2
View File
@@ -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 |
+2 -1
View File
@@ -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`; `<Nr>` ö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).
+55
View File
@@ -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").
*
+2 -29
View File
@@ -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));
+102 -5
View File
@@ -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('<info>' . __('PlenarIO – Umfragen verwalten') . '</info>');
$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([
+38 -2
View File
@@ -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();