<?php

// set the following line to 0 if you want to disable postbit modnotes
//  note, this number must be at least 3
define('POSTBIT_MODNOTES_MAXLEN', 30);

$plugins->add_hook('member_profile_start', 'modusernotes_run');
$plugins->add_hook('member_profile_end', 'modusernotes_profile');

if(defined('POSTBIT_MODNOTES_MAXLEN') && POSTBIT_MODNOTES_MAXLEN>2)
	$plugins->add_hook('postbit', 'modusernotes_postbit');

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.3',
		'compatibility'	=> '14*',
		//'guid'			=> '3c04744f6d4d2bf28e30c4c38ccbfc6c'
	);
}

function modusernotes_is_installed() {
	return $GLOBALS['db']->field_exists('modnotes', 'users');
}
function modusernotes_install()
{
	$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('templates', array(
		'title' => 'member_profile_modusernotes',
		'template' => $db->escape_string($new_template),
		'sid' => -1,
		'version' => 1400
	));
	
	// add notes column
	$db->write_query('ALTER TABLE '.TABLE_PREFIX.'users ADD (
		`modnotes` TEXT NOT NULL
	)');
}
function modusernotes_activate() {
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('member_profile', '#\{\$modoptions\}#', '{$modoptions}{$modnotes}');
	find_replace_templatesets('postbit', '#\{\$post\[\'user_details\'\]\}#', '{$post[\'user_details\']}{$post[\'modnotes\']}');
	find_replace_templatesets('postbit_classic', '#\{\$post\[\'user_details\'\]\}#', '{$post[\'user_details\']}{$post[\'modnotes\']}');
}
function modusernotes_deactivate() {
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('member_profile', '#\{\$modnotes\}#', '', 0);
	find_replace_templatesets('postbit', '#\{\$post\[\'modnotes\'\]\}#', '', 0);
	find_replace_templatesets('postbit_classic', '#\{\$post\[\'modnotes\'\]\}#', '', 0);
}

function modusernotes_uninstall()
{
	global $db;
	$db->delete_query('templates', 'title = "member_profile_modusernotes"');
	
	$db->write_query('ALTER TABLE '.TABLE_PREFIX.'users DROP `modnotes`');
}

function modusernotes_run()
{
	global $mybb;
	if($mybb->usergroup['canviewprofiles'] == 0) return;
	if($mybb->input['updatemodnotes'] == '1')
	{
		if($mybb->usergroup['cancp'] != 1 && $mybb->usergroup['issupermod'] != 1)
			error_no_permission();
		
		verify_post_check($mybb->input['my_post_key']);
		
		$uid = intval($mybb->input['uid']);
		if(!$uid) error('Invalid UserID.');
		global $db;
		$db->update_query('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'] != 1 && $mybb->usergroup['issupermod'] != 1) return;
	$uid = intval($mybb->input['uid']);
	if(!$uid) $uid = $mybb->user['uid'];
	eval('$modnotes = "'.$templates->get('member_profile_modusernotes').'";');
}


function modusernotes_postbit(&$post)
{
	global $mybb;
	static $xuid=1;
	if(!$post['modnotes'] || ($mybb->usergroup['cancp'] != 1 && $mybb->usergroup['issupermod'] != 1)) {
		$post['modnotes'] = '';
		return;
	}
	if(my_strlen($post['modnotes']) > POSTBIT_MODNOTES_MAXLEN) // exceeds max len?
	{
		$xnotes = htmlspecialchars_uni(str_replace("\n", '', nl2br(htmlspecialchars_uni(my_substr($post['modnotes'], POSTBIT_MODNOTES_MAXLEN-2)))));
		if($xnotes == '...')
			$notes = nl2br(htmlspecialchars_uni($post['modnotes']));
		else
		{
			$notes = nl2br(htmlspecialchars_uni(my_substr($post['modnotes'], 0, 28))).'<a href="javascript:if($(\'modnotex_'.$xuid.'\').innerHTML==\'...\')$(\'modnotex_'.$xuid.'\').innerHTML=\''.$xnotes.'\';else $(\'modnotex_'.$xuid.'\').innerHTML=\'...\';void(0);" title="Show full note" id="modnotex_'.$xuid.'">...</a>';
			++$xuid;
		}
	}
	else
		$notes = nl2br(htmlspecialchars_uni($post['modnotes']));
	$post['modnotes'] = '<br /><br /><strong>Admin Note:</strong> '.$notes;
}

?>