<?php
/*
	Attachments Gallery
	Display forum attachments gallery from specified forum(s)
	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 mnflatatgal_info()
{
	return array(
		'name'			=> 'Forum Attachment Gallery',
		'description'	=> 'Forum Attachment Gallery',
		'website'		=> 'http://mybbhacks.zingaburga.com/member.php?action=profile&amp;uid=171',
		'author'		=> 'My Nie',
		'authorsite'	=> 'http://mybbhacks.zingaburga.com/member.php?action=profile&amp;uid=171',
		'version'		=> '1.0',
		'compatibility' => '1*',
		'guid'        	=> ''
	);
}

function mnflatatgal_activate()
{
	global $db;
	
	$settinggroups = array(
		'name'			=> 'mnflatatgal_title',
		'title'			=> 'Forum Attachments Gallery',
		'description'	=> 'Settings For Forum Attachments Gallery',
		'disporder'		=> '99',// My Favorite Number :)
		'isdefault'		=> 'no'
	);
	
	$db->insert_query('settinggroups',$settinggroups);
	$gid = $db->insert_id();
	
	$settings = array(
		'name'			=> 'mnflatatgal_fid',
		'title'			=> 'Enable The Forum Attachments Gallery For This Forum ID(s)',
		'description'	=> 'Type the forum id(s). Seperate with commas. 0 to disable.',
		'optionscode'	=> 'text',
		'value'			=> '3,5,9',
		'disporder'		=> '1',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);

	$settings = array(
		'name'			=> 'mnflatatgal_num',
		'title'			=> 'Number Of Attachments Thumbnail',
		'description'	=> 'How many attachments thumbnail do you want to be displayed?',
		'optionscode'	=> 'text',
		'value'			=> '5',
		'disporder'		=> '2',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);

	$template = array(
		'tid'		=> NULL,
		'title'		=> "fatt_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[\'fmnlatatgal\']}.gif\" id=\"fmnlatatgal_img\" class=\"expander\" alt=\"[-]\" title=\"\" />
				</div>
				<div><strong>Forum Attachments Gallery</strong></div>
			</td>
		</tr>
	</tbody>
	<tbody style=\"{\$collapsed[\'fmnlatatgal_e\']}\" id=\"fmnlatatgal_e\">
		<tr>
			<td class=\"trow1\" align=\"center\">
				{\$mnfatt_gallery_gallery}
			</td>
		</tr>
	</tbody>
</table>
<br />",
		'sid'		=> "-1"
	);
	$db->insert_query("templates", $template);
	
	$template = array(
		'tid'		=> NULL,
		'title'		=> "fatt_gallery_gallery",
		'template'	=> "<a href=\"{\$mnflag_postlink}\"><img src=\"{\$mnflag_att_thumb}\" alt=\"{\$mnflag_title}\" title=\"{\$mnflag_title}, Posted By: {\$mnflag_username} Uploaded At: {\$mnflag_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" . '<!-- Forum Attachment Gallery -->');
}

function mnflatatgal_deactivate()
{
	global $db;

	$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='fatt_gallery'");
	$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='fatt_gallery_gallery'");
	$db->query('DELETE FROM '.TABLE_PREFIX.'settinggroups WHERE name=\'mnflatatgal_title\'');
	$db->query('DELETE FROM '.TABLE_PREFIX.'settings WHERE name IN(\'mnflatatgal_fid\',\'mnflatatgal_num\')');
	rebuild_settings();
	
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('header', '#<!-- Forum Attachment Gallery -->#', '', 0);
}

$plugins->add_hook('global_start', 'mnflatatgal_tcache');
function mnflatatgal_tcache()
{
	global $mybb, $templatelist, $plugins;

	if($mybb->settings['mnflatatgal_fid'] != 0)
	{
		if(THIS_SCRIPT == 'forumdisplay.php' || THIS_SCRIPT == 'showthread.php')
		{
			if(isset($templatelist))
			{
				$templatelist .= ',fatt_gallery,fatt_gallery_gallery';
			}
		}
		$plugins->add_hook('global_end', 'mnflatatgal_run');
		$plugins->add_hook('forumdisplay_start', 'mnflatatgal_run');
		$plugins->add_hook('showthread_start', 'mnflatatgal_run');
	}
}

function mnflatatgal_run()
{
    global $mybb, $db, $thread, $fid, $collapsed, $collapsedimg, $templates, $theme, $mnfattachment_gallery;
	if(in_array($fid, explode(',', $mybb->settings['mnflatatgal_fid'])) || in_array($thread['fid'], explode(',', $mybb->settings['mnflatatgal_fid'])))
	{
		$query = $db->query("
			SELECT a.updatetime, a.md5hash, a.uploadtime, a.aid, a.attachname, a.filename, a.tid AS atid, t.*
			FROM ".TABLE_PREFIX."xtattachments a
			LEFT JOIN ".TABLE_PREFIX."threads t ON (t.tid=a.tid)
			WHERE t.visible=1 AND t.fid=$fid
			GROUP BY a.tid
			ORDER BY a.aid DESC
			LIMIT ".intval($mybb->settings['mnflatatgal_num'])."
		");
		while($mnflag = $db->fetch_array($query))
		{
			$mnflag_username = htmlspecialchars_uni($mnflag['username']);
			$mnflag_title = htmlspecialchars_uni($mnflag['subject']);
			$mnflag_postlink = get_thread_link(intval($mnflag['tid']));
			$mnflag_datetime = my_date($mybb->settings['dateformat'], $mnflag['uploadtime']).' '.my_date($mybb->settings['timeformat'], $mnflag['uploadtime']);
			$mnflag_att_thumb = xthreads_get_xta_url($mnflag).'/thumb160x120';
			eval("\$mnfatt_gallery_gallery .= \"".$templates->get("fatt_gallery_gallery")."\";");
		}
		eval("\$mnfattachment_gallery = \"".$templates->get("fatt_gallery")."\";");
		$GLOBALS['header'] = str_replace('<!-- Forum Attachment Gallery -->', $mnfattachment_gallery, $GLOBALS['header']);
	}
}
?>