<?php
/**
* Handles listing of topics and subforums in a forum, etc.
*
* @author Nils W.
*
*/
class ForumController extends AppController
{
protected $requireLogin = array('post_topic');
private function check_access($forum)
{
// Find forum and see if it's really a forum, accessible, etc
if($forum == NULL || $forum->is_category)
{
$this->set_flash(TDE_FORUMNOTFOUND, 'Invalid forum number. (#' . $id . ')');
return FALSE;
}
// TODO: passworded forums or for specified users only etc
return TRUE;
}
/**
* Shows the forum index with all categories + (sub)forums collapsed.
*/
function index()
{
$this->title = 'There are over 9000 users online, and 320 of them are guests';
if(!$categories = ObjectCache::fetch('forumindex'))
{
$categories = $this->db->multiple
(
"SELECT id,name,description
FROM tde_forums
WHERE is_category = '1'
ORDER BY orderid ASC"
);
foreach($categories as $category)
{
$category->forums = DiscussionHelper::fetch_forums($category->id, 999);
}
ObjectCache::store('forumindex', $categories);
}
$this->push('categories', $categories);
}
/**
* Lists a forum and all contents.
* @param int $id The ID of the forum to list.
*/
function view($id = 0)
{
// Find forum
$forum = DiscussionHelper::fetch_forum(intval($id));
if(!$this->check_access($forum)) return;
// Set the title of the page and add a breadcrumb <3
$this->title = $forum->name;
$this->push('forum', $forum);
if($forum->parent_id != -1)
{
$parent = DiscussionHelper::fetch_forum($forum->parent_id);
if(!$parent->is_category)
{
$this->push('parent', $parent);
$this->breadcrumb($parent->name, mkurl('forum', $parent->id));
}
}
$this->breadcrumb($forum->name);
// Fetch subforums (only show these at 0 depth)
$this->push('subforums', DiscussionHelper::fetch_forums($forum->id), 0);
// Fetch topics
$this->push('topics', $this->db->multiple
(
"SELECT
t.id AS id,
t.title AS title,
t.icon AS icon,
t.closed AS closed,
t.sticky AS sticky,
t.views AS views,
(SELECT COUNT(0)-1 FROM tde_posts AS p WHERE p.topic_id = t.id) AS posts,
t.lastpost_id AS lastpost_id,
t.lastpost_time AS lastpost_time,
u.id AS starter_id,
u.username AS starter,
(SELECT username FROM users WHERE id = t.lastpost_user_id) AS lastpost_user
FROM tde_topics AS t
LEFT JOIN users AS u ON u.id = t.user_id
WHERE t.forum_id = '" . $id . "'
ORDER BY t.sticky DESC,t.closed ASC,t.lastpost_time DESC"
));
}
/**
* Opens a new topic in this forum.
* @param The ID of the forum to post the topic in.
*/
function post_topic($forum_id = 0)
{
// Find out forum
$forum = DiscussionHelper::fetch_forum(intval($forum_id));
if(!$this->accesscheck($forum)) return;
// Add breadcrumbs
$this->title = 'Start new topic';
$this->breadcrumb($forum->name, mkurl('forum', $forum->id));
$this->breadcrumb('Start new topic');
// Push the id to the view
$this->push('forum_id', $forum->id);
// Exit here when this is no postback
if(!$this->form->is_postback())
{
return;
}
// Get topic data
$title = $this->form->title;
$posticon = intval($this->form->pi);
$text = $this->form->text;
if(strlen($text) < 2 || strlen($title) < 2)
{
$this->set_flash(3283, 'Invalid title/text for new topic.', TRUE);
return;
}
// Store the new topic info
$now = tun_now();
$topic_id = $this->db->insert('tde_topics', array
(
'forum_id' => $forum->id,
'user_id' => $this->session->user->id,
'title' => $title,
'icon' => $posticon,
'timestamp' => $now,
'lastpost_time' => $now,
));
// Store the topic content as the 'OP' and increment postcount of user
$post_id = DiscussionHelper::create_post($topic_id, $this->session->user->id, $posticon, $text);
// Topic done, redirect to the forum!
$this->set_flash(TDE_POSTTOPIC_OK, 'Your topic has been posted! You are being redirected to it\'s forum now...', FALSE, mkurl('forum', $forum->id));
}
}
?>