firefly-iii/app/Repositories/ObjectGroup/ObjectGroupRepository.php

180 lines
5.3 KiB
PHP
Raw Normal View History

2020-06-07 04:31:01 -05:00
<?php
2020-06-30 12:05:35 -05:00
/**
* ObjectGroupRepository.php
* Copyright (c) 2020 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.
*
* This program 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 Affero General Public License for more details.
*
* 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/>.
*/
2020-06-07 04:31:01 -05:00
declare(strict_types=1);
namespace FireflyIII\Repositories\ObjectGroup;
use FireflyIII\Models\ObjectGroup;
2020-06-20 11:57:20 -05:00
use FireflyIII\Models\PiggyBank;
2020-06-20 03:22:07 -05:00
use FireflyIII\User;
2023-02-19 01:43:28 -06:00
use Illuminate\Contracts\Auth\Authenticatable;
2020-06-07 04:31:01 -05:00
use Illuminate\Support\Collection;
/**
* Class ObjectGroupRepository
*/
class ObjectGroupRepository implements ObjectGroupRepositoryInterface
{
2021-03-13 23:20:23 -06:00
private User $user;
2020-06-20 03:22:07 -05:00
2021-03-11 23:20:01 -06:00
public function deleteAll(): void
{
$all = $this->get();
2023-12-20 12:35:52 -06:00
2021-03-11 23:20:01 -06:00
/** @var ObjectGroup $group */
foreach ($all as $group) {
$group->piggyBanks()->sync([]);
$group->bills()->sync([]);
$group->delete();
}
}
2023-06-21 05:34:58 -05:00
public function get(): Collection
{
return $this->user->objectGroups()
2023-12-20 12:35:52 -06:00
->with(['piggyBanks', 'bills'])
->orderBy('order', 'ASC')
->orderBy('title', 'ASC')->get()
;
2023-06-21 05:34:58 -05:00
}
2021-03-11 23:20:01 -06:00
public function deleteEmpty(): void
{
$all = $this->get();
2023-12-20 12:35:52 -06:00
2021-03-11 23:20:01 -06:00
/** @var ObjectGroup $group */
foreach ($all as $group) {
2023-12-20 12:35:52 -06:00
$count = \DB::table('object_groupables')->where('object_groupables.object_group_id', $group->id)->count();
2021-03-11 23:20:01 -06:00
if (0 === $count) {
$group->delete();
}
}
}
public function destroy(ObjectGroup $objectGroup): void
{
$list = $objectGroup->piggyBanks;
2023-12-20 12:35:52 -06:00
2021-03-11 23:20:01 -06:00
/** @var PiggyBank $piggy */
foreach ($list as $piggy) {
$piggy->objectGroups()->sync([]);
$piggy->save();
}
$objectGroup->delete();
}
public function getBills(ObjectGroup $objectGroup): Collection
{
return $objectGroup->bills;
}
public function getPiggyBanks(ObjectGroup $objectGroup): Collection
{
return $objectGroup->piggyBanks;
}
2021-03-21 03:15:40 -05:00
public function resetOrder(): void
{
2023-10-29 00:33:43 -05:00
app('log')->debug('Now in resetOrder');
2021-03-21 03:15:40 -05:00
$list = $this->get();
$index = 1;
2023-12-20 12:35:52 -06:00
2021-03-21 03:15:40 -05:00
/** @var ObjectGroup $objectGroup */
foreach ($list as $objectGroup) {
2023-11-05 12:55:39 -06:00
if ($index !== $objectGroup->order) {
2023-10-29 00:33:43 -05:00
app('log')->debug(
2021-03-21 03:15:40 -05:00
sprintf('objectGroup #%d ("%s"): order should %d be but is %d.', $objectGroup->id, $objectGroup->title, $index, $objectGroup->order)
);
$objectGroup->order = $index;
$objectGroup->save();
}
2023-12-20 12:35:52 -06:00
++$index;
2021-03-21 03:15:40 -05:00
}
}
2020-07-20 23:30:37 -05:00
public function search(string $query, int $limit): Collection
2020-06-07 04:31:01 -05:00
{
2020-06-20 03:22:07 -05:00
$dbQuery = $this->user->objectGroups()->orderBy('order', 'ASC')->orderBy('title', 'ASC');
2020-06-07 04:31:01 -05:00
if ('' !== $query) {
// split query on spaces just in case:
$parts = explode(' ', $query);
foreach ($parts as $part) {
$search = sprintf('%%%s%%', $part);
$dbQuery->where('title', 'LIKE', $search);
}
}
2020-07-20 23:30:37 -05:00
return $dbQuery->take($limit)->get(['object_groups.*']);
2020-06-07 04:31:01 -05:00
}
2020-06-07 09:38:15 -05:00
2023-12-20 12:35:52 -06:00
public function setUser(null|Authenticatable|User $user): void
2023-06-21 05:34:58 -05:00
{
2023-10-30 13:49:40 -05:00
if ($user instanceof User) {
2023-06-21 05:34:58 -05:00
$this->user = $user;
}
}
public function update(ObjectGroup $objectGroup, array $data): ObjectGroup
{
if (array_key_exists('title', $data)) {
$objectGroup->title = $data['title'];
}
if (array_key_exists('order', $data)) {
$this->setOrder($objectGroup, (int)$data['order']);
}
$objectGroup->save();
return $objectGroup;
}
2021-03-13 23:20:23 -06:00
public function setOrder(ObjectGroup $objectGroup, int $newOrder): ObjectGroup
2020-06-07 09:38:15 -05:00
{
2023-11-05 12:55:39 -06:00
$oldOrder = $objectGroup->order;
2021-03-11 23:20:01 -06:00
2021-03-13 23:20:23 -06:00
if ($newOrder > $oldOrder) {
$this->user->objectGroups()->where('object_groups.order', '<=', $newOrder)->where('object_groups.order', '>', $oldOrder)
2023-12-20 12:35:52 -06:00
->where('object_groups.id', '!=', $objectGroup->id)
->decrement('object_groups.order')
;
2021-03-11 23:20:01 -06:00
2021-03-13 23:20:23 -06:00
$objectGroup->order = $newOrder;
$objectGroup->save();
}
if ($newOrder < $oldOrder) {
$this->user->objectGroups()->where('object_groups.order', '>=', $newOrder)->where('object_groups.order', '<', $oldOrder)
2023-12-20 12:35:52 -06:00
->where('object_groups.id', '!=', $objectGroup->id)
->increment('object_groups.order')
;
2020-06-07 09:38:15 -05:00
2021-03-13 23:20:23 -06:00
$objectGroup->order = $newOrder;
$objectGroup->save();
2020-06-07 09:38:15 -05:00
}
2021-03-13 23:20:23 -06:00
2023-10-29 00:33:43 -05:00
app('log')->debug(sprintf('Objectgroup #%d order is now %d', $objectGroup->id, $newOrder));
2021-03-13 23:20:23 -06:00
return $objectGroup;
2020-06-07 09:38:15 -05:00
}
2020-06-07 04:31:01 -05:00
}