MyBB Community Forums

Full Version: how to get lastpost(just one lastpost) from whole category instead of it's forum's ?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
By default we are getting the lastpost of forum that comes under a category.

But i want to have the lastpost of whole category . I mean let seeĀ 

A category named 'Discussion' has 5 forums named a,b,c,d and e

what we see in by default :- The category 'Discussion' and under this all the forum names views , replies and it's (forum's) lastpost.

but what i want I will list the forums (a,b,c,d & e) under this category(Discussion) and at the end of the list , beneath the list i will have just one lastpost that filtered from all those forums. The lastpost should be only that one which is most recent .

Is this possible or it already available in MyBB ?
Hook to build_forumbits_forum with the following code :
function _build_forumbits_forum(&$args)
{
	global $parser, $templates, $lang;
	global $fcache, $forumpermissions;
	global $lastPostCache, $catLastPost;

	isset($lastPostCache) || $lastPostCache = [];

	foreach($fcache as $pid => $fcache_p)
	{
		foreach($fcache_p as $disporder => $parent_p)
		{
			foreach($parent_p as $fid => $forum)
			{
				if($forum['type'] != 'f')
				{
					continue;
				}

				$permissions = $forumpermissions[$forum['fid']];

				if(
					!$forum['pid'] ||
					(!$permissions['canview'] && $mybb->settings['hideprivateforums']) ||
					!$permissions['canview'] ||
					(isset($permissions['canviewthreads']) && !$permissions['canviewthreads']) ||
					!forum_password_validated($forum, true)
				)
				{
					continue;
				}

				if(
					!isset($lastPostCache[$forum['pid']]) ||
					$lastPostCache[$forum['pid']]['lastpost'] < $forum['lastpost']
				)
				{
					$lastPostCache[$forum['pid']] = [
						'lastpost' => (int)$forum['lastpost'],
						'lastposter' => htmlspecialchars_uni($forum['lastposter']),
						'lastposteruid' => (int)$forum['lastposteruid'],
						'lastposttid' => (int)$forum['lastposttid'],
						'lastpostsubject' => $forum['lastpostsubject'],
					];
				}
			}
		}
	}

	if($isForumDisplay = (constant('THIS_SCRIPT') == 'forumdisplay.php'))
	{
		global $fid;
	}
	else
	{
		$fid = $args['fid'];
	}

	if(($args['type'] != 'c' && !$isForumDisplay) || !isset($lastPostCache[$fid]))
	{
		return;
	}

	$lastPostData = $lastPostCache[$fid];

	$date = my_date('relative', $lastPostData['lastpost']);

	$profileLink = build_profile_link($lastPostData['lastposter'], $lastPostData['lastposteruid']);

	$threadLink = get_thread_link($lastPostData['lastposttid'], 0, 'lastpost');

	$threadSubject = $threadFullSubject = $parser->parse_badwords($lastPostData['lastpostsubject']);

	if(my_strlen($threadSubject) > 25)
	{
		$threadSubject = my_substr($threadSubject, 0, 25).'...';
	}

	$threadSubject = htmlspecialchars_uni($threadSubject);

	$threadFullSubject = htmlspecialchars_uni($threadFullSubject);

	$catLastPost = eval($templates->render('forumbit_depth1_cat_lastpost'));
}

In your forumbit_depth1_cat template find {$sub_forums} and paste {$GLOBALS['catLastPost']} right after.

Finally, create a template with name forumbit_depth1_cat_lastpost and the following contents :
<tr>
	<td class="trow2" colspan="5">
		<span class="smalltext">
			<a href="{$threadLink}" title="{$threadFullSubject}"><strong>{$threadSubject}</strong></a>
			<br />{$date}<br />{$lang->by} {$profileLink}
		</span>
	</td>
</tr>
Thank you so much Omar for this kind , and if i want to have total view and replies , then ?
Hook to build_forumbits_forum with the following code :
function _build_forumbits_forum(&$args)
{
	global $parser, $templates, $lang;
	global $fcache, $forumpermissions;
	global $lastPostCache, $catLastPost;

	isset($lastPostCache) || $lastPostCache = [];

	foreach($fcache as $pid => $fcache_p)
	{
		foreach($fcache_p as $disporder => $parent_p)
		{
			foreach($parent_p as $fid => $forum)
			{
				if($forum['type'] != 'f')
				{
					continue;
				}

				$permissions = $forumpermissions[$forum['fid']];

				if(
					!$forum['pid'] ||
					(!$permissions['canview'] && $mybb->settings['hideprivateforums']) ||
					!$permissions['canview'] ||
					(isset($permissions['canviewthreads']) && !$permissions['canviewthreads']) ||
					!forum_password_validated($forum, true)
				)
				{
					continue;
				}

				isset($lastPostCache[$forum['pid']]) || $lastPostCache[$forum['pid']] = [
					'lastpost' => 0,
					'lastposter' => '',
					'lastposteruid' => 0,
					'lastposttid' => 0,
					'lastpostsubject' => '',
					'threads' => 0,
					'posts' => 0,
					'unapprovedposts' => 0,
					'unapprovedthreads' => 0,
					'viewers' => 0,
				];

				if($lastPostCache[$forum['pid']]['lastpost'] < $forum['lastpost'])
				{
					$lastPostCache[$forum['pid']]['lastpost'] = (int)$forum['lastpost'];

					$lastPostCache[$forum['pid']]['lastposter'] = htmlspecialchars_uni($forum['lastposter']);

					$lastPostCache[$forum['pid']]['lastposteruid'] = (int)$forum['lastposteruid'];

					$lastPostCache[$forum['pid']]['lastposttid'] = (int)$forum['lastposttid'];

					$lastPostCache[$forum['pid']]['lastpostsubject'] = $forum['lastpostsubject'];
				}

				if(empty($permissions['canonlyviewownthreads']))
				{
					$lastPostCache[$forum['pid']]['threads'] += $forum['threads'];

					$lastPostCache[$forum['pid']]['posts'] += $forum['posts'];

					$lastPostCache[$forum['pid']]['unapprovedposts'] += $forum['unapprovedposts'];

					$lastPostCache[$forum['pid']]['unapprovedthreads'] += $forum['unapprovedthreads'];
	
					if(!empty($forum['viewers']))
					{
						$lastPostCache[$forum['pid']]['viewers'] += $forum['viewers'];
					}
				}
			}
		}
	}

	if($isForumDisplay = (constant('THIS_SCRIPT') == 'forumdisplay.php'))
	{
		global $fid;
	}
	else
	{
		$fid = $args['fid'];
	}

	if(($args['type'] != 'c' && !$isForumDisplay) || !isset($lastPostCache[$fid]))
	{
		return;
	}

	$lastPostData = $lastPostCache[$fid];

	foreach(['threads', 'posts', 'unapprovedposts', 'unapprovedthreads', 'viewers'] as $key)
	{
		${$key} = my_number_format($lastPostData[$key]);
	}

	$date = my_date('relative', $lastPostData['lastpost']);

	$profileLink = build_profile_link($lastPostData['lastposter'], $lastPostData['lastposteruid']);

	$threadLink = get_thread_link($lastPostData['lastposttid'], 0, 'lastpost');

	$threadSubject = $threadFullSubject = $parser->parse_badwords($lastPostData['lastpostsubject']);

	if(my_strlen($threadSubject) > 25)
	{
		$threadSubject = my_substr($threadSubject, 0, 25).'...';
	}

	$threadSubject = htmlspecialchars_uni($threadSubject);

	$threadFullSubject = htmlspecialchars_uni($threadFullSubject);

	$catLastPost = eval($templates->render('forumbit_depth1_cat_lastpost'));
}

In your forumbit_depth1_cat template find {$sub_forums} and paste {$GLOBALS['catLastPost']} right after.

Finally, create a template with name forumbit_depth1_cat_lastpost and the following contents :
<tr>
	<td class="trow2" colspan="5">
		<div class="float_right">
			Threads: {$threads} ({$unapprovedposts})<br />
			Posts: {$posts} ({$unapprovedthreads})<br />
			Viewers: {$viewers}<br />
		</div>
		<span class="smalltext">
			<a href="{$threadLink}" title="{$threadFullSubject}"><strong>{$threadSubject}</strong></a>
			<br />{$date}<br />{$lang->by} {$profileLink}
		</span>
	</td>
</tr>

(This is an extended version of the above code.)
is this valid that the guests can also seen the numbers of unapproved threads and posts ? Because with this hook and help of @Laird i managed to did this in my forum. everything shows correct but with that i notice ; guest and members can also view the number of unapproved threads and posts.

Also i am using lastpost avatar plugin , does the same variable works for represent the avatar of lastposter as it was for the lastposter of a forum ?
(2021-09-15, 09:55 AM)PARADOXP Wrote: [ -> ]is this valid that the guests can also seen the numbers of unapproved threads and posts ?

That is probably undesired effect, but you can edit the template to remove them. My code would need to be improved to take this into account, but I avoided so as to avoid adding more complexity (templates) to it.
https://github.com/mybb/mybb/blob/a9054f...#L237-L242

(2021-09-15, 09:55 AM)PARADOXP Wrote: [ -> ]Also i am using lastpost avatar plugin , does the same variable works for represent the avatar of lastposter as it was for the lastposter of a forum ?

No, my code doesn't take this into account, you would need to improve it to accomplish that.
Thank you @Omar for all of this. Thank you so much.