Support oft parent groups
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use App\Scopes\CommonModel;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
/**
|
||||
* Übergeordnete Gruppe (Parent Group, im UI z. B. „Bund"). Mehrere Mandanten (Local Groups) gehören zur selben
|
||||
* Parent Group. Nicht mandantengebunden -- die Liste gilt app-weit.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property boolean $is_fallback
|
||||
*/
|
||||
class ParentGroup extends CommonModel
|
||||
{
|
||||
protected $table = 'parent_groups';
|
||||
|
||||
protected $fillable = [
|
||||
'name',
|
||||
'is_fallback',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'is_fallback' => 'boolean',
|
||||
];
|
||||
|
||||
/** Die Gruppe „Ohne Zuordnung" -- Vorgabe für Mandanten, denen keine Gruppe zugewiesen wurde. */
|
||||
public static function fallback() : self {
|
||||
return self::where('is_fallback', true)->orderBy('id')->firstOrFail();
|
||||
}
|
||||
|
||||
/** Anzeigereihenfolge: alphabetisch, „Ohne Zuordnung" (Fallback) immer zuletzt. */
|
||||
public function scopeOrdered(Builder $query) : Builder {
|
||||
return $query->orderBy('is_fallback')->orderBy('name');
|
||||
}
|
||||
|
||||
/**
|
||||
* Dieselbe Reihenfolge wie {@see scopeOrdered()} für bereits geladene Gruppen.
|
||||
*
|
||||
* @param Collection<int, self> $parentGroups
|
||||
* @return Collection<int, self>
|
||||
*/
|
||||
public static function sortForDisplay(Collection $parentGroups) : Collection {
|
||||
return $parentGroups
|
||||
->sort(static fn (self $a, self $b): int =>
|
||||
[$a->is_fallback, mb_strtolower($a->name)] <=> [$b->is_fallback, mb_strtolower($b->name)])
|
||||
->values();
|
||||
}
|
||||
|
||||
public function tenants() : HasMany {
|
||||
return $this->hasMany(Tenant::class, 'parent_group_id');
|
||||
}
|
||||
|
||||
public function eventParticipants() : HasMany {
|
||||
return $this->hasMany(EventParticipant::class, 'parent_group_id');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user