From 4697fbdeef9c2db4d9dacb7323f88664764b2932 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 13 Jan 2016 21:44:26 +0100 Subject: [PATCH] Routine to delete rules. Should include routine to move rules to other group. --- app/Http/Controllers/RuleController.php | 39 +++++++++++++++- app/Http/routes.php | 1 + app/Models/Rule.php | 2 + app/Models/RuleGroup.php | 2 + app/Repositories/Rule/RuleRepository.php | 44 ++++++++++++++++++- .../Rule/RuleRepositoryInterface.php | 15 ++++++- .../2016_01_11_193428_changes_for_v370.php | 4 -- resources/lang/en_US/firefly.php | 2 + resources/lang/en_US/form.php | 3 ++ resources/views/rules/rule-group/delete.twig | 41 +++++++++++++++++ 10 files changed, 146 insertions(+), 7 deletions(-) create mode 100644 resources/views/rules/rule-group/delete.twig diff --git a/app/Http/Controllers/RuleController.php b/app/Http/Controllers/RuleController.php index cb4d63f2ce..abe2bf9dc4 100644 --- a/app/Http/Controllers/RuleController.php +++ b/app/Http/Controllers/RuleController.php @@ -124,7 +124,7 @@ class RuleController extends Controller 'active' => intval($request->input('active')) == 1, ]; - $repository->update($ruleGroup, $data); + $repository->updateRuleGroup($ruleGroup, $data); Session::flash('success', trans('firefly.updated_rule_group', ['title' => $ruleGroup->title])); Preferences::mark(); @@ -141,6 +141,43 @@ class RuleController extends Controller } + /** + * @param RuleGroup $budget + * + * @return \Illuminate\View\View + */ + public function deleteRuleGroup(RuleGroup $ruleGroup) + { + $subTitle = trans('firefly.delete_rule_group', ['title' => $ruleGroup->title]); + + // put previous url in session + Session::put('rules.rule-group.delete.url', URL::previous()); + Session::flash('gaEventCategory', 'rules'); + Session::flash('gaEventAction', 'delete-rule-group'); + + return view('rules.rule-group.delete', compact('ruleGroup', 'subTitle')); + } + + /** + * @param RuleGroup $ruleGroup + * @param RuleRepositoryInterface $repository + * + * @return \Illuminate\Http\RedirectResponse + */ + public function destroyRuleGroup(RuleGroup $ruleGroup, RuleRepositoryInterface $repository) + { + + $title = $ruleGroup->title; + $repository->destroyRuleGroup($ruleGroup); + + + Session::flash('success', trans('firefly.deleted_rule_group', ['title' => $title])); + Preferences::mark(); + + + return redirect(Session::get('rules.rule-group.delete.url')); + } + /** * @return View diff --git a/app/Http/routes.php b/app/Http/routes.php index a454ddbf92..c6d261bd91 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -249,6 +249,7 @@ Route::group( Route::post('/rules/groups/store', ['uses' => 'RuleController@storeRuleGroup', 'as' => 'rules.rule-group.store']); Route::post('/rules/groups/update/{ruleGroup}', ['uses' => 'RuleController@updateRuleGroup', 'as' => 'rules.rule-group.update']); + Route::post('/rules/groups/destroy/{ruleGroup}', ['uses' => 'RuleController@destroyRuleGroup', 'as' => 'rules.rule-group.destroy']); /** * Search Controller diff --git a/app/Models/Rule.php b/app/Models/Rule.php index ca438e3224..b99d6334b0 100644 --- a/app/Models/Rule.php +++ b/app/Models/Rule.php @@ -11,6 +11,7 @@ namespace FireflyIII\Models; use Auth; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -35,6 +36,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; */ class Rule extends Model { + use SoftDeletes; /** * @return \Illuminate\Database\Eloquent\Relations\BelongsTo */ diff --git a/app/Models/RuleGroup.php b/app/Models/RuleGroup.php index e9a9793f33..88c3733836 100644 --- a/app/Models/RuleGroup.php +++ b/app/Models/RuleGroup.php @@ -11,6 +11,7 @@ namespace FireflyIII\Models; use Auth; use Illuminate\Database\Eloquent\Model; +use Illuminate\Database\Eloquent\SoftDeletes; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; /** @@ -31,6 +32,7 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; */ class RuleGroup extends Model { + use SoftDeletes; protected $fillable = ['user_id', 'order', 'title', 'description', 'active']; diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index 9dba43eaf7..3a7bfb1c2d 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -10,6 +10,7 @@ namespace FireflyIII\Repositories\Rule; use Auth; +use FireflyIII\Models\Rule; use FireflyIII\Models\RuleGroup; /** @@ -50,6 +51,7 @@ class RuleRepository implements RuleRepositoryInterface ] ); $newRuleGroup->save(); + $this->resetRuleGroupOrder(); return $newRuleGroup; } @@ -60,14 +62,54 @@ class RuleRepository implements RuleRepositoryInterface * * @return RuleGroup */ - public function update(RuleGroup $ruleGroup, array $data) + public function updateRuleGroup(RuleGroup $ruleGroup, array $data) { // update the account: $ruleGroup->title = $data['title']; $ruleGroup->description = $data['description']; $ruleGroup->active = $data['active']; $ruleGroup->save(); + $this->resetRuleGroupOrder(); return $ruleGroup; } + + /** + * @param RuleGroup $ruleGroup + * + * @return boolean + */ + public function destroyRuleGroup(RuleGroup $ruleGroup) + { + /** @var Rule $rule */ + foreach ($ruleGroup->rules as $rule) { + $rule->delete(); + } + + $ruleGroup->delete(); + + $this->resetRuleGroupOrder(); + + return true; + } + + /** + * @return bool + */ + public function resetRuleGroupOrder() + { + Auth::user()->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]); + + $set = Auth::user()->ruleGroups()->where('active', 1)->orderBy('order', 'ASC')->get(); + $count = 1; + /** @var RuleGroup $entry */ + foreach ($set as $entry) { + $entry->order = $count; + $entry->save(); + $count++; + } + + + return true; + } } \ No newline at end of file diff --git a/app/Repositories/Rule/RuleRepositoryInterface.php b/app/Repositories/Rule/RuleRepositoryInterface.php index e1b910cef2..ea4e2e1b5f 100644 --- a/app/Repositories/Rule/RuleRepositoryInterface.php +++ b/app/Repositories/Rule/RuleRepositoryInterface.php @@ -37,6 +37,19 @@ interface RuleRepositoryInterface * * @return RuleGroup */ - public function update(RuleGroup $ruleGroup, array $data); + public function updateRuleGroup(RuleGroup $ruleGroup, array $data); + + + /** + * @param RuleGroup $ruleGroup + * + * @return boolean + */ + public function destroyRuleGroup(RuleGroup $ruleGroup); + + /** + * @return bool + */ + public function resetRuleGroupOrder(); } \ No newline at end of file diff --git a/database/migrations/2016_01_11_193428_changes_for_v370.php b/database/migrations/2016_01_11_193428_changes_for_v370.php index adb0833a9f..20ba91e746 100644 --- a/database/migrations/2016_01_11_193428_changes_for_v370.php +++ b/database/migrations/2016_01_11_193428_changes_for_v370.php @@ -37,8 +37,6 @@ class ChangesForV370 extends Migration // connect rule groups to users $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade'); - // order must be unique for rule group: - $table->unique(['user_id', 'order']); } ); @@ -65,8 +63,6 @@ class ChangesForV370 extends Migration // connect rules to rule groups $table->foreign('rule_group_id')->references('id')->on('rule_groups')->onDelete('cascade'); - // order must be unique for rules: - $table->unique(['user_id', 'order']); } ); diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 90d7bcd70b..2e80cf5fc9 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -53,6 +53,8 @@ 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"', + 'delete_rule_group' => 'Delete rule group ":title"', + 'deleted_rule_group' => 'Deleted rule group ":title"', 'update_rule_group' => 'Update rule group', 'no_rules_in_group' => 'There are no rules in this group', diff --git a/resources/lang/en_US/form.php b/resources/lang/en_US/form.php index b76eac684f..8b3b34f4c6 100644 --- a/resources/lang/en_US/form.php +++ b/resources/lang/en_US/form.php @@ -76,10 +76,12 @@ return [ 'delete_currency' => 'Delete currency ":name"', 'delete_journal' => 'Delete transaction with description ":description"', 'delete_attachment' => 'Delete attachment ":name"', + 'delete_rule_group' => 'Delete rule group ":title"', 'attachment_areYouSure' => 'Are you sure you want to delete the attachment named ":name"?', 'account_areYouSure' => 'Are you sure you want to delete the account named ":name"?', 'bill_areYouSure' => 'Are you sure you want to delete the bill named ":name"?', + 'ruleGroup_areYouSure' => 'Are you sure you want to delete the rule group titles ":title"?', 'budget_areYouSure' => 'Are you sure you want to delete the budget named ":name"?', 'category_areYouSure' => 'Are you sure you want to delete the category named ":name"?', 'currency_areYouSure' => 'Are you sure you want to delete the currency named ":name"?', @@ -89,6 +91,7 @@ return [ 'permDeleteWarning' => 'Deleting stuff from Firely is permanent and cannot be undone.', 'also_delete_transactions' => 'The only transaction connected to this account will be deleted as well.|All :count transactions connected to this account will be deleted as well.', + 'also_delete_rules' => 'The only rule connected to this rule group will be deleted as well.|All :count rules connected to this rule group will be deleted as well.', 'also_delete_piggyBanks' => 'The only piggy bank connected to this account will be deleted as well.|All :count piggy bank connected to this account will be deleted as well.', 'bill_keep_transactions' => 'The only transaction connected to this bill will not be deleted.|All :count transactions connected to this bill will spared deletion.', 'budget_keep_transactions' => 'The only transaction connected to this budget will not be deleted.|All :count transactions connected to this budget will spared deletion.', diff --git a/resources/views/rules/rule-group/delete.twig b/resources/views/rules/rule-group/delete.twig new file mode 100644 index 0000000000..7d5e8e1c1b --- /dev/null +++ b/resources/views/rules/rule-group/delete.twig @@ -0,0 +1,41 @@ +{% extends "./layout/default.twig" %} + +{% block breadcrumbs %} + {{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName, ruleGroup) }} +{% endblock %} + +{% block content %} + {{ Form.open({'class' : 'form-horizontal','id' : 'destroy','url' : route('rules.rule-group.destroy',ruleGroup.id) }) }} +
+
+
+
+

{{ trans('form.delete_rule_group', {'title': ruleGroup.title}) }}

+
+
+

+ {{ trans('form.permDeleteWarning') }} +

+ +

+ {{ trans('form.ruleGroup_areYouSure', {'title': ruleGroup.title}) }} +

+ +

+ {% if ruleGroup.rules|length > 0 %} + {{ Lang.choice('form.also_delete_rules', ruleGroup.rules|length, {count: ruleGroup.rules|length}) }} + {% endif %} +

+ +
+ + + +
+
+
+ {{ Form.close|raw }} +{% endblock %}