Files
usher-manage-stack/app/Models/DocumentCategory.php
2025-11-20 23:21:05 +08:00

86 lines
1.9 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class DocumentCategory extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'description',
'icon',
'sort_order',
'default_access_level',
];
protected static function boot()
{
parent::boot();
// Auto-generate slug from name if not provided
static::creating(function ($category) {
if (empty($category->slug)) {
$category->slug = Str::slug($category->name);
}
});
}
// ==================== Relationships ====================
/**
* Get all documents in this category
*/
public function documents()
{
return $this->hasMany(Document::class);
}
/**
* Get active (non-archived) documents in this category
*/
public function activeDocuments()
{
return $this->hasMany(Document::class)->where('status', 'active');
}
// ==================== Accessors ====================
/**
* Get the count of active documents in this category
*/
public function getDocumentCountAttribute(): int
{
return $this->activeDocuments()->count();
}
// ==================== Helper Methods ====================
/**
* Get the icon with fallback
*/
public function getIconDisplay(): string
{
return $this->icon ?? '📄';
}
/**
* Get the access level label
*/
public function getAccessLevelLabel(): string
{
return match($this->default_access_level) {
'public' => '公開',
'members' => '會員',
'admin' => '管理員',
'board' => '理事會',
default => '未知',
};
}
}