Files
Plenario/src/Command/PollsListCommand.php
T
gnilebeinandClaude Sonnet 5.5 3d6ade668f
CI / Deploy / PHP 8.5 (push) Successful in 1m37s
CI / Deploy / PHP 8.4 (push) Successful in 1m39s
CI / Deploy / test (push) Successful in 2s
CI / Deploy / deploy (push) Skipped
Add --json to polls list and polls show
Machine-readable output for scripts; never contains the admin token
unless --link is given.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-16 13:59:38 +01:00

115 lines
3.9 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Command;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
/**
* Lists polls with owner, activity and expiry date. Never prints admin links.
*
* Usage: bin/cake polls list [--expired] [--email=<part>] [--sort=created|activity|title]
*/
class PollsListCommand extends PollsCommand
{
/**
* @return string
*/
public static function defaultName(): string
{
return 'polls list';
}
/**
* @param \Cake\Console\ConsoleOptionParser $parser Parser
* @return \Cake\Console\ConsoleOptionParser
*/
protected function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
return $parser
->setDescription(__('Zeigt alle Umfragen mit E-Mail-Adresse, Ablaufdatum und Erinnerung.'))
->addOption('expired', [
'help' => __('Nur abgelaufene Umfragen.'),
'boolean' => true,
])
->addOption('email', [
'help' => __('Nur Umfragen, deren E-Mail-Adresse diesen Text enthält.'),
])
->addOption('json', [
'help' => __('Ausgabe als JSON statt als Tabelle (für Skripte).'),
'boolean' => true,
])
->addOption('sort', [
'help' => __('Sortierung (aufsteigend).'),
'default' => 'created',
'choices' => ['created', 'activity', 'title'],
]);
}
/**
* @param \Cake\Console\Arguments $args Arguments
* @param \Cake\Console\ConsoleIo $io Console
* @return int
*/
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();
$sort = (string)$args->getOption('sort');
if ($sort !== 'created') {
usort($polls, fn($a, $b): int => match ($sort) {
'activity' => ($a->last_admin_access ?? $a->created) <=> ($b->last_admin_access ?? $b->created),
default => strcasecmp($a->title, $b->title),
});
}
if ($args->getOption('json')) {
$this->printJson($io, array_map($this->pollData(...), $polls));
return static::CODE_SUCCESS;
}
if ($polls === []) {
$io->out(__('Keine Umfragen gefunden.'));
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);
$expired = count(array_filter($polls, fn($poll): bool => $poll->isExpired()));
$io->out(__('{0} Umfrage(n), davon {1} abgelaufen.', count($polls), $expired));
return static::CODE_SUCCESS;
}
}