<?php

# MODxBB * LatestTopicsBB * by N!Ce (A.S.) April, 2011
# Version 0.9.4
# Featuring phpBB KB & Forum resources
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::



define('IN_PHPBB', true);
global $user, $auth, $db, $phpEx, $modx;
$phpbb_root_path = './forum/';
include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);



// open block add headline
echo '<div class="latestposts"><h4>Latest Forum Posts</h4>';

// configuration
$num_posts = 5;        // number of total posts to display
$num_chars = 193;    // number of characters per post
$DefaultAvatar = 'assets/images/NoAvatar.png'; // URL to your default avatar

// trim text to a certain length
function phpbb_trim_text(&$text, &$is_trimmed, $number)
{
	if ($number != 0 and strlen($text) > $number)
	{
		$text       = substr($text, 0, $number);
		$is_trimmed = true;
	}

	return true;
}

// get the forums the user can read from
$can_read_forum = $auth->acl_getf('f_read');
$forums_auth_ary = array();
foreach($can_read_forum as $key => $forum)
{
	if ($forum['f_read'] != 0)
	{
		$forums_auth_ary[] = $key;
	}
}
unset($can_read_forum);

// recent posts
if (sizeof($forums_auth_ary))
{
	$sql = 'SELECT p.post_id, t.topic_id, p.post_subject, p.post_text, p.post_time, u.username
	FROM ' . TOPICS_TABLE . ' t
	INNER JOIN ' . POSTS_TABLE . ' p ON (t.topic_id = p.topic_id)
	INNER JOIN ' . USERS_TABLE . ' u ON (p.poster_id = u.user_id)
	WHERE t.topic_approved = 1
	AND p.post_approved = 1
	AND ' . $db->sql_in_set('p.forum_id', $forums_auth_ary) . '
	ORDER BY p.post_time DESC';

	$result = $db->sql_query_limit($sql, $num_posts);
	$row = $db->sql_fetchrowset($result);

	// check if requested posts exceeds available ones
	if (sizeof($row) < $num_posts)
	{
		$num_posts = sizeof($row);
	}

	for ($i = 0; $i < $num_posts; $i++)
	{
		$post_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", '&amp;t=' . $row[$i]['topic_id'] . '&amp;start=' . $row[$i]['post_id'] . '#p' . $row[$i]['post_id']);

		// prepare text (strip bbcodes and trim it)
		$row[$i]['post_trimmed'] = false;
		strip_bbcode($row[$i]['post_text']);
		phpbb_trim_text($row[$i]['post_text'], $row[$i]['post_trimmed'], $num_chars);

		$post_text = '';
		if ($row[$i]['post_trimmed'])
		{
			$post_text = $row[$i]['post_text'] . '...';
		}
		else
		{
			$post_text = $row[$i]['post_text'];
		}
		// output result
		echo '<div> &bull; <a href="' . $post_url .'">' . $row[$i]['post_subject'] . '</a><br />';

		// grab avatar from DB
		$username = '' . $row[$i][username];
		$sql_array = array();

		$sql = 'SELECT user_avatar
		FROM ' . USERS_TABLE . '
		WHERE username = ' . "'$username'";

		$result2 = $db->sql_query($sql);
		$dset = $db->sql_fetchrow($result2);
		$db->sql_freeresult($result2);

		if ($dset['user_avatar']) {
			if (strstr($dset['user_avatar'], '/')) {
				echo '<img src="forum/images/avatars/gallery/' . $dset['user_avatar'] . '"  alt="AV" />';
			} else {
				echo '<img src="forum/download/file.php?avatar=' . $dset['user_avatar'] . '"  alt="AV" />';
			}
		} else {
			echo '<img  src="' . $DefaultAvatar . '"  alt="AV" />';
		}

		echo '<p>' . $post_text . '</p>'
		. $user->format_date($row[$i]['post_time'], '|D - M d| - G:i') . ' by <strong>' . $row[$i]['username'] . '</strong></div>';
	}

	$db->sql_freeresult($result);
}
echo '</div>';

?>