Can order rules.

This commit is contained in:
James Cole 2016-01-14 09:38:48 +01:00
parent 245f06c93a
commit 15d3414443
3 changed files with 106 additions and 3 deletions

View File

@ -184,16 +184,38 @@ class RuleController extends Controller
*/ */
public function index() public function index()
{ {
$ruleGroups = Auth::user()->ruleGroups()->with('rules')->get(); $ruleGroups = Auth::user()->ruleGroups()->with(['rules' => function($query) {
$query->orderBy('order','ASC');
}])->get();
return view('rules.index', compact('ruleGroups')); return view('rules.index', compact('ruleGroups'));
} }
/** /**
* @param RuleRepositoryInterface $repository
* @param Rule $rule * @param Rule $rule
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/ */
public function upRule(Rule $rule) public function upRule(RuleRepositoryInterface $repository, Rule $rule)
{ {
$repository->moveRuleUp($rule);
return redirect(route('rules.index'));
}
/**
* @param RuleRepositoryInterface $repository
* @param Rule $rule
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function downRule(RuleRepositoryInterface $repository, Rule $rule)
{
$repository->moveRuleDown($rule);
return redirect(route('rules.index'));
} }

View File

@ -12,6 +12,7 @@ namespace FireflyIII\Repositories\Rule;
use Auth; use Auth;
use FireflyIII\Models\Rule; use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup; use FireflyIII\Models\RuleGroup;
use Log;
/** /**
* Class RuleRepository * Class RuleRepository
@ -112,4 +113,66 @@ class RuleRepository implements RuleRepositoryInterface
return true; return true;
} }
/**
* @param Rule $rule
* @return bool
*/
public function moveRuleUp(Rule $rule)
{
$order = $rule->order;
// find the rule with order-1 and give it order+1
$other = $rule->ruleGroup->rules()->where('order', ($order - 1))->first();
if ($other) {
$other->order = ($other->order + 1);
$other->save();
}
$rule->order = ($rule->order - 1);
$rule->save();
$this->resetRulesInGroupOrder($rule->ruleGroup);
}
/**
* @param Rule $rule
* @return bool
*/
public function moveRuleDown(Rule $rule)
{
$order = $rule->order;
// find the rule with order+1 and give it order-1
$other = $rule->ruleGroup->rules()->where('order', ($order + 1))->first();
if ($other) {
$other->order = $other->order - 1;
$other->save();
}
$rule->order = ($rule->order + 1);
$rule->save();
$this->resetRulesInGroupOrder($rule->ruleGroup);
}
/**
* @return bool
*/
public function resetRulesInGroupOrder(RuleGroup $ruleGroup)
{
$ruleGroup->rules()->whereNotNull('deleted_at')->update(['order' => 0]);
$set = $ruleGroup->rules()
->orderBy('order', 'ASC')
->orderBy('updated_at', 'DESC')
->get();
$count = 1;
/** @var Rule $entry */
foreach ($set as $entry) {
$entry->order = $count;
$entry->save();
$count++;
}
}
} }

View File

@ -9,6 +9,7 @@
namespace FireflyIII\Repositories\Rule; namespace FireflyIII\Repositories\Rule;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup; use FireflyIII\Models\RuleGroup;
/** /**
@ -52,4 +53,21 @@ interface RuleRepositoryInterface
*/ */
public function resetRuleGroupOrder(); public function resetRuleGroupOrder();
/**
* @return bool
*/
public function resetRulesInGroupOrder(RuleGroup $ruleGroup);
/**
* @param Rule $rule
* @return bool
*/
public function moveRuleUp(Rule $rule);
/**
* @param Rule $rule
* @return bool
*/
public function moveRuleDown(Rule $rule);
} }