From b9b04135106bc32f432f9042de4428c211fee012 Mon Sep 17 00:00:00 2001
From: James Cole
Date: Thu, 14 Jan 2016 10:33:24 +0100
Subject: [PATCH] More resources for rules.
---
app/Http/Controllers/RuleController.php | 12 ++++++---
app/Repositories/Rule/RuleRepository.php | 25 +++++++++++++++++--
.../Rule/RuleRepositoryInterface.php | 9 ++++++-
app/Support/ExpandedForm.php | 2 +-
resources/lang/en_US/firefly.php | 1 +
resources/views/rules/rule-group/delete.twig | 12 +++++++++
6 files changed, 54 insertions(+), 7 deletions(-)
diff --git a/app/Http/Controllers/RuleController.php b/app/Http/Controllers/RuleController.php
index 563c653154..0bae1ae214 100644
--- a/app/Http/Controllers/RuleController.php
+++ b/app/Http/Controllers/RuleController.php
@@ -10,6 +10,7 @@
namespace FireflyIII\Http\Controllers;
use Auth;
+use ExpandedForm;
use FireflyIII\Http\Requests;
use FireflyIII\Http\Requests\RuleGroupFormRequest;
use FireflyIII\Models\Rule;
@@ -146,16 +147,19 @@ class RuleController extends Controller
*
* @return \Illuminate\View\View
*/
- public function deleteRuleGroup(RuleGroup $ruleGroup)
+ public function deleteRuleGroup(RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$subTitle = trans('firefly.delete_rule_group', ['title' => $ruleGroup->title]);
+ $ruleGroupList = Expandedform::makeSelectList($repository->getRuleGroups(), true);
+ unset($ruleGroupList[$ruleGroup->id]);
+
// 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'));
+ return view('rules.rule-group.delete', compact('ruleGroup', 'subTitle', 'ruleGroupList'));
}
/**
@@ -168,7 +172,9 @@ class RuleController extends Controller
{
$title = $ruleGroup->title;
- $repository->destroyRuleGroup($ruleGroup);
+ $moveTo = Auth::user()->ruleGroups()->find(intval(Input::get('move_rules_before_delete')));
+
+ $repository->destroyRuleGroup($ruleGroup, $moveTo);
Session::flash('success', trans('firefly.deleted_rule_group', ['title' => $title]));
diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php
index 71509ee640..c2f03e4b2b 100644
--- a/app/Repositories/Rule/RuleRepository.php
+++ b/app/Repositories/Rule/RuleRepository.php
@@ -12,6 +12,7 @@ namespace FireflyIII\Repositories\Rule;
use Auth;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
+use Illuminate\Support\Collection;
use Log;
/**
@@ -77,19 +78,31 @@ class RuleRepository implements RuleRepositoryInterface
/**
* @param RuleGroup $ruleGroup
+ * @param RuleGroup $moveTo
*
* @return boolean
*/
- public function destroyRuleGroup(RuleGroup $ruleGroup)
+ public function destroyRuleGroup(RuleGroup $ruleGroup, RuleGroup $moveTo = null)
{
/** @var Rule $rule */
foreach ($ruleGroup->rules as $rule) {
- $rule->delete();
+
+ if (is_null($moveTo)) {
+
+ $rule->delete();
+ } else {
+ // move
+ $rule->ruleGroup()->associate($moveTo);
+ $rule->save();
+ }
}
$ruleGroup->delete();
$this->resetRuleGroupOrder();
+ if (!is_null($moveTo)) {
+ $this->resetRulesInGroupOrder($moveTo);
+ }
return true;
}
@@ -215,4 +228,12 @@ class RuleRepository implements RuleRepositoryInterface
$ruleGroup->save();
$this->resetRuleGroupOrder();
}
+
+ /**
+ * @return Collection
+ */
+ public function getRuleGroups()
+ {
+ return Auth::user()->ruleGroups()->orderBy('order', 'ASC')->get();
+ }
}
\ No newline at end of file
diff --git a/app/Repositories/Rule/RuleRepositoryInterface.php b/app/Repositories/Rule/RuleRepositoryInterface.php
index f497e0568d..28c1d8f148 100644
--- a/app/Repositories/Rule/RuleRepositoryInterface.php
+++ b/app/Repositories/Rule/RuleRepositoryInterface.php
@@ -11,6 +11,7 @@ namespace FireflyIII\Repositories\Rule;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
+use Illuminate\Support\Collection;
/**
* Interface RuleRepositoryInterface
@@ -43,10 +44,11 @@ interface RuleRepositoryInterface
/**
* @param RuleGroup $ruleGroup
+ * @param RuleGroup $moveTo
*
* @return boolean
*/
- public function destroyRuleGroup(RuleGroup $ruleGroup);
+ public function destroyRuleGroup(RuleGroup $ruleGroup, RuleGroup $moveTo = null);
/**
* @return bool
@@ -82,4 +84,9 @@ interface RuleRepositoryInterface
*/
public function moveRuleGroupDown(RuleGroup $ruleGroup);
+ /**
+ * @return Collection
+ */
+ public function getRuleGroups();
+
}
\ No newline at end of file
diff --git a/app/Support/ExpandedForm.php b/app/Support/ExpandedForm.php
index d9153dc098..37e4cc6c53 100644
--- a/app/Support/ExpandedForm.php
+++ b/app/Support/ExpandedForm.php
@@ -269,7 +269,7 @@ class ExpandedForm
$title = null;
foreach ($fields as $field) {
- if (isset($entry->$field)) {
+ if (isset($entry->$field) && is_null($title)) {
$title = $entry->$field;
}
}
diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php
index 17c03e889d..34c16a8eb5 100644
--- a/resources/lang/en_US/firefly.php
+++ b/resources/lang/en_US/firefly.php
@@ -59,6 +59,7 @@ return [
'no_rules_in_group' => 'There are no rules in this group',
'move_rule_group_up' => 'Move rule group up',
'move_rule_group_down' => 'Move rule group down',
+ 'save_rules_by_moving' => 'Save these rule(s) by moving them to another rule group:',
// actions and triggers
'rule_trigger_user_action' => 'User action is ":trigger_value"',
diff --git a/resources/views/rules/rule-group/delete.twig b/resources/views/rules/rule-group/delete.twig
index 7d5e8e1c1b..17aa5a2bd5 100644
--- a/resources/views/rules/rule-group/delete.twig
+++ b/resources/views/rules/rule-group/delete.twig
@@ -27,6 +27,18 @@
{% endif %}
+ {% if ruleGroup.rules|length > 0 %}
+
+ {{ 'save_rules_by_moving'|_ }}
+
+
+
+ {{ Form.select('move_rules_before_delete', ruleGroupList, null, {class: 'form-control'}) }}
+
+ {% else %}
+
+ {% endif %}
+