<?php

if(!defined('IN_MYBB')){
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function xtdispportal_info(){
	return array(
		'name'			=> 'Thread In Portal',
		'description'	=> 'Display selected thread title in portal',
		'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.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function xtdispportal_activate(){
	global $db;
	$template = array(
		'title'		=> 'xtdispportal_threads',
		'template'	=> $db->escape_string('<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr>
		<td class="thead">
			<strong>{$xtdispportal_threads_num} Featured Threads</strong>
		</td>
	</tr>
	{$xtdispportal_threads_thread}
</table>'),
		'sid'		=> -1
	);
	$db->insert_query('templates', $template);
	$template = array(
		'title'		=> 'xtdispportal_threads_thread',
		'template'	=> $db->escape_string('<tr>
	<td class="{$altbg}">
		<div>
			{$xtdispportal_thread_icon}
			<strong><a href="{$xtdispportal_thread_link}">{$xtdispportal_thread_subject}</a></strong>
			By: {$xtdispportal_profile_link}
		</div>
		<br class="clear" />
		<div class="post_body">
			{$xtdispportal_postpreview}
		</div>
		<div class="smalltext">
			<em>Posted In: <a href="{$xtdispportal_forum_link}">{$xtdispportal_forum_name}</a>,
			{$xtdispportal_date} at {$xtdispportal_time}<br />
			Replies: {$xtdispportal_replies}, Views: {$xtdispportal_views}</em>
		</div>
	</td>
</tr>'),
		'sid'		=> -1
	);
	$db->insert_query('templates', $template);
}

function xtdispportal_deactivate(){
	$GLOBALS['db']->delete_query('templates', 'title IN("xtdispportal_threads", "xtdispportal_threads_thread")');
}

$plugins->add_hook('global_start', 'xtdispportal_tcache');
function xtdispportal_tcache(){
	if(THIS_SCRIPT == 'portal.php'){
		if(isset($GLOBALS['templatelist'])){
			$GLOBALS['templatelist'] .= ',xtdispportal_threads,xtdispportal_threads_thread';
		}
	}
}

$plugins->add_hook('portal_start', 'xtdispportal_run');
function xtdispportal_run(){
	global $db;
	$xtdispportal_unviewable = get_unviewable_forums(true);
	if($xtdispportal_unviewable){
		$xtdispportal_unviewwhere = ' AND t.fid NOT IN ('.$xtdispportal_unviewable.')';
	}
	$altbg = alt_trow();
	$xtdispportal_icon_cache = $GLOBALS['cache']->read('posticons');
	$query = $db->query('
		SELECT t.*, t.username AS tusername, u.username, u.avatar, u.usergroup, u.displaygroup, f.name, a.updatetime, a.md5hash, a.uploadtime, a.aid, a.attachname, a.filename
		FROM '.TABLE_PREFIX.'threads t
		LEFT JOIN '.TABLE_PREFIX.'users u ON (u.uid=t.uid)
		LEFT JOIN '.TABLE_PREFIX.'threadfields_data td ON (td.tid=t.tid)
		LEFT JOIN '.TABLE_PREFIX.'xtattachments a ON (a.aid=td.img)
		LEFT JOIN '.TABLE_PREFIX.'forums f ON (f.fid=t.fid)
		WHERE t.visible=1 AND td.xtdispportal="Yes" AND t.closed NOT LIKE "moved|%"'.$xtdispportal_unviewwhere.'
		ORDER BY t.dateline desc
	');
	if($db->num_rows($query)){
		global $xtdispportal_threads, $theme;
		$xtdispportal_threads_num = my_number_format($db->num_rows($query));
		while($xtdispportal = $db->fetch_array($query)){
			$xtdispportal_date = my_date($GLOBALS['mybb']->settings['dateformat'], $xtdispportal['dateline']);
			$xtdispportal_time = my_date($GLOBALS['mybb']->settings['timeformat'], $xtdispportal['dateline']);
			$xtdispportal_thread_subject = htmlspecialchars_uni($GLOBALS['parser']->parse_badwords($xtdispportal['subject']));
			$xtdispportal_thread_link = get_thread_link($xtdispportal['tid']);
			$xtdispportal_forum_name = preg_replace('#&(?!\#[0-9]+;)#si', '&amp;', $xtdispportal['name']);
			$xtdispportal_forum_link = get_forum_link($xtdispportal['fid']);
			$xtdispportal_replies = my_number_format($xtdispportal['replies']);
			$xtdispportal_views = my_number_format($xtdispportal['views']);
			$xtdispportal_img = xthreads_get_xta_url($xtdispportal);
			if($xtdispportal['postpreview']){
				$xtdispportal_postpreview = htmlspecialchars_uni($xtdispportal['postpreview']);
			}
			if($xtdispportal['icon'] > 0 && $xtdispportal_icon_cache[$xtdispportal['icon']]){
				$xtdispportal_thread_icon = $xtdispportal_icon_cache[$xtdispportal['icon']];
				$xtdispportal_thread_icon = '<img src="'.$xtdispportal_thread_icon['path'].'" alt="" title="" />';
			}else{
				$xtdispportal_thread_icon = '';
			}
			if($xtdispportal['uid'] == 0){
				$xtdispportal_profile_link = htmlspecialchars_uni($xtdispportal['tusername']);
			}else{
				$xtdispportal_profile_link = build_profile_link(format_name(htmlspecialchars_uni($xtdispportal['username']), $xtdispportal['usergroup'], $xtdispportal['displaygroup']), $xtdispportal['uid']);
			}
			if($xtdispportal['avatar']){
				$xtdispportal_user_avatar = '<img src="'.htmlspecialchars_uni($xtdispportal['avatar']).'" alt="" title="" width="45" height="45" />';
			}
			eval('$xtdispportal_threads_thread .= "'.$GLOBALS['templates']->get('xtdispportal_threads_thread').'";');
			$altbg = alt_trow();
		}
		eval('$xtdispportal_threads = "'.$GLOBALS['templates']->get('xtdispportal_threads').'";');
	}
}
?>