Add headless CMS for official site content management

Integrate article and page management into the Laravel admin dashboard
to serve as a headless CMS for the Next.js frontend (usher-site).

Backend:
- 7 migrations: article_categories, article_tags, articles, pivots, attachments, pages
- 5 models with relationships: Article, ArticleCategory, ArticleTag, ArticleAttachment, Page
- 4 admin controllers: articles (with publish/archive/pin), categories, tags, pages
- Admin views with EasyMDE markdown editor, multi-select categories/tags
- Navigation section "官網管理" in admin sidebar

API (v1):
- GET /api/v1/articles (filtered by type, category, tag, search; paginated)
- GET /api/v1/articles/{slug} (with related articles)
- GET /api/v1/categories
- GET /api/v1/pages/{slug} (with children)
- GET /api/v1/homepage (aggregated homepage data)
- Attachment download endpoint
- CORS configured for usher.org.tw, vercel.app, localhost:3000

Content migration:
- ImportHugoContent command: imports Hugo markdown files as articles/pages
- Successfully imported 27 articles, 17 categories, 11 tags, 9 pages

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-02-07 11:58:22 +08:00
parent bfbec861d0
commit a30af8eaf7
45 changed files with 4816 additions and 31 deletions

View File

@@ -0,0 +1,527 @@
<?php
namespace App\Console\Commands;
use App\Models\Article;
use App\Models\ArticleCategory;
use App\Models\ArticleTag;
use App\Models\Page;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use Symfony\Component\Yaml\Yaml;
class ImportHugoContent extends Command
{
protected $signature = 'hugo:import
{--path= : Path to Hugo content/zh directory}
{--images= : Path to Hugo static/images directory}
{--dry-run : Preview import without writing to database}';
protected $description = 'Import Hugo site content (markdown files) into CMS articles and pages';
private int $articlesCreated = 0;
private int $pagesCreated = 0;
private int $categoriesCreated = 0;
private int $tagsCreated = 0;
private int $imagescopied = 0;
private int $skipped = 0;
public function handle(): int
{
$contentPath = $this->option('path') ?: base_path('../usher-site/content/zh');
$imagesPath = $this->option('images') ?: base_path('../usher-site/static/images');
// Expand tilde
$contentPath = str_replace('~', getenv('HOME'), $contentPath);
$imagesPath = str_replace('~', getenv('HOME'), $imagesPath);
if (! is_dir($contentPath)) {
$this->error("Content directory not found: {$contentPath}");
return self::FAILURE;
}
$isDryRun = $this->option('dry-run');
if ($isDryRun) {
$this->warn('DRY RUN — no changes will be written to database.');
}
$this->info("Importing Hugo content from: {$contentPath}");
$this->newLine();
// Get or create a system user for attribution
$systemUser = User::where('email', 'admin@test.com')->first()
?? User::first();
if (! $systemUser) {
$this->error('No user found to attribute content to. Please create a user first.');
return self::FAILURE;
}
$this->info("Attributing content to: {$systemUser->name} ({$systemUser->email})");
$this->newLine();
DB::beginTransaction();
try {
// Import articles by content type
$contentTypes = [
'blog' => Article::CONTENT_TYPE_BLOG,
'notice' => Article::CONTENT_TYPE_NOTICE,
'document' => Article::CONTENT_TYPE_DOCUMENT,
'related_news' => Article::CONTENT_TYPE_RELATED_NEWS,
];
foreach ($contentTypes as $dir => $contentType) {
$dirPath = "{$contentPath}/{$dir}";
if (is_dir($dirPath)) {
$this->importArticles($dirPath, $contentType, $systemUser, $isDryRun);
} else {
$this->warn("Directory not found, skipping: {$dir}/");
}
}
// Import static pages
$pageDirectories = [
'about' => '關於我們',
'message' => '理事長的話',
'structure' => '組織架構',
'mission' => '協會任務',
'contact' => '聯繫我們',
'logo_represent' => 'Logo象徵',
];
foreach ($pageDirectories as $dir => $fallbackTitle) {
$dirPath = "{$contentPath}/{$dir}";
if (is_dir($dirPath)) {
$this->importPage($dirPath, $dir, $fallbackTitle, $systemUser, $isDryRun);
} else {
$this->warn("Page directory not found, skipping: {$dir}/");
}
}
// Copy images
if (is_dir($imagesPath)) {
$this->copyImages($imagesPath, $isDryRun);
} else {
$this->warn("Images directory not found: {$imagesPath}");
}
if ($isDryRun) {
DB::rollBack();
$this->newLine();
$this->warn('DRY RUN complete — no changes were made.');
} else {
DB::commit();
}
} catch (\Exception $e) {
DB::rollBack();
$this->error("Import failed: {$e->getMessage()}");
$this->error($e->getTraceAsString());
return self::FAILURE;
}
$this->newLine();
$this->info('=== Import Summary ===');
$this->table(
['Item', 'Count'],
[
['Articles created', $this->articlesCreated],
['Pages created', $this->pagesCreated],
['Categories created', $this->categoriesCreated],
['Tags created', $this->tagsCreated],
['Images copied', $this->imagescopied],
['Skipped (_index.md etc.)', $this->skipped],
]
);
return self::SUCCESS;
}
private function importArticles(string $dirPath, string $contentType, User $user, bool $isDryRun): void
{
$this->info("Importing {$contentType} articles from: {$dirPath}");
$files = File::glob("{$dirPath}/*.md");
foreach ($files as $filePath) {
$filename = basename($filePath);
// Skip _index.md files (Hugo section pages, not content)
if ($filename === '_index.md') {
$this->skipped++;
continue;
}
$parsed = $this->parseMarkdownFile($filePath);
if (! $parsed) {
$this->warn(" Could not parse: {$filename}");
$this->skipped++;
continue;
}
$frontmatter = $parsed['frontmatter'];
$body = $parsed['body'];
// Skip drafts
if (! empty($frontmatter['draft']) && $frontmatter['draft'] === true) {
$this->line(" Skipping draft: {$filename}");
$this->skipped++;
continue;
}
$title = $frontmatter['title'] ?? pathinfo($filename, PATHINFO_FILENAME);
// Prefer filename-based slug (Hugo filenames are usually descriptive ASCII)
$filenameBase = pathinfo($filename, PATHINFO_FILENAME);
$slug = Str::slug($filenameBase);
if (empty($slug)) {
$slug = Str::slug($title);
}
if (empty($slug)) {
// For Chinese-only titles/filenames, use content_type + short hash
$slug = $contentType.'-'.substr(md5($title), 0, 8);
}
// Ensure unique slug
$originalSlug = $slug;
$counter = 1;
while (! $isDryRun && Article::withTrashed()->where('slug', $slug)->exists()) {
$slug = "{$originalSlug}-{$counter}";
$counter++;
}
$publishedAt = $this->parseDate($frontmatter['date'] ?? null);
$description = $frontmatter['description'] ?? null;
$summary = $frontmatter['summary'] ?? null;
$author = $frontmatter['author'] ?? null;
$image = $this->resolveImagePath($frontmatter['image'] ?? null);
$this->line(" [{$contentType}] {$title}{$slug}");
if (! $isDryRun) {
$article = Article::create([
'title' => $title,
'slug' => $slug,
'summary' => $summary,
'content' => $body,
'content_type' => $contentType,
'status' => Article::STATUS_PUBLISHED,
'access_level' => Article::ACCESS_LEVEL_PUBLIC,
'featured_image_path' => $image,
'author_name' => $author,
'meta_description' => $description ?: null,
'published_at' => $publishedAt ?? now(),
'created_by_user_id' => $user->id,
'view_count' => 0,
]);
// Sync categories
$categoryNames = $this->extractList($frontmatter, 'categories', 'category');
foreach ($categoryNames as $catName) {
$category = $this->findOrCreateCategory($catName);
$article->categories()->attach($category->id);
}
// Sync tags
$tagNames = $this->extractList($frontmatter, 'tags');
foreach ($tagNames as $tagName) {
$tag = $this->findOrCreateTag($tagName);
$article->tags()->attach($tag->id);
}
}
$this->articlesCreated++;
}
}
private function importPage(string $dirPath, string $slug, string $fallbackTitle, User $user, bool $isDryRun): void
{
$indexFile = "{$dirPath}/_index.md";
if (! file_exists($indexFile)) {
$this->warn(" No _index.md in {$slug}/, skipping.");
$this->skipped++;
return;
}
$parsed = $this->parseMarkdownFile($indexFile);
if (! $parsed) {
$this->warn(" Could not parse: {$slug}/_index.md");
$this->skipped++;
return;
}
$frontmatter = $parsed['frontmatter'];
$body = $parsed['body'];
$title = $frontmatter['title'] ?? $fallbackTitle;
$description = $frontmatter['description'] ?? null;
$this->info(" [page] {$title}{$slug}");
$parentPage = null;
if (! $isDryRun) {
// Check for existing page with same slug
if (Page::withTrashed()->where('slug', $slug)->exists()) {
$this->warn(" Page with slug '{$slug}' already exists, skipping.");
$this->skipped++;
return;
}
$parentPage = Page::create([
'title' => $title,
'slug' => $slug,
'content' => $body,
'status' => Page::STATUS_PUBLISHED,
'meta_description' => $description ?: null,
'published_at' => now(),
'created_by_user_id' => $user->id,
'sort_order' => 0,
]);
}
$this->pagesCreated++;
// Import child pages (non-_index.md files in the same directory)
$childFiles = File::glob("{$dirPath}/*.md");
$sortOrder = 1;
foreach ($childFiles as $childFile) {
$childFilename = basename($childFile);
if ($childFilename === '_index.md') {
continue;
}
$childParsed = $this->parseMarkdownFile($childFile);
if (! $childParsed) {
$this->skipped++;
continue;
}
$childFm = $childParsed['frontmatter'];
$childBody = $childParsed['body'];
$childTitle = $childFm['title'] ?? pathinfo($childFilename, PATHINFO_FILENAME);
$childSlug = Str::slug(pathinfo($childFilename, PATHINFO_FILENAME));
if (empty($childSlug)) {
$childSlug = Str::slug($childTitle);
}
if (empty($childSlug)) {
$childSlug = 'page-'.md5($childTitle);
}
$this->line(" [child] {$childTitle}{$childSlug}");
if (! $isDryRun) {
$originalChildSlug = $childSlug;
$counter = 1;
while (Page::withTrashed()->where('slug', $childSlug)->exists()) {
$childSlug = "{$originalChildSlug}-{$counter}";
$counter++;
}
Page::create([
'title' => $childTitle,
'slug' => $childSlug,
'content' => $childBody,
'status' => Page::STATUS_PUBLISHED,
'meta_description' => $childFm['description'] ?? null,
'parent_id' => $parentPage->id,
'sort_order' => $sortOrder,
'published_at' => now(),
'created_by_user_id' => $user->id,
]);
}
$this->pagesCreated++;
$sortOrder++;
}
}
private function copyImages(string $imagesPath, bool $isDryRun): void
{
$this->info("Copying images from: {$imagesPath}");
$destPath = storage_path('app/public/migrated-images');
if (! $isDryRun && ! is_dir($destPath)) {
File::makeDirectory($destPath, 0755, true);
}
$files = File::allFiles($imagesPath);
foreach ($files as $file) {
$relativePath = $file->getRelativePathname();
$destFile = "{$destPath}/{$relativePath}";
$this->line(" {$relativePath}");
if (! $isDryRun) {
$destDir = dirname($destFile);
if (! is_dir($destDir)) {
File::makeDirectory($destDir, 0755, true);
}
File::copy($file->getPathname(), $destFile);
}
$this->imagescopied++;
}
}
/**
* Parse a markdown file into frontmatter array and body string.
*/
private function parseMarkdownFile(string $filePath): ?array
{
$content = file_get_contents($filePath);
if ($content === false) {
return null;
}
// Match YAML frontmatter between --- delimiters
if (! preg_match('/\A---\s*\n(.*?)\n---\s*\n?(.*)\z/s', $content, $matches)) {
// No frontmatter — treat entire content as body
return [
'frontmatter' => [],
'body' => trim($content),
];
}
$yamlString = $matches[1];
$body = trim($matches[2]);
try {
// Strip YAML comments (lines starting with #) before parsing
$yamlLines = array_filter(
explode("\n", $yamlString),
fn ($line) => ! preg_match('/^\s*#/', $line)
);
$cleanYaml = implode("\n", $yamlLines);
$frontmatter = Yaml::parse($cleanYaml) ?? [];
} catch (\Exception $e) {
$this->warn(" YAML parse error in {$filePath}: {$e->getMessage()}");
$frontmatter = [];
}
return [
'frontmatter' => $frontmatter,
'body' => $body,
];
}
/**
* Parse a date value from Hugo frontmatter.
* YAML parser may convert ISO 8601 dates to Unix timestamps (int).
*
* @param string|int|null $dateValue
*/
private function parseDate(mixed $dateValue): ?\Carbon\Carbon
{
if ($dateValue === null || $dateValue === '') {
return null;
}
try {
if (is_int($dateValue) || is_float($dateValue)) {
return \Carbon\Carbon::createFromTimestamp((int) $dateValue);
}
return \Carbon\Carbon::parse((string) $dateValue);
} catch (\Exception $e) {
$this->warn(" Could not parse date: {$dateValue}");
return null;
}
}
/**
* Extract a list of values from frontmatter.
* Hugo uses both plural array (categories: ["a", "b"]) and singular string (category: "a").
*/
private function extractList(array $frontmatter, string $pluralKey, ?string $singularKey = null): array
{
$items = [];
if (! empty($frontmatter[$pluralKey])) {
$value = $frontmatter[$pluralKey];
$items = is_array($value) ? $value : [$value];
} elseif ($singularKey && ! empty($frontmatter[$singularKey])) {
$value = $frontmatter[$singularKey];
$items = is_array($value) ? $value : [$value];
}
// Clean up strings
return array_filter(array_map('trim', $items));
}
private function findOrCreateCategory(string $name): ArticleCategory
{
$slug = Str::slug($name) ?: 'category-'.mt_rand(100, 999);
$category = ArticleCategory::where('slug', $slug)->first();
if (! $category) {
$category = ArticleCategory::create([
'name' => $name,
'slug' => $slug,
'sort_order' => 0,
]);
$this->categoriesCreated++;
$this->line(" Created category: {$name}");
}
return $category;
}
private function findOrCreateTag(string $name): ArticleTag
{
$slug = Str::slug($name) ?: 'tag-'.mt_rand(100, 999);
$tag = ArticleTag::where('slug', $slug)->first();
if (! $tag) {
$tag = ArticleTag::create([
'name' => $name,
'slug' => $slug,
]);
$this->tagsCreated++;
$this->line(" Created tag: {$name}");
}
return $tag;
}
/**
* Convert Hugo image paths to migrated storage paths.
* Hugo: "images/blog/Update.jpg" "migrated-images/blog/Update.jpg"
*/
private function resolveImagePath(?string $hugoPath): ?string
{
if (empty($hugoPath)) {
return null;
}
// Remove leading "images/" prefix
$relativePath = preg_replace('#^images/#', '', $hugoPath);
return "migrated-images/{$relativePath}";
}
}

View File

@@ -0,0 +1,112 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\ArticleCategory;
use App\Models\AuditLog;
use Illuminate\Http\Request;
class ArticleCategoryController extends Controller
{
public function __construct()
{
$this->middleware('can:view_articles');
}
public function index()
{
$categories = ArticleCategory::withCount('articles')
->orderBy('sort_order')
->get();
return view('admin.article-categories.index', compact('categories'));
}
public function create()
{
return view('admin.article-categories.create');
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:article_categories,slug',
'description' => 'nullable|string|max:1000',
'sort_order' => 'nullable|integer',
]);
$category = ArticleCategory::create([
'name' => $validated['name'],
'slug' => $validated['slug'] ?? null,
'description' => $validated['description'] ?? null,
'sort_order' => $validated['sort_order'] ?? 0,
]);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article_category.created',
'description' => "建立文章分類:{$category->name}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.article-categories.index')
->with('status', '分類已成功建立');
}
public function edit(ArticleCategory $articleCategory)
{
return view('admin.article-categories.edit', ['category' => $articleCategory]);
}
public function update(Request $request, ArticleCategory $articleCategory)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:article_categories,slug,'.$articleCategory->id,
'description' => 'nullable|string|max:1000',
'sort_order' => 'nullable|integer',
]);
$articleCategory->update([
'name' => $validated['name'],
'slug' => $validated['slug'] ?? $articleCategory->slug,
'description' => $validated['description'] ?? null,
'sort_order' => $validated['sort_order'] ?? 0,
]);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article_category.updated',
'description' => "更新文章分類:{$articleCategory->name}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.article-categories.index')
->with('status', '分類已成功更新');
}
public function destroy(ArticleCategory $articleCategory)
{
if ($articleCategory->articles()->count() > 0) {
return back()->with('error', '此分類下仍有文章,無法刪除');
}
$name = $articleCategory->name;
$articleCategory->delete();
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article_category.deleted',
'description' => "刪除文章分類:{$name}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.article-categories.index')
->with('status', '分類已成功刪除');
}
}

View File

@@ -0,0 +1,449 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Article;
use App\Models\ArticleAttachment;
use App\Models\ArticleCategory;
use App\Models\ArticleTag;
use App\Models\AuditLog;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class ArticleController extends Controller
{
public function __construct()
{
$this->middleware('can:view_articles')->only(['index', 'show']);
$this->middleware('can:create_articles')->only(['create', 'store']);
$this->middleware('can:edit_articles')->only(['edit', 'update']);
$this->middleware('can:delete_articles')->only(['destroy']);
$this->middleware('can:publish_articles')->only(['publish', 'archive']);
}
public function index(Request $request)
{
$query = Article::with(['creator', 'categories'])
->orderByDesc('is_pinned')
->orderByDesc('created_at');
if ($request->filled('status')) {
$query->where('status', $request->status);
}
if ($request->filled('content_type')) {
$query->where('content_type', $request->content_type);
}
if ($request->filled('category')) {
$query->whereHas('categories', function ($q) use ($request) {
$q->where('article_categories.id', $request->category);
});
}
if ($request->filled('search')) {
$search = $request->search;
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('content', 'like', "%{$search}%");
});
}
$articles = $query->paginate(20);
$stats = [
'total' => Article::count(),
'draft' => Article::draft()->count(),
'published' => Article::published()->count(),
'archived' => Article::archived()->count(),
'pinned' => Article::pinned()->count(),
];
$categories = ArticleCategory::orderBy('sort_order')->get();
return view('admin.articles.index', compact('articles', 'stats', 'categories'));
}
public function create()
{
$categories = ArticleCategory::orderBy('sort_order')->get();
$tags = ArticleTag::orderBy('name')->get();
return view('admin.articles.create', compact('categories', 'tags'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:articles,slug',
'summary' => 'nullable|string|max:1000',
'content' => 'required|string',
'content_type' => 'required|in:blog,notice,document,related_news',
'access_level' => 'required|in:public,members,board,admin',
'author_name' => 'nullable|string|max:255',
'meta_description' => 'nullable|string|max:500',
'published_at' => 'nullable|date',
'expires_at' => 'nullable|date|after:published_at',
'is_pinned' => 'boolean',
'display_order' => 'nullable|integer',
'categories' => 'nullable|array',
'categories.*' => 'exists:article_categories,id',
'tags_input' => 'nullable|string',
'featured_image' => 'nullable|image|max:5120',
'featured_image_alt' => 'nullable|string|max:255',
'save_action' => 'required|in:draft,publish',
]);
$articleData = [
'title' => $validated['title'],
'slug' => $validated['slug'] ?? null,
'summary' => $validated['summary'] ?? null,
'content' => $validated['content'],
'content_type' => $validated['content_type'],
'access_level' => $validated['access_level'],
'author_name' => $validated['author_name'] ?? null,
'meta_description' => $validated['meta_description'] ?? null,
'status' => $validated['save_action'] === 'publish' ? Article::STATUS_PUBLISHED : Article::STATUS_DRAFT,
'published_at' => $validated['save_action'] === 'publish' ? ($validated['published_at'] ?? now()) : ($validated['published_at'] ?? null),
'expires_at' => $validated['expires_at'] ?? null,
'is_pinned' => $validated['is_pinned'] ?? false,
'display_order' => $validated['display_order'] ?? 0,
'created_by_user_id' => auth()->id(),
'last_updated_by_user_id' => auth()->id(),
'featured_image_alt' => $validated['featured_image_alt'] ?? null,
];
if ($request->hasFile('featured_image')) {
$articleData['featured_image_path'] = $request->file('featured_image')->store('articles/images', 'public');
}
$article = Article::create($articleData);
// Sync categories
if (! empty($validated['categories'])) {
$article->categories()->sync($validated['categories']);
}
// Handle tags
if (! empty($validated['tags_input'])) {
$tagNames = array_filter(array_map('trim', explode(',', $validated['tags_input'])));
$tagIds = [];
foreach ($tagNames as $tagName) {
$tag = ArticleTag::firstOrCreate(
['name' => $tagName],
['slug' => Str::slug($tagName) ?: 'tag-'.time()]
);
$tagIds[] = $tag->id;
}
$article->tags()->sync($tagIds);
}
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.created',
'description' => "建立文章:{$article->title} (類型:{$article->getContentTypeLabel()},狀態:{$article->getStatusLabel()})",
'ip_address' => request()->ip(),
]);
$message = $validated['save_action'] === 'publish' ? '文章已成功發布' : '文章已儲存為草稿';
return redirect()
->route('admin.articles.show', $article)
->with('status', $message);
}
public function show(Article $article)
{
if (! $article->canBeViewedBy(auth()->user())) {
abort(403, '您沒有權限查看此文章');
}
$article->load(['creator', 'lastUpdatedBy', 'categories', 'tags', 'attachments']);
return view('admin.articles.show', compact('article'));
}
public function edit(Article $article)
{
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限編輯此文章');
}
$article->load(['categories', 'tags', 'attachments']);
$categories = ArticleCategory::orderBy('sort_order')->get();
$tags = ArticleTag::orderBy('name')->get();
return view('admin.articles.edit', compact('article', 'categories', 'tags'));
}
public function update(Request $request, Article $article)
{
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限編輯此文章');
}
$validated = $request->validate([
'title' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:articles,slug,'.$article->id,
'summary' => 'nullable|string|max:1000',
'content' => 'required|string',
'content_type' => 'required|in:blog,notice,document,related_news',
'access_level' => 'required|in:public,members,board,admin',
'author_name' => 'nullable|string|max:255',
'meta_description' => 'nullable|string|max:500',
'published_at' => 'nullable|date',
'expires_at' => 'nullable|date|after:published_at',
'is_pinned' => 'boolean',
'display_order' => 'nullable|integer',
'categories' => 'nullable|array',
'categories.*' => 'exists:article_categories,id',
'tags_input' => 'nullable|string',
'featured_image' => 'nullable|image|max:5120',
'featured_image_alt' => 'nullable|string|max:255',
'remove_featured_image' => 'boolean',
]);
$updateData = [
'title' => $validated['title'],
'summary' => $validated['summary'] ?? null,
'content' => $validated['content'],
'content_type' => $validated['content_type'],
'access_level' => $validated['access_level'],
'author_name' => $validated['author_name'] ?? null,
'meta_description' => $validated['meta_description'] ?? null,
'published_at' => $validated['published_at'],
'expires_at' => $validated['expires_at'] ?? null,
'is_pinned' => $validated['is_pinned'] ?? false,
'display_order' => $validated['display_order'] ?? 0,
'last_updated_by_user_id' => auth()->id(),
'featured_image_alt' => $validated['featured_image_alt'] ?? null,
];
if (! empty($validated['slug']) && $validated['slug'] !== $article->slug) {
$updateData['slug'] = $validated['slug'];
}
if ($request->hasFile('featured_image')) {
if ($article->featured_image_path) {
Storage::disk('public')->delete($article->featured_image_path);
}
$updateData['featured_image_path'] = $request->file('featured_image')->store('articles/images', 'public');
} elseif ($request->boolean('remove_featured_image') && $article->featured_image_path) {
Storage::disk('public')->delete($article->featured_image_path);
$updateData['featured_image_path'] = null;
}
$article->update($updateData);
// Sync categories
$article->categories()->sync($validated['categories'] ?? []);
// Handle tags
if (isset($validated['tags_input'])) {
$tagNames = array_filter(array_map('trim', explode(',', $validated['tags_input'])));
$tagIds = [];
foreach ($tagNames as $tagName) {
$tag = ArticleTag::firstOrCreate(
['name' => $tagName],
['slug' => Str::slug($tagName) ?: 'tag-'.time()]
);
$tagIds[] = $tag->id;
}
$article->tags()->sync($tagIds);
} else {
$article->tags()->sync([]);
}
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.updated',
'description' => "更新文章:{$article->title}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.articles.show', $article)
->with('status', '文章已成功更新');
}
public function destroy(Article $article)
{
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限刪除此文章');
}
$title = $article->title;
$article->delete();
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.deleted',
'description' => "刪除文章:{$title}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.articles.index')
->with('status', '文章已成功刪除');
}
public function publish(Article $article)
{
if (! auth()->user()->can('publish_articles')) {
abort(403, '您沒有權限發布文章');
}
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限發布此文章');
}
if ($article->isPublished()) {
return back()->with('error', '此文章已經發布');
}
$article->publish(auth()->user());
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.published',
'description' => "發布文章:{$article->title}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '文章已成功發布');
}
public function archive(Article $article)
{
if (! auth()->user()->can('publish_articles')) {
abort(403, '您沒有權限歸檔文章');
}
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限歸檔此文章');
}
if ($article->isArchived()) {
return back()->with('error', '此文章已經歸檔');
}
$article->archive(auth()->user());
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.archived',
'description' => "歸檔文章:{$article->title}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '文章已成功歸檔');
}
public function pin(Request $request, Article $article)
{
if (! auth()->user()->can('edit_articles')) {
abort(403, '您沒有權限置頂文章');
}
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限置頂此文章');
}
$validated = $request->validate([
'display_order' => 'nullable|integer',
]);
$article->pin($validated['display_order'] ?? 0, auth()->user());
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.pinned',
'description' => "置頂文章:{$article->title}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '文章已成功置頂');
}
public function unpin(Article $article)
{
if (! auth()->user()->can('edit_articles')) {
abort(403, '您沒有權限取消置頂文章');
}
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限取消置頂此文章');
}
$article->unpin(auth()->user());
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.unpinned',
'description' => "取消置頂文章:{$article->title}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '文章已取消置頂');
}
public function uploadAttachment(Request $request, Article $article)
{
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限上傳附件');
}
$validated = $request->validate([
'attachment' => 'required|file|max:20480',
'description' => 'nullable|string|max:255',
]);
$file = $request->file('attachment');
$path = $file->store('articles/attachments', 'public');
$attachment = $article->attachments()->create([
'file_path' => $path,
'original_filename' => $file->getClientOriginalName(),
'mime_type' => $file->getMimeType(),
'file_size' => $file->getSize(),
'description' => $validated['description'] ?? null,
]);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.attachment_uploaded',
'description' => "上傳附件至文章「{$article->title}」:{$attachment->original_filename}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '附件已成功上傳');
}
public function deleteAttachment(Article $article, ArticleAttachment $attachment)
{
if (! $article->canBeEditedBy(auth()->user())) {
abort(403, '您沒有權限刪除附件');
}
if ($attachment->article_id !== $article->id) {
abort(404);
}
Storage::disk('public')->delete($attachment->file_path);
$filename = $attachment->original_filename;
$attachment->delete();
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article.attachment_deleted',
'description' => "刪除文章「{$article->title}」的附件:{$filename}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '附件已成功刪除');
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\ArticleTag;
use App\Models\AuditLog;
use Illuminate\Http\Request;
class ArticleTagController extends Controller
{
public function __construct()
{
$this->middleware('can:view_articles');
}
public function index()
{
$tags = ArticleTag::withCount('articles')
->orderBy('name')
->get();
return view('admin.article-tags.index', compact('tags'));
}
public function create()
{
return view('admin.article-tags.create');
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:article_tags,slug',
]);
$tag = ArticleTag::create([
'name' => $validated['name'],
'slug' => $validated['slug'] ?? null,
]);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article_tag.created',
'description' => "建立文章標籤:{$tag->name}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.article-tags.index')
->with('status', '標籤已成功建立');
}
public function edit(ArticleTag $articleTag)
{
return view('admin.article-tags.edit', ['tag' => $articleTag]);
}
public function update(Request $request, ArticleTag $articleTag)
{
$validated = $request->validate([
'name' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:article_tags,slug,'.$articleTag->id,
]);
$articleTag->update([
'name' => $validated['name'],
'slug' => $validated['slug'] ?? $articleTag->slug,
]);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article_tag.updated',
'description' => "更新文章標籤:{$articleTag->name}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.article-tags.index')
->with('status', '標籤已成功更新');
}
public function destroy(ArticleTag $articleTag)
{
$name = $articleTag->name;
$articleTag->articles()->detach();
$articleTag->delete();
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'article_tag.deleted',
'description' => "刪除文章標籤:{$name}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.article-tags.index')
->with('status', '標籤已成功刪除');
}
}

View File

@@ -0,0 +1,181 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\AuditLog;
use App\Models\Page;
use Illuminate\Http\Request;
class PageController extends Controller
{
public function __construct()
{
$this->middleware('can:view_pages')->only(['index', 'show']);
$this->middleware('can:create_pages')->only(['create', 'store']);
$this->middleware('can:edit_pages')->only(['edit', 'update']);
$this->middleware('can:delete_pages')->only(['destroy']);
$this->middleware('can:publish_pages')->only(['publish']);
}
public function index()
{
$pages = Page::with(['creator', 'parent'])
->topLevel()
->orderBy('sort_order')
->get();
// Also load child pages
$pages->load('children');
return view('admin.pages.index', compact('pages'));
}
public function create()
{
$parentPages = Page::topLevel()->orderBy('sort_order')->get();
return view('admin.pages.create', compact('parentPages'));
}
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:pages,slug',
'content' => 'required|string',
'template' => 'nullable|string|max:255',
'status' => 'required|in:draft,published',
'meta_description' => 'nullable|string|max:500',
'parent_id' => 'nullable|exists:pages,id',
'sort_order' => 'nullable|integer',
]);
$page = Page::create([
'title' => $validated['title'],
'slug' => $validated['slug'] ?? null,
'content' => $validated['content'],
'template' => $validated['template'] ?? null,
'status' => $validated['status'],
'meta_description' => $validated['meta_description'] ?? null,
'parent_id' => $validated['parent_id'] ?? null,
'sort_order' => $validated['sort_order'] ?? 0,
'published_at' => $validated['status'] === 'published' ? now() : null,
'created_by_user_id' => auth()->id(),
'last_updated_by_user_id' => auth()->id(),
]);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.created',
'description' => "建立頁面:{$page->title} (狀態:{$page->getStatusLabel()})",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.pages.show', $page)
->with('status', '頁面已成功建立');
}
public function show(Page $page)
{
$page->load(['creator', 'lastUpdatedBy', 'parent', 'children']);
return view('admin.pages.show', compact('page'));
}
public function edit(Page $page)
{
$parentPages = Page::topLevel()
->where('id', '!=', $page->id)
->orderBy('sort_order')
->get();
return view('admin.pages.edit', compact('page', 'parentPages'));
}
public function update(Request $request, Page $page)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'slug' => 'nullable|string|max:255|unique:pages,slug,'.$page->id,
'content' => 'required|string',
'template' => 'nullable|string|max:255',
'status' => 'required|in:draft,published',
'meta_description' => 'nullable|string|max:500',
'parent_id' => 'nullable|exists:pages,id',
'sort_order' => 'nullable|integer',
]);
$updateData = [
'title' => $validated['title'],
'content' => $validated['content'],
'template' => $validated['template'] ?? null,
'status' => $validated['status'],
'meta_description' => $validated['meta_description'] ?? null,
'parent_id' => $validated['parent_id'] ?? null,
'sort_order' => $validated['sort_order'] ?? 0,
'last_updated_by_user_id' => auth()->id(),
];
if (! empty($validated['slug']) && $validated['slug'] !== $page->slug) {
$updateData['slug'] = $validated['slug'];
}
if ($validated['status'] === 'published' && ! $page->published_at) {
$updateData['published_at'] = now();
}
$page->update($updateData);
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.updated',
'description' => "更新頁面:{$page->title}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.pages.show', $page)
->with('status', '頁面已成功更新');
}
public function destroy(Page $page)
{
if ($page->children()->count() > 0) {
return back()->with('error', '此頁面下仍有子頁面,無法刪除');
}
$title = $page->title;
$page->delete();
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.deleted',
'description' => "刪除頁面:{$title}",
'ip_address' => request()->ip(),
]);
return redirect()
->route('admin.pages.index')
->with('status', '頁面已成功刪除');
}
public function publish(Page $page)
{
if ($page->isPublished()) {
return back()->with('error', '此頁面已經發布');
}
$page->publish(auth()->user());
AuditLog::create([
'user_id' => auth()->id(),
'action' => 'page.published',
'description' => "發布頁面:{$page->title}",
'ip_address' => request()->ip(),
]);
return back()->with('status', '頁面已成功發布');
}
}

View File

@@ -0,0 +1,94 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\ArticleCollectionResource;
use App\Http\Resources\ArticleResource;
use App\Models\Article;
use App\Models\ArticleAttachment;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
class ArticleController extends Controller
{
public function index(Request $request)
{
$query = Article::with(['categories', 'tags'])
->active()
->forAccessLevel()
->orderByDesc('is_pinned')
->orderByDesc('published_at');
if ($request->filled('type')) {
$query->byContentType($request->type);
}
if ($request->filled('category')) {
$query->whereHas('categories', function ($q) use ($request) {
$q->where('article_categories.slug', $request->category);
});
}
if ($request->filled('tag')) {
$query->whereHas('tags', function ($q) use ($request) {
$q->where('article_tags.slug', $request->tag);
});
}
if ($request->filled('search')) {
$search = $request->search;
$query->where(function ($q) use ($search) {
$q->where('title', 'like', "%{$search}%")
->orWhere('content', 'like', "%{$search}%");
});
}
$articles = $query->paginate($request->integer('per_page', 12));
return ArticleCollectionResource::collection($articles);
}
public function show(string $slug)
{
$article = Article::with(['categories', 'tags', 'attachments'])
->active()
->forAccessLevel()
->where('slug', $slug)
->firstOrFail();
$article->incrementViewCount();
// Get related articles (same content_type, excluding current)
$related = Article::active()
->forAccessLevel()
->where('content_type', $article->content_type)
->where('id', '!=', $article->id)
->orderByDesc('published_at')
->limit(4)
->get();
return response()->json([
'data' => new ArticleResource($article),
'related' => ArticleCollectionResource::collection($related),
]);
}
public function downloadAttachment(string $slug, int $attachmentId)
{
$article = Article::active()
->forAccessLevel()
->where('slug', $slug)
->firstOrFail();
$attachment = ArticleAttachment::where('article_id', $article->id)
->findOrFail($attachmentId);
$attachment->incrementDownloadCount();
return Storage::disk('public')->download(
$attachment->file_path,
$attachment->original_filename
);
}
}

View File

@@ -0,0 +1,81 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\ArticleCollectionResource;
use App\Http\Resources\CategoryResource;
use App\Http\Resources\PageResource;
use App\Models\Article;
use App\Models\ArticleCategory;
use App\Models\Page;
class HomepageController extends Controller
{
public function index()
{
// Featured/pinned articles
$featured = Article::with(['categories', 'tags'])
->active()
->forAccessLevel()
->pinned()
->orderBy('display_order')
->limit(5)
->get();
// Latest articles by type
$latestBlog = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('blog')
->orderByDesc('published_at')
->limit(6)
->get();
$latestNotice = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('notice')
->orderByDesc('published_at')
->limit(4)
->get();
$latestDocument = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('document')
->orderByDesc('published_at')
->limit(4)
->get();
$latestRelatedNews = Article::with(['categories'])
->active()
->forAccessLevel()
->byContentType('related_news')
->orderByDesc('published_at')
->limit(4)
->get();
// About page
$aboutPage = Page::published()
->where('slug', 'about')
->first();
// Categories
$categories = ArticleCategory::withCount(['articles' => function ($q) {
$q->active()->forAccessLevel();
}])
->orderBy('sort_order')
->get();
return response()->json([
'featured' => ArticleCollectionResource::collection($featured),
'latest_blog' => ArticleCollectionResource::collection($latestBlog),
'latest_notice' => ArticleCollectionResource::collection($latestNotice),
'latest_document' => ArticleCollectionResource::collection($latestDocument),
'latest_related_news' => ArticleCollectionResource::collection($latestRelatedNews),
'about' => $aboutPage ? new PageResource($aboutPage) : null,
'categories' => CategoryResource::collection($categories),
]);
}
}

View File

@@ -0,0 +1,22 @@
<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Http\Resources\PageResource;
use App\Models\Page;
class PageController extends Controller
{
public function show(string $slug)
{
$page = Page::published()
->with(['children' => function ($q) {
$q->published()->orderBy('sort_order');
}])
->where('slug', $slug)
->firstOrFail();
return new PageResource($page);
}
}

View File

@@ -0,0 +1,34 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ArticleCollectionResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'summary' => $this->summary,
'excerpt' => $this->getExcerpt(200),
'content_type' => $this->content_type,
'content_type_label' => $this->getContentTypeLabel(),
'featured_image_url' => $this->featured_image_url,
'featured_image_alt' => $this->featured_image_alt,
'author_name' => $this->author_name,
'is_pinned' => $this->is_pinned,
'published_at' => $this->published_at?->toIso8601String(),
'categories' => CategoryResource::collection($this->whenLoaded('categories')),
'tags' => $this->whenLoaded('tags', function () {
return $this->tags->map(fn ($tag) => [
'name' => $tag->name,
'slug' => $tag->slug,
]);
}),
];
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ArticleResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'summary' => $this->summary,
'content' => $this->content,
'content_type' => $this->content_type,
'content_type_label' => $this->getContentTypeLabel(),
'featured_image_url' => $this->featured_image_url,
'featured_image_alt' => $this->featured_image_alt,
'author_name' => $this->author_name,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'is_pinned' => $this->is_pinned,
'view_count' => $this->view_count,
'published_at' => $this->published_at?->toIso8601String(),
'categories' => CategoryResource::collection($this->whenLoaded('categories')),
'tags' => $this->whenLoaded('tags', function () {
return $this->tags->map(fn ($tag) => [
'id' => $tag->id,
'name' => $tag->name,
'slug' => $tag->slug,
]);
}),
'attachments' => $this->whenLoaded('attachments', function () {
return $this->attachments->map(fn ($att) => [
'id' => $att->id,
'original_filename' => $att->original_filename,
'mime_type' => $att->mime_type,
'file_size' => $att->file_size,
'description' => $att->description,
]);
}),
];
}
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class CategoryResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'name' => $this->name,
'slug' => $this->slug,
'description' => $this->description,
'articles_count' => $this->whenCounted('articles'),
];
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace App\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class PageResource extends JsonResource
{
public function toArray(Request $request): array
{
return [
'id' => $this->id,
'title' => $this->title,
'slug' => $this->slug,
'content' => $this->content,
'template' => $this->template,
'custom_fields' => $this->custom_fields,
'meta_description' => $this->meta_description,
'meta_keywords' => $this->meta_keywords,
'published_at' => $this->published_at?->toIso8601String(),
'children' => PageResource::collection($this->whenLoaded('children')),
];
}
}

452
app/Models/Article.php Normal file
View File

@@ -0,0 +1,452 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Article extends Model
{
use HasFactory, SoftDeletes;
// ==================== Constants ====================
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISHED = 'published';
const STATUS_ARCHIVED = 'archived';
const ACCESS_LEVEL_PUBLIC = 'public';
const ACCESS_LEVEL_MEMBERS = 'members';
const ACCESS_LEVEL_BOARD = 'board';
const ACCESS_LEVEL_ADMIN = 'admin';
const CONTENT_TYPE_BLOG = 'blog';
const CONTENT_TYPE_NOTICE = 'notice';
const CONTENT_TYPE_DOCUMENT = 'document';
const CONTENT_TYPE_RELATED_NEWS = 'related_news';
// ==================== Configuration ====================
protected $fillable = [
'title',
'slug',
'summary',
'content',
'content_type',
'status',
'access_level',
'featured_image_path',
'featured_image_alt',
'author_name',
'author_user_id',
'meta_description',
'meta_keywords',
'is_pinned',
'display_order',
'published_at',
'expires_at',
'archived_at',
'view_count',
'created_by_user_id',
'last_updated_by_user_id',
];
protected $casts = [
'is_pinned' => 'boolean',
'display_order' => 'integer',
'view_count' => 'integer',
'meta_keywords' => 'array',
'published_at' => 'datetime',
'expires_at' => 'datetime',
'archived_at' => 'datetime',
];
// ==================== Boot ====================
protected static function boot()
{
parent::boot();
static::creating(function (Article $article) {
if (empty($article->slug)) {
$article->slug = static::generateUniqueSlug($article->title);
}
});
}
public static function generateUniqueSlug(string $title): string
{
$slug = Str::slug($title);
// For Chinese titles, slug may be empty
if (empty($slug)) {
$slug = Str::slug(Str::ascii($title));
}
if (empty($slug)) {
$slug = 'article-'.time();
}
$originalSlug = $slug;
$count = 1;
while (static::withTrashed()->where('slug', $slug)->exists()) {
$slug = $originalSlug.'-'.$count++;
}
return $slug;
}
// ==================== Relationships ====================
public function categories()
{
return $this->belongsToMany(ArticleCategory::class, 'article_category', 'article_id', 'category_id');
}
public function tags()
{
return $this->belongsToMany(ArticleTag::class, 'article_tag', 'article_id', 'tag_id');
}
public function attachments()
{
return $this->hasMany(ArticleAttachment::class);
}
public function creator()
{
return $this->belongsTo(User::class, 'created_by_user_id');
}
public function lastUpdatedBy()
{
return $this->belongsTo(User::class, 'last_updated_by_user_id');
}
public function authorUser()
{
return $this->belongsTo(User::class, 'author_user_id');
}
// ==================== Status Check Methods ====================
public function isDraft(): bool
{
return $this->status === self::STATUS_DRAFT;
}
public function isPublished(): bool
{
return $this->status === self::STATUS_PUBLISHED;
}
public function isArchived(): bool
{
return $this->status === self::STATUS_ARCHIVED;
}
public function isPinned(): bool
{
return $this->is_pinned;
}
public function isExpired(): bool
{
if (! $this->expires_at) {
return false;
}
return $this->expires_at->isPast();
}
public function isScheduled(): bool
{
if (! $this->published_at) {
return false;
}
return $this->published_at->isFuture();
}
public function isActive(): bool
{
return $this->isPublished()
&& ! $this->isExpired()
&& (! $this->published_at || $this->published_at->isPast());
}
// ==================== Access Control Methods ====================
public function canBeViewedBy(?User $user): bool
{
if ($this->isDraft()) {
if (! $user) {
return false;
}
return $user->id === $this->created_by_user_id
|| $user->hasRole('admin')
|| $user->can('manage_all_articles');
}
if ($this->isArchived()) {
if (! $user) {
return false;
}
return $user->hasRole('admin') || $user->can('manage_all_articles');
}
if ($this->isExpired()) {
if (! $user) {
return false;
}
return $user->hasRole('admin') || $user->can('manage_all_articles');
}
if ($this->isScheduled()) {
if (! $user) {
return false;
}
return $user->id === $this->created_by_user_id
|| $user->hasRole('admin')
|| $user->can('manage_all_articles');
}
if ($this->access_level === self::ACCESS_LEVEL_PUBLIC) {
return true;
}
if (! $user) {
return false;
}
if ($user->hasRole('admin')) {
return true;
}
if ($this->access_level === self::ACCESS_LEVEL_MEMBERS) {
return $user->member && $user->member->hasPaidMembership();
}
if ($this->access_level === self::ACCESS_LEVEL_BOARD) {
return $user->hasRole(['admin', 'finance_chair', 'finance_board_member']);
}
if ($this->access_level === self::ACCESS_LEVEL_ADMIN) {
return $user->hasRole('admin');
}
return false;
}
public function canBeEditedBy(User $user): bool
{
if ($user->hasRole('admin') || $user->can('manage_all_articles')) {
return true;
}
if (! $user->can('edit_articles')) {
return false;
}
return $user->id === $this->created_by_user_id;
}
// ==================== Query Scopes ====================
public function scopePublished(Builder $query): Builder
{
return $query->where('status', self::STATUS_PUBLISHED);
}
public function scopeDraft(Builder $query): Builder
{
return $query->where('status', self::STATUS_DRAFT);
}
public function scopeArchived(Builder $query): Builder
{
return $query->where('status', self::STATUS_ARCHIVED);
}
public function scopeActive(Builder $query): Builder
{
return $query->where('status', self::STATUS_PUBLISHED)
->where(function ($q) {
$q->whereNull('published_at')
->orWhere('published_at', '<=', now());
})
->where(function ($q) {
$q->whereNull('expires_at')
->orWhere('expires_at', '>', now());
});
}
public function scopeByContentType(Builder $query, string $contentType): Builder
{
return $query->where('content_type', $contentType);
}
public function scopePinned(Builder $query): Builder
{
return $query->where('is_pinned', true);
}
public function scopeForAccessLevel(Builder $query, ?User $user = null): Builder
{
if ($user && $user->hasRole('admin')) {
return $query;
}
$accessLevels = [self::ACCESS_LEVEL_PUBLIC];
if ($user) {
if ($user->member && $user->member->hasPaidMembership()) {
$accessLevels[] = self::ACCESS_LEVEL_MEMBERS;
}
if ($user->hasRole(['finance_chair', 'finance_board_member'])) {
$accessLevels[] = self::ACCESS_LEVEL_BOARD;
}
}
return $query->whereIn('access_level', $accessLevels);
}
// ==================== Helper Methods ====================
public function publish(?User $user = null): void
{
$updates = [
'status' => self::STATUS_PUBLISHED,
];
if (! $this->published_at) {
$updates['published_at'] = now();
}
if ($user) {
$updates['last_updated_by_user_id'] = $user->id;
}
$this->update($updates);
}
public function archive(?User $user = null): void
{
$updates = [
'status' => self::STATUS_ARCHIVED,
'archived_at' => now(),
];
if ($user) {
$updates['last_updated_by_user_id'] = $user->id;
}
$this->update($updates);
}
public function pin(?int $order = null, ?User $user = null): void
{
$updates = [
'is_pinned' => true,
'display_order' => $order ?? 0,
];
if ($user) {
$updates['last_updated_by_user_id'] = $user->id;
}
$this->update($updates);
}
public function unpin(?User $user = null): void
{
$updates = [
'is_pinned' => false,
'display_order' => 0,
];
if ($user) {
$updates['last_updated_by_user_id'] = $user->id;
}
$this->update($updates);
}
public function incrementViewCount(): void
{
$this->increment('view_count');
}
public function getAccessLevelLabel(): string
{
return match ($this->access_level) {
self::ACCESS_LEVEL_PUBLIC => '公開',
self::ACCESS_LEVEL_MEMBERS => '會員',
self::ACCESS_LEVEL_BOARD => '理事會',
self::ACCESS_LEVEL_ADMIN => '管理員',
default => '未知',
};
}
public function getStatusLabel(): string
{
return match ($this->status) {
self::STATUS_DRAFT => '草稿',
self::STATUS_PUBLISHED => '已發布',
self::STATUS_ARCHIVED => '已歸檔',
default => '未知',
};
}
public function getStatusBadgeColor(): string
{
return match ($this->status) {
self::STATUS_DRAFT => 'gray',
self::STATUS_PUBLISHED => 'green',
self::STATUS_ARCHIVED => 'yellow',
default => 'gray',
};
}
public function getContentTypeLabel(): string
{
return match ($this->content_type) {
self::CONTENT_TYPE_BLOG => '部落格',
self::CONTENT_TYPE_NOTICE => '公告',
self::CONTENT_TYPE_DOCUMENT => '文件',
self::CONTENT_TYPE_RELATED_NEWS => '相關新聞',
default => '未知',
};
}
public function getExcerpt(int $length = 150): string
{
$plainText = strip_tags($this->summary ?: $this->content);
return Str::limit($plainText, $length);
}
public function getFeaturedImageUrlAttribute(): ?string
{
if (! $this->featured_image_path) {
return null;
}
return asset('storage/'.$this->featured_image_path);
}
}

View File

@@ -0,0 +1,36 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class ArticleAttachment extends Model
{
use HasFactory;
protected $fillable = [
'article_id',
'file_path',
'original_filename',
'mime_type',
'file_size',
'description',
'download_count',
];
protected $casts = [
'file_size' => 'integer',
'download_count' => 'integer',
];
public function article()
{
return $this->belongsTo(Article::class);
}
public function incrementDownloadCount(): void
{
$this->increment('download_count');
}
}

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class ArticleCategory extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
'description',
'sort_order',
];
protected $casts = [
'sort_order' => 'integer',
];
protected static function boot()
{
parent::boot();
static::creating(function (ArticleCategory $category) {
if (empty($category->slug)) {
$slug = Str::slug($category->name);
if (empty($slug)) {
$slug = 'category-'.time();
}
$originalSlug = $slug;
$count = 1;
while (static::where('slug', $slug)->exists()) {
$slug = $originalSlug.'-'.$count++;
}
$category->slug = $slug;
}
});
}
public function articles()
{
return $this->belongsToMany(Article::class, 'article_category', 'category_id', 'article_id');
}
}

42
app/Models/ArticleTag.php Normal file
View File

@@ -0,0 +1,42 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
class ArticleTag extends Model
{
use HasFactory;
protected $fillable = [
'name',
'slug',
];
protected static function boot()
{
parent::boot();
static::creating(function (ArticleTag $tag) {
if (empty($tag->slug)) {
$slug = Str::slug($tag->name);
if (empty($slug)) {
$slug = 'tag-'.time();
}
$originalSlug = $slug;
$count = 1;
while (static::where('slug', $slug)->exists()) {
$slug = $originalSlug.'-'.$count++;
}
$tag->slug = $slug;
}
});
}
public function articles()
{
return $this->belongsToMany(Article::class, 'article_tag', 'tag_id', 'article_id');
}
}

130
app/Models/Page.php Normal file
View File

@@ -0,0 +1,130 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Str;
class Page extends Model
{
use HasFactory, SoftDeletes;
const STATUS_DRAFT = 'draft';
const STATUS_PUBLISHED = 'published';
protected $fillable = [
'title',
'slug',
'content',
'template',
'custom_fields',
'status',
'meta_description',
'meta_keywords',
'parent_id',
'sort_order',
'published_at',
'created_by_user_id',
'last_updated_by_user_id',
];
protected $casts = [
'custom_fields' => 'array',
'meta_keywords' => 'array',
'sort_order' => 'integer',
'published_at' => 'datetime',
];
protected static function boot()
{
parent::boot();
static::creating(function (Page $page) {
if (empty($page->slug)) {
$slug = Str::slug($page->title);
if (empty($slug)) {
$slug = 'page-'.time();
}
$originalSlug = $slug;
$count = 1;
while (static::withTrashed()->where('slug', $slug)->exists()) {
$slug = $originalSlug.'-'.$count++;
}
$page->slug = $slug;
}
});
}
// ==================== Relationships ====================
public function parent()
{
return $this->belongsTo(Page::class, 'parent_id');
}
public function children()
{
return $this->hasMany(Page::class, 'parent_id')->orderBy('sort_order');
}
public function creator()
{
return $this->belongsTo(User::class, 'created_by_user_id');
}
public function lastUpdatedBy()
{
return $this->belongsTo(User::class, 'last_updated_by_user_id');
}
// ==================== Query Scopes ====================
public function scopePublished(Builder $query): Builder
{
return $query->where('status', self::STATUS_PUBLISHED);
}
public function scopeTopLevel(Builder $query): Builder
{
return $query->whereNull('parent_id');
}
// ==================== Helper Methods ====================
public function isDraft(): bool
{
return $this->status === self::STATUS_DRAFT;
}
public function isPublished(): bool
{
return $this->status === self::STATUS_PUBLISHED;
}
public function publish(?User $user = null): void
{
$updates = [
'status' => self::STATUS_PUBLISHED,
'published_at' => $this->published_at ?? now(),
];
if ($user) {
$updates['last_updated_by_user_id'] = $user->id;
}
$this->update($updates);
}
public function getStatusLabel(): string
{
return match ($this->status) {
self::STATUS_DRAFT => '草稿',
self::STATUS_PUBLISHED => '已發布',
default => '未知',
};
}
}