173 lines
4.7 KiB
PHP
173 lines
4.7 KiB
PHP
<?php
|
|
/**
|
|
* Holds the forum pages controllers.
|
|
* @package Sakura
|
|
*/
|
|
|
|
namespace Sakura\Controllers\Forum;
|
|
|
|
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
|
|
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
|
|
use Sakura\Config;
|
|
use Sakura\CurrentSession;
|
|
use Sakura\DB;
|
|
use Sakura\Forum\Forum;
|
|
use Sakura\Forum\Post;
|
|
use Sakura\Forum\Topic;
|
|
use Sakura\User;
|
|
|
|
/**
|
|
* Forum page controllers.
|
|
* @package Sakura
|
|
* @author Julian van de Groep <me@flash.moe>
|
|
*/
|
|
class ForumController extends Controller
|
|
{
|
|
/**
|
|
* Renders the forum index
|
|
* @return string
|
|
*/
|
|
public function index(): string
|
|
{
|
|
// Get the most active topics
|
|
$activeTopicsIds = DB::table('posts')
|
|
->where('forum_id', '!=', config('forum.trash'))
|
|
->groupBy('topic_id')
|
|
->orderByRaw('COUNT(*) DESC')
|
|
->limit(10)
|
|
->get(['topic_id']);
|
|
$activeTopics = [];
|
|
|
|
while ([$count, $row] = each($activeTopicsIds)) {
|
|
$topic = new Topic($row->topic_id);
|
|
$forum = new Forum($topic->forum);
|
|
|
|
// Check if we have permission to view it
|
|
if (!$forum->perms->view) {
|
|
$fetch = DB::table('posts')
|
|
->groupBy('topic_id')
|
|
->orderByRaw('COUNT(*) DESC')
|
|
->skip(11 + $count)
|
|
->take(1)
|
|
->get(['topic_id']);
|
|
|
|
if ($fetch) {
|
|
$activeTopicsIds[] = $fetch[0];
|
|
}
|
|
continue;
|
|
}
|
|
|
|
$activeTopics[$topic->id] = $topic;
|
|
}
|
|
|
|
// Get the latest posts
|
|
$latestPostsIds = DB::table('posts')
|
|
->where('forum_id', '!=', config('forum.trash'))
|
|
->orderBy('post_id', 'desc')
|
|
->limit(10)
|
|
->get(['post_id']);
|
|
$latestPosts = [];
|
|
|
|
while ([$count, $row] = each($latestPostsIds)) {
|
|
$post = new Post($row->post_id);
|
|
$forum = new Forum($post->forum);
|
|
|
|
// Check if we have permission to view it
|
|
if (!$forum->perms->view) {
|
|
$fetch = DB::table('posts')
|
|
->orderBy('post_id', 'desc')
|
|
->skip(11 + $count)
|
|
->take(1)
|
|
->get(['post_id']);
|
|
|
|
if ($fetch) {
|
|
$latestPostsIds[] = $fetch[0];
|
|
}
|
|
continue;
|
|
}
|
|
|
|
$latestPosts[$post->id] = $post;
|
|
}
|
|
|
|
// Get the most active poster
|
|
$activePosterId = DB::table('posts')
|
|
->where('forum_id', '!=', config('forum.trash'))
|
|
->where('post_time', '>', time() - (24 * 60 * 60))
|
|
->groupBy('poster_id')
|
|
->orderByRaw('COUNT(*) DESC')
|
|
->limit(1)
|
|
->get(['poster_id']);
|
|
$activePoster = User::construct(
|
|
$activePosterId ? $activePosterId[0]->poster_id : 0
|
|
);
|
|
|
|
$forum = new Forum;
|
|
|
|
return view('forum/index', compact('forum', 'activeTopics', 'latestPosts', 'activePoster'));
|
|
}
|
|
|
|
/**
|
|
* Renders a forum.
|
|
* @param int $id
|
|
* @throws HttpRouteNotFoundException
|
|
* @return string
|
|
*/
|
|
public function forum(int $id = 0): string
|
|
{
|
|
$forum = new Forum($id);
|
|
|
|
// Redirect forum id 0 to the main page
|
|
if ($forum->id === 0) {
|
|
return redirect(route('forums.index'));
|
|
}
|
|
|
|
// Check if the forum exists
|
|
if ($forum->id < 0
|
|
|| !$forum->perms->view) {
|
|
throw new HttpRouteNotFoundException;
|
|
}
|
|
|
|
// Check if the forum isn't a link
|
|
if ($forum->type === 2) {
|
|
return redirect($forum->link);
|
|
}
|
|
|
|
return view('forum/forum', compact('forum'));
|
|
}
|
|
|
|
/**
|
|
* Marks an entire forum as read.
|
|
* @param int $id
|
|
* @throws HttpRouteNotFoundException
|
|
* @throws HttpMethodNotAllowedException
|
|
* @return string
|
|
*/
|
|
public function markRead(int $id = 0): string
|
|
{
|
|
if (!session_check('s')) {
|
|
throw new HttpMethodNotAllowedException;
|
|
}
|
|
|
|
$forum = new Forum($id);
|
|
|
|
// Check if the forum exists
|
|
if ($forum->id < 1
|
|
|| !$forum->perms->view) {
|
|
throw new HttpRouteNotFoundException;
|
|
}
|
|
|
|
$forum->trackUpdateAll(CurrentSession::$user->id);
|
|
|
|
return redirect(route('forums.forum', $forum->id));
|
|
}
|
|
|
|
public function moveDestinations(): string
|
|
{
|
|
$forums = array_column(DB::table('forums')
|
|
->where('forum_type', 0)
|
|
->where('forum_id', '!=', config('forum.trash'))
|
|
->get(['forum_id', 'forum_name']), 'forum_name', 'forum_id');
|
|
|
|
return $this->json($forums);
|
|
}
|
|
}
|