AndyB
Well-known member
I created an addon called StickyAllPages. This add-on puts sticky threads on all pages, not just page 1 as the default XenForo does.
One way which works is to overwrite the entire function like this:
But I know that is not the proper way to do it, I need to use the following lines of code at the top and bottom of the function.
The problem is if I do that the sticky thread only appears on page 1. What am I doing wrong?
Thank you.
One way which works is to overwrite the entire function like this:
PHP:
<?php
class Andy_StickyAllPages_ControllerPublic_Forum extends XFCP_Andy_StickyAllPages_ControllerPublic_Forum
{
public function actionForum()
{
$forumId = $this->_input->filterSingle('node_id', XenForo_Input::UINT);
$forumName = $this->_input->filterSingle('node_name', XenForo_Input::STRING);
$ftpHelper = $this->getHelper('ForumThreadPost');
$forum = $this->getHelper('ForumThreadPost')->assertForumValidAndViewable(
$forumId ? $forumId : $forumName,
$this->_getForumFetchOptions()
);
$forumId = $forum['node_id'];
$visitor = XenForo_Visitor::getInstance();
$threadModel = $this->_getThreadModel();
$forumModel = $this->_getForumModel();
$page = max(1, $this->_input->filterSingle('page', XenForo_Input::UINT));
$threadsPerPage = XenForo_Application::get('options')->discussionsPerPage;
$this->canonicalizeRequestUrl(
XenForo_Link::buildPublicLink('forums', $forum, array('page' => $page))
);
list($defaultOrder, $defaultOrderDirection) = $this->_getDefaultThreadSort($forum);
$order = $this->_input->filterSingle('order', XenForo_Input::STRING, array('default' => $defaultOrder));
$orderDirection = $this->_input->filterSingle('direction', XenForo_Input::STRING, array('default' => $defaultOrderDirection));
$displayConditions = $this->_getDisplayConditions($forum);
$noDateLimit = $this->_input->filterSingle('no_date_limit', XenForo_Input::BOOLEAN);
$isDateLimited = ($forum['list_date_limit_days'] && $order == 'last_post_date' && !$noDateLimit);
if ($isDateLimited)
{
$displayConditions['last_post_date'] = array('>=', XenForo_Application::$time - 86400 * $forum['list_date_limit_days']);
}
$fetchElements = $this->_getThreadFetchElements($forum, $displayConditions);
$threadFetchConditions = $fetchElements['conditions'];
$threadFetchOptions = $fetchElements['options'] + array(
'perPage' => $threadsPerPage,
'page' => $page,
'order' => $order,
'orderDirection' => $orderDirection
);
unset($fetchElements);
$totalThreads = $threadModel->countThreadsInForum($forumId, $threadFetchConditions);
$this->canonicalizePageNumber($page, $threadsPerPage, $totalThreads, 'forums', $forum);
$threads = $threadModel->getThreadsInForum($forumId, $threadFetchConditions, $threadFetchOptions);
if ($page >= 1)
{
$stickyThreadFetchOptions = $threadFetchOptions;
unset($stickyThreadFetchOptions['perPage'], $stickyThreadFetchOptions['page']);
$stickyThreadConditions = $threadFetchConditions;
unset($stickyThreadConditions['last_post_date']);
$stickyThreads = $threadModel->getStickyThreadsInForum($forumId, $stickyThreadConditions, $stickyThreadFetchOptions);
}
else
{
$stickyThreads = array();
}
// prepare all threads for the thread list
$inlineModOptions = array();
$permissions = $visitor->getNodePermissions($forumId);
foreach ($threads AS &$thread)
{
$threadModOptions = $threadModel->addInlineModOptionToThread($thread, $forum, $permissions);
$inlineModOptions += $threadModOptions;
$thread = $threadModel->prepareThread($thread, $forum, $permissions);
}
foreach ($stickyThreads AS &$thread)
{
$threadModOptions = $threadModel->addInlineModOptionToThread($thread, $forum, $permissions);
$inlineModOptions += $threadModOptions;
$thread = $threadModel->prepareThread($thread, $forum, $permissions);
}
unset($thread);
// if we've read everything on the first page of a normal sort order, probably need to mark as read
if ($visitor['user_id'] && $page == 1 && !$displayConditions
&& $order == 'last_post_date' && $orderDirection == 'desc'
&& $forum['forum_read_date'] < $forum['last_post_date']
)
{
$hasNew = false;
foreach ($threads AS $thread)
{
if ($thread['isNew'] && !$thread['isIgnored'])
{
$hasNew = true;
break;
}
}
if (!$hasNew)
{
// everything read, but forum not marked as read. Let's check.
$this->_getForumModel()->markForumReadIfNeeded($forum);
}
}
// get the ordering params set for the header links
$orderParams = array();
foreach ($this->_getThreadSortFields($forum) AS $field)
{
$orderParams[$field] = $displayConditions;
$orderParams[$field]['order'] = ($field != $defaultOrder ? $field : false);
if ($order == $field)
{
$orderParams[$field]['direction'] = ($orderDirection == 'desc' ? 'asc' : 'desc');
}
}
$pageNavParams = $displayConditions;
$pageNavParams['order'] = ($order != $defaultOrder ? $order : false);
$pageNavParams['direction'] = ($orderDirection != $defaultOrderDirection ? $orderDirection : false);
if ($noDateLimit)
{
$pageNavParams['no_date_limit'] = 1;
}
unset($pageNavParams['last_post_date']);
$threadEndOffset = ($page - 1) * $threadsPerPage + count($threads);
$showDateLimitDisabler = ($isDateLimited && $threadEndOffset >= $totalThreads);
$viewParams = array(
'nodeList' => $this->_getNodeModel()->getNodeDataForListDisplay($forum, 0),
'forum' => $forum,
'nodeBreadCrumbs' => $ftpHelper->getNodeBreadCrumbs($forum, false),
'canPostThread' => $forumModel->canPostThreadInForum($forum),
'canSearch' => $visitor->canSearch(),
'canWatchForum' => $forumModel->canWatchForum($forum),
'inlineModOptions' => $inlineModOptions,
'threads' => $threads,
'stickyThreads' => $stickyThreads,
'ignoredNames' => $this->_getIgnoredContentUserNames($threads) + $this->_getIgnoredContentUserNames($stickyThreads),
'order' => $order,
'orderDirection' => $orderDirection,
'orderParams' => $orderParams,
'displayConditions' => $displayConditions,
'pageNavParams' => $pageNavParams,
'page' => $page,
'threadStartOffset' => ($page - 1) * $threadsPerPage + 1,
'threadEndOffset' => $threadEndOffset,
'threadsPerPage' => $threadsPerPage,
'totalThreads' => $totalThreads,
'showPostedNotice' => $this->_input->filterSingle('posted', XenForo_Input::UINT),
'showDateLimitDisabler' => $showDateLimitDisabler
);
return $this->responseView('XenForo_ViewPublic_Forum_View', 'forum_view', $viewParams);
}
}
But I know that is not the proper way to do it, I need to use the following lines of code at the top and bottom of the function.
PHP:
$parent = parent::actionForum();
return $parent;
The problem is if I do that the sticky thread only appears on page 1. What am I doing wrong?
Thank you.