Files
2026-07-04 15:16:50 +02:00

52 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
return static function (AppContext $ctx): void {
try {
$ctx->requirePost();
$id = $ctx->post('id');
$ctx->requireCsrf('toggle:' . $id);
$rule = $ctx->rules->find($ctx->daUser->username(), $id);
if ($rule === null) {
throw new RuntimeException($ctx->t('Rule not found'));
}
$next = ['enabled' => empty($rule['enabled'])];
if (!empty($next['enabled'])) {
$conflict = find_active_rule_conflict($ctx, $id);
$conflictAction = $ctx->post('conflict_action');
$conflictRuleId = $ctx->post('conflict_rule_id');
if ($conflict !== null && ($conflictAction !== 'replace' || $conflictRuleId !== (string)($conflict['id'] ?? ''))) {
render_rules_index($ctx, [], [], render_toggle_conflict_modal($ctx, $rule, $conflict));
return;
}
if ($conflict !== null) {
toggle_rule_replacing_active($ctx, $id, (string)($conflict['id'] ?? ''));
Http::redirect($ctx->url('index.html', ['status' => 'toggled']));
return;
}
}
$ctx->rules->update($ctx->daUser->username(), $id, $next);
$ctx->syncDirectAdminVacations();
Http::redirect($ctx->url('index.html', ['status' => 'toggled']));
} catch (Throwable $e) {
PluginLogger::exception($e, 'TOGGLE');
throw $e;
}
};
function toggle_rule_replacing_active(AppContext $ctx, string $targetId, string $activeId): void
{
if ($activeId === '') {
throw new RuntimeException($ctx->t('Rule not found'));
}
$ctx->rules->update($ctx->daUser->username(), $activeId, ['enabled' => false]);
$ctx->rules->update($ctx->daUser->username(), $targetId, ['enabled' => true]);
try {
$ctx->syncDirectAdminVacations();
} catch (Throwable $syncError) {
$ctx->rules->update($ctx->daUser->username(), $targetId, ['enabled' => false]);
$ctx->rules->update($ctx->daUser->username(), $activeId, ['enabled' => true]);
throw $syncError;
}
}