Files
cleargame/app/Http/Controllers/ForumPostController.php
T
2026-02-22 20:02:34 +02:00

178 lines
5.5 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\ForumPost;
use App\Models\ForumTopic;
use App\Models\ForumThread;
use Illuminate\Http\Request;
class ForumPostController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
// Not needed as posts are displayed within topics
return redirect()->route('forum.index');
}
/**
* Show the form for creating a new resource.
*/
public function create(Request $request)
{
$topic_id = $request->query('topic_id');
if (!$topic_id) {
return redirect()->route('forum.index')
->with('error', 'Topic ID is required to create a post.');
}
$topic = ForumTopic::findOrFail($topic_id);
$thread = $topic->thread;
// Check if thread is locked
if ($thread->is_locked) {
return redirect()->route('forum.topics.show', $topic)
->with('error', 'This thread is locked. You cannot create new posts.');
}
return view('forum.posts.create', compact('topic', 'thread'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
$validated = $request->validate([
'content' => 'required|string',
'topic_id' => 'required|exists:forum_topics,id',
]);
$topic = ForumTopic::findOrFail($validated['topic_id']);
$thread = $topic->thread;
// Check if thread is locked
if ($thread->is_locked) {
return redirect()->route('forum.topics.show', $topic)
->with('error', 'This thread is locked. You cannot create new posts.');
}
// Create the post
$post = new ForumPost();
$post->topic_id = $validated['topic_id'];
$post->user_id = auth()->id();
$post->content = $validated['content'];
$post->save();
// Update topic and thread last activity
$topic->last_activity_at = now();
$topic->save();
$thread->last_activity_at = now();
$thread->save();
return redirect()->route('forum.topics.show', $topic)
->with('success', 'Post created successfully.');
}
/**
* Display the specified resource.
*/
public function show(ForumPost $post)
{
// Redirect to the topic with an anchor to the post
return redirect()->route('forum.topics.show', $post->topic) . '#post-' . $post->id;
}
/**
* Show the form for editing the specified resource.
*/
public function edit(ForumPost $post)
{
// Check if user is authorized to edit this post
if (auth()->id() !== $post->user_id && !auth()->user()->hasRole('admin')) {
return redirect()->route('forum.topics.show', $post->topic)
->with('error', 'You are not authorized to edit this post.');
}
$topic = $post->topic;
$thread = $topic->thread;
// Check if thread is locked
if ($thread->is_locked) {
return redirect()->route('forum.topics.show', $topic)
->with('error', 'This thread is locked. You cannot edit posts.');
}
return view('forum.posts.edit', compact('post', 'topic', 'thread'));
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, ForumPost $post)
{
// Check if user is authorized to edit this post
if (auth()->id() !== $post->user_id && !auth()->user()->hasRole('admin')) {
return redirect()->route('forum.topics.show', $post->topic)
->with('error', 'You are not authorized to edit this post.');
}
$topic = $post->topic;
$thread = $topic->thread;
// Check if thread is locked
if ($thread->is_locked) {
return redirect()->route('forum.topics.show', $topic)
->with('error', 'This thread is locked. You cannot edit posts.');
}
$validated = $request->validate([
'content' => 'required|string',
]);
// Update the post
$post->content = $validated['content'];
$post->edited_at = now();
$post->save();
return redirect()->route('forum.topics.show', $topic)
->with('success', 'Post updated successfully.');
}
/**
* Remove the specified resource from storage.
*/
public function destroy(ForumPost $post)
{
// Check if user is authorized to delete this post
if (auth()->id() !== $post->user_id && !auth()->user()->hasRole('admin')) {
return redirect()->route('forum.topics.show', $post->topic)
->with('error', 'You are not authorized to delete this post.');
}
$topic = $post->topic;
$thread = $topic->thread;
// Check if thread is locked
if ($thread->is_locked) {
return redirect()->route('forum.topics.show', $topic)
->with('error', 'This thread is locked. You cannot delete posts.');
}
// Check if this is the only post in the topic
if ($topic->posts()->count() <= 1) {
return redirect()->route('forum.topics.show', $topic)
->with('error', 'Cannot delete the only post in a topic. Delete the topic instead.');
}
$post->delete();
return redirect()->route('forum.topics.show', $topic)
->with('success', 'Post deleted successfully.');
}
}