Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 312
0.00% covered (danger)
0.00%
0 / 3
CRAP
n/a
0 / 0
mcp_forum_view
0.00% covered (danger)
0.00%
0 / 207
0.00% covered (danger)
0.00%
0 / 1
5402
mcp_resync_topics
0.00% covered (danger)
0.00%
0 / 24
0.00% covered (danger)
0.00%
0 / 1
30
merge_topics
0.00% covered (danger)
0.00%
0 / 79
0.00% covered (danger)
0.00%
0 / 1
210
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14/**
15* @ignore
16*/
17if (!defined('IN_PHPBB'))
18{
19    exit;
20}
21
22/**
23* MCP Forum View
24*/
25function mcp_forum_view($id, $mode, $action, $forum_info)
26{
27    global $template, $db, $user, $auth, $cache, $module;
28    global $phpEx, $phpbb_root_path, $config;
29    global $request, $phpbb_dispatcher, $phpbb_container;
30
31    $user->add_lang(array('viewtopic', 'viewforum'));
32
33    include_once($phpbb_root_path . 'includes/functions_display.' . $phpEx);
34
35    // merge_topic is the quickmod action, merge_topics is the mcp_forum action, and merge_select is the mcp_topic action
36    $merge_select = ($action == 'merge_select' || $action == 'merge_topic' || $action == 'merge_topics') ? true : false;
37
38    $forum_id            = $forum_info['forum_id'];
39    $start                = $request->variable('start', 0);
40    $topic_id_list        = $request->variable('topic_id_list', array(0));
41    $post_id_list        = $request->variable('post_id_list', array(0));
42    $source_topic_ids    = array($request->variable('t', 0));
43    $to_topic_id        = $request->variable('to_topic_id', 0);
44
45    $url_extra = '';
46    $url_extra .= ($forum_id) ? "&amp;f=$forum_id" : '';
47    $url_extra .= ($GLOBALS['topic_id']) ? '&amp;t=' . $GLOBALS['topic_id'] : '';
48    $url_extra .= ($GLOBALS['post_id']) ? '&amp;p=' . $GLOBALS['post_id'] : '';
49    $url_extra .= ($GLOBALS['user_id']) ? '&amp;u=' . $GLOBALS['user_id'] : '';
50
51    $url = append_sid("{$phpbb_root_path}mcp.$phpEx?$url_extra");
52
53    add_form_key('mcp_forum');
54
55    // Resync Topics
56    switch ($action)
57    {
58        case 'resync':
59            if (!check_form_key('mcp_forum'))
60            {
61                trigger_error('FORM_INVALID');
62            }
63
64            $topic_ids = $request->variable('topic_id_list', array(0));
65            mcp_resync_topics($topic_ids);
66        break;
67
68        case 'merge_topics':
69            $source_topic_ids = $topic_id_list;
70        case 'merge_topic':
71            if ($to_topic_id)
72            {
73                merge_topics($forum_id, $source_topic_ids, $to_topic_id);
74            }
75        break;
76    }
77
78    /**
79    * Get some data in order to execute other actions.
80    *
81    * @event core.mcp_forum_view_before
82    * @var    string    action                The action
83    * @var    array    forum_info            Array with forum infos
84    * @var    int        start                Start value
85    * @var    array    topic_id_list        Array of topics ids
86    * @var    array    post_id_list        Array of posts ids
87    * @var    array    source_topic_ids    Array of source topics ids
88    * @var    int        to_topic_id            Array of destination topics ids
89    * @since 3.1.6-RC1
90    */
91    $vars = array(
92        'action',
93        'forum_info',
94        'start',
95        'topic_id_list',
96        'post_id_list',
97        'source_topic_ids',
98        'to_topic_id',
99    );
100    extract($phpbb_dispatcher->trigger_event('core.mcp_forum_view_before', compact($vars)));
101
102    /* @var $pagination \phpbb\pagination */
103    $pagination = $phpbb_container->get('pagination');
104
105    $selected_ids = '';
106    if (count($post_id_list) && $action != 'merge_topics')
107    {
108        foreach ($post_id_list as $num => $post_id)
109        {
110            $selected_ids .= '&amp;post_id_list[' . $num . ']=' . $post_id;
111        }
112    }
113    else if (count($topic_id_list) && $action == 'merge_topics')
114    {
115        foreach ($topic_id_list as $num => $topic_id)
116        {
117            $selected_ids .= '&amp;topic_id_list[' . $num . ']=' . $topic_id;
118        }
119    }
120
121    make_jumpbox($url . "&amp;i=$id&amp;action=$action&amp;mode=$mode" . (($merge_select) ? $selected_ids : ''), $forum_id, false, 'm_', true);
122
123    $topics_per_page = ($forum_info['forum_topics_per_page']) ? $forum_info['forum_topics_per_page'] : $config['topics_per_page'];
124
125    $sort_days = $total = 0;
126    $sort_key = $sort_dir = '';
127    $sort_by_sql = $sort_order_sql = array();
128    phpbb_mcp_sorting('viewforum', $sort_days, $sort_key, $sort_dir, $sort_by_sql, $sort_order_sql, $total, $forum_id);
129
130    $forum_topics = ($total == -1) ? $forum_info['forum_topics_approved'] : $total;
131    $limit_time_sql = ($sort_days) ? 'AND t.topic_last_post_time >= ' . (time() - ($sort_days * 86400)) : '';
132
133    $base_url = $url . "&amp;i=$id&amp;action=$action&amp;mode=$mode&amp;sd=$sort_dir&amp;sk=$sort_key&amp;st=$sort_days" . (($merge_select) ? $selected_ids : '');
134    $pagination->generate_template_pagination($base_url, 'pagination', 'start', $forum_topics, $topics_per_page, $start);
135
136    $template->assign_vars(array(
137        'ACTION'                => $action,
138        'FORUM_NAME'            => $forum_info['forum_name'],
139        'FORUM_DESCRIPTION'        => generate_text_for_display($forum_info['forum_desc'], $forum_info['forum_desc_uid'], $forum_info['forum_desc_bitfield'], $forum_info['forum_desc_options']),
140
141        'REPORTED_IMG'            => $user->img('icon_topic_reported', 'TOPIC_REPORTED'),
142        'UNAPPROVED_IMG'        => $user->img('icon_topic_unapproved', 'TOPIC_UNAPPROVED'),
143        'LAST_POST_IMG'            => $user->img('icon_topic_latest', 'VIEW_LATEST_POST'),
144        'NEWEST_POST_IMG'        => $user->img('icon_topic_newest', 'VIEW_NEWEST_POST'),
145
146        'S_CAN_REPORT'            => $auth->acl_get('m_report', $forum_id),
147        'S_CAN_DELETE'            => $auth->acl_get('m_delete', $forum_id),
148        'S_CAN_RESTORE'            => $auth->acl_get('m_approve', $forum_id),
149        'S_CAN_MERGE'            => $auth->acl_get('m_merge', $forum_id),
150        'S_CAN_MOVE'            => $auth->acl_get('m_move', $forum_id),
151        'S_CAN_FORK'            => $auth->acl_get('m_', $forum_id),
152        'S_CAN_LOCK'            => $auth->acl_get('m_lock', $forum_id),
153        'S_CAN_SYNC'            => $auth->acl_get('m_', $forum_id),
154        'S_CAN_APPROVE'            => $auth->acl_get('m_approve', $forum_id),
155        'S_MERGE_SELECT'        => ($merge_select) ? true : false,
156        'S_CAN_MAKE_NORMAL'        => $auth->acl_gets('f_sticky', 'f_announce', 'f_announce_global', $forum_id),
157        'S_CAN_MAKE_STICKY'        => $auth->acl_get('f_sticky', $forum_id),
158        'S_CAN_MAKE_ANNOUNCE'    => $auth->acl_get('f_announce', $forum_id),
159        'S_CAN_MAKE_ANNOUNCE_GLOBAL'    => $auth->acl_get('f_announce_global', $forum_id),
160
161        'U_VIEW_FORUM'            => append_sid("{$phpbb_root_path}viewforum.$phpEx", 'f=' . $forum_id),
162        'U_VIEW_FORUM_LOGS'        => ($auth->acl_gets('a_', 'm_', $forum_id) && $module->loaded('logs')) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=logs&amp;mode=forum_logs&amp;f=' . $forum_id) : '',
163
164        'S_MCP_ACTION'            => $url . "&amp;i=$id&amp;forum_action=$action&amp;mode=$mode&amp;start=$start" . (($merge_select) ? $selected_ids : ''),
165
166        'TOTAL_TOPICS'            => $user->lang('VIEW_FORUM_TOPICS', (int) $forum_topics),
167    ));
168
169    // Grab icons
170    $icons = $cache->obtain_icons();
171
172    $topic_rows = array();
173
174    if ($config['load_db_lastread'])
175    {
176        $sql_read_tracking['LEFT_JOIN'][] = ['FROM' => [TOPICS_TRACK_TABLE => 'tt'], 'ON' => 'tt.topic_id = t.topic_id AND tt.user_id = ' . $user->data['user_id']];
177        $sql_read_tracking['SELECT'] = ', tt.mark_time';
178    }
179    else
180    {
181        $sql_read_tracking['LEFT_JOIN'] = [];
182        $sql_read_tracking['SELECT'] = '';
183    }
184
185    /* @var $phpbb_content_visibility \phpbb\content_visibility */
186    $phpbb_content_visibility = $phpbb_container->get('content.visibility');
187
188    $sql = 'SELECT t.topic_id
189        FROM ' . TOPICS_TABLE . ' t
190        WHERE t.forum_id = ' . $forum_id . '
191            AND ' . $phpbb_content_visibility->get_visibility_sql('topic', $forum_id, 't.') . "
192            $limit_time_sql
193        ORDER BY t.topic_type DESC, $sort_order_sql";
194
195    /**
196    * Modify SQL query before MCP forum view topic list is queried
197    *
198    * @event core.mcp_view_forum_modify_sql
199    * @var    string    sql            SQL query for forum view topic list
200    * @var    int    forum_id    ID of the forum
201    * @var    string  limit_time_sql        SQL query part for limit time
202    * @var    string  sort_order_sql        SQL query part for sort order
203    * @var    int topics_per_page            Number of topics per page
204    * @var    int start            Start value
205    * @since 3.1.2-RC1
206    */
207    $vars = array('sql', 'forum_id', 'limit_time_sql', 'sort_order_sql', 'topics_per_page', 'start');
208    extract($phpbb_dispatcher->trigger_event('core.mcp_view_forum_modify_sql', compact($vars)));
209
210    $result = $db->sql_query_limit($sql, $topics_per_page, $start);
211
212    $topic_list = $topic_tracking_info = array();
213
214    while ($row_ary = $db->sql_fetchrow($result))
215    {
216        $topic_list[] = $row_ary['topic_id'];
217    }
218    $db->sql_freeresult($result);
219
220    $sql_ary = [
221        'SELECT'    => 't.*' . $sql_read_tracking['SELECT'],
222        'FROM'        => [TOPICS_TABLE => 't'],
223        'LEFT_JOIN'    => $sql_read_tracking['LEFT_JOIN'],
224        'WHERE'        => $db->sql_in_set('t.topic_id', $topic_list, false, true),
225    ];
226
227    /**
228    * Event to modify SQL query before MCP forum topic data is queried
229    *
230    * @event core.mcp_forum_topic_data_modify_sql
231    * @var    array    sql_ary        SQL query array to get the MCP forum topic data
232    * @var    int        forum_id    The forum ID
233    * @var    array    topic_list    The array of MCP forum topic IDs
234    *
235    * @since 3.3.4-RC1
236    */
237    $vars = [
238        'sql_ary',
239        'forum_id',
240        'topic_list',
241    ];
242    extract($phpbb_dispatcher->trigger_event('core.mcp_forum_topic_data_modify_sql', compact($vars)));
243
244    $sql = $db->sql_build_query('SELECT', $sql_ary);
245    $result = $db->sql_query($sql);
246    while ($row_ary = $db->sql_fetchrow($result))
247    {
248        $topic_rows[$row_ary['topic_id']] = $row_ary;
249    }
250    $db->sql_freeresult($result);
251
252    // If there is more than one page, but we have no topic list, then the start parameter is... erm... out of sync
253    if (!count($topic_list) && $forum_topics && $start > 0)
254    {
255        redirect($url . "&amp;i=$id&amp;action=$action&amp;mode=$mode");
256    }
257
258    // Get topic tracking info
259    if (count($topic_list))
260    {
261        if ($config['load_db_lastread'])
262        {
263            $topic_tracking_info = get_topic_tracking($forum_id, $topic_list, $topic_rows, array($forum_id => $forum_info['mark_time']));
264        }
265        else
266        {
267            $topic_tracking_info = get_complete_topic_tracking($forum_id, $topic_list);
268        }
269    }
270
271    foreach ($topic_list as $topic_id)
272    {
273        $row_ary = &$topic_rows[$topic_id];
274
275        $replies = $phpbb_content_visibility->get_count('topic_posts', $row_ary, $forum_id) - 1;
276
277        if ($row_ary['topic_status'] == ITEM_MOVED)
278        {
279            $unread_topic = false;
280        }
281        else
282        {
283            $unread_topic = (isset($topic_tracking_info[$topic_id]) && $row_ary['topic_last_post_time'] > $topic_tracking_info[$topic_id]) ? true : false;
284        }
285
286        // Get folder img, topic status/type related information
287        $folder_img = $folder_alt = $topic_type = '';
288        topic_status($row_ary, $replies, $unread_topic, $folder_img, $folder_alt, $topic_type);
289
290        $topic_title = censor_text($row_ary['topic_title']);
291
292        $topic_unapproved = (($row_ary['topic_visibility'] == ITEM_UNAPPROVED || $row_ary['topic_visibility'] == ITEM_REAPPROVE)  && $auth->acl_get('m_approve', $row_ary['forum_id'])) ? true : false;
293        $posts_unapproved = ($row_ary['topic_visibility'] == ITEM_APPROVED && $row_ary['topic_posts_unapproved'] && $auth->acl_get('m_approve', $row_ary['forum_id'])) ? true : false;
294        $topic_deleted = $row_ary['topic_visibility'] == ITEM_DELETED;
295        $u_mcp_queue = ($topic_unapproved || $posts_unapproved) ? $url . '&amp;i=queue&amp;mode=' . (($topic_unapproved) ? 'approve_details' : 'unapproved_posts') . '&amp;t=' . $row_ary['topic_id'] : '';
296        $u_mcp_queue = (!$u_mcp_queue && $topic_deleted) ? $url . '&amp;i=queue&amp;mode=deleted_topics&amp;t=' . $topic_id : $u_mcp_queue;
297
298        $topic_row = array(
299            'ATTACH_ICON_IMG'        => ($auth->acl_get('u_download') && $auth->acl_get('f_download', $row_ary['forum_id']) && $row_ary['topic_attachment']) ? $user->img('icon_topic_attach', $user->lang['TOTAL_ATTACHMENTS']) : '',
300            'TOPIC_IMG_STYLE'        => $folder_img,
301            'TOPIC_FOLDER_IMG'        => $user->img($folder_img, $folder_alt),
302            'TOPIC_FOLDER_IMG_ALT'    => $user->lang[$folder_alt],
303            'TOPIC_ICON_IMG'        => (!empty($icons[$row_ary['icon_id']])) ? $icons[$row_ary['icon_id']]['img'] : '',
304            'TOPIC_ICON_IMG_WIDTH'    => (!empty($icons[$row_ary['icon_id']])) ? $icons[$row_ary['icon_id']]['width'] : '',
305            'TOPIC_ICON_IMG_HEIGHT'    => (!empty($icons[$row_ary['icon_id']])) ? $icons[$row_ary['icon_id']]['height'] : '',
306            'UNAPPROVED_IMG'        => ($topic_unapproved || $posts_unapproved) ? $user->img('icon_topic_unapproved', ($topic_unapproved) ? 'TOPIC_UNAPPROVED' : 'POSTS_UNAPPROVED') : '',
307            'DELETED_IMG'            => ($topic_deleted) ? $user->img('icon_topic_deleted', 'TOPIC_DELETED') : '',
308            'S_POST_ANNOUNCE'        => $row_ary['topic_type'] == POST_ANNOUNCE,
309            'S_POST_GLOBAL'            => $row_ary['topic_type'] == POST_GLOBAL,
310            'S_POST_STICKY'            => $row_ary['topic_type'] == POST_STICKY,
311            'S_TOPIC_LOCKED'        => $row_ary['topic_status'] == ITEM_LOCKED,
312            'S_TOPIC_MOVED'            => $row_ary['topic_status'] == ITEM_MOVED,
313
314            'TOPIC_AUTHOR'                => get_username_string('username', $row_ary['topic_poster'], $row_ary['topic_first_poster_name'], $row_ary['topic_first_poster_colour']),
315            'TOPIC_AUTHOR_COLOUR'        => get_username_string('colour', $row_ary['topic_poster'], $row_ary['topic_first_poster_name'], $row_ary['topic_first_poster_colour']),
316            'TOPIC_AUTHOR_FULL'            => get_username_string('full', $row_ary['topic_poster'], $row_ary['topic_first_poster_name'], $row_ary['topic_first_poster_colour']),
317            'U_TOPIC_AUTHOR'            => get_username_string('profile', $row_ary['topic_poster'], $row_ary['topic_first_poster_name'], $row_ary['topic_first_poster_colour']),
318
319            'LAST_POST_AUTHOR'            => get_username_string('username', $row_ary['topic_last_poster_id'], $row_ary['topic_last_poster_name'], $row_ary['topic_last_poster_colour']),
320            'LAST_POST_AUTHOR_COLOUR'    => get_username_string('colour', $row_ary['topic_last_poster_id'], $row_ary['topic_last_poster_name'], $row_ary['topic_last_poster_colour']),
321            'LAST_POST_AUTHOR_FULL'        => get_username_string('full', $row_ary['topic_last_poster_id'], $row_ary['topic_last_poster_name'], $row_ary['topic_last_poster_colour']),
322            'U_LAST_POST_AUTHOR'        => get_username_string('profile', $row_ary['topic_last_poster_id'], $row_ary['topic_last_poster_name'], $row_ary['topic_last_poster_colour']),
323
324            'TOPIC_TYPE'        => $topic_type,
325            'TOPIC_TITLE'        => $topic_title,
326            'REPLIES'            => $phpbb_content_visibility->get_count('topic_posts', $row_ary, $row_ary['forum_id']) - 1,
327            'LAST_POST_TIME'    => $user->format_date($row_ary['topic_last_post_time']),
328            'FIRST_POST_TIME'    => $user->format_date($row_ary['topic_time']),
329            'LAST_POST_SUBJECT'    => $row_ary['topic_last_post_subject'],
330            'LAST_VIEW_TIME'    => $user->format_date($row_ary['topic_last_view_time']),
331
332            'S_TOPIC_REPORTED'        => (!empty($row_ary['topic_reported']) && empty($row_ary['topic_moved_id']) && $auth->acl_get('m_report', $row_ary['forum_id'])) ? true : false,
333            'S_TOPIC_UNAPPROVED'    => $topic_unapproved,
334            'S_POSTS_UNAPPROVED'    => $posts_unapproved,
335            'S_TOPIC_DELETED'        => $topic_deleted,
336            'S_UNREAD_TOPIC'        => $unread_topic,
337        );
338
339        if ($row_ary['topic_status'] == ITEM_MOVED)
340        {
341            $topic_row = array_merge($topic_row, array(
342                'U_VIEW_TOPIC'        => append_sid("{$phpbb_root_path}viewtopic.$phpEx", "t={$row_ary['topic_moved_id']}"),
343                'U_DELETE_TOPIC'    => ($auth->acl_get('m_delete', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", "i=$id&amp;f=$forum_id&amp;topic_id_list[]={$row_ary['topic_id']}&amp;mode=forum_view&amp;action=delete_topic") : '',
344                'S_MOVED_TOPIC'        => true,
345                'TOPIC_ID'            => $row_ary['topic_moved_id'],
346            ));
347        }
348        else
349        {
350            if ($action == 'merge_topic' || $action == 'merge_topics')
351            {
352                $u_select_topic = $url . "&amp;i=$id&amp;mode=forum_view&amp;action=$action&amp;to_topic_id=" . $row_ary['topic_id'] . $selected_ids;
353            }
354            else
355            {
356                $u_select_topic = $url . "&amp;i=$id&amp;mode=topic_view&amp;action=merge&amp;to_topic_id=" . $row_ary['topic_id'] . $selected_ids;
357            }
358            $topic_row = array_merge($topic_row, array(
359                'U_VIEW_TOPIC'        => append_sid("{$phpbb_root_path}mcp.$phpEx", "i=$id&amp;f=$forum_id&amp;t={$row_ary['topic_id']}&amp;mode=topic_view"),
360                'U_NEWEST_POST'     => append_sid("{$phpbb_root_path}mcp.$phpEx", "i=$id&amp;f=$forum_id&amp;t={$row_ary['topic_id']}&amp;mode=topic_view&amp;view=unread#unread"),
361
362                'S_SELECT_TOPIC'    => ($merge_select && !in_array($row_ary['topic_id'], $source_topic_ids)) ? true : false,
363                'U_SELECT_TOPIC'    => $u_select_topic,
364                'U_MCP_QUEUE'        => $u_mcp_queue,
365                'U_MCP_REPORT'        => ($auth->acl_get('m_report', $forum_id)) ? append_sid("{$phpbb_root_path}mcp.$phpEx", 'i=main&amp;mode=topic_view&amp;t=' . $row_ary['topic_id'] . '&amp;action=reports') : '',
366                'TOPIC_ID'            => $row_ary['topic_id'],
367                'S_TOPIC_CHECKED'    => ($topic_id_list && in_array($row_ary['topic_id'], $topic_id_list)) ? true : false,
368            ));
369        }
370
371        $row = $row_ary;
372        /**
373        * Modify the topic data before it is assigned to the template in MCP
374        *
375        * @event core.mcp_view_forum_modify_topicrow
376        * @var    array    row        Array with topic data
377        * @var    array    topic_row    Template array with topic data
378        * @since 3.1.0-a1
379        */
380        $vars = array('row', 'topic_row');
381        extract($phpbb_dispatcher->trigger_event('core.mcp_view_forum_modify_topicrow', compact($vars)));
382        $row_ary = $row;
383        unset($row);
384
385        $template->assign_block_vars('topicrow', $topic_row);
386    }
387    unset($topic_rows);
388}
389
390/**
391* Resync topics
392*/
393function mcp_resync_topics($topic_ids)
394{
395    global $db, $user, $phpbb_log, $request;
396
397    if (!count($topic_ids))
398    {
399        trigger_error('NO_TOPIC_SELECTED');
400    }
401
402    if (!phpbb_check_ids($topic_ids, TOPICS_TABLE, 'topic_id', array('m_')))
403    {
404        return;
405    }
406
407    // Sync everything and perform extra checks separately
408    sync('topic_reported', 'topic_id', $topic_ids, false, true);
409    sync('topic_attachment', 'topic_id', $topic_ids, false, true);
410    sync('topic', 'topic_id', $topic_ids, true, false);
411
412    $sql = 'SELECT topic_id, forum_id, topic_title
413        FROM ' . TOPICS_TABLE . '
414        WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
415    $result = $db->sql_query($sql);
416
417    // Log this action
418    while ($row = $db->sql_fetchrow($result))
419    {
420        $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_TOPIC_RESYNC', false, array(
421            'forum_id' => $row['forum_id'],
422            'topic_id' => $row['topic_id'],
423            $row['topic_title']
424        ));
425    }
426    $db->sql_freeresult($result);
427
428    $msg = (count($topic_ids) == 1) ? $user->lang['TOPIC_RESYNC_SUCCESS'] : $user->lang['TOPICS_RESYNC_SUCCESS'];
429
430    $redirect = $request->variable('redirect', $user->data['session_page']);
431
432    meta_refresh(3, $redirect);
433    trigger_error($msg . '<br /><br />' . sprintf($user->lang['RETURN_PAGE'], '<a href="' . $redirect . '">', '</a>'));
434
435    return;
436}
437
438/**
439* Merge selected topics into selected topic
440*/
441function merge_topics($forum_id, $topic_ids, $to_topic_id)
442{
443    global $db, $template, $user, $phpEx, $phpbb_root_path, $phpbb_log, $request, $phpbb_dispatcher;
444
445    if (!count($topic_ids))
446    {
447        $template->assign_var('MESSAGE', $user->lang['NO_TOPIC_SELECTED']);
448        return;
449    }
450    if (!$to_topic_id)
451    {
452        $template->assign_var('MESSAGE', $user->lang['NO_FINAL_TOPIC_SELECTED']);
453        return;
454    }
455
456    $sync_topics = array_merge($topic_ids, array($to_topic_id));
457
458    $all_topic_data = phpbb_get_topic_data($sync_topics, 'm_merge');
459
460    if (!count($all_topic_data) || empty($all_topic_data[$to_topic_id]))
461    {
462        $template->assign_var('MESSAGE', $user->lang['NO_FINAL_TOPIC_SELECTED']);
463        return;
464    }
465
466    $sync_forums = array();
467    $topic_views = 0;
468    foreach ($all_topic_data as $data)
469    {
470        $sync_forums[$data['forum_id']] = $data['forum_id'];
471        $topic_views = max($topic_views, $data['topic_views']);
472    }
473
474    $to_topic_data = $all_topic_data[$to_topic_id];
475
476    $post_id_list    = $request->variable('post_id_list', array(0));
477    $start            = $request->variable('start', 0);
478
479    if (!count($post_id_list) && count($topic_ids))
480    {
481        $sql = 'SELECT post_id
482            FROM ' . POSTS_TABLE . '
483            WHERE ' . $db->sql_in_set('topic_id', $topic_ids);
484        $result = $db->sql_query($sql);
485
486        $post_id_list = array();
487        while ($row = $db->sql_fetchrow($result))
488        {
489            $post_id_list[] = $row['post_id'];
490        }
491        $db->sql_freeresult($result);
492    }
493
494    if (!count($post_id_list))
495    {
496        $template->assign_var('MESSAGE', $user->lang['NO_POST_SELECTED']);
497        return;
498    }
499
500    if (!phpbb_check_ids($post_id_list, POSTS_TABLE, 'post_id', array('m_merge')))
501    {
502        return;
503    }
504
505    $redirect = $request->variable('redirect', "{$phpbb_root_path}mcp.$phpEx?f=$forum_id&amp;i=main&amp;mode=forum_view");
506
507    $s_hidden_fields = build_hidden_fields(array(
508        'i'                => 'main',
509        'f'                => $forum_id,
510        'post_id_list'    => $post_id_list,
511        'to_topic_id'    => $to_topic_id,
512        'mode'            => 'forum_view',
513        'action'        => 'merge_topics',
514        'start'            => $start,
515        'redirect'        => $redirect,
516        'topic_id_list'    => $topic_ids)
517    );
518    $return_link = '';
519
520    if (confirm_box(true))
521    {
522        $to_forum_id = $to_topic_data['forum_id'];
523
524        move_posts($post_id_list, $to_topic_id, false);
525
526        $phpbb_log->add('mod', $user->data['user_id'], $user->ip, 'LOG_MERGE', false, array(
527            'forum_id' => $to_forum_id,
528            'topic_id' => $to_topic_id,
529            $to_topic_data['topic_title']
530        ));
531
532        // Update topic views count
533        $sql = 'UPDATE ' . TOPICS_TABLE . '
534                SET topic_views = ' . $topic_views . '
535                WHERE topic_id = ' . $to_topic_id;
536        $db->sql_query($sql);
537
538        // Message and return links
539        $success_msg = 'POSTS_MERGED_SUCCESS';
540
541        if (!function_exists('phpbb_update_rows_avoiding_duplicates_notify_status'))
542        {
543            include($phpbb_root_path . 'includes/functions_database_helper.' . $phpEx);
544        }
545
546        // Update the topic watch table.
547        phpbb_update_rows_avoiding_duplicates_notify_status($db, TOPICS_WATCH_TABLE, 'topic_id', $topic_ids, $to_topic_id);
548
549        // Update the bookmarks table.
550        phpbb_update_rows_avoiding_duplicates($db, BOOKMARKS_TABLE, 'topic_id', $topic_ids, $to_topic_id);
551
552        // Re-sync the topics and forums because the auto-sync was deactivated in the call of  move_posts()
553        sync('topic_reported', 'topic_id', $sync_topics);
554        sync('topic_attachment', 'topic_id', $sync_topics);
555        sync('topic', 'topic_id', $sync_topics, true);
556        sync('forum', 'forum_id', $sync_forums, true, true);
557
558        // Link to the new topic
559        $return_link .= (($return_link) ? '<br /><br />' : '') . sprintf($user->lang['RETURN_NEW_TOPIC'], '<a href="' . append_sid("{$phpbb_root_path}viewtopic.$phpEx", 't=' . $to_topic_id) . '">', '</a>');
560        $redirect = $request->variable('redirect', "{$phpbb_root_path}viewtopic.$phpEx?t=$to_topic_id");
561        $redirect = reapply_sid($redirect);
562
563        /**
564         * Perform additional actions after merging topics.
565         *
566         * @event core.mcp_forum_merge_topics_after
567         * @var    array    all_topic_data            The data from all topics involved in the merge
568         * @var    int        to_topic_id                The ID of the topic into which the rest are merged
569         * @since 3.1.11-RC1
570         */
571        $vars = array(
572            'all_topic_data',
573            'to_topic_id',
574        );
575        extract($phpbb_dispatcher->trigger_event('core.mcp_forum_merge_topics_after', compact($vars)));
576
577        meta_refresh(3, $redirect);
578        trigger_error($user->lang[$success_msg] . '<br /><br />' . $return_link);
579    }
580    else
581    {
582        confirm_box(false, 'MERGE_TOPICS', $s_hidden_fields);
583    }
584}