<?php
/*
	Attachments Gallery
	Display attachments gallery from specified forum(s)
	Adapted from a plug in by RateU at MyBBHacks.
	The author doesn't have responsibility for any harm to your forum that could have been caused by this plugin/mod.
	Use at your own risk.
*/

if(!defined('IN_MYBB'))
{
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}

function mbhattatgal_info()
{
	return array(
		'name'			=> 'Latest X threads Attachments Gallery',
		'description'	=> 'Latest X threads Attachments Gallery',
		'website'		=> 'http://mybbhacks.zingaburga.com/member.php?action=profile&uid=171',
		'author'		=> 'RateU',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com/member.php?action=profile&uid=171',
		'version'		=> '1.1',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function mbhattatgal_activate()
{
	global $db;
	
	$settinggroups = array(
		'name'			=> 'mbhattatgal_title',
		'title'			=> 'Latest X threads Attachments Gallery',
		'description'	=> 'Settings For Latest X threads Attachments Gallery',
		'disporder'		=> '99',// My Favorite Number :)
		'isdefault'		=> 'no'
	);
	
	$db->insert_query('settinggroups',$settinggroups);
	$gid = $db->insert_id();
	
	$settings = array(
		'name'			=> 'mbhattatgal_ed',
		'title'			=> 'Enable/Disbale',
		'description'	=> 'Do you want to enable The Latest X threads Attachments Gallery?',
		'optionscode'	=> 'yesno',
		'value'			=> '1',
		'disporder'		=> '1',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);

	$settings = array(
		'name'			=> 'mbhattatgal_fid',
		'title'			=> 'Forum ID',
		'description'	=> 'Type the forum id for the Latest X threads Attachments Gallery. Seperate with commas.',
		'optionscode'	=> 'text',
		'value'			=> '2,3',
		'disporder'		=> '2',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);

	$settings = array(
		'name'			=> 'mbhattatgal_num',
		'title'			=> 'Number Of Attachments Thumbnails',
		'description'	=> 'How many attachments thumbnails do you want to be displayed?',
		'optionscode'	=> 'text',
		'value'			=> '5',
		'disporder'		=> '3',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);
	
	$settings = array(
		'name'			=> 'mbhattatgal_dsp',
		'title'			=> 'Displaying Latest X threads Attachments Gallery',
		'description'	=> 'Where do you want the Latest X threads Attachments Gallery to be displayed?',
		'optionscode'	=> 'select
portal=Portal
index=Index
poin=Portal And Index
global=All Pages',
		'value'			=> 'portal',
		'disporder'		=> '4',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);

	$template = array(
		'tid'		=> NULL,
		'title'		=> "mbhatt_gallery",
		'template'	=> "<table border=\"0\" cellspacing=\"{\$theme[\'borderwidth\']}\" cellpadding=\"{\$theme[\'tablespace\']}\" class=\"tborder\">
	<tbody>
		<tr>
			<td class=\"thead\">
				<div class=\"expcolimage\">
					<img src=\"{\$theme[\'imgdir\']}/collapse{\$collapsedimg[\'mbhattatgal\']}.gif\" id=\"mbhattatgal_img\" class=\"expander\" alt=\"[-]\" title=\"\" />
				</div>
				<div><strong>Attachments Gallery</strong></div>
			</td>
		</tr>
	</tbody>
	<tbody style=\"{\$collapsed[\'mbhattatgal_e\']}\" id=\"mbhattatgal_e\">
		<tr>
			<td class=\"trow1\" align=\"center\">
				{\$mbhatt_gallery_gallery}
			</td>
		</tr>
	</tbody>
</table>
<br />",
		'sid'		=> "-1"
	);
	$db->insert_query("templates", $template);
	
	$template = array(
		'tid'		=> NULL,
		'title'		=> "mbhatt_gallery_gallery",
		'template'	=> "<a href=\"{\$mbhatt_postlink}\"><img src=\"{\$mbhatt_att_thumb}\" alt=\"{\$mbhatt_title}\" title=\"{\$mbhatt_title}, Posted By: {\$mbhatt_username} Uploaded At: {\$mbhatt_datetime}\" class=\"attachment\" style=\"vertical-align: middle\" /></a>",
		'sid'		=> "-1"
	);
	$db->insert_query("templates", $template);
	rebuild_settings();
	
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('header', '#^(.*)$#s', "\\1\r\n" . '<!-- Attachment Gallery -->');
}

function mbhattatgal_deactivate()
{
	global $db;

	$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='mbhatt_gallery'");
	$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='mbhatt_gallery_gallery'");
	$db->query('DELETE FROM '.TABLE_PREFIX.'settinggroups WHERE name=\'mbhattatgal_title\'');
	$db->query('DELETE FROM '.TABLE_PREFIX.'settings WHERE name IN(
		\'mbhattatgal_ed\',
		\'mbhattatgal_fid\',
		\'mbhattatgal_num\',
		\'mbhattatgal_dsp\'
	)');
	rebuild_settings();
	
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('header', '#<!-- Attachment Gallery -->#', '', 0);
}

$plugins->add_hook('global_start', 'mbhattatgal_tcache');
function mbhattatgal_tcache()
{
	if(isset($GLOBALS['templatelist']))
	{
		$GLOBALS['templatelist'] .= ',mbhatt_gallery,mbhatt_gallery_gallery';
	}

	if($GLOBALS['mybb']->settings['mbhattatgal_dsp'] == 'portal')
	{
		$GLOBALS['plugins']->add_hook('portal_start', 'mbhattatgal_run');
	}
	elseif($GLOBALS['mybb']->settings['mbhattatgal_dsp'] == 'index')
	{
		$GLOBALS['plugins']->add_hook('index_end', 'mbhattatgal_run');
	}
	elseif($GLOBALS['mybb']->settings['mbhattatgal_dsp'] == 'poin')
	{
		$GLOBALS['plugins']->add_hook('index_end', 'mbhattatgal_run');
		$GLOBALS['plugins']->add_hook('portal_start', 'mbhattatgal_run');
	}
	else
	{
		$GLOBALS['plugins']->add_hook('global_end', 'mbhattatgal_run');
	}
}

function mbhattatgal_run()
{
    global $mybb, $db, $collapsed, $collapsedimg, $templates, $theme, $mbhattachment_gallery;
	if($mybb->settings['mbhattatgal_ed'] != 0)
	{
		$mbhattatgal_num = intval($mybb->settings['mbhattatgal_num']);
		if(is_array(explode(',', $mybb->settings['mbhattatgal_fid'])))
		{
			foreach(explode(',', $mybb->settings['mbhattatgal_fid']) as $mbhattatgal_tid)
			{
				$mbhattatgal_tid_array[] = intval($mbhattatgal_tid);
			}
		}
			$query = $db->query("
			SELECT t.*, f.name, a.updatetime, a.md5hash, a.uploadtime, a.aid, a.attachname, a.filename
			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 a.thumbs!=""('".implode("','", $mbhattatgal_tid_array)."')
			GROUP BY a.tid
			ORDER BY a.aid DESC
			LIMIT $mbhattatgal_num
		");
		
		while($mbhatt = $db->fetch_array($query))
		{
			$mbhatt_username = htmlspecialchars_uni($mbhatt['username']);
			$mbhatt_title = htmlspecialchars_uni($mbhatt['subject']);
			$mbhatt_postlink = get_thread_link(intval($mbhatt['tid']));
			$mbhatt_datetime = my_date($mybb->settings['dateformat'], $mbhatt['uploadtime']).' '.my_date($mybb->settings['timeformat'], $mbhatt['uploadtime']);
			$mbhatt_att_thumb = xthreads_get_xta_url($mbhatt).'/thumb240x180';
			eval("\$mbhatt_gallery_gallery .= \"".$templates->get("mbhatt_gallery_gallery")."\";");
		}
		eval("\$mbhattachment_gallery = \"".$templates->get("mbhatt_gallery")."\";");
	}
}
?>