diff --git a/app/Http/Controllers/Rule/CreateController.php b/app/Http/Controllers/Rule/CreateController.php index b80af3a389..561d41a3cf 100644 --- a/app/Http/Controllers/Rule/CreateController.php +++ b/app/Http/Controllers/Rule/CreateController.php @@ -27,6 +27,7 @@ namespace FireflyIII\Http\Controllers\Rule; use FireflyIII\Http\Controllers\Controller; use FireflyIII\Http\Requests\RuleFormRequest; use FireflyIII\Models\Bill; +use FireflyIII\Models\Rule; use FireflyIII\Models\RuleGroup; use FireflyIII\Models\TransactionJournal; use FireflyIII\Repositories\Rule\RuleRepositoryInterface; @@ -113,6 +114,21 @@ class CreateController extends Controller ); } + /** + * @param Rule $rule + * + * @return RedirectResponse + */ + public function duplicate(Rule $rule): RedirectResponse + { + /** @var Rule $newRule */ + $newRule = $this->ruleRepos->duplicate($rule); + + session()->flash('success', trans('firefly.duplicated_rule', ['title' => $rule->title,'newTitle' => $newRule->title])); + + return redirect(route('rules.index')); + } + /** * Create a new rule. It will be stored under the given $ruleGroup. * diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index 75c5a32e1f..c9b16dbe8a 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -475,4 +475,32 @@ class RuleRepository implements RuleRepositoryInterface return true; } + + /** + * @inheritDoc + */ + public function duplicate(Rule $rule): Rule + { + $newRule = $rule->replicate(); + $newRule->title = (string)trans('firefly.rule_copy_of', ['title' => $rule->title]); + $newRule->save(); + + // replicate all triggers + /** @var RuleTrigger $trigger */ + foreach ($rule->ruleTriggers as $trigger) { + $newTrigger = $trigger->replicate(); + $newTrigger->rule_id = $newRule->id; + $newTrigger->save(); + } + + // replicate all actions + /** @var RuleAction $action */ + foreach ($rule->ruleActions as $action) { + $newAction = $action->replicate(); + $newAction->rule_id = $newRule->id; + $newAction->save(); + } + + return $newRule; + } } diff --git a/app/Repositories/Rule/RuleRepositoryInterface.php b/app/Repositories/Rule/RuleRepositoryInterface.php index 1cec41bdf7..a979201e34 100644 --- a/app/Repositories/Rule/RuleRepositoryInterface.php +++ b/app/Repositories/Rule/RuleRepositoryInterface.php @@ -46,6 +46,13 @@ interface RuleRepositoryInterface */ public function destroy(Rule $rule): bool; + /** + * @param Rule $rule + * + * @return Rule + */ + public function duplicate(Rule $rule): Rule; + /** * @param int $ruleId * diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 2d589d5bef..c93febc83b 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -307,6 +307,9 @@ return [ 'created_new_rule_group' => 'New rule group ":title" stored!', 'updated_rule_group' => 'Successfully updated rule group ":title".', 'edit_rule_group' => 'Edit rule group ":title"', + 'duplicate_rule' => 'Duplicate rule ":title"', + 'rule_copy_of' => 'Copy of ":title"', + 'duplicated_rule' => 'Duplicated rule ":title" into ":newTitle"', 'delete_rule_group' => 'Delete rule group ":title"', 'deleted_rule_group' => 'Deleted rule group ":title"', 'update_rule_group' => 'Update rule group', diff --git a/resources/views/v1/rules/index.twig b/resources/views/v1/rules/index.twig index ba52eaa5a6..059b00707b 100644 --- a/resources/views/v1/rules/index.twig +++ b/resources/views/v1/rules/index.twig @@ -115,6 +115,11 @@ + + {# duplicate rule #} + +