<?xml version="1.0"?>
<!DOCTYPE modification SYSTEM "http://www.simplemachines.org/xml/modification">
<modification xmlns="http://www.simplemachines.org/xml/modification" xmlns:smf="http://www.simplemachines.org/">
	<id>jaybachatero:member_awards</id>
	<version>1.1.1</version>

	<file name="$boarddir/index.php">
		<operation>
			<search position="before"><![CDATA[
		'announce' => array('Post.php', 'AnnounceTopic'),]]></search>
			<add><![CDATA[
		'awards' => array('ManageAwards.php', 'Awards'),]]></add>
		</operation>
	</file>

	<file name="$sourcedir/Subs.php">
		<operation>
			<search position="before"><![CDATA[

		if (allowedTo('manage_bans'))
			$context['admin_areas']['members']['areas']['ban_members'] = '<a href="' . $scripturl . '?action=ban">' . $txt['ban_title'] . '</a>';]]></search>
			<add><![CDATA[

		if (allowedTo('manage_awards'))
			$context['admin_areas']['layout']['areas']['manage_awards'] = '<a href="' . $scripturl . '?action=awards">' . $txt['awards'] . '</a>';]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA['manage_bans', 'manage_permissions', 'admin_forum']]></search>
			<add><![CDATA[, 'manage_awards']]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA['manage_attachments', 'manage_smileys']]></search>
			<add><![CDATA[, 'manage_awards']]></add>
		</operation>
	</file>

	<file name="$sourcedir/Load.php">
		<operation>
			<search position="after"><![CDATA[
		'permissions' => array()]]></search>
			<add><![CDATA[
		'awards' => isset($user_settings['awards']) ? $user_settings['awards'] : array(),]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
			$user_profile[$row['ID_MEMBER']] = $row;]]></search>
			<add><![CDATA[
			$user_profile[$row['ID_MEMBER']]['awards'] = array();]]></add>
		</operation>

		<operation>
			<search position="after"><![CDATA[
	}

	if (!empty($new_loaded_ids) && $set !== 'minimal')]]></search>
			<add><![CDATA[

		// I'm sorry, but I've got to stick it somewhere...
		if($modSettings['awards_in_post'] > 0 && !empty($new_loaded_ids)){
			$awardsRequest = db_query("
				SELECT
					aw.id_award, aw.award_name, aw.description, aw.minifile,
					am.ID_MEMBER, am.date_received, am.favorite
				FROM {$db_prefix}awards_members AS am
					INNER JOIN {$db_prefix}awards AS aw ON (aw.id_award = am.id_award)
				WHERE am.ID_MEMBER IN(" . implode(',', $new_loaded_ids) . ")
				ORDER BY am.favorite DESC, am.date_received DESC", __FILE__, __LINE__);

			// Fetch the award info just once
			while ($row = mysql_fetch_assoc($awardsRequest)){
				$user_profile[$row['ID_MEMBER']]['awards'][] = array(
					'id' => $row['id_award'],
					'award_name' => $row['award_name'],
					'description' => $row['description'],
					'more' => '?action=profile;sa=awardsMembers;id=' . $row['id_award'],
					'href' => '?action=profile;sa=showAwards;u=' . $row['ID_MEMBER'],
					'minifile' => $row['minifile'],
					'img' => '/' . (empty($modSettings['awards_dir']) ? '' : $modSettings['awards_dir'] . '/') . $row['minifile'],
				);
			}
			mysql_free_result($awardsRequest);
		}]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
		'local_time' => timeformat(time() + ($profile['timeOffset'] - $user_info['time_offset']) * 3600, false),]]></search>
			<add><![CDATA[
		'awards' => &$profile['awards'],]]></add>
		</operation>
	</file>

	<file name="$sourcedir/ManagePermissions.php">
		<operation>
			<search position="before"><![CDATA[
		'manage_bans',
		'admin_forum',]]></search>
			<add><![CDATA[
		'manage_awards',]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
		'manage_bans',
		'send_mail',]]></search>
			<add><![CDATA[
		'manage_awards',]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
				'karma_edit' => false,]]></search>
			<add><![CDATA[
				'manage_awards' => false,]]></add>
		</operation>
	</file>

	<file name="$sourcedir/Who.php">
		<operation>
			<search position="before"><![CDATA[
		'viewmembers' => array('moderate_forum'),]]></search>
			<add><![CDATA[
		'awards' => array('manage_awards'),]]></add>
		</operation>
	</file>

	<file name="$sourcedir/Profile.php">
		<operation>
			<search position="before"><![CDATA[
		'showPermissions' => array(array('manage_permissions'), array('manage_permissions')),]]></search>
			<add><![CDATA[
		'showAwards' => array(array('profile_view_any', 'profile_view_own'), array('profile_view_any')),
		'awardsMembers' => array(array('profile_view_any', 'profile_view_own'), array('profile_view_any')),]]></add>
		</operation>

		<operation>
			<search position="before"><![CDATA[
			$context['profile_areas']['info']['areas']['showPosts']	= '<a href="' . $scripturl . '?action=profile;u=' . $memID . ';sa=showPosts">' . $txt['showPosts'] . '</a>';]]></search>
			<add><![CDATA[
			$context['profile_areas']['info']['areas']['showAwards'] = '<a href="' . $scripturl . '?action=profile;u=' . $memID . ';sa=showAwards">' . $txt['awards'] . '</a>';]]></add>
		</operation>

		<operation>
			<search position="end" />
			<add><![CDATA[
function showAwards($memID){
	global $context, $db_prefix, $txt, $scripturl, $modSettings, $settings;

	// Do they want to make a favorite?
	if (isset($_GET['makeFavorite']) && allowedTo(array('profile_extra_any', 'profile_extra_own'))){
		checkSession('get');

		$award = (int) $_GET['id'];
		$favorite = ($_GET['makeFavorite'] > 0) ? 1 : 0;

		// Do they only allow one fav?
		if (empty($modSettings['awards_favorite']))
			db_query("
				UPDATE {$db_prefix}awards_members
				SET favorite = 0
				WHERE ID_MEMBER = " . $memID, __FILE__, __LINE__);

		// Now make this one a fav.
		db_query("
			UPDATE {$db_prefix}awards_members
			SET favorite = {$favorite}
			WHERE id_award = {$award}
				AND ID_MEMBER = {$memID}
			LIMIT 1", __FILE__, __LINE__);

		// To make changes appear redirect back to that page
		redirectexit('action=profile;sa=showAwards;u=' . $memID);
	}

	// Load language
	loadLanguage('ManageAwards');

	// Count the number of items in the database for create index
	$request = db_query("
		SELECT COUNT(*)
		FROM {$db_prefix}awards_members
		WHERE ID_MEMBER = {$memID}", __FILE__, __LINE__);
	list ($context['count_awards']) = mysql_fetch_row($request);
	mysql_free_result($request);

	// Calculate the number of results to pull up.
	$max_awards = 25;

	// Construct the page index
	$context['page_index'] = constructPageIndex($scripturl . '?action=profile;sa=showAwards;u=' . $memID, $_REQUEST['start'], $context['count_awards'], $max_awards);
	$context['start'] = isset($_REQUEST['start']) ? (int) $_REQUEST['start'] : 0;

	// Load the awards
	$request = db_query("
		SELECT
			aw.id_award, aw.award_name, aw.description, aw.filename, aw.minifile,
			am.ID_MEMBER, am.date_received, am.favorite,
			c.category_name, c.id_category
		FROM {$db_prefix}awards AS aw
			LEFT JOIN {$db_prefix}awards_members AS am ON (am.id_award = aw.id_award)
			LEFT JOIN {$db_prefix}awards_categories AS c ON (c.id_category = aw.id_category)
		WHERE am.ID_MEMBER = {$memID}
		ORDER BY am.favorite DESC, c.category_name DESC, aw.award_name DESC
		LIMIT {$context['start']}, {$max_awards}", __FILE__, __LINE__);

	$context['categories'] = array();

	// Fetch the award info just once
	while ($row = mysql_fetch_assoc($request)){
		if(!isset($context['categories'][$row['id_category']]['name']))
			$context['categories'][$row['id_category']] = array(
				'name' => $row['category_name'],
				'awards' => array(),
			);

		$context['categories'][$row['id_category']]['awards'][$row['id_award']] = array(
			'id' => $row['id_award'],
			'award_name' => $row['award_name'],
			'description' => $row['description'],
			'more' => $scripturl . '?action=profile;sa=awardsMembers;id=' . $row['id_award'],
			'favorite' => array(
				'fav' => $row['favorite'],
				'href' => $scripturl . '?action=profile;sa=showAwards;id=' . $row['id_award'] . ';makeFavorite=' . ($row['favorite'] == 1 ? '0' : '1') . (isset($_REQUEST['u']) ? ';u=' . $_REQUEST['u'] : ''),
				'img' => '<img src="' . $settings['images_url'] . '/' . ($row['favorite'] == 1 ? 'collapse' : 'expand') . '.gif" alt="' . $txt['awards_favorite'] . '" title="' . $txt['awards_favorite'] . '" />',
			),
			'filename' => $row['filename'],
			'time' => list ($year, $month, $day) = sscanf($row['date_received'], '%d-%d-%d'),
			'img' => dirname($scripturl) . '/' . $modSettings['awards_dir'] . '/' . $row['filename'],
			'mini' => dirname($scripturl) . '/' . $modSettings['awards_dir'] . '/' . $row['minifile'],
		);
	}
	mysql_free_result($request);

	$context['page_title'] = $txt[79] . ' - ' . $txt['awards_title'];
	$context['sub_template'] = 'awards';
	$context['allowed_fav'] = ($context['user']['is_owner'] && allowedTo('profile_view_own')) || allowedTo('profile_extra_any');
}

function awardsMembers(){
	global $context, $db_prefix, $modSettings, $scripturl, $txt;

	// Load language
	loadLanguage('ManageAwards');

	$id = (int) $_REQUEST['id'];

	// An award must be selected.
	if (empty($id) || $id <= 0)
		fatal_lang_error('awards_error_no_award');

	// Load the award info separately
	$request = db_query("
		SELECT *
		FROM {$db_prefix}awards
		WHERE id_award = {$id}", __FILE__, __LINE__);

	// Check if ths award actually exists
	if (mysql_num_rows($request) < 1)
		fatal_lang_error('awards_error_no_award');

	$context['award'] = array();

	// Fetch the award info just once
	$row = mysql_fetch_assoc($request);
	$context['award'] = array(
		'id' => $row['id_award'],
		'name' => $row['award_name'],
		'description' => $row['description'],
		'filename' => $row['filename'],
		'img' => dirname($scripturl) . '/' . $modSettings['awards_dir'] . '/' . $row['filename'],
		'mini' => dirname($scripturl) . '/' . $modSettings['awards_dir'] . '/' . $row['minifile'],
		'members' => array(),
	);
	mysql_free_result($request);

	// Now load the info.
	$request = db_query("
		SELECT
			m.realName, a.id_member, a.date_received
		FROM {$db_prefix}awards_members AS a
			LEFT JOIN {$db_prefix}members AS m ON (m.ID_MEMBER = a.id_member)
		WHERE a.id_award = {$id}
		ORDER BY m.realName DESC", __FILE__, __LINE__);

	while ($row = mysql_fetch_assoc($request))
		$context['award']['members'][] = '<a href="' . $scripturl . '?action=profile;u=' . $row['id_member'] . '">' . $row['realName'] . '</a>';

	mysql_free_result($request);

	// Set the context values
	$context['page_title'] = $txt['awards'] . ' - ' . $context['award']['name'];
	$context['sub_template'] = 'awards_members';
	$context['columns'] = 3;

}
]]></add>
		</operation>
	</file>

	<file name="$themedir/Display.template.php">
		<operation>
			<search position="after"><![CDATA[
			// This shows the popular messaging icons.
			echo '
								', $message['member']['icq']['link'], '
								', $message['member']['msn']['link'], '
								', $message['member']['aim']['link'], '
								', $message['member']['yim']['link'], '<br />';]]></search>
			<add><![CDATA[
			// Show their awards?
			if (!empty($message['member']['awards']) && $modSettings['awards_in_post'] > 0){
				echo '
						<li>
							<fieldset style="border: 1px solid black; padding: 5px;">
								<legend>', $txt['awards'], '</legend>';

				// Couldn't limit it in Load.php, so let's do it here.
				$awards = 0;
				foreach ($message['member']['awards'] as $award){
					if($awards < $modSettings['awards_in_post'])
						echo '
								<a href="', $scripturl, $award['more'], '"><img src="', dirname($scripturl), $award['img'], '" alt="', $award['description'], '" title="', $award['description'], '" /></a> ';
					$awards++;
				}

				echo '
							</fieldset>
						</li>';
			}]]></add>
		</operation>

		<operation>
			<search position="after"><![CDATA[

				// Since we know this person isn't a guest, you *can* message them.
				if ($context['can_send_pm'])
					echo '
								<a href="', $scripturl, '?action=pm;sa=send;u=', $message['member']['id'], '" title="', $message['member']['online']['label'], '">', $settings['use_image_buttons'] ? '<img src="' . $settings['images_url'] . '/im_' . ($message['member']['online']['is_online'] ? 'on' : 'off') . '.gif" alt="' . $message['member']['online']['label'] . '" border="0" />' : $message['member']['online']['label'], '</a>';]]></search>
			<add><![CDATA[

				// Awards!
				echo '
								<a href="', $scripturl, '?action=profile;sa=showAwards;u=', $message['member']['id'], '" title="', $txt['awards'], '">', ($settings['use_image_buttons'] ? '<img src="' . $settings['images_url'] . '/award.gif" alt="' . $txt['awards'] . '" title="' . $txt['awards'] . '" border="0" />' : $txt['awards']), '</a>';]]></add>
		</operation>
	</file>

	<file name="$themedir/Profile.template.php">
		<operation>
			<search position="end" />
			<add><![CDATA[
// This is the template for the awards
function template_awards(){
	global $context, $modSettings, $txt, $settings;

	echo '
					<table border="0" cellspacing="1" cellpadding="4" align="center" width="100%" class="bordercolor">
						<tr class="titlebg">
							<td colspan="5">' ,$txt['awards'], '</td>
						</tr>';

	// Show the amount of awards that a member has
	if (!empty($context['count_awards']))
		echo '
						<tr class="windowbg">
							<td colspan="5" align="center">
								', sprintf($txt['awards_count_badges'], $context['count_awards']), '
							</td>
						</tr>';

	// Check if this member has any awards
	if (empty($context['categories']))
		echo '
						<tr class="windowbg2">
							<td colspan="5">', $txt['awards_no_badges_member'], '</td>
						</tr>';
	else {

		foreach($context['categories'] as $category){
			echo '
						<tr class="catbg">
							<td colspan="5">', $category['name'], '</td>
						</tr>
						<tr class="windowbg3">
							<td width="15%">', $txt['awards_image'], '</td>
							<td width="15%">', $txt['awards_mini'], '</td>
							<td width="20%">', $txt['awards_name'], '</td>
							<td width="45%">', $txt['awards_description'], '</td>
							<td align="center" width="5%">', $txt['awards_favorite2'], '</td>
						</tr>';

			$which = true;

			foreach ($category['awards'] as $award){
				$which = !$which;

				echo '
						<tr class="windowbg', $which ? '2' : '', '">
							<td align="center"><a href="', $award['more'], '"><img src="', $award['img'], '" alt="', $award['award_name'], '" /></a></td>
							<td align="center"><a href="', $award['more'], '"><img src="', $award['mini'], '" alt="', $award['award_name'], '" /></a></td>
							<td><strong>', $award['award_name'], '</strong></td>
							<td><em>', $txt['months'][$award['time'][1]], ' ', $award['time'][2], ', ', $award['time'][0], '</em>: ', $award['description'], '</td>
							<td align="center">', $context['allowed_fav'] ? '<a href="' . $award['favorite']['href'] . ';sesc=' . $context['session_id'] . '">' . $award['favorite']['img'] . '</a>' : '', '', ($award['favorite']['fav'] == 1 ? ' <img src="' . $settings['images_url'] . '/star.gif" alt="' . $txt['awards_favorite']. '" />' : ''), '</td>
						</tr>';

			}
		}

		// Show the pages
		echo '
						<tr class="catbg">
							<td align="left" colspan="5">
								<b>', $txt[139], ':</b> ', $context['page_index'], '
							</td>
						</tr>';
	}

	echo '
					</table>';

}

function template_awards_members(){
	global $context, $scripturl, $txt;

	echo '
				<table width="100%" cellpadding="4" cellspacing="1" border="0" align="center" class="bordercolor">
					<tr class="titlebg">
						<td colspan="', $context['columns'], '">', $txt['awards_members_with'], ': ', $context['award']['name'], '</td>
					</tr>
					<tr class="windowbg">
						<td colspan="', $context['columns'], '" align="center">
							<img src="', $context['award']['img'], '" alt="', $context['award']['name'], '" /><br />
							<strong>', $context['award']['name'], '</strong><br />
							', $context['award']['description'], '
						</td>
					</tr>
					<tr class="titlebg">
						<td colspan="', $context['columns'], '">', $txt['members'], '</td>
					</tr>';

	// Check if there are assigned members
	if (empty($context['award']['members']))
		echo '
					<tr class="windowbg2">
						<td colspan="', $context['columns'], '">', $txt['awards_no_assigned_members2'], '</td>
					</tr>';
	else {
		$which = true;
		$counter = 0;

		// Loop through the members
		foreach($context['award']['members'] as $member){
			if ($counter < 1)
				echo '
					<tr class="windowbg', $which ? '2' : '', '">';

			echo '
						<td width="*">', $member, '</td>';

			$counter++;

			if ($counter == $context['columns']){
				echo '
					</tr>';

				$counter = 0;
				$which = !$which;
			}
		}

		// If $counter is not = 0 then we must close the last tr
		if ($counter != 0)
			echo '
						<td colspan="', ($context['columns'] - $counter), '">&nbsp;</td>
					</tr>';
	}

	echo '
				</table>';

}
]]></add>
		</operation>
	</file>

	<file name="$languagedir/Modifications.english.php">
		<operation>
			<search position="end" />
			<add><![CDATA[
### Awards
// Used by: Errors
$txt['cannot_manage_awards'] = 'You aren\'t allowed to manage awards.';

// Used by: Help
$helptxt['awards'] = 'This area allows you to manage, assign, and categorize awards, as well as change a few settings.';

// Used by: ManagePermissions
$txt['permissionname_manage_awards'] = 'Manage Awards';
$txt['permissionhelp_manage_awards'] = 'This will allow users add, edit and assign new awards';

// Used by: Multiple Areas
$txt['awards'] = 'Awards';
$txt['awards_view_album'] = 'View member\'s award album.';
$txt['awards_main'] = 'Main';
$txt['awards_modify'] = 'Add/Modify';
$txt['awards_assign'] = 'Assign';
$txt['awards_categories'] = 'Categories';
$txt['awards_settings'] = 'Settings';
$txt['whoallow_awards'] = 'Managing <a href="' . $scripturl . '?action=awards">Awards</a>.';

// Used by: Profile
$txt['showAwards'] = 'Show Awards';
$txt['members'] = 'Members';
### End Awards]]></add>
		</operation>
	</file>

	<file name="$languagedir/Modifications.english-utf8.php" error="skip">
		<operation>
			<search position="end" />
			<add><![CDATA[
### Awards
global $scripturl;
// Used by: Errors
$txt['cannot_manage_awards'] = 'You aren\'t allowed to manage awards.';

// Used by: Help
$helptxt['awards'] = 'This area allows you to manage, assign, and categorize awards, as well as change a few settings.';

// Used by: ManagePermissions
$txt['permissionname_manage_awards'] = 'Manage Awards';
$txt['permissionhelp_manage_awards'] = 'This will allow users add, edit and assign new awards';

// Used by: Multiple Areas
$txt['awards'] = 'Awards';
$txt['awards_view_album'] = 'View member\'s award album.';
$txt['awards_main'] = 'Main';
$txt['awards_modify'] = 'Add/Modify';
$txt['awards_assign'] = 'Assign';
$txt['awards_categories'] = 'Categories';
$txt['awards_settings'] = 'Settings';
$txt['whoallow_awards'] = 'Managing <a href="' . $scripturl . '?action=awards">Awards</a>.';

// Used by: Profile
$txt['showAwards'] = 'Show Awards';
$txt['members'] = 'Members';
### End Awards]]></add>
		</operation>
	</file>

</modification>
