<?php

define('COPYPOST_MAX_COPIES', 5);

if(!defined("IN_MYBB"))
	die("This file cannot be accessed directly.");

$plugins->add_hook('moderation_start', 'copypost_run');

function copypost_info()
{
	return array(
		'name'			=> 'Duplicate Posts',
		'description'	=> 'This will add an additional moderation tool which allows posts to be duplicated via Inline Post Moderation.  If only one post is selected, the post may be &quot;dissected&quot; or split.',
		'website'		=> 'http://mybbhacks.zingaburga.com/',
		'author'		=> 'ZiNgA BuRgA',
		'authorsite'	=> 'http://zingaburga.com/',
		'version'		=> '1.0',
		'compatibility'	=> '12*',
		'guid'			=> ''
	);
}

define('COPYPOST_TEMPLATE_ADD', '
	<option value="multicopyposts">Duplicate Posts</option>');
function copypost_activate()
{
	global $db;
	
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('showthread_inlinemoderation', '#'.preg_quote('<option value="multiunapproveposts">{$lang->inline_unapprove_posts}</option>').'#', '<option value="multiunapproveposts">{$lang->inline_unapprove_posts}</option>'.COPYPOST_TEMPLATE_ADD);
	
	$newtemplate = '<html>
<head>
<title>{$mybb->settings[\'bbname\']} - Duplicate Posts</title>
{$headerinclude}
</head>
<body>
{$header}
<form action="moderation.php" method="post">
<table border="0" cellspacing="{$theme[\'borderwidth\']}" cellpadding="{$theme[\'tablespace\']}" class="tborder">
<tr>
<td class="thead" colspan="2"><strong>Duplicate Post(s)</strong></td>
</tr>
{$loginbox}
{$copypost_content}
</table>
<br />
<div align="center"><input type="submit" class="button" name="submit" value="Duplicate Posts" /></div>
<input type="hidden" name="action" value="do_multicopyposts" />
<input type="hidden" name="tid" value="{$tid}" />
<input type="hidden" name="posts" value="{$inlineids}" />
</form>
{$footer}
</body>
</html>';
	$db->insert_query(TABLE_PREFIX.'templates', array(
		'title' => 'moderation_inline_copyposts',
		'template' => $db->escape_string($newtemplate),
		'sid' => -1,
		'version' => $mybb->version_code
	));
}

function copypost_deactivate()
{
	global $db;
	
	require MYBB_ROOT."/inc/adminfunctions_templates.php";
	find_replace_templatesets('showthread_inlinemoderation', '#'.preg_quote(COPYPOST_TEMPLATE_ADD).'#', '', 0);
	
	$db->delete_query(TABLE_PREFIX.'templates', 'title="moderation_inline_copyposts" AND sid=-1');
}

function copypost_run()
{
	global $mybb, $lang, $db;
	if($mybb->input['action'] != 'multicopyposts' && $mybb->input['action'] != 'do_multicopyposts') return;
	add_breadcrumb('Duplicate Post(s)');
	$tid = intval($mybb->input['tid']);
	/*
	$pid = intval($mybb->input['pid']);
	
	if($pid && !$tid)
	{
		$post = get_post($pid);
		$tid = $post['tid'];
		if(!$post['pid'])
			error($lang->error_invalidpost);
	}*/
	if($tid)
		$thread = get_thread($tid);
	if(!$thread['tid'])
		error($lang->error_invalidthread);
	
	if(!is_moderator($thread['fid']))
		error_no_permission();
	check_forum_password($thread['fid']);
	
	
	// do our moderation here
	if($mybb->input['action'] == 'do_multicopyposts')
	{
		$pidlist = array_map('intval', explode(',', $mybb->input['posts']));
		foreach($pidlist as $k => $pid) // remove 0 elements
			if(!$pid) unset($pidlist[$k]);
		if(empty($pidlist)) error('No posts selected for duplication!');
		
		// do our duplication!
		$unappr_count = $appr_count = 0;
		if(count($pidlist) == 1 && is_array($mybb->input['msg']))
		{
			$post = get_post(reset($pidlist));
			if(!$post['pid']) error('Invalid post specified!');
			$pid = $post['pid'];
			unset($post['pid'], $post['message']);
			
			foreach($post as $k => $v)
				$post[$k] = $db->escape_string($v);
			
			if(!$mybb->input['orig_message'])
				error('The original post MUST have a message!');
			
			foreach($mybb->input['msg'] as $k => $msg)
				if(!$msg)
				{
					unset($mybb->input['msg'][$k]);
					continue;
				}
			
			if(count($mybb->input['msg']) < 1)
				error('You need to have content in subsequent posts for duplication!');
			if(count($mybb->input['msg']) > COPYPOST_MAX_COPIES)
				error('You cannot make that number of duplications!');
			
			// validation done - almost :P
			require_once MYBB_ROOT."inc/datahandlers/post.php";
			$ph = new PostDataHandler("update");
			$ph->action = "post";
			$ph->set_data(array(
				'message' => $mybb->input['orig_message'],
				'edit_uid' => $mybb->user['uid'],
				'uid' => $mybb->user['uid'],
				'username' => $mybb->user['username'],
				'pid' => $pid
			));
			if(!$ph->validate_post())
			{
				$post_errors = $ph->get_friendly_errors();
				error($post_errors);
			}
			$ph->update_post();
			unset($ph);
			
			// we won't bother validating for dupe posts
			foreach($mybb->input['msg'] as $k => $msg)
			{
				$post['message'] = $db->escape_string($msg);
				$db->insert_query(TABLE_PREFIX.'posts', $post);
			}
			
			if($post['visible'] == '1')
				$appr_count += count($mybb->input['msg']);
			elseif($post['visible'] == '0')
				$unappr_count += count($mybb->input['msg']);
			
			// update user's post count
			$forum = get_forum($thread['fid']);
			if($forum['usepostcounts'] != 'no' && $appr_count)
				$db->query('UPDATE '.TABLE_PREFIX.'users SET postnum=postnum+'.$appr_count.' WHERE uid='.$post['uid'].' LIMIT 1');
			
			log_moderator_action(array('pid' => $pid, 'tid' => $thread['tid'], 'fid' => $thread['fid']), 'Dissected post.');
			$redirect_msg = 'Selected post successfully dissected!';
		}
		else
		{
			$copies = intval($mybb->input['copies']);
			if($copies < 1) error('You must make at least one copy!');
			if($copies > COPYPOST_MAX_COPIES) error('Sorry, you cannot make that many copies.');
			
			$query = $db->simple_select(TABLE_PREFIX.'posts', '*', 'pid IN ('.implode(',', $pidlist).')');
			$fidposts = array();
			while($post = $db->fetch_array($query))
			{
				//$pid = $post['pid'];
				unset($post['pid']);
				if($post['visible'] == '1')
					++$appr_count;
				elseif($post['visible'] == '0')
					++$unappr_count;
				
				foreach($post as $k => $v)
					$post[$k] = $db->escape_string($v);
				
				for($i=0; $i<$copies; $i++)
					$db->insert_query(TABLE_PREFIX.'posts', $post);
				
				$fidposts[$post['fid']][$post['uid']] += $copies;
			}
			
			// update users' post counts too >_>
			$forums = $GLOBALS['cache']->read('forums');
			$useradd = array();
			foreach($fidposts as $fid => $userlist)
				if($forums[$fid]['usepostcounts'] != 'no')
					foreach($userlist as $uid => $posts)
						$useradd[$uid] += $posts;
			
			foreach($useradd as $uid => $posts)
			{
				$db->query('UPDATE '.TABLE_PREFIX.'users SET postnum=postnum+'.$posts.' WHERE uid='.$uid.' LIMIT 1');
			}
			
			log_moderator_action(array('tid' => $thread['tid'], 'fid' => $thread['fid']), 'Duplicated posts.');
			$redirect_msg = 'Selected post(s) successfully duplicated!';
		}
		// update thread counters
		if(function_exists('update_thread_counters'))
		{
			update_thread_counters($thread['tid'], array('replies' => '+'.$appr_count, 'unapprovedposts' => '+'.$unappr_count));
			update_forum_counters($thread['fid'], array('posts' => '+'.$appr_count, 'unapprovedposts' => '+'.$unappr_count));
		}
		else
		{
			update_thread_count($thread['tid']);
			update_forum_count($thread['fid']);
		}
		redirect("showthread.php?tid=$tid".($pid ? "&pid=$pid#pid$pid" : ''), $redirect_msg);
	}
	else
	{
		// display duplication page
		$posts = getids($tid, 'thread');
		foreach($posts as $k => $post) // remove 0 elems
			if(!$post) unset($posts[$k]);
		if(empty($posts))
			error($lang->error_inline_nopostsselected);
		$inlineids = implode(',', $posts);
		clearinline($tid, 'thread');
		
		
		global $theme, $templates, $header, $footer, $headerinclude;
		if($mybb->user['uid'])
			eval("\$loginbox = \"".$templates->get("changeuserbox")."\";");
		else
			eval("\$loginbox = \"".$templates->get("loginbox")."\";");
		
		if(count($posts) == 1)
		{
			$postdata = get_post(reset($posts));
			$bgcolor2 = alt_trow();
			$bgcolor = alt_trow();
			$copypost_content = '<tr>
<td class="'.$bgcolor2.'" colspan="2" align="center">This tool allows you to dissect the selected post, into multiple posts.  Please enter the new message for each new copy you wish to create.</td>
</tr>
<tr>
<td class="'.$bgcolor.'" valign="top"><label for="orig_message"><strong>Original Post</strong></label></td>
<td class="'.$bgcolor.'"><textarea name="orig_message" cols="70" rows="10" id="orig_message">'.htmlspecialchars_uni($postdata['message']).'</textarea></td>
</tr>';
			for($i=0; $i<COPYPOST_MAX_COPIES; $i++)
			{
				$bgcolor = alt_trow();
				$copypost_content .= '<tr>
<td class="'.$bgcolor.'" valign="top"><label for="msg_'.$i.'"><strong>Post Copy #'.($i+1).'</strong></label><br /><span class="smalltext">[<a href="javascript:$(\'msg_'.$i.'\').value=$(\'orig_message\').value;void(0);">Copy Original Message</a>]</span></td>
<td class="'.$bgcolor.'"><textarea name="msg['.$i.']" cols="70" rows="10" id="msg_'.$i.'"></textarea></td>
</tr>';
			}
		}
		else
		{
			$copypost_content = '<tr>
<td class="trow1"><strong>Number of Copies to make:</strong></td>
<td class="trow1"><input type="text" name="copies" value="1" size="10" maxlen="2" /></td>
</tr>
';
		}
		
		output_page(eval('return "'.$templates->get('moderation_inline_copyposts').'";'));
		
	}
	exit;
}

?>