##############################################################
## Mod Title: Medal System
## MOD Author: ycl6 < ycl6@users.sourceforge.net > (Y.C. LIN) http://macphpbbmod.sourceforge.net/
## MOD Translation to PHP-Fusion: Evil Inc. < http://evilgenius.funpic.de/CMS/news.php >
##				  DUX < duxforum@web.de >
## MOD Description: This mod is a complete Medal system for your phpBB forum. 
##	It consists:
## 			A ACP medal management panel
##			A Medal Listing page to display all medal information
##			A Medal moderator management panel for adding/removing medal from users
##			An e-mail is sent to user receiving the medal
##			Display amount of medal a user holds on viewtopic and profile page
## 
## Files To Edit: 3
##      maincore.php
##      profile.php
##	forum/viewthread.php
##
## Included Files: 11 (+ 12 images)
##      root/includes/functions_db.php
##      root/medal_panel/medal_small.gif
##	root/medal_panel/up.gif
##	root/medal_panel/down.gif
##	root/medal_panel/medals.php
##	root/medal_panel/medal_admin.php
##	root/medal_panel/medal_cp.php
##	root/medal_panel/medal_funcs.php
##      root/medal_panel/locale/English.php
##      root/medal_panel/locale/German.php
##      root/medal_panel/locale/index.php
##	root/medal_panel/images/*.gif
##
## License: AGPL http://www.affero.org/oagpl.html
##############################################################
## Author Notes:
##	
##	Please READ the "Usage Guide" found in EXTRA folder for FAQ/Tips
##
##############################################################
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD
##############################################################

#
#-----[ COPY ]------------------------------------------
#
copy root/includes/functions_db.php to includes/functions_db.php
copy root/medal_panel to infusions/medal_panel

#
#-----[ SQL ]------------------------------------------
#
# Remember to change the table prefix used on your database
#
CREATE TABLE IF NOT EXISTS fusion_medal (
  medal_id smallint(5) unsigned NOT NULL auto_increment,
  cat_id smallint(5) unsigned NOT NULL default '1',
  medal_name varchar(50) NOT NULL default '',
  medal_description varchar(255) NOT NULL default '',
  medal_image varchar(50) default NULL,
  medal_mod tinyint(3) unsigned NOT NULL default '103',
  PRIMARY KEY  (medal_id)
) TYPE=MyISAM ;

CREATE TABLE IF NOT EXISTS fusion_medal_cat (
  cat_id smallint(5) UNSIGNED NOT NULL auto_increment,
  cat_title varchar(50) NOT NULL default '',
  cat_order smallint(5) UNSIGNED NOT NULL default '0',
  PRIMARY KEY  (cat_id),
  KEY cat_order (cat_order)
) TYPE=MyISAM ;

CREATE TABLE IF NOT EXISTS fusion_medal_user (
  issue_id smallint(5) unsigned NOT NULL auto_increment,
  medal_id smallint(5) unsigned NOT NULL default '0',
  user_id smallint(5) unsigned NOT NULL default '0',
  issue_reason varchar(255) NOT NULL default '',
  issue_time int(11) NOT NULL default '0',
  PRIMARY KEY  (issue_id)
) TYPE=MyISAM ;

CREATE TABLE IF NOT EXISTS fusion_medal_settings(
  m_status tinyint(1) unsigned NOT NULL default '1',
  display_profile tinyint(1) unsigned NOT NULL default '1',
  display_forum tinyint(1) unsigned NOT NULL default '0'
) TYPE = MYISAM;

INSERT INTO fusion_medal_cat (cat_id, cat_title, cat_order) VALUES ('1', 'Default', '10');
INSERT INTO fusion_medal_settings (m_status, display_profile, display_forum) VALUES ('1','1','0');

#
#-----[ DIY ]------------------------------------------
#
After installation go to ACP and create a sitelink
URL: infusions/medal_panel/medals.php
Visibility: Members

#
#-----[ OPEN ]------------------------------------------
#
maincore.php

#
#-----[ FIND ]------------------------------------------
#
$link = dbconnect($db_host, $db_user, $db_pass, $db_name);

#
#-----[ REPLACE WITH ]------------------------------------------
#
// $link = dbconnect($db_host, $db_user, $db_pass, $db_name);
include BASEDIR."includes/functions_db.php";
$db = new sql_db($db_host, $db_user, $db_pass, $db_name);
if(!$db->db_connect_id)
{
	die("<div style='font-family:Verdana;font-size:11px;text-align:center;'><b>Unable to establish connection to MySQL</b><br>".mysql_errno()." : ".mysql_error()."</div>");
}

#
#-----[ FIND ]------------------------------------------
#
define("THEMES", BASEDIR."themes/");

#
#-----[ AFTER, ADD ]------------------------------------------
#
define("IMAGES_MD", INFUSIONS."medal_panel/images/");

#
#-----[ OPEN ]------------------------------------------
#
profile.php

#
#-----[ FIND ]------------------------------------------
#
include LOCALE.LOCALESET."user_fields.php";

#
#-----[ AFTER, ADD ]------------------------------------------
#
if(file_exists(INFUSIONS."medal_panel/locale/".$settings['locale'].".php"))
{
	include(INFUSIONS."medal_panel/locale/".$settings['locale'].".php");
}
else
{
        include(INFUSIONS."medal_panel/locale/English.php");
}
include(INFUSIONS."medal_panel/medal_funcs.php");

#
#-----[ FIND ]------------------------------------------
#
if ($data['user_groups']) {
	tablebreak();
	echo "<table align='center' cellpadding='0' cellspacing='1' width='400' class='tbl-border'>\n";
	echo "<tr>\n<td class='tbl2'><b>".$locale['423']."</b></td>\n\n</tr>\n<tr>\n<td class='tbl1'>\n";
	$user_groups = (strpos($data['user_groups'], ".") == 0 ? explode(".", substr($data['user_groups'], 1)) : explode(".", $data['user_groups']));
	for ($i = 0;$i < count($user_groups);$i++) {
		echo "<a href='".FUSION_SELF."?group_id=".$user_groups[$i]."'>".getgroupname($user_groups[$i])."</a>";
		if ($i != (count($user_groups)-1)) { echo ",\n"; } else { echo "\n"; }
	}
	echo "</td>\n</tr>\n</table>\n";
}

#
#-----[ AFTER, ADD ]------------------------------------------
#
	// Medal Mod
	$medal_settings = get_medal_settings();
	if( $medal_settings['m_status'] == '1' && $medal_settings['display_profile'] == '1' )
	{
		$result = $db->sql_query("SELECT cat_id, cat_title FROM ".$db_prefix."medal_cat ORDER BY cat_order");
		$category_rows = array();
		while ( $row = $db->sql_fetchrow($result) )
		{
			$category_rows[] = $row;
		}

		$result = $db->sql_query("SELECT m.medal_id, mu.user_id FROM ".$db_prefix."medal m, ".$db_prefix."medal_user mu WHERE mu.user_id = '" . $lookup . "' AND m.medal_id = mu.medal_id ORDER BY m.medal_name");
		$medal_list = $db->sql_fetchrowset($result);
		$medal_count = count($medal_list);
		$i_a = 1;

		if ( $medal_count )
		{
			tablebreak();
			echo "<table class='tbl-border' width='400' cellspacing='1' cellpadding='3' align='center'>
			<tr> 
				<td class='tbl2' align='center' height='28' colspan='2'><b><span class='gen'>".$locale['Medal_Information']."</span></b></td>
			</tr>
			<tr>
				<td class='tbl2' align='left' valign='middle' width='10%'><span class='gen'>".$locale['Medals'].": <b>".$medal_count."</b></span><br><br><a name='medal'></a><div style='position:relative;'><table>";
			for ($i = 0; $i <= count($category_rows); $i++)
			{
				$cat_id = $category_rows[$i]['cat_id'];
				$cat_title = $category_rows[$i]['cat_title'];
				$issue = "SELECT m.*, mu.issue_reason, mu.issue_time, c.cat_id, c.cat_title FROM ".$db_prefix."medal m, ".$db_prefix."medal_user mu, ".$db_prefix."medal_cat c WHERE mu.user_id = '" . $lookup . "' AND m.cat_id = c.cat_id AND m.medal_id = mu.medal_id ORDER BY c.cat_order, m.medal_name, mu.issue_time";
				$result = $db->sql_query($issue);
				
				if (dbrows($result))
				{
					$row = array();
					$rowset = array();
					$medal_time = $locale['Medal_time'] . ':&nbsp;';
					$medal_reason = $locale['Medal_reason'] . ':&nbsp;';
					while ($row = $db->sql_fetchrow($result))
					{
						if (empty($rowset[$row['medal_name']]))
						{
							$rowset[$row['medal_name']]['cat_id'] = $row['cat_id'];
							$rowset[$row['medal_name']]['cat_title'] = $row['cat_title'];
							$rowset[$row['medal_name']]['medal_description'] .= $row['medal_description'];
							$rowset[$row['medal_name']]['medal_image'] = $row['medal_image'];
							$row['issue_reason'] = ( $row['issue_reason'] ) ? $row['issue_reason'] : $locale['Medal_no_reason'];
							$rowset[$row['medal_name']]['medal_issue'] = "<tr><td><span class='genmed'>" . $medal_time . showdate('longdate', $row['issue_time']) . "</span></td></tr><tr><td><span class='genmed'>" . $medal_reason . $row['issue_reason']  . "</span><hr></td></tr>";
							$rowset[$row['medal_name']]['medal_count'] = '1';
						}
						else
						{
							$row['issue_reason'] = ( $row['issue_reason'] ) ? $row['issue_reason'] : $locale['Medal_no_reason'];
							$rowset[$row['medal_name']]['medal_issue'] .= "<tr><td><span class='genmed'>" . $medal_time . showdate('longdate', $row['issue_time']) . "</span></td></tr><tr><td><span class='genmed'>" . $medal_reason . $row['issue_reason'] . "</span><hr /></td></tr>";
							$rowset[$row['medal_name']]['medal_count'] += '1';
						}
					}
					$medal_name = array();
					$data = array();
					
					$display_medal = 0;	
					$last_cat_id = 0;
					while (list($medal_name, $data) = @each($rowset))
					{
						if ( $cat_id == $data['cat_id'] ) { $display_medal = 1; }
						if($cat_id != $last_cat_id) { echo "<td>";}

						if ( !empty($display_medal) )
						{
							echo "<a href=\"javascript:ShowHide('medalDIV".$i_a."')\"><img src='". IMAGES_MD . $data['medal_image'] . "' border='0' alt='" . $medal_name . "' title='" . $medal_name . "'></a>";
							echo "<div id='medalDIV".$i_a."' style='visibility:hidden;position:absolute;z-index:".$i.";left:0px; top:-100px;width:570;'><table class='tbl-border' width='100%' cellspacing='1' cellpadding='3' border='0' align='center'>
							<tr>
							<th class='thCornerL' align='center' nowrap='nowrap' width='20%'>&nbsp;".$locale['Medal_name']."&nbsp;</th>
							<th class='thCornerR' align='center' nowrap='nowrap'>&nbsp;".$locale['Medal_details']."</th>
							</tr>
							<tr>
							<td class='tbl2' nowrap='nowrap'>
							<table width='100%' cellspacing='1' cellpadding='3' border='0'>
							<tr><td align='center'><span class='gen'>".$data['cat_title']."</span></td></tr>
							<tr><td align='center'><span class='genmed'>".$medal_name."</span></td></tr>
							<tr><td align='center'><img src='". IMAGES_MD . $data['medal_image'] . "' border='0' alt='" . $medal_name . "' title='" . $medal_name . "'>&nbsp;&nbsp;</td></tr>
							<tr><td align='center'><span class='gensmall'>".$locale['Medal_amount'].$data['medal_count']."</span></td></tr></table></td>
							<td class='tbl2' valign='top'>
							<table width='100%' cellspacing='1' cellpadding='3' border='0'>
							<tr><td><span class='gen'>".$locale['Medal_description'].": <b>".$data['medal_description']."</b></span></td></tr>
							<tr><td class='tbl2'>
							<table width='100%' cellspacing='1' cellpadding='3' border='0'>
							<tr><td><span class='genmed'>".$data['medal_issue']."</span></td></tr>
							<tr><td align='right'><a href=\"javascript:ShowHide('medalDIV".$i_a."')\">Close</a></td></tr>
							</table>
							</td></tr>
							</table></td>
							</tr></table></div>";
							$display_medal = 0;
							$i_a++;
						}
						$last_cat_id = $cat_id;
					}
					echo "</td>";
				}
			}
			echo "</table></div></td></tr></table>";
		}
	}
	// Medal Mod

#
#-----[ FIND ]------------------------------------------
#
closetable();

require_once "side_right.php";
require_once "footer.php";
?>

#
#-----[ BEFORE, ADD ]-----------------------------------
#
echo "<script type='text/javascript'>
function ShowHide(id)
{
	obj = document.getElementsByTagName(\"div\");
	if (obj[id].style.visibility == 'visible')
	{
		obj[id].style.visibility = 'hidden';
	}
	else
	{
		obj[id].style.visibility = 'visible';
	}
}
</script>\n";

#
#-----[ OPEN ]------------------------------------------
#
forum/viewthread.php

#
#-----[ FIND ]------------------------------------------
#
include LOCALE.LOCALESET."forum/main.php";

#
#-----[ AFTER, ADD ]------------------------------------------
#
if(file_exists(INFUSIONS."medal_panel/locale/".$settings['locale'].".php"))
{
	include(INFUSIONS."medal_panel/locale/".$settings['locale'].".php");
}
else
{
        include(INFUSIONS."medal_panel/locale/English.php");
}
include(INFUSIONS."medal_panel/medal_funcs.php");

#
#-----[ FIND ]------------------------------------------
#
echo "<span class='alt'>".$locale['504']."</span> ".showdate("%d.%m.%y", $data['user_joined'])."</td>

#
#-----[ REPLACE WITH ]------------------------------------------
#
echo "<span class='alt'>".$locale['504']."</span> ".showdate("%d.%m.%y", $data['user_joined'])."<br><br>";

#
#-----[ AFTER, ADD ]------------------------------------------
#
		// Medal MOD
		$medal_settings = get_medal_settings();
		if( $medal_settings['m_status'] == '1' && $medal_settings['display_forum'] == '1' )
		{
			$medal_count = dbrows(dbquery("SELECT m.medal_id, mu.user_id FROM ".$db_prefix."medal m, ".$db_prefix."medal_user mu WHERE mu.user_id = '" . $data['user_id'] . "' AND m.medal_id = mu.medal_id ORDER BY m.medal_name"));

			if ( $medal_count > 0 )
			{
				echo "<a href='../profile.php?lookup=".$data['user_id']."'><img src='". INFUSIONS . "medal_panel/medal_small.gif' border='0' alt='Award Details'></a>&nbsp;". $locale['Medal_amount'] . $medal_count;
			}
		}
		// Medal Mod

#
#-----[ FIND ]------------------------------------------
#
<td>
<table cellspacing='0' cellpadding='0' width='100%'>

#
#-----[ REPLACE WITH ]------------------------------------------
#
echo "</td><td>
<table cellspacing='0' cellpadding='0' width='100%'>

#
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------
#
# EoM