feat: derive autoresponder schedule from backend

This commit is contained in:
Marek Miklewicz
2026-06-02 19:55:07 +02:00
parent a0bd158a2e
commit f3abc7b33f
19 changed files with 248 additions and 43 deletions
+28 -15
View File
@@ -16,14 +16,12 @@ function render_rule_form(AppContext $ctx, string $mode, ?array $rule): string
$hiddenId = $isUpdate ? '<input type="hidden" name="id" value="' . AppContext::e($id) . '">' : '';
return '<form method="post" action="' . AppContext::e($action) . '">' . $csrf . $hiddenId .
'<div class="grid grid-2"><div class="panel"><h3>Treść odpowiedzi</h3><div class="row"><div><label>' . AppContext::e($ctx->t('Subject')) . '</label><input type="text" name="subject" value="' . AppContext::e((string)$subject) . '"></div>' .
'<div class="panel"><h3>' . AppContext::e($ctx->t('Response content')) . '</h3><div class="row"><div><label>' . AppContext::e($ctx->t('Subject')) . '</label><input type="text" name="subject" value="' . AppContext::e((string)$subject) . '"></div>' .
'<div><label>' . AppContext::e($ctx->t('Status')) . '</label><label class="br-time-item"><input type="checkbox" name="enabled" value="1" ' . ($enabled ? 'checked' : '') . '> ' . AppContext::e($ctx->t('Enabled')) . '</label></div></div>' .
'<label>' . AppContext::e($ctx->t('Message body')) . '</label><textarea name="body">' . AppContext::e((string)$body) . '</textarea></div>' .
'<div class="panel"><h3>Podsumowanie</h3><div class="summary-card"><div class="kv"><strong>Zakres</strong><span>' . AppContext::e($ctx->t('All valid POP/IMAP mailboxes in this account')) . '</span></div>' .
'<div class="kv"><strong>Start</strong><span>' . AppContext::e($startValue) . '</span></div><div class="kv"><strong>Koniec</strong><span>' . AppContext::e($endValue) . '</span></div></div>' .
'<div class="actions"><button class="primary" type="submit">' . AppContext::e($ctx->t($isUpdate ? 'Save changes' : 'Save autoresponder')) . '</button><a class="btn" href="' . AppContext::e($ctx->url('index.html')) . '">' . AppContext::e($ctx->t('Cancel')) . '</a></div></div></div>' .
render_calendar_picker('start_value', 'Data rozpoczęcia', $startValue) .
render_calendar_picker('end_value', 'Data zakończenia', $endValue) .
render_calendar_picker($ctx, 'start_value', $ctx->t('Start date'), $startValue) .
render_calendar_picker($ctx, 'end_value', $ctx->t('End date'), $endValue) .
'<div class="panel"><div class="actions"><button class="primary" type="submit">' . AppContext::e($ctx->t($isUpdate ? 'Save changes' : 'Save autoresponder')) . '</button><a class="btn" href="' . AppContext::e($ctx->url('index.html')) . '">' . AppContext::e($ctx->t('Cancel')) . '</a></div></div>' .
'</form>';
}
@@ -32,18 +30,28 @@ function normalize_rule_post(array $post): array
{
foreach (['start_value', 'end_value'] as $key) {
$date = trim((string)($post[$key . '_date'] ?? ''));
$hour = trim((string)($post[$key . '_hour'] ?? ''));
if ($date !== '' && preg_match('/^[0-9]{2}$/', $hour)) {
$post[$key] = $date . 'T' . $hour . ':00';
$time = trim((string)($post[$key . '_time'] ?? ''));
$period = trim((string)($post[$key . '_period'] ?? ''));
if ($date !== '' && preg_match('/^[0-9]{2}:[0-9]{2}$/', $time)) {
$post[$key] = $date . 'T' . $time;
} elseif ($date !== '' && preg_match('/^(morning|afternoon|evening)$/', $period)) {
$post[$key] = $date . '|' . $period;
}
}
return $post;
}
function render_calendar_picker(string $name, string $title, string $value): string
function render_calendar_picker(AppContext $ctx, string $name, string $title, string $value): string
{
$mode = $ctx->settings->scheduleMode();
$selectedDate = substr($value, 0, 10);
$selectedHour = substr($value, 11, 2) ?: '09';
$selectedTime = '09:00';
$selectedPeriod = 'morning';
if ($mode === 'exact_hour' && preg_match('/T([0-9]{2}:[0-9]{2})$/', $value, $m)) {
$selectedTime = $m[1];
} elseif ($mode === 'directadmin_period' && preg_match('/\|(morning|afternoon|evening)$/', $value, $m)) {
$selectedPeriod = $m[1];
}
$days = '';
for ($week = 0, $day = 1; $week < 4; $week++) {
$days .= '<tr>';
@@ -54,14 +62,19 @@ function render_calendar_picker(string $name, string $title, string $value): str
}
$days .= '</tr>';
}
$hours = '';
foreach (['08', '09', '10', '11', '12', '13', '14', '15', '16', '17', '18'] as $hour) {
$hours .= '<label class="br-time-item"><input type="radio" name="' . AppContext::e($name . '_hour') . '" value="' . $hour . '" ' . ($hour === $selectedHour ? 'checked' : '') . '> ' . $hour . ':00</label>';
if ($mode === 'exact_hour') {
$timeControl = '<label>' . AppContext::e($ctx->t('Time')) . '</label><input type="time" name="' . AppContext::e($name . '_time') . '" value="' . AppContext::e($selectedTime) . '" step="60">';
} else {
$timeControl = '<label>' . AppContext::e($ctx->t('Time of day')) . '</label><select name="' . AppContext::e($name . '_period') . '">' .
'<option value="morning"' . ($selectedPeriod === 'morning' ? ' selected' : '') . '>' . AppContext::e($ctx->t('Morning')) . '</option>' .
'<option value="afternoon"' . ($selectedPeriod === 'afternoon' ? ' selected' : '') . '>' . AppContext::e($ctx->t('Afternoon')) . '</option>' .
'<option value="evening"' . ($selectedPeriod === 'evening' ? ' selected' : '') . '>' . AppContext::e($ctx->t('Evening')) . '</option>' .
'</select>';
}
return '<div class="panel"><h3>' . AppContext::e($title) . '</h3><div class="br-restore-layout"><div class="br-restore-calendar"><h4>' . AppContext::e($title) . '</h4><div class="br-month-nav"><button type="button" class="br-month-btn">&lsaquo;</button><span class="br-month-label">Czerwiec 2026</span><button type="button" class="br-month-btn">&rsaquo;</button></div>' .
'<table class="br-calendar-grid"><thead><tr><th>Pn</th><th>Wt</th><th>Śr</th><th>Cz</th><th>Pt</th><th>Sb</th><th>Nd</th></tr></thead><tbody>' . $days . '</tbody></table>' .
'<div class="br-date-label" id="' . AppContext::e($name . '_label') . '">Wybrano: ' . AppContext::e($selectedDate) . '</div><input type="hidden" id="' . AppContext::e($name . '_date') . '" name="' . AppContext::e($name . '_date') . '" value="' . AppContext::e($selectedDate) . '"></div>' .
'<div class="br-restore-calendar"><h4>Godzina</h4><div class="br-time-picker"><div class="br-time-title">Wybierz godzinę</div><div class="br-time-list">' . $hours . '</div></div></div></div>' .
'<div class="br-restore-calendar"><h4>' . AppContext::e($ctx->t('Time')) . '</h4>' . $timeControl . '</div></div>' .
'<input type="hidden" name="' . AppContext::e($name) . '" value="' . AppContext::e($value) . '"></div>';
}