mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2024-11-26 02:40:43 -06:00
Can order rules.
This commit is contained in:
parent
245f06c93a
commit
15d3414443
@ -184,16 +184,38 @@ class RuleController extends Controller
|
||||
*/
|
||||
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'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
* @param RuleRepositoryInterface $repository
|
||||
* @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'));
|
||||
|
||||
}
|
||||
|
||||
|
@ -12,6 +12,7 @@ namespace FireflyIII\Repositories\Rule;
|
||||
use Auth;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleGroup;
|
||||
use Log;
|
||||
|
||||
/**
|
||||
* Class RuleRepository
|
||||
@ -112,4 +113,66 @@ class RuleRepository implements RuleRepositoryInterface
|
||||
|
||||
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++;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -9,6 +9,7 @@
|
||||
|
||||
namespace FireflyIII\Repositories\Rule;
|
||||
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleGroup;
|
||||
|
||||
/**
|
||||
@ -52,4 +53,21 @@ interface RuleRepositoryInterface
|
||||
*/
|
||||
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);
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user