137 lines
3.6 KiB
PHP
137 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\HasTagsLight;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
class Gruppo extends Model
|
|
{
|
|
use HasTagsLight;
|
|
protected $table = 'gruppi';
|
|
protected $fillable = [
|
|
'tenant_id', 'parent_id', 'diocesi_id', 'responsabile_ids',
|
|
'nome', 'descrizione', 'indirizzo_incontro', 'cap_incontro',
|
|
'città_incontro', 'sigla_provincia_incontro', 'mappa_posizione', 'metadata'
|
|
];
|
|
|
|
protected $casts = [
|
|
'metadata' => 'array',
|
|
'responsabile_ids' => 'array',
|
|
];
|
|
|
|
public function tenant(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Tenant::class);
|
|
}
|
|
|
|
public function parent(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Gruppo::class, 'parent_id');
|
|
}
|
|
|
|
public function children(): HasMany
|
|
{
|
|
return $this->hasMany(Gruppo::class, 'parent_id');
|
|
}
|
|
|
|
public function diocesi(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Diocesi::class);
|
|
}
|
|
|
|
public function individui(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Individuo::class, 'gruppo_individuo')
|
|
->withPivot('ruolo_ids', 'ruolo_nel_gruppo', 'data_adesione')
|
|
->withTimestamps();
|
|
}
|
|
|
|
public function avatar(): HasOne
|
|
{
|
|
return $this->hasOne(Documento::class, 'visibilita_target_id')
|
|
->where('tipologia', 'avatar')
|
|
->where('visibilita_target_type', self::class);
|
|
}
|
|
|
|
public function getAvatarUrlAttribute(): ?string
|
|
{
|
|
if ($this->avatar && $this->avatar->file_path) {
|
|
return url('/gruppi/' . $this->id . '/avatar-file');
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public function getResponsabili()
|
|
{
|
|
if (empty($this->responsabile_ids)) {
|
|
return collect();
|
|
}
|
|
|
|
$ids = is_array($this->responsabile_ids)
|
|
? $this->responsabile_ids
|
|
: json_decode($this->responsabile_ids, true);
|
|
|
|
if (empty($ids)) {
|
|
return collect();
|
|
}
|
|
|
|
return Individuo::whereIn('id', $ids)->get();
|
|
}
|
|
|
|
public function getResponsabiliIds(): array
|
|
{
|
|
if (empty($this->responsabile_ids)) {
|
|
return [];
|
|
}
|
|
|
|
return is_array($this->responsabile_ids)
|
|
? $this->responsabile_ids
|
|
: json_decode($this->responsabile_ids, true) ?? [];
|
|
}
|
|
|
|
public function eventi(): BelongsToMany
|
|
{
|
|
return $this->belongsToMany(Evento::class, 'eventi_gruppi')->withTimestamps();
|
|
}
|
|
|
|
public function documenti(): HasMany
|
|
{
|
|
return $this->hasMany(Documento::class, 'visibilita_target_id')
|
|
->where('visibilita_target_type', self::class);
|
|
}
|
|
|
|
public function getAncestors(): array
|
|
{
|
|
$ancestors = [];
|
|
$current = $this->parent;
|
|
while ($current) {
|
|
$ancestors[] = $current;
|
|
$current = $current->parent;
|
|
}
|
|
return $ancestors;
|
|
}
|
|
|
|
public function getDescendants(): array
|
|
{
|
|
$descendants = [];
|
|
foreach ($this->children as $child) {
|
|
$descendants[] = $child;
|
|
$descendants = array_merge($descendants, $child->getDescendants());
|
|
}
|
|
return $descendants;
|
|
}
|
|
|
|
public function getFullPathAttribute(): string
|
|
{
|
|
$path = [$this->nome];
|
|
foreach (array_reverse($this->getAncestors()) as $ancestor) {
|
|
$path[] = $ancestor->nome;
|
|
}
|
|
return implode(' > ', array_reverse($path));
|
|
}
|
|
} |