<?php 
/*
 _______________________________________________________
|														|
| Thanks for the report 1.0								|
| (c) 2011 by SaeedGh (SaeehGhMail@Gmail.com)			|
| Profile: http://community.mybb.com/user-14229.html	|
| Last edit: 2011-03-16									|
|_______________________________________________________|

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

*/

if (!defined("IN_MYBB"))
{
	die("Direct initialization of this file is not allowed.");
}


$plugins->add_hook('modcp_do_reports', 'thx4report_run_main');


function thx4report_info()
{
	global $mybb, $db;
	
	$settings_link = '';
	
	$query = $db->simple_select('settinggroups', '*', "name='thx4report'");

	if (count($db->fetch_array($query)))
	{
		$settings_link = '(<a href="index.php?module=config&action=change&search=thx4report" style="color:#FF1493;">Opcje</a>)';
	}
	
	return array(
		'name'			=>	'Thanks for the report',
		'description'	=>	'Wtyczka informuje użytkownika raportującego, że jego zgłoszenie zostało rozpatrzone. ' . $settings_link,
		'website'		=>	'http://community.mybb.com/user-14229.html',
		'author'		=>	'SaeedGh',
		'authorsite'	=>	'mailto:SaeehGhMail@Gmail.com',
		'version'		=>	'1.0',
		'guid'			=>	'07d90319ec03615283d0954b06f75731',
		'compatibility'	=>	'16*'
	);
}


function thx4report_activate()
{
	global $mybb, $db;
	
	$t4r_group = array(
		"name"			=> "thx4report",
		"title"			=> "Thanks for the report",
		"description"	=> "Wtyczka informuje użytkownika raportującego, że jego zgłoszenie zostało rozpatrzone.",
		"disporder"		=> "1",
		"isdefault"		=> "1",
	);
	
	$db->insert_query("settinggroups", $t4r_group);
	$gid = $db->insert_id();

	$t4r[]= array(
		"name"			=> "t4r_active",
		"title"			=> "Plugin włączony?",
		"description"	=> "Włączyć plugin?",
		"optionscode"	=> "yesno",
		"value"			=> '1',
		"disporder"		=> '1',
		"gid"			=> intval($gid),
	);
	
	$t4r[]= array(
		"name"			=> "t4r_uid",
		"title"			=> "Autor PW",
		"description"	=> "ID użytkownika, który wysyła PW, z treścią iż zgłoszenie zostało przyjęte.",
		"optionscode"	=> "text",
		"value"			=> '1',
		"disporder"		=> '5',
		"gid"			=> intval($gid),
	);
	
	$t4r[]= array(
		"name"			=> "t4r_subject",
		"title"			=> "Temat",
		"description"	=> "Temat prywatnej wiadomości",
		"optionscode"	=> "text",
		"value"			=> 'Dziękujemy za raport!',
		"disporder"		=> '10',
		"gid"			=> intval($gid),
	);
	
	$t4r[]= array(
		"name"			=> "t4r_message",
		"title"			=> "Wiadomość",
		"description"	=> "Treść prywatnych wiadomości, które dostają użytkownicy po zgłoszeniu. Można używać zmiennych:
<script type=\"text/javascript\">
function insertText(value, textarea)
{
	// Internet Explorer
	if(document.selection)
	{
		textarea.focus();
		var selection = document.selection.createRange();
		selection.text = value;
	}
	// Firefox
	else if(textarea.selectionStart || textarea.selectionStart == \'0\')
	{
		var start = textarea.selectionStart;
		var end = textarea.selectionEnd;
		textarea.value = textarea.value.substring(0, start)	+ value	+ textarea.value.substring(end, textarea.value.length);
	}
	else
	{
		textarea.value += value;
	}
}
</script>
<br />
<input type=\"button\" onclick=\"insertText(\'{poster}\', $(\'setting_t4r_message\'));\" value=\"{poster}\" />
<input type=\"button\" onclick=\"insertText(\'{thread}\', $(\'setting_t4r_message\'));\" value=\"{thread}\" />
<input type=\"button\" onclick=\"insertText(\'{reason}\', $(\'setting_t4r_message\'));\" value=\"{reason}\" />
<input type=\"button\" onclick=\"insertText(\'{date}\', $(\'setting_t4r_message\'));\" value=\"{date}\" />",
		"optionscode"	=> "textarea",
		"value"			=> 'Dziękujemy za raport. Moderator zajął się sprawą.

[b]Twoje dane w raporcie:[/b]
Użytkownik: {poster}
Temat: {thread}
Powód: {reason}
Data: {date}

Wiadomość została wygenerowana automatycznie, nie odpowiadaj na nią!',
		"disporder"		=> '15',
		"gid"			=> intval($gid),
	);
	
	foreach ($t4r as $t)
	{
		$db->insert_query("settings", $t);
	}
		
	rebuild_settings();
}


function thx4report_deactivate()
{
	global $mybb, $db;
	
	$db->delete_query("settings","name IN ('t4r_active','t4r_uid','t4r_subject','t4r_message')");
	$db->delete_query("settinggroups","name='thx4report'");
	
	rebuild_settings();
}


function thx4report_run_main()
{
	global $mybb, $db, $parser, $rids;
	
	if (!$mybb->settings['t4r_active'] || !$mybb->settings['t4r_subject'] || !$mybb->settings['t4r_message']) {return false;}
	
	if (!is_object($parser))
	{
		require_once MYBB_ROOT.'inc/class_parser.php';
		$parser = new postParser;
	}
	
	require_once MYBB_ROOT."inc/datahandlers/pm.php";
	$pmhandler = new PMDataHandler();
	
	$pm_sender_id = intval($mybb->settings['t4r_uid']);
	
	$query = $db->query("
		SELECT r.*, u.username, up.username AS postusername, up.uid AS postuid, t.subject AS threadsubject 
		FROM ".TABLE_PREFIX."reportedposts r 
		LEFT JOIN ".TABLE_PREFIX."posts p ON (r.pid=p.pid) 
		LEFT JOIN ".TABLE_PREFIX."threads t ON (p.tid=t.tid) 
		LEFT JOIN ".TABLE_PREFIX."users u ON (r.uid=u.uid) 
		LEFT JOIN ".TABLE_PREFIX."users up ON (p.uid=up.uid) 
		WHERE r.rid IN (".$rids.")  
		ORDER BY r.dateline DESC 
	");
	
	while($report = $db->fetch_array($query))
	{
		$report['postlink'] = get_post_link($report['pid'], $report['tid']);
		$report['threadlink'] = get_thread_link($report['tid']);
		$report['posterlink'] = get_profile_link($report['postuid']);
		$report['reporterlink'] = get_profile_link($report['uid']);
		$reportdate = my_date($mybb->settings['dateformat'], $report['dateline']);
		$reporttime = my_date($mybb->settings['timeformat'], $report['dateline']);
		$report['threadsubject'] = htmlspecialchars_uni($parser->parse_badwords($report['threadsubject']));
	
		$recipients_to = array($report['uid']);
		$recipients_bcc = array();
		$subject = $mybb->settings['t4r_subject'];
		$message = preg_replace(
			array(
				'#{poster}#',
				'#{thread}#',
				'#{reason}#',
				'#{date}#',
			),
			array(
				'[url='.$mybb->settings['bburl'].'/'.$report['posterlink'].']'.$report['postusername'].'[/url]',
				'[url='.$mybb->settings['bburl'].'/'.$report['threadlink'].']'.$report['threadsubject'].'[/url]',
				$report['reason'],
				$reportdate . ' ' . $reporttime,
			),
			$mybb->settings['t4r_message']
		);
		
		$pm = array(
			"subject" => $subject,
			"message" => $message,
			"icon" => -1,
			"fromid" => $pm_sender_id,
			"toid" => $recipients_to,
			"bccid" => $recipients_bcc,
			"do" => '',
			"pmid" => ''
		);
		
		$pm['options'] = array(
			"signature" => 1,
			"disablesmilies" => 0,
			"savecopy" => 1,
			"readreceipt" => 1
		);
	
		$pm['saveasdraft'] = 0;
		$pmhandler->admin_override = true;
		$pmhandler->set_data($pm);
		
		if($pmhandler->validate_pm())
		{
			$pmhandler->insert_pm();
		}
	}
}

?>