102 lines
3.4 KiB
PHP
102 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Community;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Services\LegacyService;
|
|
|
|
class ForumController extends Controller
|
|
{
|
|
protected LegacyService $legacy;
|
|
|
|
public function __construct(LegacyService $legacy)
|
|
{
|
|
$this->legacy = $legacy;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$data = $this->legacy->forumIndex();
|
|
|
|
if (empty($data['topics']) || count($data['topics']) === 0) {
|
|
try {
|
|
$categories = \App\Models\ForumCategory::query()
|
|
->withCount(['threads as num_subtopics'])
|
|
->orderBy('position')
|
|
->orderBy('id')
|
|
->get();
|
|
|
|
$topics = $categories->map(function ($category) {
|
|
$threadIds = \App\Models\ForumThread::where('category_id', $category->id)->pluck('id');
|
|
|
|
return (object) [
|
|
'topic_id' => $category->id,
|
|
'topic' => $category->name,
|
|
'discuss' => null,
|
|
'last_update' => \App\Models\ForumThread::where('category_id', $category->id)->max('last_post_at'),
|
|
'num_posts' => $threadIds->isEmpty() ? 0 : \App\Models\ForumPost::whereIn('thread_id', $threadIds)->count(),
|
|
'num_subtopics' => (int) ($category->num_subtopics ?? 0),
|
|
];
|
|
});
|
|
|
|
$data['topics'] = $topics;
|
|
} catch (\Throwable $e) {
|
|
// keep legacy response
|
|
}
|
|
}
|
|
|
|
return view('community.forum.index', $data);
|
|
}
|
|
|
|
public function topic(Request $request, $topic_id, $slug = null)
|
|
{
|
|
// Redirect to canonical slug when possible
|
|
try {
|
|
$thread = \App\Models\ForumThread::find((int) $topic_id);
|
|
if ($thread && !empty($thread->slug)) {
|
|
$correct = $thread->slug;
|
|
if ($slug !== $correct) {
|
|
$qs = $request->getQueryString();
|
|
$url = route('legacy.forum.topic', ['topic_id' => $topic_id, 'slug' => $correct]);
|
|
if ($qs) $url .= '?' . $qs;
|
|
return redirect($url, 301);
|
|
}
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// ignore
|
|
}
|
|
|
|
|
|
$data = $this->legacy->forumTopic((int) $topic_id, (int) $request->query('page', 1));
|
|
|
|
if (! $data) {
|
|
// fallback to new forum tables if migration already ran
|
|
try {
|
|
$thread = \App\Models\ForumThread::with(['posts.user'])->find((int) $topic_id);
|
|
if ($thread) {
|
|
$posts = \App\Models\ForumPost::where('thread_id', $thread->id)->orderBy('created_at')->get();
|
|
$data = [
|
|
'type' => 'posts',
|
|
'thread' => $thread,
|
|
'posts' => $posts,
|
|
'page_title' => $thread->title ?? 'Forum',
|
|
];
|
|
}
|
|
} catch (\Throwable $e) {
|
|
// ignore and fall through to placeholder
|
|
}
|
|
}
|
|
|
|
if (! $data) {
|
|
return view('shared.placeholder');
|
|
}
|
|
|
|
if (isset($data['type']) && $data['type'] === 'subtopics') {
|
|
return view('community.forum.topic', $data);
|
|
}
|
|
|
|
return view('community.forum.posts', $data);
|
|
}
|
|
}
|