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 <noreply@anthropic.com>
This commit is contained in:
1 parent
6e8fef022b
commit
7b83cf2f2c
6 files changed
+202
-39
No files matched your search
@@ -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").
|
||||
*
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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([
|
||||
|
||||
Reference in new issue
Block a user