Button to duplicate rule. #2957

This commit is contained in:
James Cole 2020-03-14 21:01:21 +01:00
parent 50b710b4f6
commit 33c73701d8
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
6 changed files with 60 additions and 0 deletions

View File

@ -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.
*

View File

@ -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;
}
}

View File

@ -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
*

View File

@ -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',

View File

@ -115,6 +115,11 @@
<a href="{{ route('rules.select-transactions',rule.id) }}" class="btn btn-default"
title=" {{ trans('firefly.apply_rule_selection', {title: rule.title}) }}">
<i class="fa fa-fw fa-power-off "></i></a>
{# duplicate rule #}
<a href="{{ route('rules.duplicate',rule.id) }}" class="btn btn-default"
title=" {{ trans('firefly.duplicate_rule', {title: rule.title}) }}">
<i class="fa fa-fw fa-copy"></i></a>
</div>
</td>
<td class="markdown">

View File

@ -881,6 +881,7 @@ Route::group(
Route::get('create-from-bill/{bill}', ['uses' => 'Rule\CreateController@createFromBill', 'as' => 'create-from-bill']);
Route::get('create-from-journal/{tj}', ['uses' => 'Rule\CreateController@createFromJournal', 'as' => 'create-from-journal']);
Route::post('store', ['uses' => 'Rule\CreateController@store', 'as' => 'store']);
Route::get('duplicate/{rule}', ['uses' => 'Rule\CreateController@duplicate', 'as' => 'duplicate']);
// delete controller
Route::get('delete/{rule}', ['uses' => 'Rule\DeleteController@delete', 'as' => 'delete']);