$topicId],
['moved_to' => $topicId]
];
DB::table('topics')
->whereAnyIs($whereDeleteTopic)
->deleteMany();
// Delete posts in topic
DB::table('posts')
->where('topic_id', $topicId)
->deleteMany();
// Delete any subscriptions for this topic
DB::table('topic_subscriptions')
->where('topic_id', $topicId)
->deleteMany();
}
// Redirect to a post in particular
public function redirectToPost($postId)
{
$postId = Hooks::fire('model.topic.redirect_to_post', $postId);
$result['select'] = ['topic_id', 'posted'];
$result = DB::table('posts')
->selectMany($result['select'])
->where('id', $postId);
$result = Hooks::fireDB('model.topic.redirect_to_post_query', $result);
$result = $result->findOne();
if (!$result) {
throw new Error(__('Bad request'), 404);
}
$post['topic_id'] = $result['topic_id'];
$posted = $result['posted'];
// Determine on which page the post is located (depending on $forumUser['disp_posts'])
$numPosts = DB::table('posts')
->where('topic_id', $post['topic_id'])
->whereLt('posted', $posted)
->count('id');
$numPosts = Hooks::fire('model.topic.redirect_to_post_num', $numPosts);
$post['get_p'] = ceil(($numPosts + 1) / User::getPref('disp.posts'));
$post = Hooks::fire('model.topic.redirect_to_post', $post);
return $post;
}
// Redirect to new posts or last post
public function handleActions($topicId, $topicSubject, $action)
{
$action = Hooks::fire('model.topic.handle_actions_start', $action, $topicId);
// If action=new, we redirect to the first new post (if any)
if ($action == 'new') {
if (!User::get()->is_guest) {
// We need to check if this topic has been viewed recently by the user
$trackedTopics = Track::getTrackedTopics();
$lastViewed = isset($trackedTopics['topics'][$topicId]) ? $trackedTopics['topics'][$topicId] : User::get()->last_visit;
$firstNewPostId = DB::table('posts')
->where('topic_id', $topicId)
->whereGt('posted', $lastViewed)
->min('id');
$firstNewPostId = Hooks::fire('model.topic.handle_actions_first_new', $firstNewPostId);
if ($firstNewPostId) {
return Router::redirect(Router::pathFor('viewPost', ['id' => $topicId, 'name' => $topicSubject, 'pid' => $firstNewPostId]).'#p'.$firstNewPostId);
}
}
// If there is no new post, we go to the last post
$action = 'last';
}
// If action=last, we redirect to the last post
if ($action == 'last') {
$lastPostId = DB::table('posts')
->where('topic_id', $topicId)
->max('id');
$lastPostId = Hooks::fire('model.topic.handle_actions_last_post', $lastPostId);
if ($lastPostId) {
return Router::redirect(Router::pathFor('viewPost', ['id' => $topicId, 'name' => $topicSubject, 'pid' => $lastPostId]).'#p'.$lastPostId);
}
}
Hooks::fire('model.topic.handle_actions', $action, $topicId);
}
// Gets some info about the topic
public function getInfoTopic($id)
{
$curTopic['where'] = [
['fp.read_forum' => 'IS NULL'],
['fp.read_forum' => '1']
];
if (!User::get()->is_guest) {
$selectGetInfoTopic = ['t.subject', 't.closed', 't.num_replies', 't.sticky', 't.first_post_id', 'forum_id' => 'f.id', 'f.forum_name', 'f.moderators', 'fp.post_replies', 'is_subscribed' => 's.user_id'];
$curTopic = DB::table('topics')
->tableAlias('t')
->selectMany($selectGetInfoTopic)
->innerJoin('forums', ['f.id', '=', 't.forum_id'], 'f')
->leftOuterJoin('topic_subscriptions', 't.id=s.topic_id AND s.user_id='.User::get()->id, 's')
->leftOuterJoin('forum_perms', 'fp.forum_id=f.id AND fp.group_id='.User::get()->g_id, 'fp')
->whereAnyIs($curTopic['where'])
->where('t.id', $id)
->whereNull('t.moved_to');
} else {
$selectGetInfoTopic = ['t.subject', 't.closed', 't.num_replies', 't.sticky', 't.first_post_id', 'forum_id' => 'f.id', 'f.forum_name', 'f.moderators', 'fp.post_replies'];
$curTopic = DB::table('topics')
->tableAlias('t')
->selectMany($selectGetInfoTopic)
->selectExpr(0, 'is_subscribed')
->innerJoin('forums', ['f.id', '=', 't.forum_id'], 'f')
->leftOuterJoin('forum_perms', 'fp.forum_id=f.id AND fp.group_id='.User::get()->g_id, 'fp')
->whereAnyIs($curTopic['where'])
->where('t.id', $id)
->whereNull('t.moved_to');
}
$curTopic = Hooks::fireDB('model.topic.get_info_topic_query', $curTopic);
$curTopic = $curTopic->findOne();
if (!$curTopic) {
throw new Error(__('Bad request'), 404);
}
$curTopic = Hooks::fire('model.topic.get_info_topic', $curTopic);
return $curTopic;
}
// Generates the post link
public function postLink($topicId, $closed, $postReplies, $isAdmmod)
{
$closed = Hooks::fire('model.topic.get_post_link_start', $closed, $topicId, $postReplies, $isAdmmod);
if ($closed == '0') {
if (($postReplies == '' && User::can('topic.reply')) || $postReplies == '1' || $isAdmmod) {
$postLink = "\t\t\t".'
'.__('Post reply').'
'."\n";
} else {
$postLink = '';
}
} else {
$postLink = __('Topic closed');
if ($isAdmmod) {
$postLink .= ' / '.__('Post reply').'';
}
$postLink = "\t\t\t".''.$postLink.'
'."\n";
}
$postLink = Hooks::fire('model.topic.get_post_link_start', $postLink, $topicId, $closed, $postReplies, $isAdmmod);
return $postLink;
}
// Should we display the quickpost?
public function isQuickpost($postReplies, $closed, $isAdmmod)
{
$quickpost = false;
if (ForumSettings::get('o_quickpost') == '1' && ($postReplies == '1' || ($postReplies == '' && User::can('topic.reply'))) && ($closed == '0' || $isAdmmod)) {
$quickpost = true;
}
$quickpost = Hooks::fire('model.topic.is_quickpost', $quickpost, $postReplies, $closed, $isAdmmod);
return $quickpost;
}
public function subscribe($topicId)
{
$topicId = Hooks::fire('model.topic.subscribe_topic_start', $topicId);
if (ForumSettings::get('o_topic_subscriptions') != '1') {
throw new Error(__('No permission'), 403);
}
// Make sure the user can view the topic
$authorized['where'] = [
['fp.read_forum' => 'IS NULL'],
['fp.read_forum' => '1']
];
$authorized = DB::table('topics')
->tableAlias('t')
->leftOuterJoin('forum_perms', 'fp.forum_id=t.forum_id AND fp.group_id='.User::get()->g_id, 'fp')
->whereAnyIs($authorized['where'])
->where('t.id', $topicId)
->whereNull('t.moved_to');
$authorized = Hooks::fireDB('model.topic.subscribe_topic_authorized_query', $authorized);
$authorized = $authorized->findOne();
if (!$authorized) {
throw new Error(__('Bad request'), 404);
}
$isSubscribed = DB::table('topic_subscriptions')
->where('user_id', User::get()->id)
->where('topic_id', $topicId);
$isSubscribed = Hooks::fireDB('model.topic.subscribe_topic_is_subscribed_query', $isSubscribed);
$isSubscribed = $isSubscribed->findOne();
if ($isSubscribed) {
throw new Error(__('Already subscribed topic'), 400);
}
$subscription['insert'] = [
'user_id' => User::get()->id,
'topic_id' => $topicId
];
// Insert the subscription
$subscription = DB::table('topic_subscriptions')
->create()
->set($subscription['insert']);
$subscription = Hooks::fireDB('model.topic.subscribe_topic_query', $subscription);
$subscription = $subscription->save();
return $subscription;
}
public function unsubscribe($topicId)
{
$topicId = Hooks::fire('model.topic.unsubscribe_topic_start', $topicId);
if (ForumSettings::get('o_topic_subscriptions') != '1') {
throw new Error(__('No permission'), 403);
}
$isSubscribed = DB::table('topic_subscriptions')
->where('user_id', User::get()->id)
->where('topic_id', $topicId);
$isSubscribed = Hooks::fireDB('model.topic.unsubscribe_topic_subscribed_query', $isSubscribed);
$isSubscribed = $isSubscribed->findOne();
if (!$isSubscribed) {
throw new Error(__('Not subscribed topic'), 400);
}
// Delete the subscription
$delete = DB::table('topic_subscriptions')
->where('user_id', User::get()->id)
->where('topic_id', $topicId);
$delete = Hooks::fireDB('model.topic.unsubscribe_topic_query', $delete);
$delete = $delete->deleteMany();
return $delete;
}
// Subscraction link
public function getSubscraction($isSubscribed, $topicId, $topicSubject)
{
if (!User::get()->is_guest && ForumSettings::get('o_topic_subscriptions') == '1') {
if ($isSubscribed) {
// I apologize for the variable naming here. It's a mix of subscription and action I guess :-)
$subscraction = "\t\t".''.__('Is subscribed').' - '.__('Unsubscribe').'
'."\n";
} else {
$subscraction = "\t\t".''.__('Subscribe').'
'."\n";
}
} else {
$subscraction = '';
}
$subscraction = Hooks::fire('model.topic.get_subscraction', $subscraction, $isSubscribed, $topicId, $topicSubject);
return $subscraction;
}
public function setSticky($id, $value)
{
$sticky = DB::table('topics')
->findOne($id)
->set('sticky', $value);
$sticky = Hooks::fireDB('model.topic.stick_topic', $sticky);
$sticky->save();
return $sticky;
}
public function setClosed($id, $value)
{
$closed = DB::table('topics')
->findOne($id)
->set('closed', $value);
$closed = Hooks::fireDB('model.topic.stick_topic', $closed);
$closed->save();
return $closed;
}
public function checkMove()
{
Hooks::fire('model.topic.check_move_possible_start');
$result['select'] = ['cid' => 'c.id', 'c.cat_name', 'fid' => 'f.id', 'f.forum_name'];
$result['where'] = [
['fp.post_topics' => 'IS NULL'],
['fp.post_topics' => '1']
];
$result['order_by'] = ['c.disp_position', 'c.id', 'f.disp_position'];
$result = DB::table('categories')
->tableAlias('c')
->selectMany($result['select'])
->innerJoin('forums', ['c.id', '=', 'f.cat_id'], 'f')
->leftOuterJoin('forum_perms', 'fp.forum_id=f.id AND fp.group_id='.User::get()->g_id, 'fp')
->whereAnyIs($result['where'])
->whereNull('f.redirect_url')
->orderByMany($result['order_by']);
$result = Hooks::fireDB('model.topic.check_move_possible', $result);
$result = $result->findMany();
if (count($result) < 2) {
return false;
}
return true;
}
public function getForumListMove($fid)
{
$output = '';
$selectGetForumListMove = ['cid' => 'c.id', 'c.cat_name', 'fid' => 'f.id', 'f.forum_name'];
$whereGetForumListMove = [
['fp.post_topics' => 'IS NULL'],
['fp.post_topics' => '1']
];
$orderByGetForumListMove = ['c.disp_position', 'c.id', 'f.disp_position'];
$result = DB::table('categories')
->tableAlias('c')
->selectMany($selectGetForumListMove)
->innerJoin('forums', ['c.id', '=', 'f.cat_id'], 'f')
->leftOuterJoin('forum_perms', 'fp.forum_id=f.id AND fp.group_id='.User::get()->g_id, 'fp')
->whereAnyIs($whereGetForumListMove)
->whereNull('f.redirect_url')
->orderByMany($orderByGetForumListMove);
$result = Hooks::fireDB('model.topic.get_forum_list_move_query', $result);
$result = $result->findResultSet();
$curCategory = 0;
foreach ($result as $curForum) {
if ($curForum->fid != $fid) {
if ($curForum->cid != $curCategory) {
// A new category since last iteration?
if ($curCategory) {
$output .= "\t\t\t\t\t\t\t".''."\n";
}
$output .= "\t\t\t\t\t\t\t".''."\n";
}
$output .= "\t\t\t\t\t\t\t".'