###############################################
##	Hack Title:		Auto-Move add-on
##	Hack Version:	n/a
##	Author:			Freakin' Booty ;-P
##	Description:	This is an add-on to Antony's "Move". It offers to possibility to Auto-Move topics to another forum.
##	Compatibility:	2.0.4 - 2.0.5
##
##	Installation Level: Easy
##	Installation Time: 15-20 minutes
##	Files To Edit: 6
##		admin/admin_forums.php
##		includes/constants.php
##		includes/move.php
##		language/lang_english/lang_main.php
##		viewforum.php
##		templates/subSilver/admin/forum_edit_body.tpl
##
##	Included Files: 1
##		move_update.php
##
##	History:
##		None
##
##	Author Notes:
##		You will have to have "Pruning" enabled to make auto-move work! It's pretty pointless to add yet another option
##		just for this add-on. So, just enable "pruning" in ACP => Configuration and that's it.
##
##	Support:		http://www.phpbbhacks.com/forums
##	Copyright:		2003 Auto-Move add-on - Freakin' Booty ;-P
##
###############################################
##   You downloaded this hack from phpBBHacks.com, the #1 source for phpBB related downloads.
##   Please visit http://www.phpbbhacks.com/forums for support.
###############################################
##
###############################################
##	This hack is released under the GPL License.
##	This hack can be freely used, but not distributed, without permission.
##	Intellectual Property is retained by the hack author(s) listed above.
###############################################

#
#-----[ COPY ]--------------------------------------------
#
move_update.php

#
#-----[ TO ]----------------------------------------------
#
# Make sure to run this file once as an administrator and then delete it immediately
#
move_update.php

#
#-----[ OPEN ]--------------------------------------------
#
admin/admin_forums.php

#
#-----[ FIND ]--------------------------------------------
#
				//
				// start forum prune stuff.
				//
				if( $row['prune_enable'] )
				{
					$prune_enabled = "checked=\"checked\"";
					$sql = "SELECT *
               			FROM " . PRUNE_TABLE . "
               			WHERE forum_id = $forum_id";
					if(!$pr_result = $db->sql_query($sql))
					{
						 message_die(GENERAL_ERROR, "Auto-Prune: Couldn't read auto_prune table.", __LINE__, __FILE__);
        			}

					$pr_row = $db->sql_fetchrow($pr_result);
				}
				else
				{
					$prune_enabled = '';
				}

#
#-----[ AFTER, ADD ]--------------------------------------
#
				//
				// start forum move stuff.
				//
				if( $row['move_enable'] )
				{
					$move_enabled = "checked=\"checked\"";
					$sql = "SELECT *
               			FROM " . MOVE_TABLE . "
               			WHERE forum_id = $forum_id";
					if(!$mv_result = $db->sql_query($sql))
					{
						 message_die(GENERAL_ERROR, "Auto-Move: Couldn't read auto_move table.", __LINE__, __FILE__);
        			}

					$mv_row = $db->sql_fetchrow($mv_result);
				}
				else
				{
					$move_enabled = '';
				}

#
#-----[ FIND ]--------------------------------------------
#
				$forum_id = ''; 
				$prune_enabled = '';

#
#-----[ AFTER, ADD ]--------------------------------------
#
				$move_enabled = '';

#
#-----[ FIND ]--------------------------------------------
#
			$catlist = get_list('category', $cat_id, TRUE);

#
#-----[ AFTER, ADD ]--------------------------------------
#
			$forumlist = make_forum_select('forum_dest', $forum_id);

#
#-----[ FIND ]--------------------------------------------
#
				'S_PRUNE_ENABLED' => $prune_enabled,

#
#-----[ AFTER, ADD ]--------------------------------------
#
				'S_MOVE_ENABLED' => $move_enabled,
				'S_FORUM_LIST' => $forumlist,

#
#-----[ FIND ]--------------------------------------------
#
				'L_DAYS' => $lang['Days'],

#
#-----[ AFTER, ADD ]--------------------------------------
#
				'L_AUTO_MOVE' => $lang['Forum_moving'],
				'L_MOVE_DAYS' => $lang['move_days'],
				'L_MOVE_FREQ' => $lang['move_freq'],
				'L_MOVE_DEST' => $lang['move_dest'],

#
#-----[ FIND ]--------------------------------------------
#
				'PRUNE_DAYS' => ( isset($pr_row['prune_days']) ) ? $pr_row['prune_days'] : 7,
				'PRUNE_FREQ' => ( isset($pr_row['prune_freq']) ) ? $pr_row['prune_freq'] : 1,

#
#-----[ AFTER, ADD ]--------------------------------------
#
				'MOVE_DAYS' => ( isset($mv_row['move_days']) ) ? $mv_row['move_days'] : 7,
				'MOVE_FREQ' => ( isset($mv_row['move_freq']) ) ? $mv_row['move_freq'] : 1,

#
#-----[ FIND ]--------------------------------------------
#
			$sql = "INSERT INTO " . FORUMS_TABLE . " (forum_id, forum_name, cat_id, forum_desc, forum_order, forum_status, prune_enable" . $field_sql . ")
				VALUES ('" . $next_id . "', '" . str_replace("\'", "''", $HTTP_POST_VARS['forumname']) . "', " . intval($HTTP_POST_VARS[POST_CAT_URL]) . ", '" . str_replace("\'", "''", $HTTP_POST_VARS['forumdesc']) . "', $next_order, " . intval($HTTP_POST_VARS['forumstatus']) . ", " . intval($HTTP_POST_VARS['prune_enable']) . $value_sql . ")";

#
#-----[ INLINE, FIND ]------------------------------------
#
, prune_enable

#
#-----[ AFTER, ADD ]--------------------------------------
#
, move_enable

#
#-----[ INLINE, FIND ]------------------------------------
#
, " . intval($HTTP_POST_VARS['prune_enable'])

#
#-----[ AFTER, ADD ]--------------------------------------
#
 . ", " . intval($HTTP_POST_VARS['move_enable'])

#
#-----[ FIND ]--------------------------------------------
#
					message_die(GENERAL_ERROR, "Couldn't insert row in prune table", "", __LINE__, __FILE__, $sql);
				}
			}

#
#-----[ AFTER, ADD ]--------------------------------------
#
			if( $HTTP_POST_VARS['move_enable'] )
			{

				if( $HTTP_POST_VARS['move_days'] == "" || $HTTP_POST_VARS['move_freq'] == "" || $HTTP_POST_VARS['forum_dest'] == "")
				{
					message_die(GENERAL_MESSAGE, $lang['Set_move_data']);
				}

				$sql = "INSERT INTO " . MOVE_TABLE . " (forum_id, forum_dest, move_days, move_freq)
					VALUES('" . $next_id . "', " . intval($HTTP_POST_VARS['forum_dest']) . ", " . intval($HTTP_POST_VARS['move_days']) . ", " . intval($HTTP_POST_VARS['move_freq']) . ")";
				if( !$result = $db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "Couldn't insert row in move table", "", __LINE__, __FILE__, $sql);
				}
			}

#
#-----[ FIND ]--------------------------------------------
#
					$HTTP_POST_VARS['prune_enable'] = 0;
				}
			}

#
#-----[ AFTER, ADD ]--------------------------------------
#
			if( isset($HTTP_POST_VARS['move_enable']))
			{
				if( $HTTP_POST_VARS['move_enable'] != 1 )
				{
					$HTTP_POST_VARS['move_enable'] = 0;
				}
			}

#
#-----[ FIND ]--------------------------------------------
#
			$sql = "UPDATE " . FORUMS_TABLE . "
				SET forum_name = '" . str_replace("\'", "''", $HTTP_POST_VARS['forumname']) . "', cat_id = " . intval($HTTP_POST_VARS[POST_CAT_URL]) . ", forum_desc = '" . str_replace("\'", "''", $HTTP_POST_VARS['forumdesc']) . "', forum_status = " . intval($HTTP_POST_VARS['forumstatus']) . ", prune_enable = " . intval($HTTP_POST_VARS['prune_enable']) . "

#
#-----[ INLINE, FIND ]------------------------------------
#
, prune_enable = " . intval($HTTP_POST_VARS['prune_enable']) . "

#
#-----[ AFTER, ADD ]--------------------------------------
#
, move_enable = " . intval($HTTP_POST_VARS['move_enable']) . "

#
#-----[ FIND ]--------------------------------------------
#
					message_die(GENERAL_ERROR, "Couldn't Update Forum Prune Information","",__LINE__, __FILE__, $sql);
				}
			}

#
#-----[ AFTER, ADD ]--------------------------------------
#
			if( $HTTP_POST_VARS['move_enable'] == 1 )
			{
				if( $HTTP_POST_VARS['move_days'] == "" || $HTTP_POST_VARS['move_freq'] == "" || $HTTP_POST_VARS['forum_dest'] == "" )
				{
					message_die(GENERAL_MESSAGE, $lang['Set_prune_data']);
				}

				$sql = "SELECT *
					FROM " . MOVE_TABLE . "
					WHERE forum_id = " . intval($HTTP_POST_VARS[POST_FORUM_URL]);
				if( !$result = $db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "Couldn't get forum Move Information","",__LINE__, __FILE__, $sql);
				}

				if( $db->sql_numrows($result) > 0 )
				{
					$sql = "UPDATE " . MOVE_TABLE . "
						SET	forum_dest = " . intval($HTTP_POST_VARS['forum_dest']) . ", move_days = " . intval($HTTP_POST_VARS['move_days']) . ", move_freq = " . intval($HTTP_POST_VARS['move_freq']) . "
				 		WHERE forum_id = " . intval($HTTP_POST_VARS[POST_FORUM_URL]);
				}
				else
				{
					$sql = "INSERT INTO " . MOVE_TABLE . " (forum_id, forum_dest, move_days, move_freq)
						VALUES(" . intval($HTTP_POST_VARS[POST_FORUM_URL]) . ", " . intval($HTTP_POST_VARS['forum_dest']) . ", " . intval($HTTP_POST_VARS['move_days']) . ", " . intval($HTTP_POST_VARS['move_freq']) . ")";
				}

				if( !$result = $db->sql_query($sql) )
				{
					message_die(GENERAL_ERROR, "Couldn't Update Forum Move Information","",__LINE__, __FILE__, $sql);
				}
			}

#
#-----[ FIND ]--------------------------------------------
#
			$sql = "DELETE FROM " . PRUNE_TABLE . "
				WHERE forum_id = $from_id";
			if( !$result = $db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "Couldn't delete forum prune information!", "", __LINE__, __FILE__, $sql);
			}

#
#-----[ AFTER, ADD ]--------------------------------------
#
			$sql = "DELETE FROM " . MOVE_TABLE . "
				WHERE forum_id = $from_id";
			if( !$result = $db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, "Couldn't delete forum move information!", "", __LINE__, __FILE__, $sql);
			}

#
#-----[ OPEN ]--------------------------------------------
#
includes/constants.php

#
#-----[ FIND ]--------------------------------------------
#
define('GROUPS_TABLE', $table_prefix.'groups');

#
#-----[ AFTER, ADD ]--------------------------------------
#
define('MOVE_TABLE', $table_prefix.'forum_move');

#
#-----[ OPEN ]--------------------------------------------
#
includes/move.php

#
#-----[ FIND ]--------------------------------------------
#
?>

#
#-----[ BEFORE, ADD ]-------------------------------------
#
function auto_move($forum_id = 0)
{
	global $db, $lang;

	$sql = "SELECT *
		FROM " . MOVE_TABLE . "
		WHERE forum_id = $forum_id";
	if ( !($result = $db->sql_query($sql)) )
	{
		message_die(GENERAL_ERROR, 'Could not read auto_move table', '', __LINE__, __FILE__, $sql);
	}

	if ( $row = $db->sql_fetchrow($result) )
	{
		if ( $row['move_freq'] && $row['move_days'] && $row['forum_dest'] )
		{
			$move_date = time() - ( $row['move_days'] * 86400 );
			$next_move = time() + ( $row['move_freq'] * 86400 );
			$forum_dest = $row['forum_dest'];

			move($forum_id, $move_date, $forum_dest);
			sync('forum', $forum_id);
			sync('forum', $forum_dest);

			$sql = "UPDATE " . FORUMS_TABLE . " 
				SET move_next = $next_move 
				WHERE forum_id = $forum_id";
			if ( !$db->sql_query($sql) )
			{
				message_die(GENERAL_ERROR, 'Could not update forum table', '', __LINE__, __FILE__, $sql);
			}
		}
	}

	return;
}

#
#-----[ OPEN ]--------------------------------------------
#
viewforum.php

#
#-----[ FIND ]--------------------------------------------
#
//
// Do the forum Prune
//
if ( $is_auth['auth_mod'] && $board_config['prune_enable'] )
{
	if ( $forum_row['prune_next'] < time() && $forum_row['prune_enable'] )
	{
		include($phpbb_root_path . 'includes/prune.'.$phpEx);
		require($phpbb_root_path . 'includes/functions_admin.'.$phpEx);
		auto_prune($forum_id);
	}
}
//
// End of forum prune
//

#
#-----[ AFTER, ADD ]--------------------------------------
#
//
// Do the forum Move
//
if ( $is_auth['auth_mod'] && $board_config['prune_enable'] )
{
	if ( $forum_row['move_next'] < time() && $forum_row['move_enable'] )
	{
		include($phpbb_root_path . 'includes/move.'.$phpEx);
		require($phpbb_root_path . 'includes/functions_admin.'.$phpEx);
		auto_move($forum_id);
	}
}
//
// End of forum move
//

#
#-----[ OPEN ]--------------------------------------------
#
# Be sure to edit this file for every language installed
#
language/lang_english/lang_admin.php

#
#-----[ FIND ]--------------------------------------------
#
$lang['Move_success'] = 'Moving of topics has been a success';

#
#-----[ AFTER, ADD ]--------------------------------------
#
$lang['Forum_moving'] = 'Auto-moving';
$lang['move_days'] = 'Move topics that have not been posted to in  ';
$lang['move_freq'] = 'Check for topic age every';
$lang['move_dest'] = 'Move topics to';

#
#-----[ OPEN ]--------------------------------------------
#
# Be sure to edit this file for every template installed
#
templates/subSilver/admin/forum_edit_body.tpl

#
#-----[ FIND ]--------------------------------------------
#
			<td align="left" valign="middle">&nbsp;<input class="post" type="text" name="prune_freq" value="{PRUNE_FREQ}" size="5" class="post" />&nbsp;{L_DAYS}</td>
		  </tr>
	  </table>

#
#-----[ AFTER, ADD ]--------------------------------------
#
	<tr> 
	  <td class="row1">{L_AUTO_MOVE}</td>
	  <td class="row2"><table cellspacing="0" cellpadding="1" border="0">
		  <tr> 
			<td align="right" valign="middle">{L_ENABLED}</td>
			<td align="left" valign="middle"><input type="checkbox" name="move_enable" value="1" {S_MOVE_ENABLED} /></td>
		  </tr>
		  <tr> 
			<td align="right" valign="middle">{L_MOVE_DAYS}</td>
			<td align="left" valign="middle">&nbsp;<input class="post" type="text" name="move_days" value="{MOVE_DAYS}" size="5" class="post" />&nbsp;{L_DAYS}</td>
		  </tr>
		  <tr> 
			<td align="right" valign="middle">{L_MOVE_FREQ}</td>
			<td align="left" valign="middle">&nbsp;<input class="post" type="text" name="move_freq" value="{MOVE_FREQ}" size="5" class="post" />&nbsp;{L_DAYS}</td>
		  </tr>
		  <tr> 
			<td align="right" valign="middle">{L_MOVE_DEST}</td>
			<td align="left" valign="middle">&nbsp;{S_FORUM_LIST}</td>
		  </tr>
	  </table></td>
	</tr>

#
#-----[ SAVE & CLOSE ALL FILES ]--------------------------
#
