Add poll expiry, reminder mail and polls admin CLI
Polls are deleted POLL_RETENTION_DAYS (default 90) after creation by the cron job `bin/cake polls cleanup`. POLL_REMINDER_DAYS (default 14) before that the owner gets a one-time reminder mail (expiry_reminded_at). New commands: polls list, show (admin link only with --link), delete, remind, cleanup. The expiry date is shown in the creation mail and the dashboard. Docs: README, AGENTS.md, .env.example. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
4caff47a09
commit
a19cd8339e
20 files changed
+976
-1
No files matched your search
@@ -0,0 +1,118 @@
|
||||
<?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\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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user