mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-18 07:38:29 +00:00
Code cleanup.
This commit is contained in:
@@ -23,7 +23,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\RuleGroup;
|
||||
|
||||
use Exception;
|
||||
use FireflyIII\Models\Rule;
|
||||
use FireflyIII\Models\RuleAction;
|
||||
use FireflyIII\Models\RuleGroup;
|
||||
@@ -40,9 +39,6 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
{
|
||||
private User $user;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function correctRuleGroupOrder(): void
|
||||
{
|
||||
$set = $this->user
|
||||
@@ -50,40 +46,32 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('active', 'DESC')
|
||||
->orderBy('title', 'ASC')
|
||||
->get(['rule_groups.id']);
|
||||
->get(['rule_groups.id'])
|
||||
;
|
||||
$index = 1;
|
||||
|
||||
/** @var RuleGroup $ruleGroup */
|
||||
foreach ($set as $ruleGroup) {
|
||||
if ($ruleGroup->order !== $index) {
|
||||
$ruleGroup->order = $index;
|
||||
$ruleGroup->save();
|
||||
}
|
||||
$index++;
|
||||
++$index;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function get(): Collection
|
||||
{
|
||||
return $this->user->ruleGroups()->orderBy('order', 'ASC')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function count(): int
|
||||
{
|
||||
return $this->user->ruleGroups()->count();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RuleGroup $ruleGroup
|
||||
* @param RuleGroup|null $moveTo
|
||||
*
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function destroy(RuleGroup $ruleGroup, ?RuleGroup $moveTo): bool
|
||||
{
|
||||
@@ -91,6 +79,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
foreach ($ruleGroup->rules as $rule) {
|
||||
if (null === $moveTo) {
|
||||
$rule->delete();
|
||||
|
||||
continue;
|
||||
}
|
||||
// move
|
||||
@@ -108,9 +97,6 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function resetOrder(): bool
|
||||
{
|
||||
$set = $this->user
|
||||
@@ -118,8 +104,10 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
->whereNull('deleted_at')
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('title', 'DESC')
|
||||
->get();
|
||||
->get()
|
||||
;
|
||||
$count = 1;
|
||||
|
||||
/** @var RuleGroup $entry */
|
||||
foreach ($set as $entry) {
|
||||
if ($entry->order !== $count) {
|
||||
@@ -136,19 +124,16 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RuleGroup $ruleGroup
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function resetRuleOrder(RuleGroup $ruleGroup): bool
|
||||
{
|
||||
$set = $ruleGroup->rules()
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('title', 'DESC')
|
||||
->orderBy('updated_at', 'DESC')
|
||||
->get(['rules.*']);
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('title', 'DESC')
|
||||
->orderBy('updated_at', 'DESC')
|
||||
->get(['rules.*'])
|
||||
;
|
||||
$count = 1;
|
||||
|
||||
/** @var Rule $entry */
|
||||
foreach ($set as $entry) {
|
||||
if ($entry->order !== $count) {
|
||||
@@ -165,57 +150,10 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
*/
|
||||
private function resetRuleActionOrder(Rule $rule): void
|
||||
{
|
||||
$actions = $rule->ruleActions()
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('active', 'DESC')
|
||||
->orderBy('action_type', 'ASC')
|
||||
->get();
|
||||
$index = 1;
|
||||
/** @var RuleAction $action */
|
||||
foreach ($actions as $action) {
|
||||
if ($action->order !== $index) {
|
||||
$action->order = $index;
|
||||
$action->save();
|
||||
app('log')->debug(sprintf('Rule action #%d was on spot %d but must be on spot %d', $action->id, $action->order, $index));
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Rule $rule
|
||||
*/
|
||||
private function resetRuleTriggerOrder(Rule $rule): void
|
||||
{
|
||||
$triggers = $rule->ruleTriggers()
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('active', 'DESC')
|
||||
->orderBy('trigger_type', 'ASC')
|
||||
->get();
|
||||
$index = 1;
|
||||
/** @var RuleTrigger $trigger */
|
||||
foreach ($triggers as $trigger) {
|
||||
$order = $trigger->order;
|
||||
if ($order !== $index) {
|
||||
$trigger->order = $index;
|
||||
$trigger->save();
|
||||
app('log')->debug(sprintf('Rule trigger #%d was on spot %d but must be on spot %d', $trigger->id, $order, $index));
|
||||
}
|
||||
$index++;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function destroyAll(): void
|
||||
{
|
||||
$groups = $this->get();
|
||||
|
||||
/** @var RuleGroup $group */
|
||||
foreach ($groups as $group) {
|
||||
$group->rules()->delete();
|
||||
@@ -223,98 +161,69 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $ruleGroupId
|
||||
*
|
||||
* @return RuleGroup|null
|
||||
*/
|
||||
public function find(int $ruleGroupId): ?RuleGroup
|
||||
{
|
||||
return $this->user->ruleGroups()->find($ruleGroupId);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $title
|
||||
*
|
||||
* @return RuleGroup|null
|
||||
*/
|
||||
public function findByTitle(string $title): ?RuleGroup
|
||||
{
|
||||
return $this->user->ruleGroups()->where('title', $title)->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveGroups(): Collection
|
||||
{
|
||||
return $this->user->ruleGroups()->with(['rules'])->where('rule_groups.active', true)->orderBy('order', 'ASC')->get(['rule_groups.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RuleGroup $group
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getActiveRules(RuleGroup $group): Collection
|
||||
{
|
||||
return $group->rules()
|
||||
->where('rules.active', true)
|
||||
->get(['rules.*']);
|
||||
->where('rules.active', true)
|
||||
->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', true)
|
||||
->get(['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', true)
|
||||
->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', true)
|
||||
->get(['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', true)
|
||||
->get(['rules.*'])
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $filter
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getAllRuleGroupsWithRules(?string $filter): Collection
|
||||
{
|
||||
$groups = $this->user->ruleGroups()
|
||||
->orderBy('order', 'ASC')
|
||||
->with(
|
||||
[
|
||||
'rules' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleTriggers' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleActions' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
]
|
||||
)->get();
|
||||
->orderBy('order', 'ASC')
|
||||
->with(
|
||||
[
|
||||
'rules' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleTriggers' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleActions' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
]
|
||||
)->get()
|
||||
;
|
||||
if (null === $filter) {
|
||||
return $groups;
|
||||
}
|
||||
@@ -345,9 +254,6 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function getHighestOrderRuleGroup(): int
|
||||
{
|
||||
$entry = $this->user->ruleGroups()->max('order');
|
||||
@@ -355,29 +261,25 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
return (int)$entry;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|null $filter
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getRuleGroupsWithRules(?string $filter): Collection
|
||||
{
|
||||
$groups = $this->user->ruleGroups()
|
||||
->orderBy('order', 'ASC')
|
||||
->where('active', true)
|
||||
->with(
|
||||
[
|
||||
'rules' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleTriggers' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleActions' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
]
|
||||
)->get();
|
||||
->orderBy('order', 'ASC')
|
||||
->where('active', true)
|
||||
->with(
|
||||
[
|
||||
'rules' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleTriggers' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
'rules.ruleActions' => static function (HasMany $query) {
|
||||
$query->orderBy('order', 'ASC');
|
||||
},
|
||||
]
|
||||
)->get()
|
||||
;
|
||||
if (null === $filter) {
|
||||
return $groups;
|
||||
}
|
||||
@@ -408,28 +310,18 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RuleGroup $group
|
||||
*
|
||||
* @return Collection
|
||||
*/
|
||||
public function getRules(RuleGroup $group): Collection
|
||||
{
|
||||
return $group->rules()
|
||||
->get(['rules.*']);
|
||||
->get(['rules.*'])
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function maxOrder(): int
|
||||
{
|
||||
return (int)$this->user->ruleGroups()->where('active', true)->max('order');
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function searchRuleGroup(string $query, int $limit): Collection
|
||||
{
|
||||
$search = $this->user->ruleGroups();
|
||||
@@ -437,26 +329,19 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
$search->where('rule_groups.title', 'LIKE', sprintf('%%%s%%', $query));
|
||||
}
|
||||
$search->orderBy('rule_groups.order', 'ASC')
|
||||
->orderBy('rule_groups.title', 'ASC');
|
||||
->orderBy('rule_groups.title', 'ASC')
|
||||
;
|
||||
|
||||
return $search->take($limit)->get(['id', 'title', 'description']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param User|Authenticatable|null $user
|
||||
*/
|
||||
public function setUser(User | Authenticatable | null $user): void
|
||||
public function setUser(null|Authenticatable|User $user): void
|
||||
{
|
||||
if ($user instanceof User) {
|
||||
$this->user = $user;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
*
|
||||
* @return RuleGroup
|
||||
*/
|
||||
public function store(array $data): RuleGroup
|
||||
{
|
||||
$newRuleGroup = new RuleGroup(
|
||||
@@ -478,17 +363,15 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
return $newRuleGroup;
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function setOrder(RuleGroup $ruleGroup, int $newOrder): void
|
||||
{
|
||||
$oldOrder = $ruleGroup->order;
|
||||
|
||||
if ($newOrder > $oldOrder) {
|
||||
$this->user->ruleGroups()->where('rule_groups.order', '<=', $newOrder)->where('rule_groups.order', '>', $oldOrder)
|
||||
->where('rule_groups.id', '!=', $ruleGroup->id)
|
||||
->decrement('order');
|
||||
->where('rule_groups.id', '!=', $ruleGroup->id)
|
||||
->decrement('order')
|
||||
;
|
||||
$ruleGroup->order = $newOrder;
|
||||
app('log')->debug(sprintf('Order of group #%d ("%s") is now %d', $ruleGroup->id, $ruleGroup->title, $newOrder));
|
||||
$ruleGroup->save();
|
||||
@@ -497,19 +380,14 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
}
|
||||
|
||||
$this->user->ruleGroups()->where('rule_groups.order', '>=', $newOrder)->where('rule_groups.order', '<', $oldOrder)
|
||||
->where('rule_groups.id', '!=', $ruleGroup->id)
|
||||
->increment('order');
|
||||
->where('rule_groups.id', '!=', $ruleGroup->id)
|
||||
->increment('order')
|
||||
;
|
||||
$ruleGroup->order = $newOrder;
|
||||
app('log')->debug(sprintf('Order of group #%d ("%s") is now %d', $ruleGroup->id, $ruleGroup->title, $newOrder));
|
||||
$ruleGroup->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param RuleGroup $ruleGroup
|
||||
* @param array $data
|
||||
*
|
||||
* @return RuleGroup
|
||||
*/
|
||||
public function update(RuleGroup $ruleGroup, array $data): RuleGroup
|
||||
{
|
||||
// update the account:
|
||||
@@ -532,4 +410,47 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
|
||||
|
||||
return $ruleGroup;
|
||||
}
|
||||
|
||||
private function resetRuleActionOrder(Rule $rule): void
|
||||
{
|
||||
$actions = $rule->ruleActions()
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('active', 'DESC')
|
||||
->orderBy('action_type', 'ASC')
|
||||
->get()
|
||||
;
|
||||
$index = 1;
|
||||
|
||||
/** @var RuleAction $action */
|
||||
foreach ($actions as $action) {
|
||||
if ($action->order !== $index) {
|
||||
$action->order = $index;
|
||||
$action->save();
|
||||
app('log')->debug(sprintf('Rule action #%d was on spot %d but must be on spot %d', $action->id, $action->order, $index));
|
||||
}
|
||||
++$index;
|
||||
}
|
||||
}
|
||||
|
||||
private function resetRuleTriggerOrder(Rule $rule): void
|
||||
{
|
||||
$triggers = $rule->ruleTriggers()
|
||||
->orderBy('order', 'ASC')
|
||||
->orderBy('active', 'DESC')
|
||||
->orderBy('trigger_type', 'ASC')
|
||||
->get()
|
||||
;
|
||||
$index = 1;
|
||||
|
||||
/** @var RuleTrigger $trigger */
|
||||
foreach ($triggers as $trigger) {
|
||||
$order = $trigger->order;
|
||||
if ($order !== $index) {
|
||||
$trigger->order = $index;
|
||||
$trigger->save();
|
||||
app('log')->debug(sprintf('Rule trigger #%d was on spot %d but must be on spot %d', $trigger->id, $order, $index));
|
||||
}
|
||||
++$index;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user