<?php
if(!defined('IN_MYBB')) die();

function mnxtigp_info(){
	return array(
		'name'			=> 'Latest Images On Portal',
		'description'	=> 'Display latest images from <a href="http://mybbhacks.zingaburga.com/showthread.php?tid=286">XThreads Images Gallery</a> on Portal',
		'website'		=> 'http://mybbhacks.zingaburga.com',
		'author'		=> 'XThreads Mania',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com',
		'version'		=> '1.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function mnxtigp_activate(){
	$GLOBALS['db']->insert_query('templates', array(
		'title'		=> 'mnxtigportal_images',
		'template'	=> $GLOBALS['db']->escape_string('<br />
<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
	<tr>
		<td class="thead largetext"><strong>Latest Images</strong></td>
	</tr>
	<tr>
		<td class="trow1" style="text-align: center">
			{$mnxtigportal_images_image}
		</td>
	</tr>
</table>'),
		'sid'		=> -1
	));
	$GLOBALS['db']->insert_query('templates', array(
		'title'		=> 'mnxtigportal_images_image',
		'template'	=> $GLOBALS['db']->escape_string('<a href="{$mnxtigps_links}"><img src="{$mnxtigps_img}" alt="{$mnxtigps_subject_text}" title="{$mnxtigps_subject_text}" width="{$mnxtigip_w}" height="{$mnxtigip_h}" style="margin-top: 3px; padding: 3px; border: 1px solid #ADCBE7;" /></a>'),
		'sid'		=> -1
	));
}

function mnxtigp_deactivate(){
	$GLOBALS['db']->delete_query('templates', 'title IN("mnxtigportal_images", "mnxtigportal_images_image")');
}

$plugins->add_hook('global_start','mnxtigp_tcache');
function mnxtigp_tcache(){
	if($GLOBALS['current_page'] == 'portal.php'){
		if(isset($GLOBALS['templatelist'])){
			$GLOBALS['templatelist'] .= ',mnxtigportal_images,mnxtigportal_images_image';
		}
	}
}

$plugins->add_hook('portal_start','mnxtigp_run');
function mnxtigp_run(){
	$xtimggal = '3,4';// Forum ID(s) for XThreads Images Gallery
	$xtimggal_limit = 10;// Number of images to be displayed
	$xtimggal_dim = '160x120';// Image dimension to be displayed
	$xtimggalfids = explode(',',$xtimggal);
	if(is_array($xtimggalfids)){
		foreach($xtimggalfids as $xtimggalfid){
			$xtimggalfid_array[] = intval($xtimggalfid);
		}
		$xtimggalfids = implode(',',$xtimggalfid_array);
	}
	$unviewable = get_unviewable_forums();
	if($unviewable) $unviewwhere = ' AND t.fid NOT IN ('.$unviewable.')';
	$query = $GLOBALS['db']->query('
		SELECT t.tid, t.subject, a.updatetime, a.md5hash, a.uploadtime, a.aid, a.attachname, a.filename, a.thumbs
		FROM '.TABLE_PREFIX.'threads t
		LEFT JOIN '.TABLE_PREFIX.'xtattachments a ON (a.tid=t.tid)
		WHERE t.visible=1 AND t.closed NOT LIKE "moved|%" AND t.fid IN ('.$xtimggalfids.')'.$unviewwhere.'
		ORDER BY t.dateline DESC
		LIMIT 0,'.intval($xtimggal_limit).'
	');
	if($GLOBALS['db']->num_rows($query)){
		global $mnxtigportal_images, $theme;
		$mnxtigis_imgs_num = my_number_format($GLOBALS['db']->num_rows($query));
		while($mnxtigip = $GLOBALS['db']->fetch_array($query)){
			if($mnxtigip['thumbs']) $mnxtigip['thumbs'] = unserialize($mnxtigip['thumbs']);
			$mnxtigps_links = get_thread_link($mnxtigip['tid']);
			$mnxtigps_subject_text = htmlspecialchars_uni($GLOBALS['parser']->parse_badwords($mnxtigip['subject']));
			$mnxtigps_img = xthreads_get_xta_url($mnxtigip,$xtimggal_dim);
			$mnxtigip_w = $mnxtigip['thumbs'][$xtimggal_dim]['w'];
			$mnxtigip_h = $mnxtigip['thumbs'][$xtimggal_dim]['h'];
			eval('$mnxtigportal_images_image .= "'.$GLOBALS['templates']->get('mnxtigportal_images_image').'";');
		}
		eval('$mnxtigportal_images = "'.$GLOBALS['templates']->get('mnxtigportal_images').'";');
	}
}
?>