Machine-readable output for scripts; never contains the admin token unless --link is given. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
157 lines
4.8 KiB
PHP
157 lines
4.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Command;
|
|
|
|
use App\Model\Entity\Poll;
|
|
use App\Model\Table\PollsTable;
|
|
use Cake\Command\Command;
|
|
use Cake\Console\ConsoleIo;
|
|
use Cake\Console\ConsoleOutput;
|
|
use Cake\I18n\DateTime;
|
|
use Cake\Routing\Router;
|
|
|
|
/**
|
|
* Shared helpers of the `polls …` admin commands (list, show, delete, cleanup).
|
|
*/
|
|
abstract class PollsCommand extends Command
|
|
{
|
|
/**
|
|
* @return \App\Model\Table\PollsTable
|
|
*/
|
|
protected function polls(): PollsTable
|
|
{
|
|
/** @var \App\Model\Table\PollsTable $polls */
|
|
$polls = $this->fetchTable('Polls');
|
|
|
|
return $polls;
|
|
}
|
|
|
|
/**
|
|
* Machine-readable form of a poll (for `--json`). Never contains the admin token.
|
|
*
|
|
* @param \App\Model\Entity\Poll $poll Poll
|
|
* @return array<string, mixed> Dates as ISO 8601 (server time zone), null where empty
|
|
*/
|
|
protected function pollData(Poll $poll): array
|
|
{
|
|
$date = fn(?DateTime $d): ?string => $d?->format(DATE_ATOM);
|
|
|
|
return [
|
|
'id' => $poll->id,
|
|
'title' => $poll->title,
|
|
'email' => $poll->email,
|
|
'joinCode' => $poll->join_code,
|
|
'joinUrl' => $this->joinLink($poll),
|
|
'created' => $date($poll->created),
|
|
'lastAdminAccess' => $date($poll->last_admin_access),
|
|
'expiresAt' => $date($poll->expiresAt()),
|
|
'expired' => $poll->isExpired(),
|
|
'reminderSentAt' => $date($poll->expiry_reminded_at),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Prints data as JSON only: nothing else on stdout, no markup interpretation (titles may contain "<").
|
|
*
|
|
* @param \Cake\Console\ConsoleIo $io Console
|
|
* @param mixed $data JSON serialisable data
|
|
* @return void
|
|
*/
|
|
protected function printJson(ConsoleIo $io, mixed $data): void
|
|
{
|
|
$io->setOutputAs(ConsoleOutput::RAW);
|
|
$io->out((string)json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
|
|
}
|
|
|
|
/**
|
|
* Finds a poll by numeric id or join code ("K7P2QX", "k7p-2qx").
|
|
*
|
|
* @param string $reference Id or join code
|
|
* @return \App\Model\Entity\Poll|null
|
|
*/
|
|
protected function findPoll(string $reference): ?Poll
|
|
{
|
|
$reference = trim($reference);
|
|
if ($reference !== '' && ctype_digit($reference)) {
|
|
$poll = $this->polls()->find()->where(['Polls.id' => (int)$reference])->first();
|
|
if ($poll !== null) {
|
|
/** @var \App\Model\Entity\Poll $poll */
|
|
return $poll;
|
|
}
|
|
}
|
|
|
|
/** @var \App\Model\Entity\Poll|null $poll */
|
|
$poll = $this->polls()->find('byJoinCode', code: $reference)->first();
|
|
|
|
return $poll;
|
|
}
|
|
|
|
/**
|
|
* Absolute admin link (contains the secret token: show it only on explicit request).
|
|
*
|
|
* @param \App\Model\Entity\Poll $poll Poll
|
|
* @return string|null Null if the token cannot be decrypted (e.g. SECURITY_SALT changed)
|
|
*/
|
|
protected function adminLink(Poll $poll): ?string
|
|
{
|
|
$token = $this->polls()->decryptToken($poll);
|
|
|
|
return $token === null ? null : Router::url(['_name' => 'admin', 'token' => $token], true);
|
|
}
|
|
|
|
/**
|
|
* @param \App\Model\Entity\Poll $poll Poll
|
|
* @return string Absolute participant link
|
|
*/
|
|
protected function joinLink(Poll $poll): string
|
|
{
|
|
return Router::url(['_name' => 'participate', 'code' => $poll->join_code], true);
|
|
}
|
|
|
|
/**
|
|
* @param \Cake\I18n\DateTime|null $date Date
|
|
* @return string "2026-11-16 14:30" in the server's time zone, "-" if empty
|
|
*/
|
|
protected function formatDate(?DateTime $date): string
|
|
{
|
|
return $date === null ? '-' : $date->format('Y-m-d H:i');
|
|
}
|
|
|
|
/**
|
|
* @param \App\Model\Entity\Poll $poll Poll
|
|
* @return string Expiry date, with a marker once it has passed
|
|
*/
|
|
protected function formatExpiry(Poll $poll): string
|
|
{
|
|
$expires = $poll->expiresAt();
|
|
if ($expires === null) {
|
|
return __('nie');
|
|
}
|
|
|
|
return $this->formatDate($expires) . ($expires->isPast() ? ' ' . __('(abgelaufen)') : '');
|
|
}
|
|
|
|
/**
|
|
* Deletes polls including slides, answers and images, one transaction per poll.
|
|
*
|
|
* @param iterable<\App\Model\Entity\Poll> $polls Polls
|
|
* @param \Cake\Console\ConsoleIo $io Console
|
|
* @return int Number of deleted polls
|
|
*/
|
|
protected function deletePolls(iterable $polls, ConsoleIo $io): int
|
|
{
|
|
$deleted = 0;
|
|
foreach ($polls as $poll) {
|
|
if ($this->polls()->delete($poll)) {
|
|
$deleted++;
|
|
$io->out(__('Gelöscht: #{0} {1} ({2})', $poll->id, $poll->title, $poll->email));
|
|
} else {
|
|
$io->err(__('Konnte #{0} nicht löschen.', $poll->id));
|
|
}
|
|
}
|
|
|
|
return $deleted;
|
|
}
|
|
}
|