<?php
/**
* Handles all discussion topic actions.
* @author Nils W.
*
*/
class TopicController extends AppController
{
protected $requireLogin = array('post_reply', 'edit_post');
/**
* Shows the content of a given discussion topic, applying pagination etc.
* @param int $id The topic ID to show.
* @param int $page The current page ID.
* @param int $pageLength The max amount of posts per page.
*/
function view($id = 0, $page = 0, $pageLength = 25)
{
// Find the topic
if(!$topic = DiscussionHelper::fetch_topic(intval($id)))
{
$this->set_flash(TDE_TOPICNOTFOUND, 'Topic #' . $id . ' does not exist.');
return;
}
// Topic not visited before during this session?
if(!in_array($topic->id, $this->session->readTopics))
{
$this->db->exec("UPDATE tde_topics SET views=views + 1 WHERE id = '" . $id . "'");
array_push($this->session->readTopics, $topic->id);
}
// Set page title and breadcrumbs
$this->title = $topic->forum_code . ' / ' . $topic->title;
$this->breadcrumb($topic->forum, mkurl('forum', $topic->forum_id));
$this->breadcrumb($topic->title);
// Push topic + content to view for presentation
$this->push('topic', $topic);
$this->push('posts', $this->db->multiple
(
"SELECT
p.id AS id,
p.icon AS icon,
p.timestamp AS timestamp,
p.text AS text,
p.lastedit AS lastedit,
p.editpct AS editpct,
u.id AS user_id,
u.username AS user_username,
u.subtitle AS user_subtitle,
u.signature AS user_signature,
u.xtag AS user_xtag
FROM tde_posts AS p
JOIN users AS u ON u.id = p.user_id
WHERE p.topic_id = '" . $id . "'"
));
}
/**
* Allows the user to reply to a discussion topic.
* @param int $id the ID of the topic to reply to.
*/
function post_reply($id = 0)
{
// Find the topic and check if it's valid
if(!$topic = DiscussionHelper::fetch_topic(intval($id)))
{
$this->set_flash(TDE_TOPICNOTFOUND, 'Topic #' . $id . ' does not exist, so you cannot reply to it.');
return;
}
if($topic->closed && !false)
{
$this->set_flash(TDE_TOPICCLOSED, 'This topic is closed and you do not have the right to post in closed topics.');
return;
}
// Apply page title and breadcrumbs
$this->title = 'Replying to topic: ' . $topic->title;
$this->breadcrumb($topic->forum, mkurl('forum', $topic->forum_id));
$this->breadcrumb($this->title, mkurl('topic', $topic->id));
// Push topic to view
$this->push('topic', $topic);
// Post submitted?
if($this->form->is_postback())
{
$posticon = intval($this->form->pi);
$text = $this->form->text;
if(strlen($text) < 2)
{
$this->set_flash(TDE_POSTREPLY_ERROR, 'Please provide a useful message!', TRUE);
$this->push('posticon', $posticon);
}
else
{
$post_id = DiscussionHelper::create_post($topic->id, $this->session->user->id, $posticon, $text);
$this->db->update('tde_topics', $topic->id, array
(
'lastpost_id' => $post_id,
'lastpost_user_id' => $this->session->user->id,
'lastpost_time' => tun_now()
));
$this->set_flash(TDE_POSTREPLY_OK, 'Reply added successfully! Redirecting...', FALSE, mkurl('topic', $topic->id, $post_id));
}
}
}
/**
* Allows a user to edit his/her post, or others post when having moderator status.
* @param int $id The ID of the post to edit.
*/
function edit_post($id)
{
// Fetch the post
$post = DiscussionHelper::fetch_post(intval($id));
if($post == NULL || $post->user_id != $this->session->user->id)
{
$this->set_flash(TDE_POSTNOTFOUND, 'Post not found, or you do not have rights to edit this post.');
return;
}
// Update post
$this->db->update('tde_posts', $post->id, array
(
'lastedit_time' => tun_now(),
'lastedit_pct' => 100 // Calculate procentual difference between old text and new text, atleast 1% and max 100%
));
}
}
?>