############################################################## 
## MOD Title:       Moved_Topic_Message
## MOD Author:      Wicher < N/A > (N/A) http://www.detecties.com/phpbb2018
## MOD Description: Este Mod envia un e-mail o un MP (o ambos) al creador de un tema cuando este es movido por un admin o un mod
##		    Y se le avisa de la nueva url de su tema, diciendole en que foro y categoria se encuentra
##		    El usuario puede configurar si desea recibir la notificacion desde el perfil
## 
## MOD Version:     1.0.8 
## 
## MOD Traduction:  Zuker - www.eddb.com.ar
## 
## Installation Level: Easy 
## Installation Time: 10 Minutes
## 
## Files To Edit:  modcp.php 
##                 lang_spanish/lang_main.php
##                 includes/usercp_register.php
##                 templates/subSilver/profile_add_body.tpl
## 
## Included Files:   language/lang_spanish/email/topic_moved.tpl
##                   includes/function_pm.php 
## 
## License:      http://opensource.org/licenses/gpl-license.php GNU General Public License v2 
## 
############################################################## 
## For security purposes, please check: http://www.phpbb.com/mods/ 
## for the latest version of this MOD. Although MODs are checked 
## before being allowed in the MODs Database there is no guarantee 
## that there are no security problems within the MOD. No support 
## will be given for MODs not found within the MODs Database which 
## can be found at http://www.phpbb.com/mods/ 
############################################################## 
## Author Notes:   
##		   This mod has been tested on phpbb 2.0.21 
## 
############################################################## 
## MOD History: 
## 
## Fixed some stuff and resubmitted 1.0.6 to phpbb 10-31-2006
##
## Submitted to phpbb 1.0.0 under the mod name Moved Topic Mail 09-26-2006
##
## DEVelopment state 0.0.1 09-12-2006
############################################################## 
## Before Adding This MOD To Your Forum, You Should Back Up All Files Related To This MOD 
############################################################## 

# 
#-----[ SQL ]------------------------------------------ 
# 
# change phpbb_ to the database prefix you use
#
ALTER TABLE phpbb_users ADD user_topic_moved_mail TINYINT(1) DEFAULT '0';
ALTER TABLE phpbb_users ADD user_topic_moved_pm TINYINT(1) DEFAULT '1';
ALTER TABLE phpbb_users ADD user_topic_moved_pm_notify TINYINT(1) DEFAULT '1';
# 
#-----[ COPY ]------------------------------------------ 
# 
copy language/lang_english/email/topic_moved.tpl to language/lang_english/email/topic_moved.tpl
copy includes/function_pm.php to includes/function_pm.php

# 
#-----[ OPEN ]------------------------------------------ 
# 
modcp.php 
# 
#-----[ FIND ]------------------------------------------ 
# 
				// Sync the forum indexes
				sync('forum', $new_forum_id);
				sync('forum', $old_forum_id);

# 
#-----[ AFTER, ADD ]------------------------------------------ 
#
				// begin topic moved mailer by Wicher
				if (($userdata['user_topic_moved_mail'] == 1) or ($userdata['user_topic_moved_pm'] == 1))
				{
					// old forumname
					$sql = "SELECT f.forum_name, c.cat_title
						FROM " . FORUMS_TABLE . " f, " . CATEGORIES_TABLE . " c 
						WHERE f.forum_id = $old_forum_id
						AND c.cat_id = f.cat_id";
					if ( !($result = $db->sql_query($sql, BEGIN_TRANSACTION)) )
					{
						message_die(GENERAL_ERROR, 'Could not select from forums or catagory table', '', __LINE__, __FILE__, $sql);
					}
					if ($oldnamerow = $db->sql_fetchrow($result))
					{
						$oldcatname = $oldnamerow['cat_title'];
						$oldforumname = $oldnamerow['forum_name'];
					}
					$db->sql_freeresult($result);		

					// new forumname
					$sql = "SELECT f.forum_name, c.cat_title
						FROM " . FORUMS_TABLE . " f, " . CATEGORIES_TABLE . " c 
						WHERE f.forum_id = $new_forum_id
						AND c.cat_id = f.cat_id";
					if ( !($result = $db->sql_query($sql, BEGIN_TRANSACTION)) )
					{
						message_die(GENERAL_ERROR, 'Could not select from forums or catagory table', '', __LINE__, __FILE__, $sql);
					}
					if ($newnamerow = $db->sql_fetchrow($result))
					{
						$newcatname = $newnamerow['cat_title'];
						$newforumname = $newnamerow['forum_name'];
					}
					$db->sql_freeresult($result);

					// topictitle, user_id, username, useremail
					$sql = "SELECT t.topic_id, t.topic_title, u.user_id, u.username, u.user_email, u.user_lang, u.user_topic_moved_pm_notify 
							FROM " . TOPICS_TABLE . " t, " . USERS_TABLE . " u 
							WHERE t.topic_id IN ($topic_list)
							AND t.topic_poster = u.user_id";
					if ( !($result = $db->sql_query($sql, BEGIN_TRANSACTION)) )
					{
						message_die(GENERAL_ERROR, 'Could not select from topic or users table', '', __LINE__, __FILE__, $sql);
					}
					$mailrow = $db->sql_fetchrowset($result);
					$db->sql_freeresult($result);

					$script_path = preg_replace('/^\/?(.*?)\/?$/', "\\1", trim($board_config['script_path']));
					$server_name = trim($board_config['server_name']);
					$server_protocol = ( $board_config['cookie_secure'] ) ? 'https://' : 'http://';

					if ($userdata['user_topic_moved_mail'] == 1)
					{
						for($i = 0; $i < count($mailrow); $i++)
						{
							//emailer
							include_once($phpbb_root_path . 'includes/emailer.'.$phpEx);
							$emailer = new emailer($board_config['smtp_delivery']);
							$emailer->from($board_config['board_email']);
							$emailer->replyto($board_config['board_email']);
							$emailer->use_template('topic_moved', $mailrow[$i]['user_lang']);
							$emailer->email_address($mailrow[$i]['user_email']);
							$emailer->set_subject($lang['topic_moved']);
							$emailer->assign_vars(array(
								'SUBJECT' => $lang['topic_moved'],
								'SITENAME' => $board_config['sitename'], 
								'BOARD_URL' => $server_protocol.$server_name.'/'.$script_path,
								'TOPICPOSTER' => $mailrow[$i]['username'], 
								'TOPICTITLE' => $mailrow[$i]['topic_title'],
								'OLD_CATAGORIE_NAME' => $oldcatname, 
								'OLD_FORUM_NAME' => $oldforumname,
								'NEW_CATAGORIE_NAME' => $newcatname, 
								'NEW_FORUM_NAME' => $newforumname,
								'EMAIL_SIG' => (!empty($board_config['board_email_sig'])) ? str_replace('<br />', "\n", "-- \n" . $board_config['board_email_sig']) : '',
								'MOVED_URL' => $server_protocol.$server_name.'/'.$script_path.'/viewtopic.'.$phpEx.'?'.POST_TOPIC_URL.'='.$mailrow[$i]['topic_id'],
								'PROFILE_LINK' => $server_protocol.$server_name.'/'.$script_path.'/profile.'.$phpEx.'?mode=editprofile')
								);
							$emailer->send();
							$emailer->reset();
						}
					}
					if ($userdata['user_topic_moved_pm'] == 1)
					{
						include($phpbb_root_path . 'includes/function_pm.'.$phpEx);
						include($phpbb_root_path . 'includes/functions_post.'.$phpEx);
						for($i = 0; $i < count($mailrow); $i++)
						{
							//pm-er
							$moved_url = $server_protocol.$server_name.'/'.$script_path.'/viewtopic.'.$phpEx.'?'.POST_TOPIC_URL.'='.$mailrow[$i]['topic_id'];
							$profile_link = $server_protocol.$server_name.'/'.$script_path.'/profile.'.$phpEx.'?mode=editprofile';
							$pm_subject = $lang['topic_moved'];
							$pm_message = $lang['hello'].$mailrow[$i]['username'].'\n\n'.$lang['pmtext1'].'\"'.$mailrow[$i]['topic_title'].'\"'.$lang['pmtext2'].'\n'.$lang['pmtext3'].'\"'.$oldforumname.'"'.$lang['pmtext4'].'\"'.$oldcatname.'\"\n'.$lang['pmtext5'].'\"'.$newcatname.'"'.$lang['pmtext6'].'\"'.$newforumname.'\".\n\n\n'.$lang['pmtext7'].'\n'.$moved_url.'\n\n\n'.$lang['profiletext'].'\n'.$profile_link;
							// Will not send a pm notification via email if set to 0
							// Simply change the 0 to a 1 to turn it on
							send_moved_topic_pm($mailrow[$i]['user_id'], $pm_subject, $pm_message, $mailrow[$i]['user_topic_moved_pm_notify']);
						}
					}
					if (($userdata['user_topic_moved_mail'] == 1) && ($userdata['user_topic_moved_pm'] != 1))
					{
						$mailmess = $lang['mail_send'] . '<br /><br />';
					}
					if (($userdata['user_topic_moved_mail'] != 1) && ($userdata['user_topic_moved_pm'] == 1))
					{
						$mailmess = $lang['pm_send'] . '<br /><br />';
					}
					if (($userdata['user_topic_moved_mail'] == 1) && ($userdata['user_topic_moved_pm'] == 1))
					{
						$mailmess = $lang['mail_pm_send'] . '<br /><br />';
					}
				}
				// end topic moved mailer by Wicher

# 
#-----[ FIND ]------------------------------------------ 
# 
				$message = $lang['Topics_Moved'] . '<br /><br />';
# 
#-----[ IN-LINE FIND ]------------------------------------------ 
# 
'<br /><br />'
# 
#-----[ IN-LINE AFTER, ADD ]------------------------------------------ 
# 
 . $mailmess
# 
#-----[ OPEN ]------------------------------------------ 
# 
includes/usercp_register.php
# 
#-----[ FIND ]------------------------------------------ 
# 
	if ( $mode == 'register' )
	{
# 
#-----[ BEFORE, ADD ]------------------------------------------ 
# 
	$mail_on_topic_moved = ( isset($HTTP_POST_VARS['topic_moved_mail']) ) ? ( ($HTTP_POST_VARS['topic_moved_mail']) ? TRUE : 0 ) : TRUE;
	$pm_on_topic_moved = ( isset($HTTP_POST_VARS['topic_moved_pm']) ) ? ( ($HTTP_POST_VARS['topic_moved_pm']) ? TRUE : 0 ) : TRUE;
	$pm_on_topic_moved_notify = ( isset($HTTP_POST_VARS['topic_moved_pm_notify']) ) ? ( ($HTTP_POST_VARS['topic_moved_pm_notify']) ? TRUE : 0 ) : TRUE;
# 
#-----[ FIND ]------------------------------------------ 
# 
		$allowsmilies = ( isset($HTTP_POST_VARS['allowsmilies']) ) ? ( ($HTTP_POST_VARS['allowsmilies']) ? TRUE : 0 ) : $userdata['user_allowsmile'];
# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
		$mail_on_topic_moved = ( isset($HTTP_POST_VARS['topic_moved_mail']) ) ? ( ($HTTP_POST_VARS['topic_moved_mail']) ? TRUE : 0 ) : $userdata['user_topic_moved_mail'];
		$pm_on_topic_moved = ( isset($HTTP_POST_VARS['topic_moved_pm']) ) ? ( ($HTTP_POST_VARS['topic_moved_pm']) ? TRUE : 0 ) : $userdata['user_topic_moved_pm'];
		$pm_on_topic_moved_notify = ( isset($HTTP_POST_VARS['topic_moved_pm_notify']) ) ? ( ($HTTP_POST_VARS['topic_moved_pm_notify']) ? TRUE : 0 ) : $userdata['user_topic_moved_pm_notify'];
# 
#-----[ FIND ]------------------------------------------ 
# note: this is a partal search, the full line is longer.
SET " . $username_sql . $passwd_sql . "user_email = '"
# 
#-----[ IN-LINE FIND ]------------------------------------------ 
# 
user_email = '" . str_replace("\'", "''", $email) ."'
# 
#-----[ IN-LINE AFTER, ADD ]------------------------------------------ 
# 
, user_topic_moved_mail = $mail_on_topic_moved, user_topic_moved_pm = $pm_on_topic_moved, user_topic_moved_pm_notify = $pm_on_topic_moved_notify
# 
#-----[ FIND ]------------------------------------------ 
# note: this is a partal search, the full line is longer.
$sql = "INSERT INTO " . USERS_TABLE . "	(user_id, username,
# 
#-----[ IN-LINE FIND ]------------------------------------------ 
# 
, user_email
# 
#-----[ IN-LINE AFTER, ADD ]------------------------------------------ 
# 
, user_topic_moved_mail, user_topic_moved_pm, user_topic_moved_pm_notify 
# 
#-----[ FIND ]------------------------------------------ 
# note: this is a partal search, the full line is longer.
VALUES ($user_id, '" . str_replace("\'", "''", $username
# 
#-----[ IN-LINE FIND ]------------------------------------------ 
# 
, '" . str_replace("\'", "''", $email) . "'
# 
#-----[ IN-LINE AFTER, ADD ]------------------------------------------ 
# 
, $mail_on_topic_moved, $pm_on_topic_moved, $pm_on_topic_moved_notify
# 
#-----[ FIND ]------------------------------------------ 
# 
	$popup_pm = $userdata['user_popup_pm'];
# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
	$mail_on_topic_moved = $userdata['user_topic_moved_mail'];
	$pm_on_topic_moved = $userdata['user_topic_moved_pm'];
	$pm_on_topic_moved_notify = $userdata['user_topic_moved_pm_notify'];
# 
#-----[ FIND ]------------------------------------------ 
# 
		'POPUP_PM_YES' => ( $popup_pm ) ? 'checked="checked"' : '',
		'POPUP_PM_NO' => ( !$popup_pm ) ? 'checked="checked"' : '',
# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
		'TOPIC_MOVED_MAIL_YES' => ( $mail_on_topic_moved ) ? 'checked="checked"' : '',
		'TOPIC_MOVED_MAIL_NO' => ( !$mail_on_topic_moved ) ? 'checked="checked"' : '',
		'TOPIC_MOVED_PM_YES' => ( $pm_on_topic_moved ) ? 'checked="checked"' : '',
		'TOPIC_MOVED_PM_NO' => ( !$pm_on_topic_moved ) ? 'checked="checked"' : '',
		'TOPIC_MOVED_PM_NOTIFY_YES' => ( $pm_on_topic_moved_notify ) ? 'checked="checked"' : '',
		'TOPIC_MOVED_PM_NOTIFY_NO' => ( !$pm_on_topic_moved_notify ) ? 'checked="checked"' : '',
# 
#-----[ FIND ]------------------------------------------ 
# 
		'L_POPUP_ON_PRIVMSG_EXPLAIN' => $lang['Popup_on_privmsg_explain'],
# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
		'L_TOPIC_MOVED_MAIL' => $lang['topic_moved_mail'],
		'L_TOPIC_MOVED_PM' => $lang['topic_moved_pm'],
		'L_TOPIC_MOVED_PM_NOTIFY' => $lang['topic_moved_pm_notify'],
# 
#-----[ OPEN ]------------------------------------------ 
# 
templates/subSilver/profile_add_body.tpl
# 
#-----[ FIND ]------------------------------------------ 
# 
		<input type="radio" name="popup_pm" value="0" {POPUP_PM_NO} />
		<span class="gen">{L_NO}</span></td>
	</tr>
# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
	<tr> 
	  <td colspan="2" class="row3" height="5"></td>
	</tr>
	<tr> 
	  <td class="row1"><span class="gen">{L_TOPIC_MOVED_MAIL}:</span></td>
	  <td class="row2"> 
		<input type="radio" name="topic_moved_mail" value="1" {TOPIC_MOVED_MAIL_YES} />
		<span class="gen">{L_YES}</span>&nbsp;&nbsp; 
		<input type="radio" name="topic_moved_mail" value="0" {TOPIC_MOVED_MAIL_NO} />
		<span class="gen">{L_NO}</span></td>
	</tr>
	<tr> 
	  <td class="row1"><span class="gen">{L_TOPIC_MOVED_PM}:</span></td>
	  <td class="row2"> 
		<input type="radio" name="topic_moved_pm" value="1" {TOPIC_MOVED_PM_YES} />
		<span class="gen">{L_YES}</span>&nbsp;&nbsp; 
		<input type="radio" name="topic_moved_pm" value="0" {TOPIC_MOVED_PM_NO} />
		<span class="gen">{L_NO}</span></td>
	</tr>
	<tr> 
	  <td class="row1"><span class="gen">{L_TOPIC_MOVED_PM_NOTIFY}:</span></td>
	  <td class="row2"> 
		<input type="radio" name="topic_moved_pm_notify" value="1" {TOPIC_MOVED_PM_NOTIFY_YES} />
		<span class="gen">{L_YES}</span>&nbsp;&nbsp; 
		<input type="radio" name="topic_moved_pm_notify" value="0" {TOPIC_MOVED_PM_NOTIFY_NO} />
		<span class="gen">{L_NO}</span></td>
	</tr>
	<tr> 
	  <td colspan="2" class="row3" height="5"></td>
	</tr>
# 
#-----[ OPEN ]------------------------------------------ 
# 
language/lang_spanish/lang_main.php
# 
#-----[ FIND ]------------------------------------------ 
# 
$lang['Total_votes']
# 
#-----[ AFTER, ADD ]------------------------------------------ 
# 
// topic moved mailer by Wicher
$lang['topic_moved'] = 'El tema que sted creo fue movido.';
$lang['mail_send'] = 'El autor del tema fue notificado va e-mail.';
$lang['pm_send'] = 'El autor del tema fue notificado va MP.';
$lang['mail_pm_send'] = 'El autor del tema fue notificado va e-maily MP.';
$lang['topic_moved_mail'] = 'Envame un e-mail cuando un tema creado por m, es movido';
$lang['topic_moved_pm'] = 'Envame un mp cuando un tema creado por m, es movido';
$lang['topic_moved_pm_notify'] = 'Envame una notificacion va mail cuando tengo un MP debido a que un tema fue movido<br>(No seleccione esto si ha seleccionado "Envame un e-mail cuando un tema creado por m, es movido")';
$lang['hello'] = 'Hola ';
$lang['pmtext1'] = 'Este MP fue enviado debido a que el tema ';
$lang['pmtext2'] = ' que usted ha creado';
$lang['pmtext3'] = 'fue movido del foro ';
$lang['pmtext4'] = ' en la categora ';
$lang['pmtext5'] = ' a la categora ';
$lang['pmtext6'] = ' en el foro ';
$lang['pmtext7'] = 'Usted puede ir directamente al Tema que fue movido haciendo click en el link:';
$lang['profiletext'] = 'Puede desactivar que se le avise acerca de sus temas movidos en:';
# 
#-----[ SAVE/CLOSE ALL FILES ]------------------------------------------ 
# 
# EoM 
