2026-05-26 08:14:29 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Models;
|
|
|
|
|
|
2026-06-09 11:22:56 +02:00
|
|
|
use App\Traits\HasTagsLight;
|
2026-05-26 08:14:29 +02:00
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
|
|
|
|
|
|
|
|
|
class Documento extends Model
|
|
|
|
|
{
|
2026-06-09 11:22:56 +02:00
|
|
|
use HasTagsLight;
|
2026-05-26 08:14:29 +02:00
|
|
|
protected $table = 'documenti';
|
|
|
|
|
protected $fillable = [
|
2026-05-28 09:34:28 +02:00
|
|
|
'tenant_id', 'user_id', 'cartella_id', 'repository_id', 'storage_disk',
|
|
|
|
|
'nome_file', 'file_path', 'tipo', 'tipologia',
|
2026-05-26 08:14:29 +02:00
|
|
|
'visibilita', 'visibilita_target_id', 'visibilita_target_type',
|
|
|
|
|
'mime_type', 'dimensione', 'note'
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
public function tenant(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(Tenant::class);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 09:34:28 +02:00
|
|
|
public function cartella(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(DocumentoCartella::class, 'cartella_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function repository(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(StorageRepository::class, 'repository_id');
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 08:14:29 +02:00
|
|
|
public function user(): BelongsTo
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo(User::class);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function target(): MorphTo
|
|
|
|
|
{
|
|
|
|
|
return $this->morphTo('target', 'visibilita_target_type', 'visibilita_target_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function eventi(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Evento::class, 'eventi_documenti')->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function gruppi(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Gruppo::class, 'gruppi_documenti')->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function individui(): BelongsToMany
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsToMany(Individuo::class, 'individui_documenti')->withTimestamps();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function isVisibilePerIndividuo(Individuo $individuo): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->visibilita === 'pubblico') return true;
|
|
|
|
|
|
|
|
|
|
if ($this->visibilita === 'individuo' && $this->visibilita_target_id === $individuo->id) return true;
|
|
|
|
|
|
|
|
|
|
if ($this->visibilita === 'gruppo' && $this->visibilita_target_type === Gruppo::class) {
|
|
|
|
|
return $individuo->gruppi()->where('gruppi.id', $this->visibilita_target_id)->exists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|