mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-11-16 14:48:11 +00:00
Organise object groups
This commit is contained in:
@@ -3,6 +3,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace FireflyIII\Repositories\ObjectGroup;
|
||||
|
||||
use DB;
|
||||
use FireflyIII\Models\ObjectGroup;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
@@ -18,7 +19,7 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
|
||||
*/
|
||||
public function get(): Collection
|
||||
{
|
||||
return ObjectGroup::orderBy('order')->get();
|
||||
return ObjectGroup::orderBy('order', 'ASC')->orderBy('title', 'ASC')->get();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -28,7 +29,7 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
|
||||
*/
|
||||
public function search(string $query): Collection
|
||||
{
|
||||
$dbQuery = ObjectGroup::orderBy('order');
|
||||
$dbQuery = ObjectGroup::orderBy('order', 'ASC')->orderBy('title', 'ASC');
|
||||
if ('' !== $query) {
|
||||
// split query on spaces just in case:
|
||||
$parts = explode(' ', $query);
|
||||
@@ -41,4 +42,35 @@ class ObjectGroupRepository implements ObjectGroupRepositoryInterface
|
||||
|
||||
return $dbQuery->get(['object_groups.*']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function deleteEmpty(): void
|
||||
{
|
||||
$all = $this->get();
|
||||
/** @var ObjectGroup $group */
|
||||
foreach ($all as $group) {
|
||||
$count = DB::table('object_groupables')->where('object_groupables.object_group_id', $group->id)->count();
|
||||
if (0 === $count) {
|
||||
$group->delete();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function sort(): void
|
||||
{
|
||||
$all = $this->get();
|
||||
/**
|
||||
* @var int $index
|
||||
* @var ObjectGroup $group
|
||||
*/
|
||||
foreach ($all as $index => $group) {
|
||||
$group->order = $index + 1;
|
||||
$group->save();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user