File manager - Edit - /var/www/html/portal/app/Models/Legislacao/Legislacao.php
Back
<?php namespace App\Models\Legislacao; use App\Models\Comunicacao\Boletim; use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Storage; class Legislacao extends Model { /** URL pública legada servida via symlink em public/downloads/legislacoes-pdf. */ public const PDF_PUBLIC_URL_SEGMENT = 'downloads/legislacoes-pdf'; /** Pasta relativa no disco public (storage/app/public/legislacoes-pdf). */ public const PDF_STORAGE_FOLDER = 'legislacoes-pdf'; protected $table = 'legislacao'; protected $primaryKey = 'id_legislacao'; public $incrementing = true; protected $keyType = 'int'; protected $fillable = [ 'nr_legislacao', 'id_tipo_legislacao', 'id_autor', 'id_autor_sancao', 'ds_ementa', 'id_boletim_oficial', 'dt_assinatura', 'dt_publicacao', 'ds_legislacao_sem_html', 'ds_legislacao_original', 'ds_legislacao_compilada', 'nm_arquivo_anexo', 'cd_status', 'id_usuario_cadastro', 'dt_cadastro', 'id_usuario_alteracao', 'dt_alteracao', ]; protected $casts = [ 'dt_assinatura' => 'date', 'dt_publicacao' => 'date', 'dt_cadastro' => 'datetime', 'dt_alteracao' => 'datetime', ]; /** * Normaliza UUID (evita falha de join com CHAR(36) preenchido com espaços no MySQL). */ protected function idBoletimOficial(): Attribute { return Attribute::make( get: function (?string $value) { if ($value === null || $value === '') { return null; } return trim($value); }, set: function ($value) { if ($value === null || $value === '' || $value === '0') { return null; } return trim((string) $value); } ); } public function getRouteKeyName(): string { return 'id_legislacao'; } public function tipo() { return $this->belongsTo(LegislacaoTipo::class, 'id_tipo_legislacao', 'id_tipo_legislacao'); } public function autor() { return $this->belongsTo(LegislacaoAutor::class, 'id_autor', 'id_autor'); } public function autorSancao() { return $this->belongsTo(LegislacaoAutor::class, 'id_autor_sancao', 'id_autor'); } public function categorias() { return $this->belongsToMany( LegislacaoCategoria::class, 'legislacao_categoria_associada', 'id_legislacao', 'id_categoria', 'id_legislacao', 'id_categoria' ); } public function boletim() { return $this->belongsTo(Boletim::class, 'id_boletim_oficial', 'id'); } public function acoes() { return $this->hasMany(LegislacaoAcao::class, 'id_legislacao', 'id_legislacao'); } public function retificacoes() { return $this->hasMany(LegislacaoRetificacao::class, 'id_legislacao', 'id_legislacao') ->orderByDesc('id_retificacao'); } /** * Diretório físico gravável no storage (storage/app/public/legislacoes-pdf). */ public static function legislacaoPdfStorageDirectory(): string { return storage_path('app/public/'.self::PDF_STORAGE_FOLDER); } /** * Caminho relativo no disco public (ex.: legislacoes-pdf/arquivo.pdf). */ public static function legislacaoPdfDiskPath(?string $storedName): ?string { $base = self::legislacaoPdfSafeFileName($storedName); if ($base === null) { return null; } return self::PDF_STORAGE_FOLDER.'/'.$base; } /** * Nome seguro do arquivo (basename) a partir do valor persistido. */ public static function legislacaoPdfSafeFileName(?string $storedName): ?string { if ($storedName === null || $storedName === '') { return null; } $base = basename($storedName); if ($base === '' || $base === '.' || $base === '..') { return null; } return $base; } /** * Indica se o PDF existe no storage ou no diretório legado físico (pré-symlink). */ public static function legislacaoPdfExists(?string $storedName): bool { $diskPath = self::legislacaoPdfDiskPath($storedName); if ($diskPath !== null && Storage::disk('public')->exists($diskPath)) { return true; } $legacyPath = self::legislacaoPdfLegacyAbsolutePath($storedName); return $legacyPath !== null && is_file($legacyPath); } /** * Caminho absoluto legado em public/downloads/legislacoes-pdf (somente diretório físico, não symlink). */ public static function legislacaoPdfLegacyAbsolutePath(?string $storedName): ?string { $base = self::legislacaoPdfSafeFileName($storedName); if ($base === null) { return null; } $publicDir = public_path(str_replace('/', DIRECTORY_SEPARATOR, self::PDF_PUBLIC_URL_SEGMENT)); if (is_link($publicDir)) { return null; } return $publicDir.DIRECTORY_SEPARATOR.$base; } /** * URL pública do PDF em /downloads/legislacoes-pdf/ (symlink para storage). */ public function getPublicPdfUrlAttribute(): ?string { $base = self::legislacaoPdfSafeFileName($this->nm_arquivo_anexo); if ($base === null) { return null; } return url('/'.self::PDF_PUBLIC_URL_SEGMENT.'/'.rawurlencode($base)); } }
| ver. 1.4 |
Github
|
.
| PHP 8.3.28 | Generation time: 0.1 |
proxy
|
phpinfo
|
Settings