firefly-iii/app/Repositories/Rule/RuleRepository.php

282 lines
6.7 KiB
PHP
Raw Normal View History

2016-01-13 11:34:56 -06:00
<?php
/**
* RuleRepository.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Repositories\Rule;
use Auth;
use FireflyIII\Models\Rule;
2016-01-13 11:34:56 -06:00
use FireflyIII\Models\RuleGroup;
use FireflyIII\Models\RuleTrigger;
2016-01-14 03:33:24 -06:00
use Illuminate\Support\Collection;
2016-01-14 02:38:48 -06:00
use Log;
2016-01-13 11:34:56 -06:00
/**
* Class RuleRepository
*
* @package FireflyIII\Repositories\Rule
*/
class RuleRepository implements RuleRepositoryInterface
{
/**
* @return int
*/
public function getHighestOrderRuleGroup()
{
$entry = Auth::user()->ruleGroups()->max('order');
return intval($entry);
}
/**
* @param array $data
*
* @return RuleGroup
*/
public function storeRuleGroup(array $data)
{
$order = $this->getHighestOrderRuleGroup();
$newRuleGroup = new RuleGroup(
[
'user_id' => $data['user'],
'title' => $data['title'],
'description' => $data['description'],
'order' => ($order + 1),
'active' => 1,
]
);
$newRuleGroup->save();
$this->resetRuleGroupOrder();
2016-01-13 11:34:56 -06:00
return $newRuleGroup;
}
2016-01-13 11:50:15 -06:00
/**
* @param RuleGroup $ruleGroup
* @param array $data
*
* @return RuleGroup
*/
public function updateRuleGroup(RuleGroup $ruleGroup, array $data)
2016-01-13 11:50:15 -06:00
{
// update the account:
$ruleGroup->title = $data['title'];
$ruleGroup->description = $data['description'];
$ruleGroup->active = $data['active'];
$ruleGroup->save();
$this->resetRuleGroupOrder();
2016-01-13 11:50:15 -06:00
return $ruleGroup;
}
/**
* @param Rule $rule
* @param array $ids
* @return bool
*/
public function reorderRuleTriggers(Rule $rule, array $ids)
{
$order = 1;
foreach ($ids as $triggerId) {
/** @var RuleTrigger $trigger */
$trigger = $rule->ruleTriggers()->find($triggerId);
if (!is_null($trigger)) {
$trigger->order = $order;
$trigger->save();
$order++;
}
}
return true;
}
/**
* @param Rule $rule
* @param array $ids
* @return bool
*/
public function reorderRuleActions(Rule $rule, array $ids)
{
$order = 1;
foreach ($ids as $actionId) {
/** @var RuleTrigger $trigger */
$action = $rule->ruleActions()->find($actionId);
if (!is_null($action)) {
$action->order = $order;
$action->save();
$order++;
}
}
return true;
}
/**
* @param RuleGroup $ruleGroup
2016-01-14 03:33:24 -06:00
* @param RuleGroup $moveTo
*
* @return boolean
*/
2016-01-14 03:33:24 -06:00
public function destroyRuleGroup(RuleGroup $ruleGroup, RuleGroup $moveTo = null)
{
/** @var Rule $rule */
foreach ($ruleGroup->rules as $rule) {
2016-01-14 03:33:24 -06:00
if (is_null($moveTo)) {
$rule->delete();
} else {
// move
$rule->ruleGroup()->associate($moveTo);
$rule->save();
}
}
$ruleGroup->delete();
$this->resetRuleGroupOrder();
2016-01-14 03:33:24 -06:00
if (!is_null($moveTo)) {
$this->resetRulesInGroupOrder($moveTo);
}
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;
}
2016-01-14 02:38:48 -06:00
/**
* @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++;
}
}
2016-01-14 02:53:59 -06:00
/**
* @param RuleGroup $ruleGroup
* @return bool
*/
public function moveRuleGroupUp(RuleGroup $ruleGroup)
{
$order = $ruleGroup->order;
// find the rule with order-1 and give it order+1
$other = Auth::user()->ruleGroups()->where('order', ($order - 1))->first();
if ($other) {
$other->order = ($other->order + 1);
$other->save();
}
$ruleGroup->order = ($ruleGroup->order - 1);
$ruleGroup->save();
$this->resetRuleGroupOrder();
}
/**
* @param RuleGroup $ruleGroup
* @return bool
*/
public function moveRuleGroupDown(RuleGroup $ruleGroup)
{
$order = $ruleGroup->order;
// find the rule with order+1 and give it order-1
$other = Auth::user()->ruleGroups()->where('order', ($order + 1))->first();
if ($other) {
$other->order = ($other->order - 1);
$other->save();
}
$ruleGroup->order = ($ruleGroup->order + 1);
$ruleGroup->save();
$this->resetRuleGroupOrder();
}
2016-01-14 03:33:24 -06:00
/**
* @return Collection
*/
public function getRuleGroups()
{
return Auth::user()->ruleGroups()->orderBy('order', 'ASC')->get();
}
2016-01-13 11:34:56 -06:00
}