<?php
/**
* This plugin depends on XThreads.
*
* XThreads official release thread:
*
*		http://mybbhacks.zingaburga.com/showthread.php?tid=288
*		Coded by: Yumi/ZiNgA BuRgA
*		@ http://mybbhacks.zingaburga.com
*/
if(!defined('IN_MYBB')){
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function mnxt_mrtxpd_info(){
	return array(
		'name'			=> 'Most Replied Threads In X Previous Days',
		'description'	=> 'Displaying most replied threads in x previous days',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function mnxt_mrtxpd_activate(){
	mnxt_mrtxpd_deactivate();
	global $db;
	$db->insert_query('templates', array(
		'title'		=> 'mnxt_mrtxpd_threads',
		'template'	=> $db->escape_string('<br class="clear" />
<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr>
		<td class="thead">
			<strong>Most Replied Threads in The Past {$xdays} Days</strong>
		</td>
	</tr>
	{$mnxt_mrtxpd_threads_thread}
</table>'),
		'sid'		=> -1
	));
	$db->insert_query('templates', array(
		'title'		=> 'mnxt_mrtxpd_threads_thread',
		'template'	=> $db->escape_string('<tr>
	<td class="{$altbg}">
		<div class="largetext">
			<a href="{$thread[\'threadlink\']}">{$thread[\'subject\']}</a>
		</div>
	</td>
</tr>'),
		'sid'		=> -1
	));
}

function mnxt_mrtxpd_deactivate(){
	$GLOBALS['db']->delete_query('templates','title IN("mnxt_mrtxpd_threads","mnxt_mrtxpd_threads_thread")');
}

$plugins->add_hook('global_start','mnxt_mrtxpd_tcache');
function mnxt_mrtxpd_tcache(){
	if(THIS_SCRIPT == 'index.php'){
		if(isset($GLOBALS['templatelist'])) $GLOBALS['templatelist'] .= ',mnxt_mrtxpd_threads,mnxt_mrtxpd_threads_thread';
	}
}

$plugins->add_hook('index_start','mnxt_mrtxpd_run');
function mnxt_mrtxpd_run(){
	global $db,$mybb;
	$forumid = 2;
	$xdays = 10;
	$tlimit = 3;
	$cutdate = TIME_NOW - (86400*intval($xdays));
	$threadfieldscache = xthreads_gettfcache($forumid);
	if(!empty($threadfieldscache)){
		$xtfields = '';
		foreach($threadfieldscache as &$tf){
			$xtfields .= ',tf.'.$tf['field'].' AS xthreads_'.$tf['field'];
		}
	}
	$query = $db->query('SELECT COUNT(p.pid) as posts,t.tid,t.fid,t.subject,t.username as threadusername,u.username,u.usergroup,u.displaygroup'.$xtfields.'
		FROM '.TABLE_PREFIX.'threads t
		LEFT JOIN '.TABLE_PREFIX.'threadfields_data tf ON(tf.tid=t.tid)
		LEFT JOIN '.TABLE_PREFIX.'posts p ON(p.tid=t.tid)
		LEFT JOIN '.TABLE_PREFIX.'users u ON(u.uid=t.uid)
		WHERE t.fid='.intval($forumid).' AND t.visible=1 AND t.closed NOT LIKE "moved|%" AND p.visible=1 AND t.replies>0 AND p.dateline>'.$cutdate.' AND p.pid!=t.firstpost
		GROUP BY p.tid
		ORDER BY posts desc
		LIMIT 0,'.intval($tlimit).'
	');
	$threadcache = array();
	while($thread = $db->fetch_array($query)){
		$threadcache[$thread['tid']] = $thread;
	}
	if(!empty($threadcache)){
		global $threadfields,$theme,$mnxt_mrtxpd_threads,$parser,$templates;
		$mnxt_mrtxpd_threads_thread = '';
		$tids = implode(',',array_keys($threadcache));
		$i = 1; $altbg = alt_trow();
		if(!is_object($parser)){
			require_once MYBB_ROOT.'inc/class_parser.php';
			$parser = new postParser;
		}
		foreach($threadcache as $thread){
			if(!$thread['username']){
				$thread['profilelink'] = format_name($thread['threadusername'],1);
			}else{
				$thread['username'] = htmlspecialchars_uni($thread['username']);
				$thread['formatname'] = format_name($thread['username'],$thread['usergroup'],$thread['displaygroup']);
				$thread['profilelink'] = build_profile_link($thread['formatname'],$thread['uid']);
			}
			$thread['threadlink'] = get_thread_link(intval($thread['tid']));
			$thread['subject'] = htmlspecialchars_uni($parser->parse_badwords($thread['subject']));
			xthreads_set_threadforum_urlvars('forum',$thread['fid']);
			$thread['rank'] = $i;
			$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']));
			}
			eval('$mnxt_mrtxpd_threads_thread .= "'.$templates->get('mnxt_mrtxpd_threads_thread').'";');
			$i++; $altbg = alt_trow();
		}
		if($mnxt_mrtxpd_threads_thread) eval('$mnxt_mrtxpd_threads = "'.$templates->get('mnxt_mrtxpd_threads').'";');
	}
}
?>