vernova/app/Models/Document.php
2026-07-26 19:58:27 +03:30

83 lines
2.4 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class Document extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'tenant_id',
'project_id',
'title',
'description',
'file_path',
'file_type',
'uploaded_by',
];
public function tenant()
{
return $this->belongsTo(Tenant::class);
}
public function project()
{
return $this->belongsTo(Project::class);
}
public function uploader()
{
return $this->belongsTo(User::class, 'uploaded_by');
}
public function scopeSearch($query, $term)
{
return $query->where(function ($q) use ($term) {
$q->where('title', 'like', "%{$term}%")
->orWhere('description', 'like', "%{$term}%");
});
}
public function getFileSizeAttribute(): ?int
{
$path = storage_path('app/public/' . $this->file_path);
if (file_exists($path)) {
return filesize($path);
}
// Try the local disk
$path = storage_path('app/' . $this->file_path);
if (file_exists($path)) {
return filesize($path);
}
return null;
}
public function getMimeTypeAttribute(): ?string
{
$path = storage_path('app/public/' . $this->file_path);
if (file_exists($path)) {
return mime_content_type($path);
}
$path = storage_path('app/' . $this->file_path);
if (file_exists($path)) {
return mime_content_type($path);
}
return null;
}
public function getFileIconAttribute(): string
{
return match($this->file_type) {
'pdf' => 'M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z',
'image' => 'M4 16l4.586-4.586a2 2 0 012.828 0L16 16m-2-2l1.586-1.586a2 2 0 012.828 0L20 14m-6-6h.01M6 20h12a2 2 0 002-2V6a2 2 0 00-2-2H6a2 2 0 00-2 2v12a2 2 0 002 2z',
'word', 'excel', 'text' => 'M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z',
default => 'M7 21h10a2 2 0 002-2V9.414a1 1 0 00-.293-.707l-5.414-5.414A1 1 0 0012.586 3H7a2 2 0 00-2 2v14a2 2 0 002 2z',
};
}
}