diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index 09659c2c30..1521372f3d 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -23,59 +23,6 @@ use Illuminate\Support\Collection; */ 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(); - - return $newRuleGroup; - } - - /** - * @param RuleGroup $ruleGroup - * @param array $data - * - * @return RuleGroup - */ - public function updateRuleGroup(RuleGroup $ruleGroup, array $data) - { - // update the account: - $ruleGroup->title = $data['title']; - $ruleGroup->description = $data['description']; - $ruleGroup->active = $data['active']; - $ruleGroup->save(); - $this->resetRuleGroupOrder(); - - return $ruleGroup; - } /** * @param Rule $rule @@ -99,6 +46,30 @@ class RuleRepository implements RuleRepositoryInterface return true; } + /** + * @param RuleGroup $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++; + } + + return true; + + } + /** * @param Rule $rule * @param array $ids @@ -121,56 +92,6 @@ class RuleRepository implements RuleRepositoryInterface return true; } - /** - * @param RuleGroup $ruleGroup - * @param RuleGroup $moveTo - * - * @return boolean - */ - public function destroyRuleGroup(RuleGroup $ruleGroup, RuleGroup $moveTo = null) - { - /** @var Rule $rule */ - foreach ($ruleGroup->rules as $rule) { - - 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; - } - - /** - * @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; - } /** * @param Rule $rule @@ -215,76 +136,6 @@ class RuleRepository implements RuleRepositoryInterface $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++; - } - - } - - /** - * @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(); - } - - /** - * @return Collection - */ - public function getRuleGroups() - { - return Auth::user()->ruleGroups()->orderBy('order', 'ASC')->get(); - } /** * @param array $data @@ -375,7 +226,6 @@ class RuleRepository implements RuleRepositoryInterface return true; } - /** * @param RuleGroup $ruleGroup * @@ -386,6 +236,7 @@ class RuleRepository implements RuleRepositoryInterface return intval($ruleGroup->rules()->max('order')); } + /** * @param Rule $rule * @param string $action diff --git a/app/Repositories/Rule/RuleRepositoryInterface.php b/app/Repositories/Rule/RuleRepositoryInterface.php index cab6741a28..80a3e832b9 100644 --- a/app/Repositories/Rule/RuleRepositoryInterface.php +++ b/app/Repositories/Rule/RuleRepositoryInterface.php @@ -22,36 +22,6 @@ use Illuminate\Support\Collection; */ interface RuleRepositoryInterface { - /** - * @param array $data - * - * @return RuleGroup - */ - public function storeRuleGroup(array $data); - - /** - * @return int - */ - public function getHighestOrderRuleGroup(); - - - /** - * @param RuleGroup $ruleGroup - * @param array $data - * - * @return RuleGroup - */ - public function updateRuleGroup(RuleGroup $ruleGroup, array $data); - - - /** - * @param RuleGroup $ruleGroup - * @param RuleGroup $moveTo - * - * @return bool - */ - public function destroyRuleGroup(RuleGroup $ruleGroup, RuleGroup $moveTo = null); - /** * @param Rule $rule * @@ -59,6 +29,13 @@ interface RuleRepositoryInterface */ public function destroyRule(Rule $rule); + /** + * @param RuleGroup $ruleGroup + * + * @return int + */ + public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup); + /** * @param Rule $rule * @param array $ids @@ -74,11 +51,7 @@ interface RuleRepositoryInterface public function reorderRuleActions(Rule $rule, array $ids); /** - * @return bool - */ - public function resetRuleGroupOrder(); - - /** + * @param RuleGroup $ruleGroup * @return bool */ public function resetRulesInGroupOrder(RuleGroup $ruleGroup); @@ -102,23 +75,6 @@ interface RuleRepositoryInterface */ public function moveRuleDown(Rule $rule); - /** - * @param RuleGroup $ruleGroup - * @return bool - */ - public function moveRuleGroupUp(RuleGroup $ruleGroup); - - /** - * @param RuleGroup $ruleGroup - * @return bool - */ - public function moveRuleGroupDown(RuleGroup $ruleGroup); - - /** - * @return Collection - */ - public function getRuleGroups(); - /** * @param array $data * @@ -126,14 +82,6 @@ interface RuleRepositoryInterface */ public function storeRule(array $data); - /** - * @param RuleGroup $ruleGroup - * - * @return int - */ - public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup); - - /** * @param Rule $rule * @param string $action diff --git a/app/Repositories/RuleGroup/RuleGroupRepository.php b/app/Repositories/RuleGroup/RuleGroupRepository.php index b8bdc5bc71..9c5b3167b5 100644 --- a/app/Repositories/RuleGroup/RuleGroupRepository.php +++ b/app/Repositories/RuleGroup/RuleGroupRepository.php @@ -3,7 +3,193 @@ namespace FireflyIII\Repositories\RuleGroup; -class RuleGroupRepository +use Auth; +use FireflyIII\Models\Rule; +use FireflyIII\Models\RuleGroup; +use Illuminate\Support\Collection; + +class RuleGroupRepository implements RuleGroupRepositoryInterface { + /** + * @param RuleGroup $ruleGroup + * @param RuleGroup $moveTo + * + * @return boolean + */ + public function destroyRuleGroup(RuleGroup $ruleGroup, RuleGroup $moveTo = null) + { + /** @var Rule $rule */ + foreach ($ruleGroup->rules as $rule) { + + 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; + } + + + /** + * @return int + */ + public function getHighestOrderRuleGroup() + { + $entry = Auth::user()->ruleGroups()->max('order'); + + return intval($entry); + } + + /** + * @return Collection + */ + public function getRuleGroups() + { + return Auth::user()->ruleGroups()->orderBy('order', 'ASC')->get(); + } + + + /** + * @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(); + } + + + /** + * @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; + } + + /** + * @param RuleGroup $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++; + } + + return true; + + } + + /** + * @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(); + + return $newRuleGroup; + } + + /** + * @param RuleGroup $ruleGroup + * @param array $data + * + * @return RuleGroup + */ + public function updateRuleGroup(RuleGroup $ruleGroup, array $data) + { + // update the account: + $ruleGroup->title = $data['title']; + $ruleGroup->description = $data['description']; + $ruleGroup->active = $data['active']; + $ruleGroup->save(); + $this->resetRuleGroupOrder(); + + return $ruleGroup; + } } \ No newline at end of file diff --git a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php index d69658cc9b..213ab70766 100644 --- a/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php +++ b/app/Repositories/RuleGroup/RuleGroupRepositoryInterface.php @@ -3,7 +3,68 @@ namespace FireflyIII\Repositories\RuleGroup; -class RuleGroupRepositoryInterface +use FireflyIII\Models\RuleGroup; +use Illuminate\Support\Collection; + +interface RuleGroupRepositoryInterface { + /** + * @param RuleGroup $ruleGroup + * @param RuleGroup $moveTo + * + * @return bool + */ + public function destroyRuleGroup(RuleGroup $ruleGroup, RuleGroup $moveTo = null); + + + + /** + * @return int + */ + public function getHighestOrderRuleGroup(); + + /** + * @return Collection + */ + public function getRuleGroups(); + + /** + * @param RuleGroup $ruleGroup + * @return bool + */ + public function moveRuleGroupUp(RuleGroup $ruleGroup); + + /** + * @param RuleGroup $ruleGroup + * @return bool + */ + public function moveRuleGroupDown(RuleGroup $ruleGroup); + + /** + * @return bool + */ + public function resetRuleGroupOrder(); + + /** + * @return bool + */ + public function resetRulesInGroupOrder(RuleGroup $ruleGroup); + + /** + * @param array $data + * + * @return RuleGroup + */ + public function storeRuleGroup(array $data); + + + /** + * @param RuleGroup $ruleGroup + * @param array $data + * + * @return RuleGroup + */ + public function updateRuleGroup(RuleGroup $ruleGroup, array $data); + } \ No newline at end of file