<?
###Разработано в студии misoft (2018).

if ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
    define( 'ROOT', 'http://'.$_SERVER['HTTP_HOST'] );
    define( 'ROOT_DIR', $_SERVER['DOCUMENT_ROOT'] );
    session_start();
    header( 'Content-type: text/html; charset=windows-1251' );
    include_once ROOT_DIR . '/engine/config.php';
    include_once ROOT_DIR . '/engine/function.php';
} else {
    if ( !defined( 'MISOFT' ) ) { exit(); }
}

$forum_cat = mysql_query( "SELECT id, cat_title FROM forum_cat WHERE id = '" . ( int ) $_GET['id'] . "' LIMIT 1" );
if ( mysql_num_rows( $forum_cat ) > 0 ) {
    
    $forum_top_page = mysql_query( "SELECT id FROM forum_top WHERE cat_id = '" . ( int ) $_GET['id'] . "'" );
    $kolvo = 15;
    $all = mysql_num_rows( $forum_top_page );
    $allpage = ceil( $all / $kolvo );
    $this_page = ( isset( $_GET['page'] ) ) ? intval( $_GET['page'] ) : 1 ;
    if ( $this_page < 1 ) $this_page = 1;
    if ( $this_page > $allpage ) $this_page = $allpage;
    $start = $this_page * $kolvo - $kolvo;
    if ( $start == -$kolvo ) { $start = 1; }
    
    $forum_cat_row = mysql_fetch_array( $forum_cat );
    ?>
    
	<div class="content_bar content_bar_fix_chain">
	
		<div class="navname">
		  <a href="/forum"  class="a_box">Форум</a> 
		  | <span class="navname_act"><?=$forum_cat_row['cat_title']?></span>
		</div>
		<?
		  if ( isset( $_SESSION['iduser'] ) and $config_user['forumblock'] == 0 and $forum_cat_row['id'] != 1 or $config_user['account'] == 5 ) {
		   ?>
			<div class="block-forum-topic-add">
			  <a href="#" onclick="forum.Newtopic(<?=( int ) $_GET['id']?>);return false;" class="btn-forum-topic-add"><i class="fa fa-plus" ></i> Новая тема</a>
			</div>
		   <? 
		  } 
		
		$forum_top = mysql_query( "SELECT id, cat_id, top_title, iduser, t_close FROM forum_top WHERE cat_id = '" . ( int ) $_GET['id'] . "' ORDER BY date DESC LIMIT $start,$kolvo" );
			
		if( mysql_num_rows( $forum_top ) > 0 ) {
		 ?>
			<table class="blueTable">
			  <thead>
				<tr>
				  <th ><i class="fa fa-bullhorn bullhorn-forum" aria-hidden="true"></i> Темы</th>
				  <th ><i class="fa fa-comment-o" aria-hidden="true"></i> Последнее сообщение</th>
				</tr>
			  </thead>
			  
			  <?
				while ( $forum_top_row = mysql_fetch_array( $forum_top ) ) {
					
					#Кнопка удаления, закрытие темы
					if ( $config_user['account'] == 5 OR $config_user['account'] == 7 ) {
						$btn_del_topic = '<span onclick="forum.DelTopic(' . $forum_top_row['id'] . '); return false;" class="del_topic" id = "ct' . $forum_top_row['id'] . '"><i class="fa fa-trash-o" aria-hidden="true"></i></span>';
						if ( $forum_top_row['t_close'] ) {
							$btn_close_topic = '<span onclick="forum.CloseTopic(' . $forum_top_row['id'] . ', ' . ( int ) $_GET['id'] . '); return false;" class="open_topic" id = "ct' . $forum_top_row['id'] . '">
							  <i class="fa fa-window-close" aria-hidden="true"></i>
							</span>';
							$theme_close = '<span class="theme_close">(тема закрыта)</span>';
						} else {
							$btn_close_topic = '<span onclick="forum.CloseTopic(' . $forum_top_row['id'] . ', ' . ( int ) $_GET['id'] . '); return false;" class="close_topic" id = "ct' . $forum_top_row['id'] . '">
							  <i class="fa fa-window-close-o" aria-hidden="true"></i>
							</span>';
							$theme_close = '';
						}
					} else {
						$btn_del_topic = '';
						$btn_close_topic = '';
					}
					
					#Автор темы
					$forum_avtor = mysql_fetch_array( mysql_query( "SELECT username FROM user WHERE id = '" . $forum_top_row['iduser'] . "' LIMIT 1" ) );
					
					#Кол-во сообщений в теме
					$all_mess = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM forum_mes WHERE top_id = '" . $forum_top_row['id'] . "'" ) );
					
					#Последнее сообщение темы
					$last_mess = mysql_query( "SELECT iduser, date, mess FROM forum_mes WHERE top_id = '" .$forum_top_row['id'] . "' ORDER BY date DESC LIMIT 1" );
					
					if ( mysql_num_rows( $last_mess ) > 0 ) {
						
						$last_mess_row = mysql_fetch_array( $last_mess );
						$last_name_user = mysql_fetch_array( mysql_query( "SELECT username, avatar FROM user WHERE id = '" . $last_mess_row['iduser'] . "' LIMIT 1" ) );
						
						if ( $last_mess_row['date'] + 60 > time() ) {
							$mess_time = '1 минуту назад';
						} else
						if ( $last_mess_row['date'] + 5 * 60 > time() ) {
							$mess_time = '5 минут назад';
						} else
						if ( $last_mess_row['date'] + 10 * 60 > time() ) {
							$mess_time = '10 минут назад';
						} else
						if ( $last_mess_row['date'] + 30 * 60 > time() ) {
							$mess_time = '30 минут назад';
						} else
						if ( $last_mess_row['date'] + 3600 > time() ) {
							$mess_time = '1 час назад';
						} else
						if ( $last_mess_row['date'] + 2 * 3600 > time() ) {
							$mess_time = '2 часа назад';
						} else {
							$month = array( 'янв', 'фев', 'мар', 'апр', 'май', 'июн', 'июл', 'авг', 'сен', 'окт', 'ноя', 'дек' );
							$mess_time = ( int ) date( 'd', $last_mess_row['date'] ) . ' ' . $month[( int ) date( 'm', $last_mess_row['date'] ) - 1] . ' ' . date( 'Y', $last_mess_row['date'] ) . ' в ' . date( 'H:i', $last_mess_row['date'] );
						}
						
						#Обрезаем длинные сообщения
						if ( strlen( strip_tags ( $last_mess_row['mess'] ) ) > 25 ) $last_mess_row['mess'] = substr( strip_tags ( $last_mess_row['mess'] ), 0, 25 ) . ' [...]';
						
						$last_forum_mes = '<div class="last-post-forum-block">
							  <a href="/posts' . $forum_top_row['id'] . '" class="last-post-forum-link">
								<div class="last-post-forum-title">' .  strip_tags ( $last_mess_row['mess'] ) .'</div>
							  </a>
							  <div class="last-post-forum-avatar"><img src="/temp/avatar/' . $last_name_user['avatar'] . '" class="img-circle" width="27" height="27" align="left" /></div>
							  <div class="last-post-forum-username"> ' . $last_name_user['username'] . '</div>
							  <div class="last-post-forum-data"><i class="fa fa-calendar" aria-hidden="true"></i> ' . $mess_time . ' </div>
							</div>';
					} else {
						$last_forum_mes = '';
					}
					
					#Обрезаем длинные заголовки тем
					if ( strlen( $forum_top_row['top_title'] ) > 45 ) $forum_top_row['top_title'] = substr( $forum_top_row['top_title'], 0, 45 ) . ' [...]';
					
					?>
					  <tr>
						<td colspan="1">
						  <div class="forum-topic">
							<div class="forum-home-icon"></div>
							<a href="/posts<?=$forum_top_row['id']?>" class="title-of-topics">
							  <?=$forum_top_row['top_title']?>
							</a></br>
							<span class="description-of-topics">
							  Автор темы: <a href="/id<?=$forum_top_row['iduser']?>" class="a_box"><?=$forum_avtor['username']?></a>
							</span>
							<div class="number-of-topics" >
							  <?=$all_mess['count']?> <?=getWord( $all_mess['count'], array( 'сообщение', 'сообщения', 'сообщений' ) )?>
							</div>
							<div class="number-of-messages " >
							  &nbsp;<?=$theme_close?> 
							  <?=$btn_close_topic ?>
							  <?=$btn_del_topic?>
							</div>
						  </div>
						</td>
						<td style="width: 30% "><?=$last_forum_mes?></td>
					  </tr>
					<?
				}
			  
			  ?>
			</table>
			<div class="news_navigation-page"><?=navigation( 15, ( int ) $_GET['page'], $all, 'topic' . ( int ) $_GET['id'] . '&p=' );?></div>
		  <?
		} else {
		  echo '<br><div class="alert alert-info " role="alert" >
			<i class="fa fa-frown-o" aria-hidden="true"></i> Сожалеем но в этом разделе все еще пока пусто. 
		  </div>';
		}
	  ?>
	</div>
	
  <?
} else {
    echo '<div class="alert alert-warning page-error-fix" role="alert" >
	  <i class="fa fa-exclamation-circle" aria-hidden="true"></i> Ошибка! Раздел не существует или удален. 
	</div>';
}

?>