includes/mcp/mcp_topic.php
author indvdum (gotoindvdum[at]gmail[dot]com)
Wed, 07 Mar 2012 23:21:18 +0400
changeset 9 e5e5221fa5e1
parent 0 ceef6d64c21d
permissions -rw-r--r--
HG ignore
indvdum@0
     1
<?php
indvdum@0
     2
/**
indvdum@0
     3
*
indvdum@0
     4
* @package mcp
indvdum@0
     5
* @version $Id$
indvdum@0
     6
* @copyright (c) 2005 phpBB Group
indvdum@0
     7
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
indvdum@0
     8
*
indvdum@0
     9
*/
indvdum@0
    10
indvdum@0
    11
/**
indvdum@0
    12
* @ignore
indvdum@0
    13
*/
indvdum@0
    14
if (!defined('IN_PHPBB'))
indvdum@0
    15
{
indvdum@0
    16
	exit;
indvdum@0
    17
}
indvdum@0
    18
indvdum@0
    19
/**
indvdum@0
    20
* View topic in MCP
indvdum@0
    21
*/
indvdum@0
    22
function mcp_topic_view($id, $mode, $action)
indvdum@0
    23
{
indvdum@0
    24
	global $phpEx, $phpbb_root_path, $config;
indvdum@0
    25
	global $template, $db, $user, $auth, $cache;
indvdum@0
    26
indvdum@0
    27
	$url = append_sid("{$phpbb_root_path}mcp.$phpEx?" . extra_url());
indvdum@0
    28
indvdum@0
    29
	$user->add_lang('viewtopic');
indvdum@0
    30
indvdum@0
    31
	$topic_id = request_var('t', 0);
indvdum@0
    32
	$topic_info = get_topic_data(array($topic_id), false, true);
indvdum@0
    33
indvdum@0
    34
	if (!sizeof($topic_info))
indvdum@0
    35
	{
indvdum@0
    36
		trigger_error('TOPIC_NOT_EXIST');
indvdum@0
    37
	}
indvdum@0
    38
indvdum@0
    39
	$topic_info = $topic_info[$topic_id];
indvdum@0
    40
indvdum@0
    41
	// Set up some vars
indvdum@0
    42
	$icon_id		= request_var('icon', 0);
indvdum@0
    43
	$subject		= utf8_normalize_nfc(request_var('subject', '', true));
indvdum@0
    44
	$start			= request_var('start', 0);
indvdum@0
    45
	$sort_days_old	= request_var('st_old', 0);
indvdum@0
    46
	$forum_id		= request_var('f', 0);
indvdum@0
    47
	$to_topic_id	= request_var('to_topic_id', 0);
indvdum@0
    48
	$to_forum_id	= request_var('to_forum_id', 0);
indvdum@0
    49
	$sort			= isset($_POST['sort']) ? true : false;
indvdum@0
    50
	$submitted_id_list	= request_var('post_ids', array(0));
indvdum@0
    51
	$checked_ids = $post_id_list = request_var('post_id_list', array(0));
indvdum@0
    52
indvdum@0
    53
	// Split Topic?
indvdum@0
    54
	if ($action == 'split_all' || $action == 'split_beyond')
indvdum@0
    55
	{
indvdum@0
    56
		if (!$sort)
indvdum@0
    57
		{
indvdum@0
    58
			split_topic($action, $topic_id, $to_forum_id, $subject);
indvdum@0
    59
		}
indvdum@0
    60
		$action = 'split';
indvdum@0
    61
	}
indvdum@0
    62
indvdum@0
    63
	// Merge Posts?
indvdum@0
    64
	if ($action == 'merge_posts')
indvdum@0
    65
	{
indvdum@0
    66
		if (!$sort)
indvdum@0
    67
		{
indvdum@0
    68
			merge_posts($topic_id, $to_topic_id);
indvdum@0
    69
		}
indvdum@0
    70
		$action = 'merge';
indvdum@0
    71
	}
indvdum@0
    72
indvdum@0
    73
	if ($action == 'split' && !$subject)
indvdum@0
    74
	{
indvdum@0
    75
		$subject = $topic_info['topic_title'];
indvdum@0
    76
	}
indvdum@0
    77
indvdum@0
    78
	// Approve posts?
indvdum@0
    79
	if ($action == 'approve' && $auth->acl_get('m_approve', $topic_info['forum_id']))
indvdum@0
    80
	{
indvdum@0
    81
		include($phpbb_root_path . 'includes/mcp/mcp_queue.' . $phpEx);
indvdum@0
    82
		include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
indvdum@0
    83
		include_once($phpbb_root_path . 'includes/functions_messenger.' . $phpEx);
indvdum@0
    84
indvdum@0
    85
		if (!sizeof($post_id_list))
indvdum@0
    86
		{
indvdum@0
    87
			trigger_error('NO_POST_SELECTED');
indvdum@0
    88
		}
indvdum@0
    89
indvdum@0
    90
		if (!$sort)
indvdum@0
    91
		{
indvdum@0
    92
			approve_post($post_id_list, $id, $mode);
indvdum@0
    93
		}
indvdum@0
    94
	}
indvdum@0
    95
indvdum@0
    96
	// Jumpbox, sort selects and that kind of things
indvdum@0
    97
	make_jumpbox($url . "&amp;i=$id&amp;mode=forum_view", $topic_info['forum_id'], false, 'm_', true);
indvdum@0
    98
	$where_sql = ($action == 'reports') ? 'WHERE post_reported = 1 AND ' : 'WHERE';
indvdum@0
    99
indvdum@0
   100
	$sort_days = $total = 0;
indvdum@0
   101
	$sort_key = $sort_dir = '';
indvdum@0
   102
	$sort_by_sql = $sort_order_sql = array();
indvdum@0
   103
	mcp_sorting('viewtopic', $sort_days, $sort_key, $sort_dir, $sort_by_sql, $sort_order_sql, $total, $topic_info['forum_id'], $topic_id, $where_sql);
indvdum@0
   104
indvdum@0
   105
	$limit_time_sql = ($sort_days) ? 'AND p.post_time >= ' . (time() - ($sort_days * 86400)) : '';
indvdum@0
   106
indvdum@0
   107
	if ($total == -1)
indvdum@0
   108
	{
indvdum@0
   109
		if ($auth->acl_get('m_approve', $topic_info['forum_id']))
indvdum@0
   110
		{
indvdum@0
   111
			$total = $topic_info['topic_replies_real'] + 1;
indvdum@0
   112
		}
indvdum@0
   113
		else
indvdum@0
   114
		{
indvdum@0
   115
			$total = $topic_info['topic_replies'] + 1;
indvdum@0
   116
		}
indvdum@0
   117
	}
indvdum@0
   118
indvdum@0
   119
	$posts_per_page = max(0, request_var('posts_per_page', intval($config['posts_per_page'])));
indvdum@0
   120
	if ($posts_per_page == 0)
indvdum@0
   121
	{
indvdum@0
   122
		$posts_per_page = $total;
indvdum@0
   123
	}
indvdum@0
   124
indvdum@0
   125
	if ((!empty($sort_days_old) && $sort_days_old != $sort_days) || $total <= $posts_per_page)
indvdum@0
   126
	{
indvdum@0
   127
		$start = 0;
indvdum@0
   128
	}
indvdum@0
   129
indvdum@0
   130
	// Make sure $start is set to the last page if it exceeds the amount
indvdum@0
   131
	if ($start < 0 || $start >= $total)
indvdum@0
   132
	{
indvdum@0
   133
		$start = ($start < 0) ? 0 : floor(($total - 1) / $posts_per_page) * $posts_per_page;
indvdum@0
   134
	}
indvdum@0
   135
indvdum@0
   136
	$sql = 'SELECT u.username, u.username_clean, u.user_colour, p.*
indvdum@0
   137
		FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . ' u
indvdum@0
   138
		WHERE ' . (($action == 'reports') ? 'p.post_reported = 1 AND ' : '') . '
indvdum@0
   139
			p.topic_id = ' . $topic_id . ' ' .
indvdum@0
   140
			((!$auth->acl_get('m_approve', $topic_info['forum_id'])) ? ' AND p.post_approved = 1 ' : '') . '
indvdum@0
   141
			AND p.poster_id = u.user_id ' .
indvdum@0
   142
			$limit_time_sql . '
indvdum@0
   143
		ORDER BY ' . $sort_order_sql;
indvdum@0
   144
	$result = $db->sql_query_limit($sql, $posts_per_page, $start);
indvdum@0
   145
indvdum@0
   146
	$rowset = $post_id_list = array();
indvdum@0
   147
	$bbcode_bitfield = '';
indvdum@0
   148
	while ($row = $db->sql_fetchrow($result))
indvdum@0
   149
	{
indvdum@0
   150
		$rowset[] = $row;
indvdum@0
   151
		$post_id_list[] = $row['post_id'];
indvdum@0
   152
		$bbcode_bitfield = $bbcode_bitfield | base64_decode($row['bbcode_bitfield']);
indvdum@0
   153
	}
indvdum@0
   154
	$db->sql_freeresult($result);
indvdum@0
   155
indvdum@0
   156
	if ($bbcode_bitfield !== '')
indvdum@0
   157
	{
indvdum@0
   158
		include_once($phpbb_root_path . 'includes/bbcode.' . $phpEx);
indvdum@0
   159
		$bbcode = new bbcode(base64_encode($bbcode_bitfield));
indvdum@0
   160
	}
indvdum@0
   161
indvdum@0
   162
	$topic_tracking_info = array();
indvdum@0
   163
indvdum@0
   164
	// Get topic tracking info
indvdum@0
   165
	if ($config['load_db_lastread'])
indvdum@0
   166
	{
indvdum@0
   167
		$tmp_topic_data = array($topic_id => $topic_info);
indvdum@0
   168
		$topic_tracking_info = get_topic_tracking($topic_info['forum_id'], $topic_id, $tmp_topic_data, array($topic_info['forum_id'] => $topic_info['forum_mark_time']));
indvdum@0
   169
		unset($tmp_topic_data);
indvdum@0
   170
	}
indvdum@0
   171
	else
indvdum@0
   172
	{
indvdum@0
   173
		$topic_tracking_info = get_complete_topic_tracking($topic_info['forum_id'], $topic_id);
indvdum@0
   174
	}
indvdum@0
   175
indvdum@0
   176
	$has_unapproved_posts = false;
indvdum@0
   177
indvdum@0
   178
	// Grab extensions
indvdum@0
   179
	$extensions = $attachments = array();
indvdum@0
   180
	if ($topic_info['topic_attachment'] && sizeof($post_id_list))
indvdum@0
   181
	{
indvdum@0
   182
		$extensions = $cache->obtain_attach_extensions($topic_info['forum_id']);
indvdum@0
   183
indvdum@0
   184
		// Get attachments...
indvdum@0
   185
		if ($auth->acl_get('u_download') && $auth->acl_get('f_download', $topic_info['forum_id']))
indvdum@0
   186
		{
indvdum@0
   187
			$sql = 'SELECT *
indvdum@0
   188
				FROM ' . ATTACHMENTS_TABLE . '
indvdum@0
   189
				WHERE ' . $db->sql_in_set('post_msg_id', $post_id_list) . '
indvdum@0
   190
					AND in_message = 0
indvdum@0
   191
				ORDER BY filetime DESC, post_msg_id ASC';
indvdum@0
   192
			$result = $db->sql_query($sql);
indvdum@0
   193
indvdum@0
   194
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   195
			{
indvdum@0
   196
				$attachments[$row['post_msg_id']][] = $row;
indvdum@0
   197
			}
indvdum@0
   198
			$db->sql_freeresult($result);
indvdum@0
   199
		}
indvdum@0
   200
	}
indvdum@0
   201
indvdum@0
   202
	foreach ($rowset as $i => $row)
indvdum@0
   203
	{
indvdum@0
   204
		$message = $row['post_text'];
indvdum@0
   205
		$post_subject = ($row['post_subject'] != '') ? $row['post_subject'] : $topic_info['topic_title'];
indvdum@0
   206
indvdum@0
   207
		if ($row['bbcode_bitfield'])
indvdum@0
   208
		{
indvdum@0
   209
			$bbcode->bbcode_second_pass($message, $row['bbcode_uid'], $row['bbcode_bitfield']);
indvdum@0
   210
		}
indvdum@0
   211
indvdum@0
   212
		$message = bbcode_nl2br($message);
indvdum@0
   213
		$message = smiley_text($message);
indvdum@0
   214
indvdum@0
   215
		if (!empty($attachments[$row['post_id']]))
indvdum@0
   216
		{
indvdum@0
   217
			$update_count = array();
indvdum@0
   218
			parse_attachments($topic_info['forum_id'], $message, $attachments[$row['post_id']], $update_count);
indvdum@0
   219
		}
indvdum@0
   220
indvdum@0
   221
		if (!$row['post_approved'])
indvdum@0
   222
		{
indvdum@0
   223
			$has_unapproved_posts = true;
indvdum@0
   224
		}
indvdum@0
   225
indvdum@0
   226
		$post_unread = (isset($topic_tracking_info[$topic_id]) && $row['post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
indvdum@0
   227
indvdum@0
   228
		$template->assign_block_vars('postrow', array(
indvdum@0
   229
			'POST_AUTHOR_FULL'		=> get_username_string('full', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
   230
			'POST_AUTHOR_COLOUR'	=> get_username_string('colour', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
   231
			'POST_AUTHOR'			=> get_username_string('username', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
   232
			'U_POST_AUTHOR'			=> get_username_string('profile', $row['poster_id'], $row['username'], $row['user_colour'], $row['post_username']),
indvdum@0
   233
indvdum@0
   234
			'POST_DATE'		=> $user->format_date($row['post_time']),
indvdum@0
   235
			'POST_SUBJECT'	=> $post_subject,
indvdum@0
   236
			'MESSAGE'		=> $message,
indvdum@0
   237
			'POST_ID'		=> $row['post_id'],
indvdum@0
   238
			'RETURN_TOPIC'	=> sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $topic_id) . '">', '</a>'),
indvdum@0
   239
indvdum@0
   240
			'MINI_POST_IMG'			=> ($post_unread) ? $user->img('icon_post_target_unread', 'UNREAD_POST') : $user->img('icon_post_target', 'POST'),
indvdum@0
   241
indvdum@0
   242
			'S_POST_REPORTED'	=> ($row['post_reported'] && $auth->acl_get('m_report', $topic_info['forum_id'])),
indvdum@0
   243
			'S_POST_UNAPPROVED'	=> (!$row['post_approved'] && $auth->acl_get('m_approve', $topic_info['forum_id'])),
indvdum@0
   244
			'S_CHECKED'			=> (($submitted_id_list && !in_array(intval($row['post_id']), $submitted_id_list)) || in_array(intval($row['post_id']), $checked_ids)) ? true : false,
indvdum@0
   245
			'S_HAS_ATTACHMENTS'	=> (!empty($attachments[$row['post_id']])) ? true : false,
indvdum@0
   246
indvdum@0
   247
			'U_POST_DETAILS'	=> "$url&amp;i=$id&amp;p={$row['post_id']}&amp;mode=post_details" . (($forum_id) ? "&amp;f=$forum_id" : ''),
indvdum@0
   248
			'U_MCP_APPROVE'		=> ($auth->acl_get('m_approve', $topic_info['forum_id'])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=queue&amp;mode=approve_details&amp;f=' . $topic_info['forum_id'] . '&amp;p=' . $row['post_id']) : '',
indvdum@0
   249
			'U_MCP_REPORT'		=> ($auth->acl_get('m_report', $topic_info['forum_id'])) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=reports&amp;mode=report_details&amp;f=' . $topic_info['forum_id'] . '&amp;p=' . $row['post_id']) : '')
indvdum@0
   250
		);
indvdum@0
   251
indvdum@0
   252
		// Display not already displayed Attachments for this post, we already parsed them. ;)
indvdum@0
   253
		if (!empty($attachments[$row['post_id']]))
indvdum@0
   254
		{
indvdum@0
   255
			foreach ($attachments[$row['post_id']] as $attachment)
indvdum@0
   256
			{
indvdum@0
   257
				$template->assign_block_vars('postrow.attachment', array(
indvdum@0
   258
					'DISPLAY_ATTACHMENT'	=> $attachment)
indvdum@0
   259
				);
indvdum@0
   260
			}
indvdum@0
   261
		}
indvdum@0
   262
indvdum@0
   263
		unset($rowset[$i]);
indvdum@0
   264
	}
indvdum@0
   265
indvdum@0
   266
	// Display topic icons for split topic
indvdum@0
   267
	$s_topic_icons = false;
indvdum@0
   268
indvdum@0
   269
	if ($auth->acl_gets('m_split', 'm_merge', (int) $topic_info['forum_id']))
indvdum@0
   270
	{
indvdum@0
   271
		include_once($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
indvdum@0
   272
		$s_topic_icons = posting_gen_topic_icons('', $icon_id);
indvdum@0
   273
indvdum@0
   274
		// Has the user selected a topic for merge?
indvdum@0
   275
		if ($to_topic_id)
indvdum@0
   276
		{
indvdum@0
   277
			$to_topic_info = get_topic_data(array($to_topic_id), 'm_merge');
indvdum@0
   278
indvdum@0
   279
			if (!sizeof($to_topic_info))
indvdum@0
   280
			{
indvdum@0
   281
				$to_topic_id = 0;
indvdum@0
   282
			}
indvdum@0
   283
			else
indvdum@0
   284
			{
indvdum@0
   285
				$to_topic_info = $to_topic_info[$to_topic_id];
indvdum@0
   286
indvdum@0
   287
				if (!$to_topic_info['enable_icons'] || $auth->acl_get('!f_icons', $topic_info['forum_id']))
indvdum@0
   288
				{
indvdum@0
   289
					$s_topic_icons = false;
indvdum@0
   290
				}
indvdum@0
   291
			}
indvdum@0
   292
		}
indvdum@0
   293
	}
indvdum@0
   294
indvdum@0
   295
	$s_hidden_fields = build_hidden_fields(array(
indvdum@0
   296
		'st_old'	=> $sort_days,
indvdum@0
   297
		'post_ids'	=> $post_id_list,
indvdum@0
   298
	));
indvdum@0
   299
indvdum@0
   300
	$template->assign_vars(array(
indvdum@0
   301
		'TOPIC_TITLE'		=> $topic_info['topic_title'],
indvdum@0
   302
		'U_VIEW_TOPIC'		=> append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $topic_info['forum_id'] . '&amp;t=' . $topic_info['topic_id']),
indvdum@0
   303
indvdum@0
   304
		'TO_TOPIC_ID'		=> $to_topic_id,
indvdum@0
   305
		'TO_TOPIC_INFO'		=> ($to_topic_id) ? sprintf($user->lang['YOU_SELECTED_TOPIC'], $to_topic_id, '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $to_topic_info['forum_id'] . '&amp;t=' . $to_topic_id) . '">' . $to_topic_info['topic_title'] . '</a>') : '',
indvdum@0
   306
indvdum@0
   307
		'SPLIT_SUBJECT'		=> $subject,
indvdum@0
   308
		'POSTS_PER_PAGE'	=> $posts_per_page,
indvdum@0
   309
		'ACTION'			=> $action,
indvdum@0
   310
indvdum@0
   311
		'REPORTED_IMG'		=> $user->img('icon_topic_reported', 'POST_REPORTED'),
indvdum@0
   312
		'UNAPPROVED_IMG'	=> $user->img('icon_topic_unapproved', 'POST_UNAPPROVED'),
indvdum@0
   313
		'INFO_IMG'			=> $user->img('icon_post_info', 'VIEW_INFO'),
indvdum@0
   314
indvdum@0
   315
		'S_MCP_ACTION'		=> "$url&amp;i=$id&amp;mode=$mode&amp;action=$action&amp;start=$start",
indvdum@0
   316
		'S_FORUM_SELECT'	=> ($to_forum_id) ? make_forum_select($to_forum_id, false, false, true, true, true) : make_forum_select($topic_info['forum_id'], false, false, true, true, true),
indvdum@0
   317
		'S_CAN_SPLIT'		=> ($auth->acl_get('m_split', $topic_info['forum_id'])) ? true : false,
indvdum@0
   318
		'S_CAN_MERGE'		=> ($auth->acl_get('m_merge', $topic_info['forum_id'])) ? true : false,
indvdum@0
   319
		'S_CAN_DELETE'		=> ($auth->acl_get('m_delete', $topic_info['forum_id'])) ? true : false,
indvdum@0
   320
		'S_CAN_APPROVE'		=> ($has_unapproved_posts && $auth->acl_get('m_approve', $topic_info['forum_id'])) ? true : false,
indvdum@0
   321
		'S_CAN_LOCK'		=> ($auth->acl_get('m_lock', $topic_info['forum_id'])) ? true : false,
indvdum@0
   322
		'S_CAN_REPORT'		=> ($auth->acl_get('m_report', $topic_info['forum_id'])) ? true : false,
indvdum@0
   323
		'S_REPORT_VIEW'		=> ($action == 'reports') ? true : false,
indvdum@0
   324
		'S_MERGE_VIEW'		=> ($action == 'merge') ? true : false,
indvdum@0
   325
		'S_SPLIT_VIEW'		=> ($action == 'split') ? true : false,
indvdum@0
   326
indvdum@0
   327
		'S_HIDDEN_FIELDS'	=> $s_hidden_fields,
indvdum@0
   328
indvdum@0
   329
		'S_SHOW_TOPIC_ICONS'	=> $s_topic_icons,
indvdum@0
   330
		'S_TOPIC_ICON'			=> $icon_id,
indvdum@0
   331
indvdum@0
   332
		'U_SELECT_TOPIC'	=> "$url&amp;i=$id&amp;mode=forum_view&amp;action=merge_select" . (($forum_id) ? "&amp;f=$forum_id" : ''),
indvdum@0
   333
indvdum@0
   334
		'RETURN_TOPIC'		=> sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f={$topic_info['forum_id']}&amp;t={$topic_info['topic_id']}&amp;start=$start") . '">', '</a>'),
indvdum@0
   335
		'RETURN_FORUM'		=> sprintf($user->lang['RETURN_FORUM'], '<a href="' . append_sid("{$phpbb_root_path}viewforum.$phpEx", "f={$topic_info['forum_id']}&amp;start=$start") . '">', '</a>'),
indvdum@0
   336
indvdum@0
   337
		'PAGE_NUMBER'		=> on_page($total, $posts_per_page, $start),
indvdum@0
   338
		'PAGINATION'		=> (!$posts_per_page) ? '' : generate_pagination(append_sid("{$phpbb_root_path}mcp.$phpEx", "i=$id&amp;t={$topic_info['topic_id']}&amp;mode=$mode&amp;action=$action&amp;to_topic_id=$to_topic_id&amp;posts_per_page=$posts_per_page&amp;st=$sort_days&amp;sk=$sort_key&amp;sd=$sort_dir"), $total, $posts_per_page, $start),
indvdum@0
   339
		'TOTAL_POSTS'		=> ($total == 1) ? $user->lang['VIEW_TOPIC_POST'] : sprintf($user->lang['VIEW_TOPIC_POSTS'], $total),
indvdum@0
   340
	));
indvdum@0
   341
}
indvdum@0
   342
indvdum@0
   343
/**
indvdum@0
   344
* Split topic
indvdum@0
   345
*/
indvdum@0
   346
function split_topic($action, $topic_id, $to_forum_id, $subject)
indvdum@0
   347
{
indvdum@0
   348
	global $db, $template, $user, $phpEx, $phpbb_root_path, $auth, $config;
indvdum@0
   349
indvdum@0
   350
	$post_id_list	= request_var('post_id_list', array(0));
indvdum@0
   351
	$forum_id		= request_var('forum_id', 0);
indvdum@0
   352
	$start			= request_var('start', 0);
indvdum@0
   353
indvdum@0
   354
	if (!sizeof($post_id_list))
indvdum@0
   355
	{
indvdum@0
   356
		$template->assign_var('MESSAGE', $user->lang['NO_POST_SELECTED']);
indvdum@0
   357
		return;
indvdum@0
   358
	}
indvdum@0
   359
indvdum@0
   360
	if (!check_ids($post_id_list, POSTS_TABLE, 'post_id', array('m_split')))
indvdum@0
   361
	{
indvdum@0
   362
		return;
indvdum@0
   363
	}
indvdum@0
   364
indvdum@0
   365
	$post_id = $post_id_list[0];
indvdum@0
   366
	$post_info = get_post_data(array($post_id));
indvdum@0
   367
indvdum@0
   368
	if (!sizeof($post_info))
indvdum@0
   369
	{
indvdum@0
   370
		$template->assign_var('MESSAGE', $user->lang['NO_POST_SELECTED']);
indvdum@0
   371
		return;
indvdum@0
   372
	}
indvdum@0
   373
indvdum@0
   374
	$post_info = $post_info[$post_id];
indvdum@0
   375
	$subject = trim($subject);
indvdum@0
   376
indvdum@0
   377
	// Make some tests
indvdum@0
   378
	if (!$subject)
indvdum@0
   379
	{
indvdum@0
   380
		$template->assign_var('MESSAGE', $user->lang['EMPTY_SUBJECT']);
indvdum@0
   381
		return;
indvdum@0
   382
	}
indvdum@0
   383
indvdum@0
   384
	if ($to_forum_id <= 0)
indvdum@0
   385
	{
indvdum@0
   386
		$template->assign_var('MESSAGE', $user->lang['NO_DESTINATION_FORUM']);
indvdum@0
   387
		return;
indvdum@0
   388
	}
indvdum@0
   389
indvdum@0
   390
	$forum_info = get_forum_data(array($to_forum_id), 'f_post');
indvdum@0
   391
indvdum@0
   392
	if (!sizeof($forum_info))
indvdum@0
   393
	{
indvdum@0
   394
		$template->assign_var('MESSAGE', $user->lang['USER_CANNOT_POST']);
indvdum@0
   395
		return;
indvdum@0
   396
	}
indvdum@0
   397
indvdum@0
   398
	$forum_info = $forum_info[$to_forum_id];
indvdum@0
   399
indvdum@0
   400
	if ($forum_info['forum_type'] != FORUM_POST)
indvdum@0
   401
	{
indvdum@0
   402
		$template->assign_var('MESSAGE', $user->lang['FORUM_NOT_POSTABLE']);
indvdum@0
   403
		return;
indvdum@0
   404
	}
indvdum@0
   405
indvdum@0
   406
	$redirect = request_var('redirect', build_url(array('quickmod')));
indvdum@0
   407
indvdum@0
   408
	$s_hidden_fields = build_hidden_fields(array(
indvdum@0
   409
		'i'				=> 'main',
indvdum@0
   410
		'post_id_list'	=> $post_id_list,
indvdum@0
   411
		'f'				=> $forum_id,
indvdum@0
   412
		'mode'			=> 'topic_view',
indvdum@0
   413
		'start'			=> $start,
indvdum@0
   414
		'action'		=> $action,
indvdum@0
   415
		't'				=> $topic_id,
indvdum@0
   416
		'redirect'		=> $redirect,
indvdum@0
   417
		'subject'		=> $subject,
indvdum@0
   418
		'to_forum_id'	=> $to_forum_id,
indvdum@0
   419
		'icon'			=> request_var('icon', 0))
indvdum@0
   420
	);
indvdum@0
   421
	$success_msg = $return_link = '';
indvdum@0
   422
indvdum@0
   423
	if (confirm_box(true))
indvdum@0
   424
	{
indvdum@0
   425
		if ($action == 'split_beyond')
indvdum@0
   426
		{
indvdum@0
   427
			$sort_days = $total = 0;
indvdum@0
   428
			$sort_key = $sort_dir = '';
indvdum@0
   429
			$sort_by_sql = $sort_order_sql = array();
indvdum@0
   430
			mcp_sorting('viewtopic', $sort_days, $sort_key, $sort_dir, $sort_by_sql, $sort_order_sql, $total, $forum_id, $topic_id);
indvdum@0
   431
indvdum@0
   432
			$limit_time_sql = ($sort_days) ? 'AND t.topic_last_post_time >= ' . (time() - ($sort_days * 86400)) : '';
indvdum@0
   433
indvdum@0
   434
			if ($sort_order_sql[0] == 'u')
indvdum@0
   435
			{
indvdum@0
   436
				$sql = 'SELECT p.post_id, p.forum_id, p.post_approved
indvdum@0
   437
					FROM ' . POSTS_TABLE . ' p, ' . USERS_TABLE . " u
indvdum@0
   438
					WHERE p.topic_id = $topic_id
indvdum@0
   439
						AND p.poster_id = u.user_id
indvdum@0
   440
						$limit_time_sql
indvdum@0
   441
					ORDER BY $sort_order_sql";
indvdum@0
   442
			}
indvdum@0
   443
			else
indvdum@0
   444
			{
indvdum@0
   445
				$sql = 'SELECT p.post_id, p.forum_id, p.post_approved
indvdum@0
   446
					FROM ' . POSTS_TABLE . " p
indvdum@0
   447
					WHERE p.topic_id = $topic_id
indvdum@0
   448
						$limit_time_sql
indvdum@0
   449
					ORDER BY $sort_order_sql";
indvdum@0
   450
			}
indvdum@0
   451
			$result = $db->sql_query_limit($sql, 0, $start);
indvdum@0
   452
indvdum@0
   453
			$store = false;
indvdum@0
   454
			$post_id_list = array();
indvdum@0
   455
			while ($row = $db->sql_fetchrow($result))
indvdum@0
   456
			{
indvdum@0
   457
				// If split from selected post (split_beyond), we split the unapproved items too.
indvdum@0
   458
				if (!$row['post_approved'] && !$auth->acl_get('m_approve', $row['forum_id']))
indvdum@0
   459
				{
indvdum@0
   460
//					continue;
indvdum@0
   461
				}
indvdum@0
   462
indvdum@0
   463
				// Start to store post_ids as soon as we see the first post that was selected
indvdum@0
   464
				if ($row['post_id'] == $post_id)
indvdum@0
   465
				{
indvdum@0
   466
					$store = true;
indvdum@0
   467
				}
indvdum@0
   468
indvdum@0
   469
				if ($store)
indvdum@0
   470
				{
indvdum@0
   471
					$post_id_list[] = $row['post_id'];
indvdum@0
   472
				}
indvdum@0
   473
			}
indvdum@0
   474
			$db->sql_freeresult($result);
indvdum@0
   475
		}
indvdum@0
   476
indvdum@0
   477
		if (!sizeof($post_id_list))
indvdum@0
   478
		{
indvdum@0
   479
			trigger_error('NO_POST_SELECTED');
indvdum@0
   480
		}
indvdum@0
   481
indvdum@0
   482
		$icon_id = request_var('icon', 0);
indvdum@0
   483
indvdum@0
   484
		$sql_ary = array(
indvdum@0
   485
			'forum_id'		=> $to_forum_id,
indvdum@0
   486
			'topic_title'	=> $subject,
indvdum@0
   487
			'icon_id'		=> $icon_id,
indvdum@0
   488
			'topic_approved'=> 1
indvdum@0
   489
		);
indvdum@0
   490
indvdum@0
   491
		$sql = 'INSERT INTO ' . TOPICS_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
indvdum@0
   492
		$db->sql_query($sql);
indvdum@0
   493
indvdum@0
   494
		$to_topic_id = $db->sql_nextid();
indvdum@0
   495
		move_posts($post_id_list, $to_topic_id);
indvdum@0
   496
indvdum@0
   497
		$topic_info = get_topic_data(array($topic_id));
indvdum@0
   498
		$topic_info = $topic_info[$topic_id];
indvdum@0
   499
indvdum@0
   500
		add_log('mod', $to_forum_id, $to_topic_id, 'LOG_SPLIT_DESTINATION', $subject);
indvdum@0
   501
		add_log('mod', $forum_id, $topic_id, 'LOG_SPLIT_SOURCE', $topic_info['topic_title']);
indvdum@0
   502
indvdum@0
   503
		// Change topic title of first post
indvdum@0
   504
		$sql = 'UPDATE ' . POSTS_TABLE . "
indvdum@0
   505
			SET post_subject = '" . $db->sql_escape($subject) . "'
indvdum@0
   506
			WHERE post_id = {$post_id_list[0]}";
indvdum@0
   507
		$db->sql_query($sql);
indvdum@0
   508
indvdum@0
   509
		$success_msg = 'TOPIC_SPLIT_SUCCESS';
indvdum@0
   510
indvdum@0
   511
		// Update forum statistics
indvdum@0
   512
		set_config_count('num_topics', 1, true);
indvdum@0
   513
indvdum@0
   514
		// Link back to both topics
indvdum@0
   515
		$return_link = sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $post_info['forum_id'] . '&amp;t=' . $post_info['topic_id']) . '">', '</a>') . '<br /><br />' . sprintf($user->lang['RETURN_NEW_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $to_forum_id . '&amp;t=' . $to_topic_id) . '">', '</a>');
indvdum@0
   516
	}
indvdum@0
   517
	else
indvdum@0
   518
	{
indvdum@0
   519
		confirm_box(false, ($action == 'split_all') ? 'SPLIT_TOPIC_ALL' : 'SPLIT_TOPIC_BEYOND', $s_hidden_fields);
indvdum@0
   520
	}
indvdum@0
   521
indvdum@0
   522
	$redirect = request_var('redirect', "index.$phpEx");
indvdum@0
   523
	$redirect = reapply_sid($redirect);
indvdum@0
   524
indvdum@0
   525
	if (!$success_msg)
indvdum@0
   526
	{
indvdum@0
   527
		return;
indvdum@0
   528
	}
indvdum@0
   529
	else
indvdum@0
   530
	{
indvdum@0
   531
		meta_refresh(3, append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$to_forum_id&amp;t=$to_topic_id"));
indvdum@0
   532
		trigger_error($user->lang[$success_msg] . '<br /><br />' . $return_link);
indvdum@0
   533
	}
indvdum@0
   534
}
indvdum@0
   535
indvdum@0
   536
/**
indvdum@0
   537
* Merge selected posts into selected topic
indvdum@0
   538
*/
indvdum@0
   539
function merge_posts($topic_id, $to_topic_id)
indvdum@0
   540
{
indvdum@0
   541
	global $db, $template, $user, $phpEx, $phpbb_root_path, $auth;
indvdum@0
   542
indvdum@0
   543
	if (!$to_topic_id)
indvdum@0
   544
	{
indvdum@0
   545
		$template->assign_var('MESSAGE', $user->lang['NO_FINAL_TOPIC_SELECTED']);
indvdum@0
   546
		return;
indvdum@0
   547
	}
indvdum@0
   548
indvdum@0
   549
	$topic_data = get_topic_data(array($to_topic_id), 'm_merge');
indvdum@0
   550
indvdum@0
   551
	if (!sizeof($topic_data))
indvdum@0
   552
	{
indvdum@0
   553
		$template->assign_var('MESSAGE', $user->lang['NO_FINAL_TOPIC_SELECTED']);
indvdum@0
   554
		return;
indvdum@0
   555
	}
indvdum@0
   556
indvdum@0
   557
	$topic_data = $topic_data[$to_topic_id];
indvdum@0
   558
indvdum@0
   559
	$post_id_list	= request_var('post_id_list', array(0));
indvdum@0
   560
	$start			= request_var('start', 0);
indvdum@0
   561
indvdum@0
   562
	if (!sizeof($post_id_list))
indvdum@0
   563
	{
indvdum@0
   564
		$template->assign_var('MESSAGE', $user->lang['NO_POST_SELECTED']);
indvdum@0
   565
		return;
indvdum@0
   566
	}
indvdum@0
   567
indvdum@0
   568
	if (!check_ids($post_id_list, POSTS_TABLE, 'post_id', array('m_merge')))
indvdum@0
   569
	{
indvdum@0
   570
		return;
indvdum@0
   571
	}
indvdum@0
   572
indvdum@0
   573
	$redirect = request_var('redirect', build_url(array('quickmod')));
indvdum@0
   574
indvdum@0
   575
	$s_hidden_fields = build_hidden_fields(array(
indvdum@0
   576
		'i'				=> 'main',
indvdum@0
   577
		'post_id_list'	=> $post_id_list,
indvdum@0
   578
		'to_topic_id'	=> $to_topic_id,
indvdum@0
   579
		'mode'			=> 'topic_view',
indvdum@0
   580
		'action'		=> 'merge_posts',
indvdum@0
   581
		'start'			=> $start,
indvdum@0
   582
		'redirect'		=> $redirect,
indvdum@0
   583
		't'				=> $topic_id)
indvdum@0
   584
	);
indvdum@0
   585
	$success_msg = $return_link = '';
indvdum@0
   586
indvdum@0
   587
	if (confirm_box(true))
indvdum@0
   588
	{
indvdum@0
   589
		$to_forum_id = $topic_data['forum_id'];
indvdum@0
   590
indvdum@0
   591
		move_posts($post_id_list, $to_topic_id);
indvdum@0
   592
		add_log('mod', $to_forum_id, $to_topic_id, 'LOG_MERGE', $topic_data['topic_title']);
indvdum@0
   593
indvdum@0
   594
		// Message and return links
indvdum@0
   595
		$success_msg = 'POSTS_MERGED_SUCCESS';
indvdum@0
   596
indvdum@0
   597
		// Does the original topic still exist? If yes, link back to it
indvdum@0
   598
		$sql = 'SELECT forum_id
indvdum@0
   599
			FROM ' . TOPICS_TABLE . '
indvdum@0
   600
			WHERE topic_id = ' . $topic_id;
indvdum@0
   601
		$result = $db->sql_query_limit($sql, 1);
indvdum@0
   602
		$row = $db->sql_fetchrow($result);
indvdum@0
   603
		$db->sql_freeresult($result);
indvdum@0
   604
indvdum@0
   605
		if ($row)
indvdum@0
   606
		{
indvdum@0
   607
			$return_link .= sprintf($user->lang['RETURN_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $row['forum_id'] . '&amp;t=' . $topic_id) . '">', '</a>');
indvdum@0
   608
		}
indvdum@0
   609
		else
indvdum@0
   610
		{
indvdum@0
   611
			// If the topic no longer exist, we will update the topic watch table.
indvdum@0
   612
			// To not let it error out on users watching both topics, we just return on an error...
indvdum@0
   613
			$db->sql_return_on_error(true);
indvdum@0
   614
			$db->sql_query('UPDATE ' . TOPICS_WATCH_TABLE . ' SET topic_id = ' . (int) $to_topic_id . ' WHERE topic_id = ' . (int) $topic_id);
indvdum@0
   615
			$db->sql_return_on_error(false);
indvdum@0
   616
indvdum@0
   617
			$db->sql_query('DELETE FROM ' . TOPICS_WATCH_TABLE . ' WHERE topic_id = ' . (int) $topic_id);
indvdum@0
   618
		}
indvdum@0
   619
indvdum@0
   620
		// Link to the new topic
indvdum@0
   621
		$return_link .= (($return_link) ? '<br /><br />' : '') . sprintf($user->lang['RETURN_NEW_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 'f=' . $to_forum_id . '&amp;t=' . $to_topic_id) . '">', '</a>');
indvdum@0
   622
	}
indvdum@0
   623
	else
indvdum@0
   624
	{
indvdum@0
   625
		confirm_box(false, 'MERGE_POSTS', $s_hidden_fields);
indvdum@0
   626
	}
indvdum@0
   627
indvdum@0
   628
	$redirect = request_var('redirect', "index.$phpEx");
indvdum@0
   629
	$redirect = reapply_sid($redirect);
indvdum@0
   630
indvdum@0
   631
	if (!$success_msg)
indvdum@0
   632
	{
indvdum@0
   633
		return;
indvdum@0
   634
	}
indvdum@0
   635
	else
indvdum@0
   636
	{
indvdum@0
   637
		meta_refresh(3, append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$to_forum_id&amp;t=$to_topic_id"));
indvdum@0
   638
		trigger_error($user->lang[$success_msg] . '<br /><br />' . $return_link);
indvdum@0
   639
	}
indvdum@0
   640
}
indvdum@0
   641
indvdum@0
   642
?>