Initial commit
This commit is contained in:
85
app/Models/DocumentCategory.php
Normal file
85
app/Models/DocumentCategory.php
Normal file
@@ -0,0 +1,85 @@
|
||||
<?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 => '未知',
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user