<?php

if(!defined('IN_MYBB'))
{
	die('Direct initialization of this file is not allowed.<br /><br />Please make sure IN_MYBB is defined.');
}


function randomgameslists_info()
{
	return array(
		'name'			=> 'Games - Random Games Gallery',
		'description'	=> 'Random Games Gallery',
		'website'		=> 'http://www.leefish.nl/mybb/member.php?action=profile&uid=1',
		'author'		=> 'LeeFish',
		'authorsite'	=> 'http://www.leefish.nl/mybb/member.php?action=profile&uid=1',
		'version'		=> '1.1',
		'compatibility' => '1*',
	);
}

function randomgameslists_activate()
{
	global $db;
    
    $query = $db->simple_select("settinggroups", "COUNT(*) as rows");
    $rows = $db->fetch_field($query, "rows");
	
	$settinggroups = array(
		'name'			=> 'randomgameslists_title',
		'title'			=> 'Games - Random Games Gallery',
		'description'	=> 'Settings For Random Games Gallery',
	    'disporder' => $rows+1, 
	    'isdefault' => 0
	);
	
	$db->insert_query('settinggroups',$settinggroups);
	$gid = $db->insert_id();
	
	$settings = array(
		'name'			=> 'randomgameslists_ed',
		'title'			=> 'Enable/Disable',
		'description'	=> 'Do you want to enable the Random Games Gallery?',
		'optionscode'	=> 'yesno',
		'value'			=> '1',
		'disporder'		=> '1',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);

	$settings = array(
		'name'			=> 'randomgameslists_num',
		'title'			=> 'Number Of Games',
		'description'	=> 'How many Random Games do you want to be displayed?',
		'optionscode'	=> 'text',
		'value'			=> '5',
		'disporder'		=> '2',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);
	
	$settings = array(
		'name'			=> 'randomgameslists_dsp',
		'title'			=> 'Displaying Random Games Gallery',
		'description'	=> 'Where do you want the Random Games Gallery to be displayed? <br />Use Variable {$randomgameslists_gallery} in your template',
		'optionscode'	=> 'select
index=Index
portal=Portal
poin=Portal And Index
global=All Pages',
		'value'			=> 'index',
		'disporder'		=> '4',
		'gid'			=> intval($gid)
	);
	$db->insert_query("settings",$settings);

$template = array(
		'tid'		=> NULL,
		'title'		=> "randomgameslists_gallery",
		'template'	=> "<table border=\"0\" cellspacing=\"{\$theme[\'borderwidth\']}\" cellpadding=\"{\$theme[\'tablespace\']}\" class=\"tborder\">
	<tbody>
		<tr>
			<td class=\"thead\" colspan=\"6\">
				<div class=\"expcolimage\">
					<img src=\"{\$theme[\'imgdir\']}/collapse{\$collapsedimg[\'randomgameslists\']}.gif\" id=\"randomgameslists_img\" class=\"expander\" alt=\"[-]\" title=\"\" />
				</div>
				<div><strong>Random Games</strong></div>
			</td>
		</tr>
	</tbody>
	<tbody style=\"{\$collapsed[\'randomgameslists_e\']}\" id=\"randomgameslists_e\">
			<tr>
				<td class=\"tcat\" colspan=\"2\"><span class=\"smalltext\"><strong><a href=\"{\$mybb->settings[\'bburl\']}/games.php\" />All Games</a></strong></span></td>
			</tr>
			{\$randomgameslists_gallery_gallery}\
		</tbody>
</table>
<br />",
		'sid'		=> "-1"
	);
	$db->insert_query("templates", $template);
	
	$template = array(
		'tid'		=> NULL,
		'title'		=> "randomgameslists_gallery_gallery",
		'template'	=> "<tr>
		<td class=\"trow1\" align=\"left\"><a href=\"{\$mybb->settings[\'bburl\']\}/games.php?action=play&amp;gid={\$ggid}\">{\$game}\</a>
		<br />	
		<br />		
		<div style=\"position: relative;width:60px;height:11px;\">
		<div style=\"position: absolute;background: transparent url({\$mybb->settings[\'bburl\']\}/images/star_ratinggrey.png)top left no-repeat;top: 0px;left: 0px;width: 60px;height:11px;z-index:101;\"></div>
		<div style=\"position: absolute;background: transparent url({\$mybb->settings[\'bburl\']\}/images/star_ratingorange.png)top left no-repeat;top: 0px;left: 0px;height:11px;z-index: 102;width:{\$grating}0%;\"></div>
		</div>
		</td>
		<td class=\"trow1\" align=\"center\"><a href=\"{\$mybb->settings[\'bburl\']\}/games.php?action=play&amp;gid={\$ggid}\"><img src=\"./games/images/{\$gamename}1.gif\" alt=\"\" /></a></td>
</tr>",
		'sid'		=> "-1"
	);
	$db->insert_query("templates", $template);
	rebuild_settings();
	
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('header', '#^(.*)$#s', "\\1\r\n" . '<!-- randomgameslists Gallery -->');
}

function randomgameslists_deactivate()
{
	global $db;

	$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='randomgameslists_gallery'");
	$db->query("DELETE FROM ".TABLE_PREFIX."templates WHERE title='randomgameslists_gallery_gallery'");
	$db->query('DELETE FROM '.TABLE_PREFIX.'settinggroups WHERE name=\'randomgameslists_title\'');
	$db->query('DELETE FROM '.TABLE_PREFIX.'settings WHERE name IN(
		\'randomgameslists_ed\',
		\'randomgameslists_fid\',
		\'randomgameslists_num\',
		\'randomgameslists_dsp\'
	)');
	rebuild_settings();
	
	require MYBB_ROOT.'/inc/adminfunctions_templates.php';
	find_replace_templatesets('header', '#<!-- randomgameslists Gallery -->#', '', 0);
}

$plugins->add_hook('global_start', 'randomgameslists_tcache');
function randomgameslists_tcache()
{
	if(isset($GLOBALS['templatelist']))
	{
		$GLOBALS['templatelist'] .= ',randomgameslists_gallery,randomgameslists_gallery_gallery';
	}
	if($GLOBALS['mybb']->settings['randomgameslists_dsp'] == 'portal')
	{
		$GLOBALS['plugins']->add_hook('portal_start', 'randomgameslists_run');
	}
	elseif($GLOBALS['mybb']->settings['randomgameslists_dsp'] == 'index')
	{
		$GLOBALS['plugins']->add_hook('index_end', 'randomgameslists_run');
	}
	elseif($GLOBALS['mybb']->settings['randomgameslists_dsp'] == 'poin')
	{
		$GLOBALS['plugins']->add_hook('index_end', 'randomgameslists_run');
		$GLOBALS['plugins']->add_hook('portal_start', 'randomgameslists_run');
	}
	else
	{
		$GLOBALS['plugins']->add_hook('global_end', 'randomgameslists_run');
	}
}
function randomgameslists_run()
{
    global $mybb, $db, $collapsed, $collapsedimg, $templates, $theme, $randomgameslists_gallery;
	if($mybb->settings['randomgameslists_ed'] != 0)
	{
		//Get the number of games we want to return
		$randomgameslists_num = intval($mybb->settings['randomgameslists_num']);
	
		//Filter the inactive games
		$where = "active !=0";
		
		//set the base for the rand
		$gamecount = $db->fetch_field($db->simple_select('games', 'COUNT(*) AS gamecount', $where), 'gamecount'); 
		if($gamecount < 1)
		error('There are no games on this board.');

		// select random game
		$rndnum = mt_rand(0, $gamecount-1);
		$query = $db->simple_select('games', 'gid,title,active,name', $where, array('limit' => $randomgameslists_num, 'limit_start' => $rndnum));
		
		while($randomgameslist = $db->fetch_array($query))
		{
		$game = htmlspecialchars_uni($randomgameslist['title']);
		$gamename = htmlspecialchars_uni($randomgameslist['name']);
		$ggid = htmlspecialchars_uni($randomgameslist['gid']);
		
		eval("\$randomgameslists_gallery_gallery .= \"".$templates->get("randomgameslists_gallery_gallery")."\";");
		}
			eval("\$randomgameslists_gallery = \"".$templates->get("randomgameslists_gallery")."\";");
		}
}
