<?php

$plugins->add_hook('datahandler_pm_validate', 'staffignorepm_pm');
$plugins->add_hook('usercp_options_start', 'staffignorepm_usercp');
$plugins->add_hook('usercp_do_options_end', 'staffignorepm_do_usercp');

function staffignorepm_info()
{
	return array(
		'name'			=> 'Allow Forum Staff to Ignore PMs',
		'description'	=> 'Gives the option to Super Moderators and Administrators to ignore PMs from non Super Mod/Admin members.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0'
	);
}

function staffignorepm_activate()
{
	global $db;
	$db->query('ALTER TABLE '.TABLE_PREFIX.'users ADD (`allowpmsfrommembers` CHAR(3) NOT NULL DEFAULT "no")');
	
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('usercp_options', '#'.preg_quote('<td><span class="smalltext"><label for="pmnotify">{$lang->pm_notify}</label></span></td>
</tr>').'#', '<td><span class="smalltext"><label for="pmnotify">{$lang->pm_notify}</label></span></td>
</tr>{$staffignorepm}');
	
	$newtemplate = '<tr>
<td valign="top" width="1"><input type="checkbox" class="checkbox" name="allowpmsfrommembers" id="allowpmsfrommembers" value="yes"{$staffignorepmcheck} /></td>
<td><span class="smalltext"><label for="allowpmsfrommembers">Ignore PMs from users who are not Super Moderators or Administrators</label></span></td>
</tr>';
	$db->insert_query(TABLE_PREFIX.'templates', array(
		'title' => 'usercp_options_staffignorepms',
		'template' => $db->escape_string($newtemplate),
		'sid' => -1,
		'version' => 120
	));
}
function staffignorepm_deactivate()
{
	global $db;
	$db->query('ALTER TABLE '.TABLE_PREFIX.'users DROP `allowpmsfrommembers`');
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('usercp_options', '#'.preg_quote('{$staffignorepm}').'#', '', 0);
	
	$db->delete_query(TABLE_PREFIX.'templates', 'title = "usercp_options_staffignorepms"');
}

function staffignorepm_pm($uh)
{
	// Cache the to user information.
	$touser = get_user($uh->data['toid']);

	if($touser['allowpmsfrommembers'] != 'yes') return;
	// Check if we have a valid recipient or not.
	if(!$touser['uid'] && !$uh->data['saveasdraft'])
		return false;
	$recipient_permissions = user_permissions($touser['uid']);
	$sender_permissions = user_permissions($uh->data['fromid']);
	
	if($sender_permissions['cancp'] != 'yes' && $sender_permissions['issupermod'] != 'yes' && ($recipient_permissions['cancp'] == 'yes' || $recipient_permissions['issupermod'] == 'yes'))
	{
		// ignored!
		$uh->set_error("recipient_is_ignoring");
		return false;
	}
}

function staffignorepm_usercp()
{
	global $staffignorepm, $mybb, $templates;
	if($mybb->usergroup['cancp'] != 'yes' && $mybb->usergroup['issupermod'] != 'yes') return;
	if($mybb->user['allowpmsfrommembers'] != 'no')
		$staffignorepmcheck = ' checked="checked"';
	eval('$staffignorepm = "'.$templates->get('usercp_options_staffignorepms').'";');
}

function staffignorepm_do_usercp()
{
	// due to the smart *cough* placement of the hooks, we have to do more work...
	global $mybb, $db;
	if($mybb->usergroup['cancp'] != 'yes' && $mybb->usergroup['issupermod'] != 'yes') return;
	if($mybb->input['allowpmsfrommembers'] == 'yes') $s = 'yes';
	else $s = 'no';
	$db->update_query(TABLE_PREFIX.'users', array('allowpmsfrommembers' => $s), 'uid='.$mybb->user['uid']);
}

?>