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

363 lines
9.2 KiB
PHP
Raw Normal View History

2016-01-15 04:16:41 -06:00
<?php
/**
* RuleGroupRepository.php
2020-02-16 07:00:57 -06:00
* Copyright (c) 2019 james@firefly-iii.org
*
* This file is part of Firefly III (https://github.com/firefly-iii).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2017-10-21 01:40:00 -05:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 01:40:00 -05:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2017-10-21 01:40:00 -05:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://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;
use Log;
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();
}
/**
2020-05-19 04:18:58 -05:00
* @param RuleGroup $ruleGroup
2017-01-14 12:43:33 -06:00
* @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;
}
2019-05-29 11:28:28 -05:00
/**
* @return bool
*/
public function resetRuleGroupOrder(): bool
{
$this->user->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]);
2020-05-19 12:37:12 -05:00
$set = $this->user
->ruleGroups()
->orderBy('order', 'ASC')->get();
2019-05-29 11:28:28 -05:00
$count = 1;
/** @var RuleGroup $entry */
foreach ($set as $entry) {
$entry->order = $count;
$entry->save();
2020-05-19 04:18:58 -05:00
// also update rules in group.
$this->resetRulesInGroupOrder($entry);
2019-05-29 11:28:28 -05:00
++$count;
}
return true;
}
/**
* @param RuleGroup $ruleGroup
*
* @return bool
*/
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();
++$count;
}
return true;
}
2016-10-23 05:10:22 -05:00
/**
* @param int $ruleGroupId
*
2018-07-22 14:09:57 -05:00
* @return RuleGroup|null
2016-10-23 05:10:22 -05:00
*/
2018-07-22 14:09:57 -05:00
public function find(int $ruleGroupId): ?RuleGroup
2016-10-23 05:10:22 -05:00
{
$group = $this->user->ruleGroups()->find($ruleGroupId);
2017-11-15 05:25:49 -06:00
if (null === $group) {
2018-07-22 14:09:57 -05:00
return null;
2016-10-23 05:10:22 -05:00
}
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
}
/**
* @return Collection
*/
2019-05-29 11:28:28 -05:00
public function getActiveGroups(): Collection
{
2019-06-07 10:57:46 -05:00
return $this->user->ruleGroups()->with(['rules'])->where('rule_groups.active', 1)->orderBy('order', 'ASC')->get(['rule_groups.*']);
}
2018-12-07 08:36:04 -06:00
/**
* @param RuleGroup $group
*
* @return Collection
*/
public function getActiveRules(RuleGroup $group): Collection
{
return $group->rules()
->where('rules.active', 1)
->get(['rules.*']);
}
/**
* @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 Collection
*/
2020-12-18 12:16:56 -06:00
public function getRuleGroupsWithRules(): Collection
{
2020-12-18 12:16:56 -06:00
return $this->user->ruleGroups()
->orderBy('order', 'ASC')
2020-12-18 12:16:56 -06:00
->where('active', true)
->with(
[
2020-05-19 12:37:12 -05:00
'rules' => static function (HasMany $query) {
$query->orderBy('order', 'ASC');
},
2020-05-19 12:37:12 -05:00
'rules.ruleTriggers' => static function (HasMany $query) {
$query->orderBy('order', 'ASC');
},
2020-05-19 12:37:12 -05:00
'rules.ruleActions' => static function (HasMany $query) {
$query->orderBy('order', 'ASC');
},
]
)->get();
}
2018-12-07 08:36:04 -06:00
/**
* @param RuleGroup $group
*
* @return Collection
*/
public function getRules(RuleGroup $group): Collection
{
return $group->rules()
->get(['rules.*']);
}
/**
* @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;
}
2017-02-17 13:14:22 -06:00
/**
* @param User $user
*/
2018-07-22 11:50:27 -05:00
public function setUser(User $user): void
2017-02-17 13:14:22 -06:00
{
$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,
2018-12-07 00:49:16 -06:00
'active' => $data['active'],
]
);
$newRuleGroup->save();
$this->resetRuleGroupOrder();
return $newRuleGroup;
}
2019-05-29 11:28:28 -05:00
/**
* @return int
*/
public function getHighestOrderRuleGroup(): int
{
$entry = $this->user->ruleGroups()->max('order');
return (int)$entry;
}
/**
* @param RuleGroup $ruleGroup
2020-05-19 04:18:58 -05:00
* @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;
}
2019-06-12 23:39:05 -05:00
/**
* @param string $title
*
* @return RuleGroup|null
*/
public function findByTitle(string $title): ?RuleGroup
{
return $this->user->ruleGroups()->where('title', $title)->first();
}
2020-05-19 04:18:58 -05:00
2020-07-11 08:13:15 -05:00
/**
* @inheritDoc
*/
public function destroyAll(): void
{
$groups = $this->get();
/** @var RuleGroup $group */
foreach ($groups as $group) {
$group->rules()->delete();
$group->delete();
}
}
2020-05-19 12:37:12 -05:00
}