This repository has been archived on 2024-06-26. You can view files and clone it, but cannot push or open issues or pull requests.
sakura/app/Controllers/Forum/ForumController.php

164 lines
4.4 KiB
PHP
Raw Normal View History

2016-07-31 19:36:13 +00:00
<?php
/**
* Holds the forum pages controllers.
* @package Sakura
*/
namespace Sakura\Controllers\Forum;
use Phroute\Phroute\Exception\HttpMethodNotAllowedException;
use Phroute\Phroute\Exception\HttpRouteNotFoundException;
2016-07-31 19:36:13 +00:00
use Sakura\Config;
2016-08-07 14:10:27 +00:00
use Sakura\CurrentSession;
2016-07-31 19:36:13 +00:00
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
{
2016-08-05 02:35:37 +00:00
/**
* Renders the forum index
* @return string
*/
2016-12-04 16:33:52 +00:00
public function index(): string
2016-07-31 19:36:13 +00:00
{
// 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 = [];
2016-12-09 21:51:40 +00:00
while ([$count, $row] = each($activeTopicsIds)) {
$topic = new Topic($row->topic_id);
2016-07-31 19:36:13 +00:00
$forum = new Forum($topic->forum);
// Check if we have permission to view it
2016-11-02 18:58:51 +00:00
if (!$forum->perms->view) {
2016-07-31 19:36:13 +00:00
$fetch = DB::table('posts')
->groupBy('topic_id')
->orderByRaw('COUNT(*) DESC')
2016-12-09 21:51:40 +00:00
->skip(11 + $count)
2016-07-31 19:36:13 +00:00
->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 = [];
2016-12-09 21:51:40 +00:00
while ([$count, $row] = each($latestPostsIds)) {
$post = new Post($row->post_id);
2016-07-31 19:36:13 +00:00
$forum = new Forum($post->forum);
// Check if we have permission to view it
2016-11-02 18:58:51 +00:00
if (!$forum->perms->view) {
2016-07-31 19:36:13 +00:00
$fetch = DB::table('posts')
->orderBy('post_id', 'desc')
2016-12-09 21:51:40 +00:00
->skip(11 + $count)
2016-07-31 19:36:13 +00:00
->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'));
}
2016-08-05 02:35:37 +00:00
/**
* Renders a forum.
2016-12-04 16:33:52 +00:00
* @param int $id
* @throws HttpRouteNotFoundException
2016-08-05 02:35:37 +00:00
* @return string
*/
2016-12-04 16:33:52 +00:00
public function forum(int $id = 0): string
2016-07-31 19:36:13 +00:00
{
$forum = new Forum($id);
// Redirect forum id 0 to the main page
if ($forum->id === 0) {
2016-12-04 16:33:52 +00:00
return redirect(route('forums.index'));
2016-07-31 19:36:13 +00:00
}
// Check if the forum exists
if ($forum->id < 0
2016-11-02 18:58:51 +00:00
|| !$forum->perms->view) {
2016-12-04 16:33:52 +00:00
throw new HttpRouteNotFoundException;
2016-07-31 19:36:13 +00:00
}
// Check if the forum isn't a link
if ($forum->type === 2) {
2016-12-04 16:33:52 +00:00
return redirect($forum->link);
2016-07-31 19:36:13 +00:00
}
return view('forum/forum', compact('forum'));
}
2016-08-05 02:35:37 +00:00
/**
* Marks an entire forum as read.
* @param int $id
2016-12-04 16:33:52 +00:00
* @throws HttpRouteNotFoundException
* @throws HttpMethodNotAllowedException
2016-08-05 02:35:37 +00:00
* @return string
*/
2016-12-04 16:33:52 +00:00
public function markRead(int $id = 0): string
2016-07-31 19:36:13 +00:00
{
if (!session_check('s')) {
2016-12-04 16:33:52 +00:00
throw new HttpMethodNotAllowedException;
2016-07-31 19:36:13 +00:00
}
$forum = new Forum($id);
// Check if the forum exists
if ($forum->id < 1
2016-11-02 18:58:51 +00:00
|| !$forum->perms->view) {
2016-12-04 16:33:52 +00:00
throw new HttpRouteNotFoundException;
2016-07-31 19:36:13 +00:00
}
2016-08-07 14:10:27 +00:00
$forum->trackUpdateAll(CurrentSession::$user->id);
2016-07-31 19:36:13 +00:00
2016-12-04 16:33:52 +00:00
return redirect(route('forums.forum', $forum->id));
2016-07-31 19:36:13 +00:00
}
}