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/libraries/Controllers/ForumController.php

262 lines
7.5 KiB
PHP
Raw Normal View History

2016-02-04 20:56:40 +00:00
<?php
/**
* Holds the forum pages controllers.
2016-03-08 23:07:58 +00:00
*
2016-02-04 20:56:40 +00:00
* @package Sakura
*/
namespace Sakura\Controllers;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
2016-03-10 18:54:36 +00:00
use Sakura\Forum\Forum;
use Sakura\Forum\Thread;
2016-02-04 20:56:40 +00:00
use Sakura\Perms\Forum as ForumPerms;
2016-03-10 18:54:36 +00:00
use Sakura\Router;
2016-02-04 20:56:40 +00:00
use Sakura\Template;
use Sakura\User;
use Sakura\Users;
/**
* Forum page controllers.
2016-03-08 23:07:58 +00:00
*
2016-02-04 20:56:40 +00:00
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
2016-02-27 16:46:16 +00:00
class ForumController extends Controller
2016-02-04 20:56:40 +00:00
{
/**
* Serves the forum index.
2016-03-08 23:07:58 +00:00
*
2016-02-04 20:56:40 +00:00
* @return mixed HTML for the forum index.
*/
2016-02-14 22:46:07 +00:00
public function index()
2016-02-04 20:56:40 +00:00
{
// Merge index specific stuff with the global render data
Template::vars([
2016-03-10 18:54:36 +00:00
'forum' => (new Forum()),
2016-02-04 20:56:40 +00:00
'stats' => [
2016-02-25 16:06:29 +00:00
'userCount' => DB::table('users')->where('password_algo', '!=', 'disabled')->whereNotIn('rank_main', [1, 10])->count(),
2016-02-04 20:56:40 +00:00
'newestUser' => User::construct(Users::getNewestUserId()),
2016-02-25 16:06:29 +00:00
'lastRegDate' => date_diff(
2016-02-04 20:56:40 +00:00
date_create(date('Y-m-d', User::construct(Users::getNewestUserId())->registered)),
date_create(date('Y-m-d'))
)->format('%a'),
2016-02-25 16:06:29 +00:00
'topicCount' => DB::table('topics')->count(),
'postCount' => DB::table('posts')->count(),
2016-02-04 20:56:40 +00:00
'onlineUsers' => Users::checkAllOnline(),
],
]);
// Return the compiled page
return Template::render('forum/index');
}
2016-02-14 22:46:07 +00:00
public function forum($id = 0)
2016-02-04 20:56:40 +00:00
{
global $currentUser;
// Get the forum
2016-03-10 18:54:36 +00:00
$forum = new Forum($id);
2016-02-04 20:56:40 +00:00
// Redirect forum id 0 to the main page
if ($forum->id === 0) {
2016-03-10 18:54:36 +00:00
header('Location: ' . Router::route('forums.index'));
2016-02-04 20:56:40 +00:00
exit;
}
// Check if the forum exists
if ($forum->id < 0) {
// Set render data
Template::vars([
'page' => [
'message' => 'The forum you tried to access does not exist.',
2016-03-10 18:54:36 +00:00
'redirect' => Router::route('forums.index'),
2016-02-04 20:56:40 +00:00
],
]);
// Print page contents
return Template::render('global/information');
}
// Check if the user has access to the forum
if (!$forum->permission(ForumPerms::VIEW, $currentUser->id)) {
// Set render data
Template::vars([
'page' => [
'message' => 'You do not have access to this forum.',
2016-03-10 18:54:36 +00:00
'redirect' => Router::route('forums.index'),
2016-02-04 20:56:40 +00:00
],
]);
// Print page contents
return Template::render('global/information');
}
// Check if the forum isn't a link
if ($forum->type === 2) {
// Set render data
Template::vars([
'page' => [
'message' => 'The forum you tried to access is a link. You\'re being redirected.',
'redirect' => $forum->link,
2016-03-10 18:54:36 +00:00
],
2016-02-04 20:56:40 +00:00
]);
2016-03-10 18:54:36 +00:00
2016-02-04 20:56:40 +00:00
// Print page contents
return Template::render('global/information');
}
2016-03-10 18:54:36 +00:00
// Set parse variables
Template::vars([
'forum' => $forum,
]);
// Print page contents
return Template::render('forum/viewforum');
}
public function markForumRead($id = 0)
{
global $currentUser;
2016-02-04 20:56:40 +00:00
2016-03-10 18:54:36 +00:00
// Check if the session id was supplied
if (!isset($_GET['s']) || $_GET['s'] != session_id()) {
2016-02-04 20:56:40 +00:00
// Set render data
Template::vars([
'page' => [
2016-03-10 18:54:36 +00:00
'message' => 'Your session expired! Go back and try again.',
'redirect' => Router::route('forums.index'),
],
2016-02-04 20:56:40 +00:00
]);
// Print page contents
return Template::render('global/information');
}
2016-03-10 18:54:36 +00:00
// Get the forum
$forum = new Forum($id);
// Check if the forum exists
if ($forum->id < 1) {
// Set render data
Template::vars([
'page' => [
'message' => 'The forum you tried to access does not exist.',
'redirect' => Router::route('forums.index'),
],
]);
// Print page contents
return Template::render('global/information');
}
// Check if the user has access to the forum
if (!$forum->permission(ForumPerms::VIEW, $currentUser->id)) {
// Set render data
Template::vars([
'page' => [
'message' => 'You do not have access to this forum.',
'redirect' => Router::route('forums.index'),
],
]);
// Print page contents
return Template::render('global/information');
}
// Run the function
$forum->trackUpdateAll($currentUser->id);
// Set render data
Template::vars([
'page' => [
'message' => 'All threads have been marked as read.',
'redirect' => Router::route('forums.forum', $forum->id),
],
]);
// Print page contents
return Template::render('global/information');
}
public function thread($id = 0)
{
global $currentUser;
// Attempt to get the thread
$thread = new Thread($id);
// And attempt to get the forum
$forum = new Forum($thread->forum);
// Check if the forum exists
if ($thread->id == 0 || !$forum->permission(ForumPerms::VIEW, $currentUser->id)) {
// Set render data
Template::vars([
'page' => [
'message' => 'This thread doesn\'t exist or you don\'t have access to it!',
'redirect' => Router::route('forums.index'),
],
]);
// Print page contents
return Template::render('global/information');
}
// Update the tracking status
$thread->trackUpdate($currentUser->id);
// Update views
$thread->viewsUpdate();
2016-02-04 20:56:40 +00:00
// Set parse variables
Template::vars([
2016-03-10 18:54:36 +00:00
'thread' => $thread,
2016-02-04 20:56:40 +00:00
'forum' => $forum,
]);
// Print page contents
2016-03-10 18:54:36 +00:00
return Template::render('forum/viewtopic');
}
public function threadModerate($id = 0)
{
global $currentUser;
// Attempt to get the thread
$thread = new Thread($id);
// And attempt to get the forum
$forum = new Forum($thread->forum);
// Check if the forum exists
if ($thread->id == 0 || !$forum->permission(ForumPerms::VIEW, $currentUser->id)) {
// Set render data
Template::vars([
'page' => [
'message' => 'This thread doesn\'t exist or you don\'t have access to it!',
'redirect' => Router::route('forums.index'),
],
]);
} else {
// Take the action
$action = isset($_POST['action']) ? $_POST['action'] : null;
// Switch
switch ($action) {
default:
Template::vars([
'page' => [
'message' => 'Unknown moderation action.',
'redirect' => Router::route('forums.thread', $thread->id),
],
]);
break;
}
}
// Print page contents
return Template::render('global/information');
2016-02-04 20:56:40 +00:00
}
}