104 lines
3.0 KiB
PHP
104 lines
3.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\DocumentCategory;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
|
|
class DocumentCategoryController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of document categories
|
|
*/
|
|
public function index()
|
|
{
|
|
$categories = DocumentCategory::withCount('activeDocuments')
|
|
->orderBy('sort_order')
|
|
->get();
|
|
|
|
return view('admin.document-categories.index', compact('categories'));
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new category
|
|
*/
|
|
public function create()
|
|
{
|
|
return view('admin.document-categories.create');
|
|
}
|
|
|
|
/**
|
|
* Store a newly created category
|
|
*/
|
|
public function store(Request $request)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'slug' => 'nullable|string|max:255|unique:document_categories,slug',
|
|
'description' => 'nullable|string',
|
|
'icon' => 'nullable|string|max:10',
|
|
'sort_order' => 'nullable|integer',
|
|
'default_access_level' => 'required|in:public,members,admin,board',
|
|
]);
|
|
|
|
// Auto-generate slug if not provided
|
|
if (empty($validated['slug'])) {
|
|
$validated['slug'] = Str::slug($validated['name']);
|
|
}
|
|
|
|
$category = DocumentCategory::create($validated);
|
|
|
|
return redirect()
|
|
->route('admin.document-categories.index')
|
|
->with('status', '文件類別已成功建立');
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing a category
|
|
*/
|
|
public function edit(DocumentCategory $documentCategory)
|
|
{
|
|
return view('admin.document-categories.edit', compact('documentCategory'));
|
|
}
|
|
|
|
/**
|
|
* Update the specified category
|
|
*/
|
|
public function update(Request $request, DocumentCategory $documentCategory)
|
|
{
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'slug' => 'nullable|string|max:255|unique:document_categories,slug,' . $documentCategory->id,
|
|
'description' => 'nullable|string',
|
|
'icon' => 'nullable|string|max:10',
|
|
'sort_order' => 'nullable|integer',
|
|
'default_access_level' => 'required|in:public,members,admin,board',
|
|
]);
|
|
|
|
$documentCategory->update($validated);
|
|
|
|
return redirect()
|
|
->route('admin.document-categories.index')
|
|
->with('status', '文件類別已成功更新');
|
|
}
|
|
|
|
/**
|
|
* Remove the specified category
|
|
*/
|
|
public function destroy(DocumentCategory $documentCategory)
|
|
{
|
|
// Check if category has documents
|
|
if ($documentCategory->documents()->count() > 0) {
|
|
return back()->with('error', '此類別下有文件,無法刪除');
|
|
}
|
|
|
|
$documentCategory->delete();
|
|
|
|
return redirect()
|
|
->route('admin.document-categories.index')
|
|
->with('status', '文件類別已成功刪除');
|
|
}
|
|
}
|