<?php
# Script by seg0ro http://mobilarts.ru
# Not for sale!!!

defined('_IN_JOHNCMS') or die('Error: restricted access');

$textl .= ' | Перемещение';
require_once('../incfiles/head.php');

if (!$topic){
  echo functions::display_error('Отсутствует идентификатор темы!<br /><a href="index.php">Форум</a>');
  require_once('../incfiles/end.php');
  exit;
}

if ($rights != 3 && $rights < 6){
  echo functions::display_error('У вас недостаточно прав для просмотра этой страницы!<br /><a href="index.php">Форум</a>');
  require_once('../incfiles/end.php');
  exit;
}

if (isset($_GET['otherforum']))
  $sql = abs(intval($_GET['otherforum']));
else
  $sql = $topicRefid['forum'];

$tree = array('<a href="index.php">Форум</a>', '<a href="index.php?topic='.$topicRes['topic'].'&amp;page='.$page.'">'.functions::checkout($topicRes['topicname']).'</a>', 'Перенести тему');
echo '<div class="phdr">'.functions::display_menu($tree).'</div>';


if (isset($_POST['send'])){
  if (empty($_POST['forum']))
    $error .= 'Неверные данные!';

  if (!$error){
    $forumName = mysql_fetch_assoc(mysql_query("SELECT `forumname` FROM `forum_forums` WHERE `forum` = '".abs(intval($_POST['forum']))."' LIMIT 1 "));
    
    mysql_query("INSERT INTO `forum_posts` SET
    `topic` = '$topic',
    `time` = '".time()."',
    `user_id` = '$user_id',
    `user` = '$login',
    `text` = 'Тема перенесена (из [b]".mysql_real_escape_string($topicRefid['forumname'])."[/b])',
    `files` = '0',
    `browser` = '".mysql_real_escape_string($agn)."',
    `ip` = '".core::$ip."',
    `ip_via_proxy` = '".core::$ip_via_proxy."' ");
    $pid = mysql_insert_id();
    
    mysql_query("UPDATE `forum_topics` SET
    `forum` = '".abs(intval($_POST['forum']))."',
    `time` = '".time()."',
    `forumname` = '".functions::checkout($forumName['forumname'])."',
    `lastpost` = '$login:|:$pid',
    `count` = count + 1
    WHERE `topic` = '$topic' LIMIT 1 ");
        
    mysql_query("UPDATE `users` SET `lastpost` = '".time()."', `postforum` = postforum + 1 WHERE `id` = '$user_id' ");
    
    if ($topicRes['user_id'] != $user_id)
      journal::addEntry($topicRes['user_id'], 'Ваша [url='.$set['homeurl'].'/forum/index.php?post='.$pid.'&find]тема[/url] перенесена (из [b]'.$topicRes['forumname'].'[/b])');
    
    $lastTopic = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_topics` WHERE `forum` = '".$topicRes['forum']."' ORDER BY `time` DESC LIMIT 1 "));
    $sql = empty($lastTopic['topicname']) ? '' : $lastTopic['topic'].":|:".$lastTopic['topicname'].":|:".$lastTopic['time']; 
    mysql_query("UPDATE `forum_forums` SET
    `last_topic`='$sql',
    `count`= count - 1 WHERE `forum` = '".$topicRes['forum']."' LIMIT 1 ");

    $lastTopic2 = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_topics` WHERE `forum` = '".abs(intval($_POST['forum']))."' ORDER BY `time` DESC LIMIT 1 "));
    mysql_query("UPDATE `forum_forums` SET
    `last_topic`='".$lastTopic2['topic'].":|:".$lastTopic2['topicname'].":|:".$lastTopic2['time']."',
    `count`= count + 1 WHERE `forum` = '".abs(intval($_POST['forum']))."' LIMIT 1 ");

    header ('Refresh:1; URL=index.php?topic='.$topic.'&page='.$page);
    echo '<div class="gmenu">Тема перенесена<br /><a href="index.php?topic='.$topic.'&amp;page='.$page.'">Далее</a></div>';
    require_once('../incfiles/end.php');
    exit;
  }
}

if ($error)
  echo functions::display_error($error);

echo '<form name="form" action="index.php?act=movetopic&amp;topic='.$topic.'&amp;page='.$page.'" method="post"><div class="list1">Переместить тему в: <select name="forum">';
    
$req = mysql_query("SELECT * FROM `forum_forums` WHERE `type` = '1' ORDER BY `sort` ASC ");
while ($res = mysql_fetch_assoc($req)){
  if ($res['parent'])
    $parentForum = mysql_fetch_assoc(mysql_query("SELECT `forumname` FROM `forum_forums` WHERE `forum` = '".$res['parent']."' LIMIT 1 "));
  echo '<option value="'.$res['forum'].'"'.($res['forum'] == $topicRes['forum'] ? ' selected="selected"' : '').'>'.functions::checkout($res['forumname']).($res['parent'] ? ' ('.functions::checkout($parentForum['forumname']).')' : '').'</option>';
}
echo '</select><br /><input type="submit" name="send" value="Переместить" /></div></form>';