requireCsrf('create'); $input = normalize_rule_post($_POST); $rule = attach_rule_scope($ctx, RuleValidator::validate($input, $ctx->settings)); $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'])); } } catch (Throwable $e) { PluginLogger::exception($e, 'CREATE'); $errors[] = $e->getMessage(); } } $ctx->render('Add autoresponder', render_rule_form($ctx, 'create', $formRule) . $modal, [], $errors); }; /** @param array $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 $rule @param array $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; }