More resources for rules.

This commit is contained in:
James Cole 2016-01-14 10:33:24 +01:00
parent 97770da619
commit b9b0413510
6 changed files with 54 additions and 7 deletions

View File

@ -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]));

View File

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

View File

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

View File

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

View File

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

View File

@ -27,6 +27,18 @@
{% endif %}
</p>
{% if ruleGroup.rules|length > 0 %}
<p class="text-success">
{{ 'save_rules_by_moving'|_ }}
</p>
<p>
{{ Form.select('move_rules_before_delete', ruleGroupList, null, {class: 'form-control'}) }}
</p>
{% else %}
<input type="hidden" name="move_rules_before_delete" value="0"/>
{% endif %}
</div>
<div class="box-footer">
<input type="submit" name="submit" value="{{ trans('form.deletePermanently') }}" class="btn btn-danger pull-right"/>