<?php
if(!defined('IN_MYBB')) die();
function mnxt_latest_articles_info(){
	return array(
		'name'			=> 'Latest Articles On Index',
		'description'	=> 'Displaying latest articles title on index',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function mnxt_latest_articles_activate(){
	mnxt_latest_articles_deactivate();
	global $db;
	$db->insert_query('templates', array(
		'title'		=> 'mnxt_latest_articles',
		'template'	=> $db->escape_string('<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr>
		<td class="thead">
			<strong>Latest Articles</strong>
		</td>
	</tr>
	{$mnxt_latest_articles_article}
</table>'),
		'sid'		=> -1
	));
	$db->insert_query('templates', array(
		'title'		=> 'mnxt_latest_articles_article',
		'template'	=> $db->escape_string('<tr>
	<td class="{$altbg}">
		<div>
			<strong><a href="{$article[\'articlelink\']}">{$article[\'subject\']}</a></strong>
		</div>
		<div class="smalltext">
			By: {$article[\'profilelink\']}, {$article[\'articledate\']} at {$article[\'articletime\']}<br />
			{$articles[\'atcgal\']}<br />
			Replies: {$article[\'articlereplies\']}, Views: {$article[\'articleviews\']}
		</div>
		<div class="post_body">
			{$article[\'postpreview\']}
		</div>
	</td>
</tr>'),
		'sid'		=> -1
	));
}

function mnxt_latest_articles_deactivate(){
	global $db;
	$db->delete_query('templates', 'title IN("mnxt_latest_articles","mnxt_latest_articles_article")');
}

$plugins->add_hook('global_start','mnxt_latest_articles_tcache');
function mnxt_latest_articles_tcache(){
	if(THIS_SCRIPT == 'index.php'){
		global $templatelist;
		if(isset($templatelist)) $templatelist .= ',mnxt_latest_articles,mnxt_latest_articles_article';
	}
}

$plugins->add_hook('index_start','mnxt_latest_articles_run');
function mnxt_latest_articles_run(){
	global $db;
	$fid = 27;// Articles Gallery Forum ID
	$limit = 5;// How many latest articles should be displayed
	$articletfcache = xthreads_gettfcache($fid);
	$altbg = alt_trow();
	$query = $db->query('
		SELECT t.*, t.username AS tusername, td.atcgal AS xthreads_atcgal, u.username, 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 t.fid='.(int)$fid.' AND t.visible=1 AND t.closed NOT LIKE "moved|%"
		ORDER BY t.dateline desc
		LIMIT 0,'.(int)$limit.'
	');
	$articlecache = array();
	while($article = $GLOBALS['db']->fetch_array($query)){
		$articlecache[$article['tid']] = $article;
	}
	$mnxt_latest_articles_article = '';
	if(is_array($articlecache)){
		global $parser,$mybb,$theme,$articles,$mnxt_latest_articles;
		if(!is_object($parser)){
			require_once MYBB_ROOT.'inc/class_parser.php';
			$parser = new postParser;
		}
		foreach($articlecache as $article){
			$article['profilelink'] = $article['username']?build_profile_link(format_name(htmlspecialchars_uni($article['username']),$article['usergroup'],$article['displaygroup']),$article['uid']):htmlspecialchars_uni($article['tusername']);
			$article['articledate'] = my_date($mybb->settings['dateformat'],$article['dateline']);
			$article['articletime'] = my_date($mybb->settings['timeformat'],$article['dateline']);
			$article['subject'] = htmlspecialchars_uni($parser->parse_badwords($article['subject']));
			$article['articlelink'] = get_thread_link($article['tid']);
			$article['articlereplies'] = my_number_format($article['replies']);
			$article['articleviews'] = my_number_format($article['views']);
			xthreads_set_threadforum_urlvars('forum',$article['fid']);
			if(is_array($articletfcache)){
				$articles = array();
				foreach($articletfcache as $k => &$v){
					$articles[$k] =& $article['xthreads_'.$k];
					xthreads_sanitize_disp($articles[$k],$v);
				}
			}
			eval('$mnxt_latest_articles_article .= "'.$GLOBALS['templates']->get('mnxt_latest_articles_article').'";');
			$altbg = alt_trow();
		}
		eval('$mnxt_latest_articles = "'.$GLOBALS['templates']->get('mnxt_latest_articles').'";');
	}
}
?>