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

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

if (isset($_POST['yes'])){

  $postReq = mysql_query("SELECT `user_id` FROM `forum_posts` WHERE `topic` = '$topic' ");
  while ($postRes = mysql_fetch_array($postReq)){
    mysql_query("UPDATE `users` SET `postforum` = postforum - 1 WHERE `id` = '".$postRes['user_id']."' LIMIT 1 ");
    
	$user = functions::get_user($postRes['user_id']);
  $shop = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'deltopic' LIMIT 1"));

mysql_query("UPDATE `users` SET `balans` = '".($user['balans']-$shop['val'])."' WHERE `id` = '".$postRes['user_id']."' LIMIT 1");

	if ($postRes['rating'])
      mysql_query("DELETE FROM `forum_posts_rating` WHERE `post` = '".$postRes['post']."' ");
      
  }

  $fileReq = mysql_query("SELECT * FROM `forum_files` WHERE `topic` = '$topic' ");
  if (mysql_num_rows($fileReq)){
    while ($fileRes = mysql_fetch_array($fileReq)){
      unlink('../files/forum/'.$fileRes['filename']);
     
    }
    mysql_query("DELETE FROM `forum_files` WHERE `topic` = '$topic' ");
    mysql_query("OPTIMIZE TABLE `forum_files` ");
  
  }

  if ($topicRes['poll_name']){
    mysql_query("DELETE FROM `forum_polls` WHERE `topic` = '$topic' ");
    mysql_query("DELETE FROM `forum_polled` WHERE `topic` = '$topic' ");
    mysql_query("OPTIMIZE TABLE `forum_polled` ");
  }

  mysql_query("DELETE FROM `forum_posts` WHERE `topic` = '$topic' ");
  if ($topicRes['count'] > 100)
    mysql_query("OPTIMIZE TABLE `forum_posts` ");
  mysql_query("DELETE FROM `forum_topics` WHERE `topic` = '$topic' LIMIT 1 ");
  mysql_query("DELETE FROM `forum_readed` WHERE `topic` = '$topic' ");
  mysql_query("DELETE FROM `forum_favourites` WHERE `topic` = '$topic' ");
  
  $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 ");
 
  if ($topicRes['user_id'] != $user_id)
    journal::addEntry($topicRes['user_id'], 'Ваша тема [b]'.$topicRes['topicname'].'[/b] удалена (удалил [url='.$set['homeurl'].'/users/profile.php?user='.$user_id.']'.$login.'[/url])'.(!empty($_POST['reason']) ? PHP_EOL.'Причина удаления: '.trim($_POST['reason']) : ''));
  
  header ('Refresh:1; URL=index.php?forum='.$topicRes['forum']);
  forum_log('Удаление темы [b]'.$topicRes['topicname'].'[/b]');

  echo '<div class="gmenu">Тема удалена<br /><a href="index.php?forum='.$topicRes['forum'].'">Далее</a></div>';

}else{
  echo '<form action="index.php?act=deltopic&amp;topic='.$topic.'" method="post"><div class="rmenu">Вы хотите удалить тему?<br />Причина удаления темы <img style="cursor: pointer;" onclick="alert(this.title);" src="images/question.png" alt="?" title="Причина будет вставлена в уведомление автору темы" />:<br /><input type="text" name="reason" value="" /><br /><input type="submit" name="yes" value="Удалить" /> <a href="index.php?topic='.$topic.'&amp;page='.$page.'">Отмена</a></div></form>';
}