Page 1 of 1

[MOD-DB]Split posts and merge in one step

Posted: Sat Apr 09, 2005 1:57 am
by asinshesq
Here's a simple mod that allows you to split posts from a topic using phpbb's regular split feature and merge the split posts into an existing destination topic in one single step.

If you fill in the name for a new topic in the usual way for the split feature, the split feature will create a new topic like usual...but if you leave the new topic name blank and fill in a destination topic (using the topic_id or topic url for the destination topic or using the url of a post in the destiination topic), the split posts will get merged into the destination topic in the regular posting order.

Code: Select all

############################################################## 
## MOD Title: Split posts and merge in one step
## MOD Author: asinshesq < asinsh@speakeasy.net > (Alan Sinsheimer) N/A
## MOD Description:	Allow admin to split posts from a topic (using the regular split feature) and merge them
##			into an existing topic in a single step
##
## MOD Version:		1.0.3
## 
## Installation Level:	Easy
## Installation Time:	5 Minutes ( 1 minute with easymod) 
##
## Files To Edit:	modcp.php
##			language/lang_english/lang_main.php
##			templates/subSilver/modcp_split.tpl
##
## Included Files:	n/a
##
############################################################## 
## For Security Purposes, Please Check: http://www.phpbb.com/mods/ for the 
## latest version of this MOD. Downloading this MOD from other sites could cause malicious code 
## to enter into your phpBB Forum. As such, phpBB will not offer support for MOD's not offered 
## in our MOD-Database, located at: http://www.phpbb.com/mods/ 
############################################################## 
## Author Notes:	n/a
############################################################## 
## MOD History:
##
##   2005-5-06	- Version 1.0.3
##		  added an intval to protect against injection risk
##
##   2005-4-09	- Version 1.0.2
##		  a few more changes in textual instructions to ensure that the instructions don't wrap on monitors 
##		  with resolutions lower than 1280 accross 
##
##   2005-4-09	- Version 1.0.1
##		  a few changes in textual instructions suggested by mistakeprone (no change in functionality) 
##
##   2005-4-08	- Version 1.0.0
##		  initial version
##
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
##############################################################
#
#-----[ OPEN ]-------------------------------------------------
#
modcp.php

#
#-----[ FIND ]-------------------------------------------------
#
			$sql = "SELECT post_id, poster_id, topic_id, post_time

#
#-----[ BEFORE, ADD ]-------------------------------------------------
#
			// start mod split posts and merge in one step
			// check to see if subject (new title of split topic) is empty...if so, and if there is a destination topic, do the regular split (copied from the regular modcp code) and then change the split posts to the destination topic
			if (empty($HTTP_POST_VARS['subject']))
			{
				if (!empty($HTTP_POST_VARS['destination_topic']))
				{
					$destination_topic = $HTTP_POST_VARS['destination_topic'];

					// the next block of code is borrowed from the simply merge mod...it extracts the topic id from topic_id, topic url or post url
					// is this a direct value ?
					$num_topic = intval($destination_topic);
					if ($destination_topic == "$num_topic")
					{
						$destination_topic_id = $num_topic;
					}
					// is this a url with topic id or post id ?
					else
					{
						$name = explode('?', $destination_topic);
						$parms = ( isset($name[1]) ) ? $name[1] : $name[0];
						parse_str($parms, $parm);
						$found = false;
						$destination_topic_id = 0;
						while ((list($key, $val) = each($parm)) && !$found)
						{
							$vals = explode('#', $val);
							$val = $vals[0];
							if (empty($val))
							{
								$val = 0;
							}
							else $val = intval($val);
							switch($key)
							{
								case POST_POST_URL:
								$sql = "SELECT topic_id FROM " . POSTS_TABLE . " WHERE post_id=$val";
								if ( !($result = $db->sql_query($sql)) ) message_die(GENERAL_ERROR, 'Could not get post information', '', __LINE__, __FILE__, $sql);
								if ($row = $db->sql_fetchrow($result))
								{
									$val = $row['topic_id'];
									$found = true;
								}
								break;

								case POST_TOPIC_URL:
								$found = true;
								break;
							}
							if ($found)
							{
								$destination_topic_id = $val;
							}
						}
					}

					// done with getting topic_id
					// now, get forum id for destination topic
					$sql = "SELECT forum_id
						FROM " . TOPICS_TABLE . "
						WHERE topic_id = $destination_topic_id";
					if ( !($result = $db->sql_query($sql)) )
					{
						message_die(GENERAL_ERROR, 'Could not get forum information for destination topic', '', __LINE__, __FILE__, $sql);
					}
					$row = $db->sql_fetchrow($result);
					$destination_forum_id = $row['forum_id'];
					$db->sql_freeresult($result);

					// if there is no forum_id (probably because there is no such topic_id), give error message
					if ($destination_forum_id == '') message_die(GENERAL_MESSAGE, 'Could not get forum information (no such topic_id?)');

					// now get the posts that are being spit....
					$sql = "SELECT post_id, poster_id, post_time
						FROM " . POSTS_TABLE . "
						WHERE post_id IN ($post_id_sql)
						ORDER BY post_time ASC";
					if (!($result = $db->sql_query($sql)))
					{
						message_die(GENERAL_ERROR, 'Could not get post information', '', __LINE__, __FILE__, $sql);
					}

					if ($row = $db->sql_fetchrow($result))
					{
					$post_time = $row['post_time'];
					$user_id_sql = intval($row['poster_id']);
					$post_id_sql = intval($row['post_id']);
					}

					do
					{
						$user_id_sql .= (($user_id_sql != '') ? ', ' : '') . intval($row['poster_id']);
						$post_id_sql .= (($post_id_sql != '') ? ', ' : '') . intval($row['post_id']);
					}
					while ($row = $db->sql_fetchrow($result));

					// Update topic watch table, switch users whose posts
					// have moved, over to watching the destination topic
					$sql = "UPDATE " . TOPICS_WATCH_TABLE . "
						SET topic_id = $destination_topic_id
						WHERE topic_id = $topic_id
						AND user_id IN ($user_id_sql)";
					if (!$db->sql_query($sql))
					{
						message_die(GENERAL_ERROR, 'Could not update topics watch table', '', __LINE__, __FILE__, $sql);
					}

					$sql_where = (!empty($HTTP_POST_VARS['split_type_beyond'])) ? " post_time >= $post_time AND topic_id = $topic_id" : "post_id IN ($post_id_sql)";

					// now do a sql for switching the split posts over to the destination topic and forum
					$sql = 	"UPDATE " . POSTS_TABLE . "
						SET topic_id = $destination_topic_id, forum_id = $destination_forum_id
						WHERE $sql_where";
					if (!$db->sql_query($sql, END_TRANSACTION))
					{
						message_die(GENERAL_ERROR, 'Could not update posts table', '', __LINE__, __FILE__, $sql);
					}

					sync('topic', $destination_topic_id);
					sync('topic', $topic_id);
					sync('forum', $destination_forum_id);
					sync('forum', $forum_id);

					$template->assign_vars(array(
						'META' => '<meta http-equiv="refresh" content="3;url=' . "viewtopic.$phpEx?" . POST_TOPIC_URL . "=$destination_topic_id&sid=" . $userdata['session_id'] . '">')
					);

					$message = $lang['Topic_split'] . '<br /><br />' . sprintf($lang['Click_return_topic'], '<a href="' . "viewtopic.$phpEx?" . POST_TOPIC_URL . "=$destination_topic_id&sid=" . $userdata['session_id'] . '">', '</a>');
					message_die(GENERAL_MESSAGE, $message);
				}
			}
			// end mod split posts and merge in one step

#
#-----[ FIND ]-------------------------------------------------
#
					'L_POST' => $lang['Post'],

#
#-----[ AFTER, ADD ]-------------------------------------------------
#
					'L_IF_TITLE_BLANK' => $lang['If_title_blank'],
					'L_DESTINATION_TOPIC' => $lang['Destination_topic'],
					'L_DESTINATION_TOPIC_FORMAT' => $lang['Destination_topic_format'],

#
#-----[ OPEN ]-------------------------------------------------
#
language/lang_english/lang_main.php

#
#-----[ FIND ]-------------------------------------------------
#
$lang['Topic_split'] = 'The selected topic has been split successfully';

#
#-----[ AFTER, ADD ]-------------------------------------------------
#
// start mod split posts and merge in one step
$lang['If_title_blank'] = '<br />(specify new topic title; leave blank to merge split-off posts into existing topic specified below)';
$lang['Destination_topic'] = 'Destination topic';
$lang['Destination_topic_format'] = '<br />(if merging into existing topic, insert topic id or url of that topic, or insert url of a post in that topic)';
// end mod split posts and merge in one step

#
#-----[ OPEN ]-------------------------------------------------
#
templates/subSilver/modcp_split.tpl

#
#-----[ FIND ]-------------------------------------------------
#
	  <td class="row2" colspan="2"><input class="post" type="text" size="35" style="width: 350px" maxlength="60" name="subject" /></td>

#
#-----[ REPLACE WITH ]-------------------------------------------------
#
	  <td class="row2" colspan="2"><input class="post" type="text" size="35" style="width: 350px" maxlength="60" name="subject" /><span class="gensmall">{L_IF_TITLE_BLANK}</span></td>

#
#-----[ FIND ]-------------------------------------------------
#
	<tr> 
	  <td class="row1" nowrap="nowrap"><span class="gen">{L_SPLIT_FORUM}</span></td>
	  <td class="row2" colspan="2">{S_FORUM_SELECT}</td>
	</tr>

#
#-----[ AFTER, ADD ]-------------------------------------------------
#
	<tr> 
	  <td class="row1" nowrap="nowrap"><span class="gen">{L_DESTINATION_TOPIC}</span></td>
	  <td class="row2" colspan="2"><input class="post" type="text" size="35" style="width: 350px" maxlength="60" name="destination_topic" /><span class="gensmall">{L_DESTINATION_TOPIC_FORMAT}</span></td>
	</tr>

#
#-----[ SAVE/CLOSE ALL FILES ]--------------------------------
#
# EoM
This mod does NOT replace the simply merge threads mod ( http://www.phpbb.com/phpBB/viewtopic.php?t=149057 ) since you would still use that mod to merge entire threads into other threads.

[edit: just updated the above code to version 1.0.3; if you scroll down a few posts you will find the changes from 1.0.2 to 1.0.3]

Posted: Sat Apr 09, 2005 8:44 am
by asinshesq
I updated the previous post with a new version. There are no changes in that version other than refining the textual instructions as suggested by mistakeprone.

Posted: Sat Apr 09, 2005 4:59 pm
by niekas
Great idea - these types of moderation mods should be included by default in phpbb.

Posted: Sat Apr 09, 2005 6:44 pm
by trv
Is it compatible with Categories Hierarchy 2.1.1 ?

Posted: Sat Apr 09, 2005 6:49 pm
by asinshesq
trv wrote: Is it compatible with Categories Hierarchy 2.1.1 ?


Don't know since I have never used CH. But I suspect so since the only thing it changes is the split post function and I don't think CH impacts that. If you try it, let us know if it works.

Posted: Fri May 06, 2005 9:48 pm
by asinshesq
Just updated to version 1.0.3. If you are running 1.0.2, here are the only changes:

Code: Select all

OPEN
modcp.php

FIND
							if (empty($val)) $val = 0;

REPLACE WITH
							if (empty($val))
							{
								$val = 0;
							}
							else $val = intval($val);

FIND
								$destination_topic_id = intval($val);

REPLACE WITH
								$destination_topic_id = $val;

Posted: Sun May 15, 2005 11:33 am
by asinshesq
OK, this just got validated...please post any furhter comments here:
http://www.phpbb.com/phpBB/viewtopic.php?t=287988

Posted: Sun May 15, 2005 9:47 pm
by wGEric
Locked.