Various code reshuffelling.

This commit is contained in:
James Cole
2021-03-12 06:20:01 +01:00
parent 97561ab9c9
commit 748d61fb8f
51 changed files with 1874 additions and 1873 deletions

View File

@@ -22,13 +22,14 @@ declare(strict_types=1);
namespace FireflyIII\Repositories\RuleGroup;
use DB;
use Exception;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
use FireflyIII\User;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
use Log;
use DB;
/**
* Class RuleGroupRepository.
@@ -37,6 +38,28 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
{
private User $user;
/**
* @inheritDoc
*/
public function correctRuleGroupOrder(): void
{
$set = $this->user
->ruleGroups()
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('title', 'ASC')
->get(['rule_groups.id']);
$index = 1;
/** @var RuleGroup $ruleGroup */
foreach ($set as $ruleGroup) {
if ($ruleGroup->order !== $index) {
$ruleGroup->order = $index;
$ruleGroup->save();
}
$index++;
}
}
/**
* @return int
*/
@@ -50,7 +73,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
* @param RuleGroup|null $moveTo
*
* @return bool
* @throws \Exception
* @throws Exception
*/
public function destroy(RuleGroup $ruleGroup, ?RuleGroup $moveTo): bool
{
@@ -76,52 +99,16 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
}
/**
* @return bool
* @inheritDoc
*/
public function resetRuleGroupOrder(): bool
public function destroyAll(): void
{
$this->user->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]);
$set = $this->user
->ruleGroups()
->orderBy('order', 'ASC')->get();
$count = 1;
/** @var RuleGroup $entry */
foreach ($set as $entry) {
$entry->order = $count;
$entry->save();
// also update rules in group.
$this->resetRulesInGroupOrder($entry);
++$count;
$groups = $this->get();
/** @var RuleGroup $group */
foreach ($groups as $group) {
$group->rules()->delete();
$group->delete();
}
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;
}
/**
@@ -139,6 +126,16 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
return $group;
}
/**
* @param string $title
*
* @return RuleGroup|null
*/
public function findByTitle(string $title): ?RuleGroup
{
return $this->user->ruleGroups()->where('title', $title)->first();
}
/**
* @return Collection
*/
@@ -197,6 +194,16 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
->get(['rules.*']);
}
/**
* @return int
*/
public function getHighestOrderRuleGroup(): int
{
$entry = $this->user->ruleGroups()->max('order');
return (int)$entry;
}
/**
* @param string|null $filter
*
@@ -261,6 +268,14 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
->get(['rules.*']);
}
/**
* @inheritDoc
*/
public function maxOrder(): int
{
return (int)$this->user->ruleGroups()->max('order');
}
/**
* @param RuleGroup $ruleGroup
*
@@ -307,6 +322,70 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
return true;
}
/**
* @return bool
*/
public function resetRuleGroupOrder(): bool
{
$this->user->ruleGroups()->whereNotNull('deleted_at')->update(['order' => 0]);
$set = $this->user
->ruleGroups()
->orderBy('order', 'ASC')->get();
$count = 1;
/** @var RuleGroup $entry */
foreach ($set as $entry) {
$entry->order = $count;
$entry->save();
// also update rules in group.
$this->resetRulesInGroupOrder($entry);
++$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;
}
/**
* @inheritDoc
*/
public function searchRuleGroup(string $query, int $limit): Collection
{
$search = $this->user->ruleGroups();
if ('' !== $query) {
$search->where('rule_groups.title', 'LIKE', sprintf('%%%s%%', $query));
}
$search->orderBy('rule_groups.order', 'ASC')
->orderBy('rule_groups.title', 'ASC');
return $search->take($limit)->get(['id', 'title', 'description']);
}
/**
* @param User $user
*/
@@ -339,16 +418,6 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
return $newRuleGroup;
}
/**
* @return int
*/
public function getHighestOrderRuleGroup(): int
{
$entry = $this->user->ruleGroups()->max('order');
return (int)$entry;
}
/**
* @param RuleGroup $ruleGroup
* @param array $data
@@ -373,7 +442,7 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
$max = $this->maxOrder();
// TODO also for bills and accounts:
$data['order'] = $data['order'] > $max ? $max : $data['order'];
$ruleGroup = $this->updateOrder($ruleGroup, $ruleGroup->order, $data['order']);
$ruleGroup = $this->updateOrder($ruleGroup, $ruleGroup->order, $data['order']);
}
$ruleGroup->save();
@@ -381,66 +450,6 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
return $ruleGroup;
}
/**
* @param string $title
*
* @return RuleGroup|null
*/
public function findByTitle(string $title): ?RuleGroup
{
return $this->user->ruleGroups()->where('title', $title)->first();
}
/**
* @inheritDoc
*/
public function destroyAll(): void
{
$groups = $this->get();
/** @var RuleGroup $group */
foreach ($groups as $group) {
$group->rules()->delete();
$group->delete();
}
}
/**
* @inheritDoc
*/
public function searchRuleGroup(string $query, int $limit): Collection
{
$search = $this->user->ruleGroups();
if ('' !== $query) {
$search->where('rule_groups.title', 'LIKE', sprintf('%%%s%%', $query));
}
$search->orderBy('rule_groups.order', 'ASC')
->orderBy('rule_groups.title', 'ASC');
return $search->take($limit)->get(['id', 'title', 'description']);
}
/**
* @inheritDoc
*/
public function correctRuleGroupOrder(): void
{
$set = $this->user
->ruleGroups()
->orderBy('order', 'ASC')
->orderBy('active', 'DESC')
->orderBy('title', 'ASC')
->get(['rule_groups.id']);
$index = 1;
/** @var RuleGroup $ruleGroup */
foreach ($set as $ruleGroup) {
if ($ruleGroup->order !== $index) {
$ruleGroup->order = $index;
$ruleGroup->save();
}
$index++;
}
}
/**
* @inheritDoc
*/
@@ -463,12 +472,4 @@ class RuleGroupRepository implements RuleGroupRepositoryInterface
return $ruleGroup;
}
/**
* @inheritDoc
*/
public function maxOrder(): int
{
return (int)$this->user->ruleGroups()->max('order');
}
}