<!DOCTYPE html>
<html>

<!-- Meta data -->
<?php include('meta.php'); ?>

<body>

<!-- Header -->
<?php include('header.php'); ?>

<!-- Menu -->
<?php include('menu.php'); ?>

<!-- START MAIN CONTENT -->
<div class="qet-main">

  <div class="w3-row w3-padding-48">
    <div class="w3-twothird w3-container">
        <div class="w3-xxxlarge w3-left" style="margin-right:12px"><i class="fa fa-newspaper-o"></i></div>
        <h1 class="w3-text-dark-grey">News</h1>
        <p>Stay up-to-date with the latest news about the software updates and everything around the QET project.</p>
        <b><p class="qet-text-red">Sorry, the news are always written in french. Only sometimes, they are translated into english.</p></b><br />

<?php
	include_once('../../private/connexion_db.inc.php');

	// create the links for the pagination	
	function indent($indent) { for($i = 0 ; $i < $indent ; ++ $i) echo "\t"; }
	
	function writePageLink($text, $link) {
		$has_link = ($link != '');

		echo "\t".'<span class="qet-white" style="vertical-align:middle;text-align:center;cursor:default;float:left;border:none;padding:2px;margin:0px 8px 4px 0px;width:28px;height:28px;border-radius:50%">';
		if ($has_link) echo '<a href="'.$link.'" class="qet-btn-round qet-hover-dark-gray qet-very-light-gray">';
		echo htmlentities($text);
		if ($has_link) echo '</a>';
		echo '</span>'."\n";
	}

	// display the pagination	
	function writePages($max_page_num, $page_number = 1, $indent = 4) {
		$has_previous_page = $page_number > 1;
		$has_next_page     = $page_number < $max_page_num;
		
		echo '<div class="w3-container">';
		for ($i = 1 ; $i <= $max_page_num ; ++ $i) {
			indent($indent);
			writePageLink($i, $i != $page_number ? $_SERVER['PHP_SELF'].'?page='.$i : '');
		}
		echo '</div>';
	}
?>

<?php
	// display the news
	if ($qet_mysql_ok) {
		$request_news_count = 'SELECT COUNT(*) AS count FROM punbb_topics WHERE forum_id='.$qet_news_forum_id.';';
		$news_count = mysql_query($request_news_count);
		if ($news_count) {
			$news_count = mysql_fetch_assoc($news_count);
			$news_count = $news_count['count'];
			
			// number of news per page
			$news_per_page = 5;
			
			// number of pages
			$max_page_num = ceil($news_count / $news_per_page);
			
			// find the number of the page to be displayed
			$page_number = isset($_GET['page']) && preg_match('/^[0-9]+$/', $_GET['page']) ? $_GET['page'] : 1;
			if ($page_number < 1) $page_number = 1;
			else if ($page_number > $max_page_num) $page_number = $max_page_num;
			
			$requete_news  = 'SELECT t.id, t.subject, p.poster, p.posted, p.message FROM punbb_topics t ';
			$requete_news .= 'LEFT JOIN punbb_posts p ON p.id = t.first_post_id ';
			$requete_news .= 'WHERE forum_id = '.$qet_news_forum_id.' ';
			$requete_news .= 'ORDER BY t.posted DESC ';
			$requete_news .= 'LIMIT '.($page_number - 1) * $news_per_page.', '.$news_per_page.' ;';
			
			$news = mysql_query($requete_news);
			if ($news) {
				writePages($max_page_num, $page_number);
				while ($new = mysql_fetch_assoc($news)) {
					foreach($new as $cle => $val) if ($cle != 'message') $new[$cle] = htmlentities($val);
?>
                <!-- display one news -->
    			<a name="<?=$new['id']?>"></a>
				<div class="w3-xlarge w3-card-2 qet-serif qet-very-light-gray" style="margin-top:50px">
                    <?=$new['subject']?><br />
					<div style="font-size:80%";">
					<i>by <span style="font-weight:bold;"><?=$new['poster']?></span> on <?=date('d/m/Y \a\t H:i:s', $new['posted'])."\n"?></i>
					</div>
				</div>
				<div style="margin-top:20px">
					<?=forum_parser($new['message'])?>
				</div>
				<div class="w3-padding-12" style="margin-bottom:20px;">
					<a href="../<?=$qet_forum_base_url?>viewtopic.php?id=<?=$new['id']?>" class="qet-text-link qet-hover-text-link"><i class="fa fa-comment"></i> Comment on the forum</a>
				</div>
<?php
				}
				writePages($max_page_num, $page_number);
			}
		}
	}
?>
    </div>







<!-- Side panel for sponsors, partners and last news -->
    <?php include('sidepanel.php'); ?>
  </div>

<!-- Footer -->
<?php include('footer.php'); ?>

<!-- END MAIN CONTENT -->
</div>

<!-- Javascript -->
<script src="../js-responsive.js"></script>
<script src="../js-menu.js"></script>

</body>
</html>
