<?php

if(!defined("IN_MYBB"))
	die("This file cannot be accessed directly.");

$plugins->add_hook('showthread_start', 'showthreadnote_run');
$plugins->add_hook('xmlhttp', 'showthreadnote_ajax');

function showthreadnote_info()
{
	return array(
		'name'			=> 'Show Thread Notes in showthread.',
		'description'	=> 'Displays, to moderators, moderator thread notes at the top of each thread (if set).',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.1',
		'compatibility'	=> '1*',
		'guid'			=> ''
	);
}


function showthreadnote_activate()
{
	global $db, $mybb;
	require_once MYBB_ROOT.'inc/adminfunctions_templates.php';
	find_replace_templatesets('showthread', '#'.preg_quote('{$header}').'#', '{$header}{$threadnotes}');
	
	
	$template = '<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder" style="clear: both; border-bottom-width: 0;">
	<tr><td class="thead"><strong>Moderator Thread Notes</strong> [<a href="moderation.php?action=threadnotes" onclick="return editThreadNotes();">Edit</a>]</td></tr>
	<tr><td class="trow1">
		<form action="moderation.php" method="post" onsubmit="return saveThreadNotes();" id="threadnotes_form" name="threadnotes_form">
		<input type="hidden" name="my_post_key" value="{$mybb->post_code}" />
		<input type="hidden" name="action" value="do_threadnotes" />
		<input type="hidden" name="tid" value="{$thread[\'tid\']}" />
		<div id="threadnotes">{$thread[\'notes\']}</div>
		</form>
	</td></tr>
</table><br />
<script language="javascript" type="text/javascript"><!--
	function editThreadNotes()
	{
		if(!$("threadnotes_editor"))
		{
			$("threadnotes").innerHTML = "<textarea id=\'threadnotes_editor\' rows=\'10\' cols=\'80\' name=\'threadnotes\' style=\'width: 100%;\'>"
			+ $("threadnotes").innerHTML.replace(/[\n\r]/g, "").replace(/<br( \/|)>/ig, "\n")
			+ "</textarea><div style=\'text-align: center; margin-top: 3px;\'><input type=\'submit\' value=\'Save\' /></div>";
		}
		return false;
	}
	
	function saveThreadNotes()
	{
		if($("threadnotes_editor"))
		{
			if(typeof(imagepath)=="undefined")
				imagepath = "images";
			Thread.spinner = new ActivityIndicator("body", {image: imagepath + "/spinner_big.gif"});
			var opt = {method: "post", postBody: Form.serialize("threadnotes_form"), onComplete: saveThreadNotesDone};
			if(Ajax.Request) // MyBB 1.4
				new Ajax.Request("xmlhttp.php", opt);
			else // MyBB 1.2
				new ajax("xmlhttp.php", opt);
			
			$("threadnotes").innerHTML = $("threadnotes_editor").value.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/\n/g, "<br />").replace(/\r/g, "");
		}
		return false;
	}
	
	function saveThreadNotesDone(r)
	{
		if(r.responseText!="okay")
			alert("There was an error saving the thread notes.");
		if(Thread.spinner)
		{
			Thread.spinner.destroy();
			Thread.spinner = \'\';
		}
	}
//--></script>
';
	
	if($mybb->version_code >= 1400)
		$table_prefix = '';
	else
		$table_prefix = TABLE_PREFIX;
	$db->insert_query($table_prefix.'templates', array(
		'title' => 'showthread_note',
		'template' => $db->escape_string($template),
		'sid' => -1,
		'version' => $mybb->version_code
	));
}

function showthreadnote_deactivate()
{
	global $db, $mybb;
	require_once MYBB_ROOT.'inc/adminfunctions_templates.php';
	find_replace_templatesets('showthread', '#'.preg_quote('{$threadnotes}').'#', '', 0);
	
	if($mybb->version_code >= 1400)
		$table_prefix = '';
	else
		$table_prefix = TABLE_PREFIX;
	$db->delete_query($table_prefix.'templates', 'title="showthread_note" AND sid=-1');
}



function showthreadnote_run()
{
	global $thread, $threadnotes, $templates, $theme, $ismod, $mybb;
	if(!$thread['notes'] || !$ismod) return;
	
	$thread['notes'] = nl2br(htmlspecialchars_uni($thread['notes']));
	eval('$threadnotes = "'.$templates->get('showthread_note').'";');
}

function showthreadnote_ajax()
{
	global $mybb;
	if($mybb->input['action'] != 'do_threadnotes') return;
	
	verify_post_check($mybb->input['my_post_key']);
	
	$thread = get_thread(intval($mybb->input['tid']));
	if(!$thread) die('Invalid thread.');
	
	if(!is_moderator($thread['fid'], 'canmanagethreads'))
		die('No permissions to modify thread notes.');
	
	global $lang, $db;
	$lang->load('moderation');
	log_moderator_action(array('tid' => $thread['tid'], 'fid' => $thread['fid']), $lang->thread_notes_edited);
	
	if($mybb->version_code >= 1400)
		$table_prefix = '';
	else
		$table_prefix = TABLE_PREFIX;
	$db->update_query($table_prefix.'threads', array('notes' => $db->escape_string($mybb->input['threadnotes'])), 'tid='.$thread['tid']);
	die('okay');
}
?>