Sync global-autoresponder current state
This commit is contained in:
@@ -3,24 +3,84 @@ declare(strict_types=1);
|
||||
|
||||
return static function (AppContext $ctx): void {
|
||||
$errors = [];
|
||||
$formRule = null;
|
||||
$modal = '';
|
||||
if (Http::isPost()) {
|
||||
try {
|
||||
$ctx->requireCsrf('create');
|
||||
$input = normalize_rule_post($_POST);
|
||||
$rule = attach_rule_scope($ctx, RuleValidator::validate($input, $ctx->settings));
|
||||
enforce_rule_backend_constraints($ctx, null, $rule);
|
||||
$created = $ctx->rules->create($ctx->daUser->username(), $rule);
|
||||
try {
|
||||
$ctx->syncActiveBackend();
|
||||
} catch (Throwable $syncError) {
|
||||
$ctx->rules->delete($ctx->daUser->username(), (string)$created['id']);
|
||||
throw $syncError;
|
||||
$formRule = $rule;
|
||||
$conflict = !empty($rule['enabled']) ? find_active_rule_conflict($ctx, null) : null;
|
||||
$conflictAction = $ctx->post('conflict_action');
|
||||
$conflictRuleId = $ctx->post('conflict_rule_id');
|
||||
|
||||
if ($conflict !== null && ($conflictAction !== 'replace' || $conflictRuleId !== (string)($conflict['id'] ?? ''))) {
|
||||
$modal = render_create_conflict_modal($ctx, $rule, $conflict);
|
||||
} else {
|
||||
if (!empty($rule['enabled'])) {
|
||||
$nativeAction = $ctx->post('native_conflict_action');
|
||||
if ($nativeAction === 'disable_native') {
|
||||
delete_untracked_native_da_vacations($ctx);
|
||||
} else {
|
||||
$nativeEntries = list_untracked_native_da_vacations($ctx);
|
||||
if ($nativeEntries !== []) {
|
||||
$extra = [];
|
||||
if ($conflict !== null) {
|
||||
$extra = [
|
||||
'conflict_action' => 'replace',
|
||||
'conflict_rule_id' => (string)($conflict['id'] ?? ''),
|
||||
];
|
||||
}
|
||||
$modal = render_native_vacation_conflict_modal($ctx, $rule, $nativeEntries, $extra);
|
||||
$ctx->render('Add autoresponder', render_rule_form($ctx, 'create', $formRule) . $modal, [], $errors);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
if ($conflict !== null) {
|
||||
create_rule_replacing_active($ctx, $rule, $conflict);
|
||||
} else {
|
||||
create_rule_with_sync($ctx, $rule);
|
||||
}
|
||||
Http::redirect($ctx->url('index.html', ['status' => 'created']));
|
||||
}
|
||||
Http::redirect($ctx->url('index.html', ['status' => 'created']));
|
||||
} catch (Throwable $e) {
|
||||
PluginLogger::exception($e, 'CREATE');
|
||||
$errors[] = $e->getMessage();
|
||||
}
|
||||
}
|
||||
$ctx->render('Add autoresponder', render_rule_form($ctx, 'create', null), [], $errors);
|
||||
$ctx->render('Add autoresponder', render_rule_form($ctx, 'create', $formRule) . $modal, [], $errors);
|
||||
};
|
||||
|
||||
/** @param array<string,mixed> $rule */
|
||||
function create_rule_with_sync(AppContext $ctx, array $rule): array
|
||||
{
|
||||
$created = $ctx->rules->create($ctx->daUser->username(), $rule);
|
||||
try {
|
||||
$ctx->syncDirectAdminVacations();
|
||||
} catch (Throwable $syncError) {
|
||||
$ctx->rules->delete($ctx->daUser->username(), (string)$created['id']);
|
||||
throw $syncError;
|
||||
}
|
||||
return $created;
|
||||
}
|
||||
|
||||
/** @param array<string,mixed> $rule @param array<string,mixed> $active */
|
||||
function create_rule_replacing_active(AppContext $ctx, array $rule, array $active): array
|
||||
{
|
||||
$activeId = (string)($active['id'] ?? '');
|
||||
if ($activeId === '') {
|
||||
throw new RuntimeException($ctx->t('Rule not found'));
|
||||
}
|
||||
$ctx->rules->update($ctx->daUser->username(), $activeId, ['enabled' => false]);
|
||||
$created = $ctx->rules->create($ctx->daUser->username(), $rule);
|
||||
try {
|
||||
$ctx->syncDirectAdminVacations();
|
||||
} catch (Throwable $syncError) {
|
||||
$ctx->rules->delete($ctx->daUser->username(), (string)$created['id']);
|
||||
$ctx->rules->update($ctx->daUser->username(), $activeId, ['enabled' => true]);
|
||||
throw $syncError;
|
||||
}
|
||||
return $created;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user