<?php

$plugins->add_hook('member_profile_start', 'modusernotes_run');
$plugins->add_hook('member_profile_end', 'modusernotes_profile');

function modusernotes_info()
{
	return array(
		'name'			=> 'Mod User Notes',
		'description'	=> 'Allows Super Moderators and Administrators to keep notes on users in their User Profile.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0'
	);
}

function modusernotes_activate()
{
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('member_profile', '#\{\$adminoptions\}#', '{$adminoptions}{$modnotes}');
	
	$new_template = '<br /><table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" width="100%" class="tborder">
<tr>
<td colspan="2" class="thead"><strong>Administrative Notes</strong></td>
</tr>
<tr>
<td class="trow1" style="text-align: center;">
<form action="member.php" method="post">
<input type="hidden" name="my_post_key" value="{$mybb->post_code}" />
<input type="hidden" name="action" value="profile" />
<input type="hidden" name="updatemodnotes" value="1" />
<input type="hidden" name="uid" value="{$uid}" />
<textarea name="modnotes" cols="50" rows="5">{$memprofile[\'modnotes\']}</textarea>
<br /><input type="submit" value="Submit Changes" /> <input type="reset" value="Reset" />
</form>
</td>
</tr>
</table>';
	global $db;
	$db->insert_query(TABLE_PREFIX.'templates', array(
		'title' => 'member_profile_modusernotes',
		'template' => $db->escape_string($new_template),
		'sid' => -1,
		'version' => 120
	));
	
	// add notes column
	$db->query('ALTER TABLE '.TABLE_PREFIX.'users ADD (
		`modnotes` TEXT NOT NULL
	)');
}

function modusernotes_deactivate()
{
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('member_profile', '#\{\$modnotes\}#', '', 0);
	
	global $db;
	$db->delete_query(TABLE_PREFIX.'templates', 'title = "member_profile_modusernotes"');
	
	$db->query('ALTER TABLE '.TABLE_PREFIX.'users DROP `modnotes`');
}

function modusernotes_run()
{
	global $mybb;
	if($mybb->usergroup['canviewprofiles'] == "no") return;
	if($mybb->input['updatemodnotes'] == '1')
	{
		if($mybb->usergroup['cancp'] != "yes" && $mybb->usergroup['issupermod'] != "yes")
			error_no_permission();
		
		if($mybb->version_code >= 1212)
			verify_post_check($mybb->input['my_post_key']);
		
		$uid = intval($mybb->input['uid']);
		if(!$uid) $uid = $mybb->user['uid'];
		global $db;
		$db->update_query(TABLE_PREFIX.'users', array(
			'modnotes' => $db->escape_string($mybb->input['modnotes'])
		), 'uid='.$uid);
		redirect('member.php?action=profile&uid='.$uid, 'Administrative Notes Updated.  You will be redirected back to the user\'s profile page.');
		exit;
	}
}
function modusernotes_profile()
{
	global $modnotes, $mybb, $templates, $memprofile, $theme;
	if($mybb->usergroup['cancp'] != "yes" && $mybb->usergroup['issupermod'] != "yes") return;
	$uid = intval($mybb->input['uid']);
	if(!$uid) $uid = $mybb->user['uid'];
	eval('$modnotes = "'.$templates->get('member_profile_modusernotes').'";');
}

?>