<?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 (isset($_POST['day'])){
	$shop = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'topcost' LIMIT 1"));
$user = functions::get_user($user);

	   $day=intval($_POST['day']);
	   $alcost = $day * $shop['val'];
	
	$a = $day * 24 * 60 * 60;
	$b = time() + $a;
	
   if ($user['balans']<$alcost){
  echo functions::display_error('Не достаточно денег');
  require_once('incfiles/end.php');
   exit;
}

  if (!$error){
    
     
      mysql_query("UPDATE `forum_topics` SET `timetop` = '$b' WHERE `topic` = '$topic' LIMIT 1");
mysql_query("UPDATE `users` SET `balans` = '".($user['balans']-$alcost)."' WHERE `id` = '$user[id]' 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;
  }
}
$shop = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'topcost' LIMIT 1"));

$user = functions::get_user($user);

$post = mysql_fetch_assoc(mysql_query("SELECT * FROM `forum_topics` WHERE topic = '$topic' LIMIT 1"));
$tim = time();
$total = mysql_result(mysql_query("SELECT COUNT(*) FROM `forum_topics` WHERE `timetop` > '$tim'"), 0);
 
 
 $op = mysql_fetch_assoc(mysql_query("SELECT * FROM `cms_settings` WHERE `key` = 'toppl' LIMIT 1"));
 
    if ($total>=$op['val']) { echo '<div class="alert alert-success">Все места заняты (' .$op['val'] .'). <br /> Место освободится ';
	$q = mysql_query("SELECT * FROM `forum_topics` WHERE `timetop` > '$time' ORDER BY `timetop` DESC  LIMIT 1");
while ($tak = mysql_fetch_assoc($q)){
echo functions::display_date($tak['timetop']);} 

	 echo'</div>'; 
	} else {
	if ($post['timetop']>time()){ echo '<div class="alert alert-success">Ваша тема уже в топе. Дата окончания ' . functions::display_date($post['timetop']) . '</div>'; } else {

 

echo'<div class="phdr">Закрепить тему</div>' .
    '<div class="user"><p>' . functions::display_user($user, array('iphide' => 1,)) . '</p></div>';
	echo '<div class="gmenu"><p>Ваш баланс: ' . $user['balans'] . ' рублей</p>';
	echo '<p>Цена закрепления в топ: ' . $shop['val'] . ' рублей</p>';
	
	
echo '</div>';
echo '<div class="user"><form id="form" action="index.php?act=top&amp;topic='.$topic.'&amp;page='.$page.'" method="post">';
echo "Введите количество дней: <br /><input  placeholder = 'Дней' type='text' name='day' value='1' />\n";
echo ' <br /><input type="submit" class="btn btn-default" name="send" value="Закперить" /></form> </div>';

}}