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/Forums.php

136 lines
3.8 KiB
PHP
Raw Normal View History

2016-02-04 20:56:40 +00:00
<?php
/**
* Holds the forum pages controllers.
*
* @package Sakura
*/
namespace Sakura\Controllers;
use Sakura\Config;
2016-02-18 23:28:44 +00:00
use Sakura\DB;
2016-02-25 16:06:29 +00:00
use Sakura\DBv2;
2016-02-04 20:56:40 +00:00
use Sakura\Forum;
use Sakura\Perms\Forum as ForumPerms;
use Sakura\Template;
use Sakura\User;
use Sakura\Users;
use Sakura\Utils;
/**
* Forum page controllers.
*
* @package Sakura
* @author Julian van de Groep <me@flash.moe>
*/
2016-02-15 21:20:46 +00:00
class Forums extends Controller
2016-02-04 20:56:40 +00:00
{
/**
* Serves the forum index.
*
* @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([
'forum' => (new Forum\Forum()),
'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
$forum = new Forum\Forum($id);
// Redirect forum id 0 to the main page
if ($forum->id === 0) {
header('Location: ' . (new \Sakura\Urls)->format('FORUM_INDEX'));
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.',
],
]);
// 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.',
],
]);
// 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,
]
]);
// Print page contents
return Template::render('global/information');
}
// Check if we're marking as read
if (isset($_GET['read']) && $_GET['read'] && isset($_GET['session']) && $_GET['session'] == session_id()) {
// Run the function
$forum->trackUpdateAll($currentUser->id);
// Set render data
Template::vars([
'page' => [
'message' => 'All threads have been marked as read.',
'redirect' => (new \Sakura\Urls)->format('FORUM_SUB', [$forum->id]),
]
]);
// Print page contents
return Template::render('global/information');
}
$renderData['forum'] = $forum;
// Set parse variables
Template::vars([
'forum' => $forum,
]);
// Print page contents
return Template::render('forum/viewforum');
}
}