Files
cleargame/app/Http/Controllers/ForumThreadController.php
T

160 lines
5.1 KiB
PHP
Raw Normal View History

2026-02-22 20:02:34 +02:00
<?php
namespace App\Http\Controllers;
use App\Models\ForumCategory;
use App\Models\ForumThread;
use App\Models\ForumTopic;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
class ForumThreadController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
$threads = ForumThread::with(['user', 'category'])
->orderBy('is_sticky', 'desc')
->orderBy('last_activity_at', 'desc')
->paginate(20);
return view('forum.threads.index', compact('threads'));
}
/**
* Show the form for creating a new resource.
*/
public function create(Request $request)
{
$categories = ForumCategory::orderBy('order')->get();
$category_id = $request->query('category_id');
return view('forum.threads.create', compact('categories', 'category_id'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'title' => 'required|string|max:255',
'content' => 'required|string',
'category_id' => 'required|exists:forum_categories,id',
]);
// Create the thread
$thread = new ForumThread();
$thread->title = $validated['title'];
$thread->slug = Str::slug($validated['title']) . '-' . uniqid();
$thread->category_id = $validated['category_id'];
$thread->user_id = auth()->id();
$thread->last_activity_at = now();
$thread->save();
// Create the initial topic
$topic = new ForumTopic();
$topic->thread_id = $thread->id;
$topic->user_id = auth()->id();
$topic->title = $validated['title'];
$topic->content = $validated['content'];
$topic->last_activity_at = now();
$topic->save();
return redirect()->route('forum.threads.show', $thread)
->with('success', 'Thread created successfully.');
}
/**
* Display the specified resource.
*/
public function show(ForumThread $thread)
{
// Increment view count
$thread->increment('views');
// Get topics with pagination
$topics = $thread->topics()
->with('user')
->orderBy('is_pinned', 'desc')
->orderBy('created_at', 'asc')
->paginate(15);
return view('forum.threads.show', compact('thread', 'topics'));
}
/**
* Show the form for editing the specified resource.
*/
public function edit(ForumThread $thread)
{
// Check if user is authorized to edit this thread
if (auth()->id() !== $thread->user_id && !auth()->user()->hasRole('admin')) {
return redirect()->route('forum.threads.show', $thread)
->with('error', 'You are not authorized to edit this thread.');
}
$categories = ForumCategory::orderBy('order')->get();
$initialTopic = $thread->topics()->oldest()->first();
return view('forum.threads.edit', compact('thread', 'categories', 'initialTopic'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, ForumThread $thread)
{
// Check if user is authorized to edit this thread
if (auth()->id() !== $thread->user_id && !auth()->user()->hasRole('admin')) {
return redirect()->route('forum.threads.show', $thread)
->with('error', 'You are not authorized to edit this thread.');
}
$validated = $request->validate([
'title' => 'required|string|max:255',
'content' => 'required|string',
'category_id' => 'required|exists:forum_categories,id',
]);
// Update the thread
$thread->title = $validated['title'];
if ($thread->isDirty('title')) {
$thread->slug = Str::slug($validated['title']) . '-' . uniqid();
}
$thread->category_id = $validated['category_id'];
$thread->save();
// Update the initial topic
$initialTopic = $thread->topics()->oldest()->first();
if ($initialTopic) {
$initialTopic->title = $validated['title'];
$initialTopic->content = $validated['content'];
$initialTopic->save();
}
return redirect()->route('forum.threads.show', $thread)
->with('success', 'Thread updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(ForumThread $thread)
{
// Check if user is authorized to delete this thread
if (auth()->id() !== $thread->user_id && !auth()->user()->hasRole('admin')) {
return redirect()->route('forum.threads.show', $thread)
->with('error', 'You are not authorized to delete this thread.');
}
$categoryId = $thread->category_id;
$thread->delete();
return redirect()->route('forum.categories.show', ['category' => ForumCategory::find($categoryId)])
->with('success', 'Thread deleted successfully.');
}
}