firefly-iii/app/Repositories/RuleGroup/RuleGroupRepository.php

315 lines
8.2 KiB
PHP
Raw Normal View History

2016-01-15 04:16:41 -06:00
<?php
/**
* RuleGroupRepository.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
2016-01-15 04:16:41 -06:00
namespace FireflyIII\Repositories\RuleGroup;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
2016-03-03 02:05:09 -06:00
/**
2017-11-15 05:25:49 -06:00
* Class RuleGroupRepository.
*/
class RuleGroupRepository implements RuleGroupRepositoryInterface
2016-01-15 04:16:41 -06:00
{
/** @var User */
private $user;
/**
* @return int
*/
2016-04-06 02:27:45 -05:00
public function count(): int
{
return $this->user->ruleGroups()->count();
}
/**
2017-01-14 12:43:33 -06:00
* @param RuleGroup $ruleGroup
* @param RuleGroup|null $moveTo
*
2016-04-06 02:27:45 -05:00
* @return bool
2018-04-27 23:23:13 -05:00
* @throws \Exception
*/
2017-07-23 12:06:24 -05:00
public function destroy(RuleGroup $ruleGroup, ?RuleGroup $moveTo): bool
{
/** @var Rule $rule */
foreach ($ruleGroup->rules as $rule) {
2017-11-15 05:25:49 -06:00
if (null === $moveTo) {
$rule->delete();
continue;
}
// move
$rule->ruleGroup()->associate($moveTo);
$rule->save();
}
$ruleGroup->delete();
$this->resetRuleGroupOrder();
2017-11-15 05:25:49 -06:00
if (null !== $moveTo) {
$this->resetRulesInGroupOrder($moveTo);
}
return true;
}
2016-10-23 05:10:22 -05:00
/**
* @param int $ruleGroupId
*
* @return RuleGroup
*/
public function find(int $ruleGroupId): RuleGroup
{
$group = $this->user->ruleGroups()->find($ruleGroupId);
2017-11-15 05:25:49 -06:00
if (null === $group) {
2016-10-23 05:10:22 -05:00
return new RuleGroup;
}
return $group;
}
2016-01-19 06:59:54 -06:00
/**
* @return Collection
*/
2016-04-06 02:27:45 -05:00
public function get(): Collection
2016-01-19 06:59:54 -06:00
{
return $this->user->ruleGroups()->orderBy('order', 'ASC')->get();
2016-01-19 06:59:54 -06:00
}
/**
* @param User $user
*
* @return Collection
*/
public function getActiveGroups(User $user): Collection
{
return $user->ruleGroups()->where('rule_groups.active', 1)->orderBy('order', 'ASC')->get(['rule_groups.*']);
}
/**
* @param RuleGroup $group
*
* @return Collection
*/
public function getActiveStoreRules(RuleGroup $group): Collection
{
return $group->rules()
->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rule_triggers.trigger_type', 'user_action')
->where('rule_triggers.trigger_value', 'store-journal')
->where('rules.active', 1)
->get(['rules.*']);
}
/**
* @param RuleGroup $group
*
* @return Collection
*/
public function getActiveUpdateRules(RuleGroup $group): Collection
{
return $group->rules()
->leftJoin('rule_triggers', 'rules.id', '=', 'rule_triggers.rule_id')
->where('rule_triggers.trigger_type', 'user_action')
->where('rule_triggers.trigger_value', 'update-journal')
->where('rules.active', 1)
->get(['rules.*']);
}
/**
* @return int
*/
2016-04-06 02:27:45 -05:00
public function getHighestOrderRuleGroup(): int
{
$entry = $this->user->ruleGroups()->max('order');
2018-04-02 08:10:40 -05:00
return (int)$entry;
}
/**
* @param User $user
*
* @return Collection
*/
public function getRuleGroupsWithRules(User $user): Collection
{
return $user->ruleGroups()
->orderBy('active', 'DESC')
->orderBy('order', 'ASC')
->with(
[
'rules' => function (HasMany $query) {
$query->orderBy('active', 'DESC');
$query->orderBy('order', 'ASC');
},
'rules.ruleTriggers' => function (HasMany $query) {
$query->orderBy('order', 'ASC');
},
'rules.ruleActions' => function (HasMany $query) {
$query->orderBy('order', 'ASC');
},
]
)->get();
}
/**
* @param RuleGroup $ruleGroup
*
* @return bool
*/
2016-04-06 02:27:45 -05:00
public function moveDown(RuleGroup $ruleGroup): bool
{
$order = $ruleGroup->order;
2016-01-19 06:59:54 -06:00
// find the rule with order+1 and give it order-1
2018-03-29 12:01:47 -05:00
$other = $this->user->ruleGroups()->where('order', $order + 1)->first();
if ($other) {
2018-03-29 12:01:47 -05:00
--$other->order;
$other->save();
}
2018-03-29 12:01:47 -05:00
++$ruleGroup->order;
$ruleGroup->save();
$this->resetRuleGroupOrder();
2016-04-25 11:43:09 -05:00
2016-04-06 02:27:45 -05:00
return true;
}
/**
* @param RuleGroup $ruleGroup
*
* @return bool
*/
2016-04-06 02:27:45 -05:00
public function moveUp(RuleGroup $ruleGroup): bool
{
$order = $ruleGroup->order;
2016-01-19 06:59:54 -06:00
// find the rule with order-1 and give it order+1
2018-03-29 12:01:47 -05:00
$other = $this->user->ruleGroups()->where('order', $order - 1)->first();
if ($other) {
2018-03-29 12:01:47 -05:00
++$other->order;
$other->save();
}
2018-03-29 12:01:47 -05:00
--$ruleGroup->order;
$ruleGroup->save();
$this->resetRuleGroupOrder();
2016-04-25 11:43:09 -05:00
2016-04-06 14:05:43 -05:00
return true;
}
/**
* @return bool
*/
2016-04-06 02:27:45 -05:00
public function resetRuleGroupOrder(): bool
{
$this->user->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]);
$set = $this->user->ruleGroups()->where('active', 1)->orderBy('order', 'ASC')->get();
$count = 1;
/** @var RuleGroup $entry */
foreach ($set as $entry) {
$entry->order = $count;
$entry->save();
2017-11-15 05:25:49 -06:00
++$count;
}
return true;
}
/**
* @param RuleGroup $ruleGroup
2016-01-15 06:13:21 -06:00
*
* @return bool
*/
2016-04-06 02:27:45 -05:00
public function resetRulesInGroupOrder(RuleGroup $ruleGroup): bool
{
$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();
2017-11-15 05:25:49 -06:00
++$count;
}
return true;
}
2017-02-17 13:14:22 -06:00
/**
* @param User $user
*/
public function setUser(User $user)
{
$this->user = $user;
}
/**
* @param array $data
*
* @return RuleGroup
*/
2016-04-06 02:27:45 -05:00
public function store(array $data): RuleGroup
{
$order = $this->getHighestOrderRuleGroup();
$newRuleGroup = new RuleGroup(
[
2016-10-23 05:19:32 -05:00
'user_id' => $this->user->id,
'title' => $data['title'],
'description' => $data['description'],
2018-03-29 12:01:47 -05:00
'order' => $order + 1,
'active' => 1,
]
);
$newRuleGroup->save();
$this->resetRuleGroupOrder();
return $newRuleGroup;
}
/**
* @param RuleGroup $ruleGroup
* @param array $data
*
* @return RuleGroup
*/
2016-04-06 02:27:45 -05:00
public function update(RuleGroup $ruleGroup, array $data): RuleGroup
{
// update the account:
$ruleGroup->title = $data['title'];
$ruleGroup->description = $data['description'];
$ruleGroup->active = $data['active'];
$ruleGroup->save();
$this->resetRuleGroupOrder();
return $ruleGroup;
}
}