<?php
class Andy_SimilarThreads_ControllerPublic_Thread extends XFCP_Andy_SimilarThreads_ControllerPublic_Thread
{
public function actionIndex()
{
// get options from Admin CP -> Options -> Similar Threads -> Show on Thread View
$showThreadView = XenForo_Application::get('options')->showThreadView;
// show similar threads if true
if ($showThreadView)
{
//########################################
// start default xenforo code
//########################################
$threadId = $this->_input->filterSingle('thread_id', XenForo_Input::UINT);
$ftpHelper = $this->getHelper('ForumThreadPost');
list($threadFetchOptions, $forumFetchOptions) = $this->_getThreadForumFetchOptions();
list($thread, $forum) = $ftpHelper->assertThreadValidAndViewable($threadId, $threadFetchOptions, $forumFetchOptions);
$visitor = XenForo_Visitor::getInstance();
$threadModel = $this->_getThreadModel();
$postModel = $this->_getPostModel();
if ($threadModel->isRedirect($thread))
{
$redirect = $this->getModelFromCache('XenForo_Model_ThreadRedirect')->getThreadRedirectById($thread['thread_id']);
if (!$redirect)
{
return $this->responseNoPermission();
}
else
{
return $this->responseRedirect(
XenForo_ControllerResponse_Redirect::RESOURCE_CANONICAL_PERMANENT,
$redirect['target_url']
);
}
}
$page = max(1, $this->_input->filterSingle('page', XenForo_Input::UINT));
$postsPerPage = XenForo_Application::get('options')->messagesPerPage;
$this->canonicalizePageNumber($page, $postsPerPage, $thread['reply_count'] + 1, 'threads', $thread);
$this->canonicalizeRequestUrl(
XenForo_Link::buildPublicLink('threads', $thread, array('page' => $page))
);
$postFetchOptions = $this->_getPostFetchOptions($thread, $forum);
$postFetchOptions += array(
'perPage' => $postsPerPage,
'page' => $page
);
$posts = $postModel->getPostsInThread($threadId, $postFetchOptions);
// TODO: add a sanity check to ensure we got posts (invalid thread if page 1, invalid page otherwise)
$posts = $postModel->getAndMergeAttachmentsIntoPosts($posts);
$inlineModOptions = array();
$maxPostDate = 0;
$firstUnreadPostId = 0;
$deletedPosts = 0;
$moderatedPosts = 0;
$pagePosition = 0;
$permissions = $visitor->getNodePermissions($thread['node_id']);
foreach ($posts AS &$post)
{
$post['position_on_page'] = ++$pagePosition;
$postModOptions = $postModel->addInlineModOptionToPost(
$post, $thread, $forum, $permissions
);
$inlineModOptions += $postModOptions;
$post = $postModel->preparePost($post, $thread, $forum, $permissions);
if ($post['post_date'] > $maxPostDate)
{
$maxPostDate = $post['post_date'];
}
if ($post['isDeleted'])
{
$deletedPosts++;
}
if ($post['isModerated'])
{
$moderatedPosts++;
}
if (!$firstUnreadPostId && $post['isNew'])
{
$firstUnreadPostId = $post['post_id'];
}
}
if ($firstUnreadPostId)
{
$requestPaths = XenForo_Application::get('requestPaths');
$unreadLink = $requestPaths['requestUri'] . '#post-' . $firstUnreadPostId;
}
else if ($thread['isNew'])
{
$unreadLink = XenForo_Link::buildPublicLink('threads/unread', $thread);
}
else
{
$unreadLink = '';
}
$attachmentHash = null;
if (!empty($thread['draft_extra']))
{
$draftExtra = @unserialize($thread['draft_extra']);
if (!empty($draftExtra['attachment_hash']))
{
$attachmentHash = $draftExtra['attachment_hash'];
}
}
$attachmentParams = $this->_getForumModel()->getAttachmentParams($forum, array(
'thread_id' => $thread['thread_id']
), null, null, $attachmentHash);
if ($thread['discussion_type'] == 'poll')
{
$pollModel = $this->_getPollModel();
$poll = $pollModel->getPollByContent('thread', $threadId);
if ($poll)
{
$poll = $pollModel->preparePoll($poll, $threadModel->canVoteOnPoll($thread, $forum));
$poll['canEdit'] = $threadModel->canEditPoll($thread, $forum);
}
}
else
{
$poll = false;
}
$threadModel->markThreadRead($thread, $forum, $maxPostDate);
$threadModel->logThreadView($threadId);
//########################################
// start add-on code
//########################################
// declare variables
$searchWord = '';
$searchWords = array();
$safeSearchWord = '';
// get thread title
$parent = parent::actionIndex();
if ($parent instanceof XenForo_ControllerResponse_View)
{
$threadTitle = $parent->params['thread']['title'];
}
// put into array
$threadTitle = explode(' ', $threadTitle);
// get options from Admin CP -> Options -> Similar Threads -> Common Words
$commonWords = XenForo_Application::get('options')->commonWords;
// convert to lowercase
$commonWords = strtolower($commonWords);
// put $commonWordsLower into an array
$commonWords = explode(' ', $commonWords);
// remove any common words from array
foreach($threadTitle as $var)
{
if (!in_array(strtolower($var), $commonWords))
{
$searchWords[] = $var;
}
}
$count = count($searchWords);
// only continue if we have a search word
if ($count > 0)
{
// get first none common word
$searchWord = $searchWords[0];
// make safe for database
$safeSearchWord = addslashes($searchWord);
}
// run query only if we have a search
if ($safeSearchWord != '')
{
// run query in model
$threads = $this->getModelFromCache('Andy_SimilarThreads_Model')->getThreads($safeSearchWord,$threadId);
}
else
{
// $viewParams needs to be an array
$threads = array();
}
//########################################
// end add-on code
//########################################
$viewParams = $this->_getDefaultViewParams($forum, $thread, $posts, $page, array(
'deletedPosts' => $deletedPosts,
'moderatedPosts' => $moderatedPosts,
'inlineModOptions' => $inlineModOptions,
'firstPost' => reset($posts),
'lastPost' => end($posts),
'unreadLink' => $unreadLink,
'poll' => $poll,
'attachmentParams' => $attachmentParams,
'attachmentConstraints' => $this->_getAttachmentModel()->getAttachmentConstraints(),
'showPostedNotice' => $this->_input->filterSingle('posted', XenForo_Input::UINT),
'nodeBreadCrumbs' => $ftpHelper->getNodeBreadCrumbs($forum),
// add-on variables added here
'threads' => $threads,
'searchWord' => $searchWord,
));
return $this->responseView('XenForo_ViewPublic_Thread_View', 'thread_view', $viewParams);
}
else
{
// run the original actionIndex() function
return parent::actionIndex();
}
}
}
?>