<?php

if(!defined('IN_MYBB')){
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function xtldfeat_info(){
	return array(
		'name'			=> 'Selected Threads Title On index',
		'description'	=> 'Displaying selected threads title on index',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.1',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function xtldfeat_activate(){
	if(!function_exists('xthreads_gettfcache')){
		global $mybb;
		$sep = $mybb->version_code >= 1500 ? '-':'/';
		flash_message('This plugin required XThreads to be installed or actived', 'error');
		admin_redirect('index.php?module=config'.$sep.'plugins');
	}
	global $db;
	$db->insert_query('templates', array(
		'title'		=> 'xtldfeat_threads',
		'template'	=> $db->escape_string('<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr>
		<td class="thead">
			<strong>{$xtldfeat_threads_num} Featured Threads</strong>
		</td>
	</tr>
	{$xtldfeat_threads_thread}
</table>'),
		'sid'		=> -1
	));
	$db->insert_query('templates', array(
		'title'		=> 'xtldfeat_threads_thread',
		'template'	=> $db->escape_string('<tr>
	<td class="{$altbg}">
		<div>
			{$xtldfeat_thread_icon}
			<strong><a href="{$xtldfeat_thread_link}">{$xtldfeat_thread_subject}</a></strong>
			By: {$xtldfeat_profile_link}
		</div>
		<br class="clear" />
		<div class="post_body">
			{$xtldfeat_postpreview}
		</div>
		<div class="smalltext">
			<em>Posted In: <a href="{$xtldfeat_forum_link}">{$xtldfeat_forum_name}</a>,
			{$xtldfeat_date} at {$xtldfeat_time}<br />
			Replies: {$xtldfeat_replies}, Views: {$xtldfeat_views}</em>
		</div>
	</td>
</tr>'),
		'sid'		=> -1
	));
}

function xtldfeat_deactivate(){
	global $db;
	$db->delete_query('templates', 'title IN("xtldfeat_threads", "xtldfeat_threads_thread")');
}

$plugins->add_hook('xthreads_uninstall_end', 'xtldfeat_xtdu');
$plugins->add_hook('xthreads_deactivate_end', 'xtldfeat_xtdu');
function xtldfeat_xtdu(){
	global $active_plugins;
	xtldfeat_deactivate();
	unset($active_plugins['xtldfeat']);
}

$plugins->add_hook('global_start', 'xtldfeat_tcache');
function xtldfeat_tcache(){
	if(THIS_SCRIPT == 'index.php'){
		global $templatelist;
		if(isset($templatelist)){
			$templatelist .= ',xtldfeat_threads,xtldfeat_threads_thread';
		}
	}
}

$plugins->add_hook('index_start', 'xtldfeat_run');
function xtldfeat_run(){
	$xtdpf = xthreads_gettfcache();
	if(!empty($xtdpf) && is_array($xtdpf['xtldfeat'])){
		global $db, $cache;
		$xtdpf_fid_in = '';
		if($xtdpf['xtldfeat']['forums']){
			$xtdpf_fids = explode(',',$xtdpf['xtldfeat']['forums']);
			if(is_array($xtdpf_fids)){
				foreach($xtdpf_fids as $xtdpffid){
					$xtdpffids[] = intval($xtdpffid);
				}
				$xtdpf_fid = implode(',',$xtdpffids);
				$xtdpf_fid_in = 't.fid IN ('.$xtdpf_fid.') AND ';
			}
		}
		$xtldfeat_unviewable = get_unviewable_forums(true);
		if($xtldfeat_unviewable){
			$xtldfeat_unviewwhere = ' AND t.fid NOT IN ('.$xtldfeat_unviewable.')';
		}
		$altbg = alt_trow();
		$xtldfeat_icon_cache = $cache->read('posticons');
		$query = $db->query('
			SELECT t.*, t.username AS tusername, u.username, u.avatar, u.usergroup, u.displaygroup
			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)
			WHERE '.$xtdpf_fid_in.'t.visible=1 AND td.xtldfeat="Yes" AND t.closed NOT LIKE "moved|%"'.$xtldfeat_unviewwhere.'
			ORDER BY t.dateline desc
		');
		if($db->num_rows($query)){
			global $xtldfeat_threads, $threadfields, $theme, $mybb, $parser, $templates, $forum_cache;
			$xtldfeat_threads_num = my_number_format($db->num_rows($query));
			while($xtldfeat = $db->fetch_array($query)){
				$xtldfeat_date = my_date($mybb->settings['dateformat'], $xtldfeat['dateline']);
				$xtldfeat_time = my_date($mybb->settings['timeformat'], $xtldfeat['dateline']);
				$xtldfeat_thread_subject = htmlspecialchars_uni($parser->parse_badwords($xtldfeat['subject']));
				$xtldfeat_thread_link = get_thread_link($xtldfeat['tid']);
				$xtldfeat_forum_name = preg_replace('#&(?!\#[0-9]+;)#si', '&amp;', $forum_cache[$xtldfeat['fid']]['name']);
				$xtldfeat_forum_link = get_forum_link($xtldfeat['fid']);
				$xtldfeat_replies = my_number_format($xtldfeat['replies']);
				$xtldfeat_views = my_number_format($xtldfeat['views']);
				$xtldfeat_desc = htmlspecialchars_uni($xtldfeat['threadfields']['xtlddesc']);
				if($xtldfeat['uid'] == 0){
					$xtldfeat_profile_link = htmlspecialchars_uni($xtldfeat['tusername']);
				}else{
					$xtldfeat_profile_link = build_profile_link(format_name(htmlspecialchars_uni($xtldfeat['username']), $xtldfeat['usergroup'], $xtldfeat['displaygroup']), $xtldfeat['uid']);
				}
				$threadfields = array();
				foreach($threadfieldscache as $k => &$v) {
					xthreads_get_xta_cache($v,$tids);
					$threadfields[$k] =& $thread['xthreads_'.$k];
					xthreads_sanitize_disp($threadfields[$k],$v,(!xthreads_empty($thread['username']) ? $thread['username'] : $thread['threadusername']));
				}
				if($xtldfeat['avatar']){
					$xtldfeat_user_avatar = '<img src="'.htmlspecialchars_uni($xtldfeat['avatar']).'" alt="" title="" width="45" height="45" />';
				}
				eval('$xtldfeat_threads_thread .= "'.$templates->get('xtldfeat_threads_thread').'";');
				$altbg = alt_trow();
			}
			eval('$xtldfeat_threads = "'.$templates->get('xtldfeat_threads').'";');
		}
	}
}
?>