<?php
/**
* This plugin depends on XThreads.
* This plugin uses Control Object Function.
* The XThreads and Control Object Function is coded by:
*
* 		Yumi/ZiNgA BuRgA at http://mybbhacks.zingaburga.com
*
* XThreads official release thread:
*
*		http://mybbhacks.zingaburga.com/showthread.php?tid=288
*/

if(!defined('IN_MYBB')){
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function mnxt_fn_av_info(){
	return array(
		'name'			=> 'Format Name And Avatar In Thread List - XThreads Plugin',
		'description'	=> 'Displaying formatted username and avatar in threadlist.',
		'website'		=> 'http://mybbhacks.zingaburga.com/member.php?action=profile&amp;uid=171',
		'author'		=> 'My Nie',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com/member.php?action=profile&amp;uid=171',
		'version'		=> '1.1',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function mnxt_fn_av_activate(){}
function mnxt_fn_av_deactivate(){}

$plugins->add_hook('forumdisplay_start', 'mnxt_fn_av_run');
function mnxt_fn_av_run(){
	control_object($GLOBALS['db'], '
		function query($string, $hide_errors=0, $write_query=0) {
			static $done=false;
			if(!$done && !$write_query && strpos($string, \'t.username AS threadusername, u.username\')) {
				$done = true;
				$string = strtr($string, array(
					\'t.username AS threadusername, u.username\' => \'t.username AS threadusername, u.username, u.avatar, lp.avatar AS lpavatar\',
					\'WHERE t.fid=\' => \'LEFT JOIN '.TABLE_PREFIX.'users lp ON (lp.uid=t.lastposteruid) WHERE t.fid=\'
				));
			}
			return parent::query($string, $hide_errors, $write_query);
		}
	');
}

$plugins->add_hook('forumdisplay_thread', 'mnxt_fn_av_thread_run');
function mnxt_fn_av_thread_run(){
	if($GLOBALS['thread']['avatar']){
		$GLOBALS['thread']['avatar'] = htmlspecialchars_uni($GLOBALS['thread']['avatar']);
	}else{
		$GLOBALS['thread']['avatar'] = 'images/default_avatar.gif';
	}
	if($GLOBALS['thread']['lpavatar']){
		$GLOBALS['thread']['lpavatar'] = htmlspecialchars_uni($GLOBALS['thread']['lpavatar']);
	}else{
		$GLOBALS['thread']['lpavatar'] = 'images/default_avatar.gif';
	}
}
?>