<?
	###Разработано в студии misoft (2018).
	
	if ( $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest' ) {
		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(); }
	}

?>

<div class="content_bar content_bar_fix_chain">
    
	<div class="navname-root">
	  <span class="">Форум</span>
	</div>
	
	<?
    $forum = mysql_query( "SELECT * FROM forum" );
    while ( $forum_row = mysql_fetch_array( $forum ) ) {
      
	  ?>
		<table class="blueTable">
		  <thead>
			<tr>
			  <th ><i class="fa fa-bullhorn bullhorn-forum" aria-hidden="true"></i> <?=$forum_row['forumname']?></th>
			  <th ><i class="fa fa-comment-o" aria-hidden="true"></i> Последняя тема</th>
			</tr>
		  </thead>
		  <tbody>
		    <?
			  $forum_cat = mysql_query( "SELECT * FROM forum_cat WHERE forum_id = '" . $forum_row['id'] . "'" );
			  while ( $forum_cat_row = mysql_fetch_array( $forum_cat ) ) {
				
				#Считаем количество тем и сообщений в конкретном разделе
				$all_forum_topic = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM forum_top WHERE cat_id = '" . $forum_cat_row['id'] . "'" ) );
				$all_forum_messages = mysql_fetch_array( mysql_query( "SELECT COUNT(id) as count FROM forum_mes WHERE top_id in (SELECT id FROM forum_top WHERE cat_id = '" . $forum_cat_row['id'] . "') " ) );
				
				#Получаем последний пост форума
				$forum_top = mysql_query( "SELECT t1.*, t2.top_id, t2.date FROM forum_top t1, forum_mes t2 WHERE t1.cat_id = '" . $forum_cat_row['id'] . "' and t2.top_id = t1.id ORDER BY t2.date DESC LIMIT 1" );
				
				if ( mysql_num_rows( $forum_top ) > 0 ) {
					$forum_top_row = mysql_fetch_array( $forum_top );
					
					#Получаем id пользователя последнего сообщения форума
					$forum_mes_id_user = mysql_fetch_array( mysql_query( "SELECT iduser FROM forum_mes WHERE top_id = '" . $forum_top_row['id'] . "' ORDER BY date DESC" ) );
					$last_name_user = mysql_fetch_array( mysql_query( "SELECT avatar, username FROM user WHERE id = '" . $forum_mes_id_user['iduser'] . "' LIMIT 1" ) );
					
					#Обрезаем длинные названия
					if ( strlen( $forum_top_row['top_title'] ) > 28 ) $forum_top_row['top_title'] = substr( $forum_top_row['top_title'], 0, 28 ) . ' [...]';
					
					#Дата последнего сообщения
					$month = array( 'янв', 'фев', 'мар', 'апр', 'май', 'июн', 'июл', 'авг', 'сен', 'окт', 'ноя', 'дек' );
					if ( date( 'd.m.Y', $forum_top_row['date'] ) == date( 'd.m.Y', time() ) ) {
						$mess_time = 'сегодня в ' . date( 'H:i', $forum_top_row['date'] );
					} else
					if ( date( 'd.m.Y', $forum_top_row['date'] ) == date( 'd.m.Y', time() - 86400 ) ) {
						$mess_time = 'вчера в ' . date( 'H:i', $forum_top_row['date'] );
					} else {
						$mess_time = ( int ) date( 'd', $forum_top_row['date'] ) . ' ' . $month[( int ) date( 'm', $forum_top_row['date'] ) - 1] . ' ' . date( 'Y', $forum_top_row['date'] )  . ' в ' . date( 'H:i', $forum_top_row['date'] );
					}
					
					$last_name = '
				        <div class="last-post-forum-block">
						  <a href="/posts' . $forum_top_row['id'] . '" class="last-post-forum-link">
						    <div class="last-post-forum-title">' . $forum_top_row['top_title'] .'</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"><i class="fa fa-user-o" aria-hidden="true"></i> ' . $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_name = '<div class="not-last-post-forum"><i class="fa fa-comment-o" aria-hidden="true"></i> Тем еще нет!</div>';
				}
		        
				?>
				  <tr>
					<td colspan="1">
					  <div class="forum-topic">
						
						<div class="forum-home-icon"></div>
						<a href="/topic<?=$forum_cat_row['id']?>" class="title-of-topics">
						  <?=$forum_cat_row['cat_title']?>
						</a></br>
						<span class="description-of-topics">
						  <?=$forum_cat_row['cat_des']?>
						</span>
						<div class="number-of-topics" >
						  <?=$all_forum_topic['count']?> <?=getWord( $all_forum_topic['count'], array( 'тема', 'темы', 'тем' ) )?>
						</div>
						<div class="number-of-messages" >
						  <?=$all_forum_messages['count']?> <?=getWord( $all_forum_messages['count'], array( 'сообщение', 'сообщения', 'сообщений' ) )?>
						</div>
					  
					  </div>
					</td>
					<td style="width: 30% "><?=$last_name?></td>
				  </tr>
		        <?
		      }
		    ?>
		  </tbody>
		</table>
		<br>
      <?
	
	}
?>
</div>
<center><script src="//static.surfe.pro/js/net.js"></script>
<ins class="surfe-be" data-sid="230174"></ins> 
<script>(adsurfebe = window.adsurfebe || []).push({});</script></center>