<?php
	$page_title="News about KDE Localization";

	include_once("lib/config.php");
	include_once("lib/functions.php");
	include_once("stats/configuration.inc.php");
	include_once("stats/stats-functions.inc.php");
	
	include("header.inc");

	$showedit=false;

	$newsCount=config("news_per_page");
	
	$GLOBALS["dbh"]=initdb(config("mysql_host"), config("mysql_user"), config("mysql_password"), config("mysql_database"));
	
	if (isset($_GET["ofs"]))
		$offset=(int) abs($_GET["ofs"]);
	else
		$offset=0;
		
	
	if (isset($_GET["id"])) {
		$id=(int) abs($_GET["id"]);
	}
	else {
		$id=-1;
	}
	
	
	if ($id!=-1) {
		$query="SELECT * FROM news WHERE id=$id ORDER BY ndate DESC LIMIT $offset,$newsCount";
	}
	else {
		$query="SELECT * FROM news ORDER BY ndate DESC LIMIT $offset,$newsCount";
	}
	
	
	if ($result=@mysql_query($query,$GLOBALS["dbh"])) {
		echo "
			<table class='table_box'>
		";
	
		while ($row=@mysql_fetch_array($result)) {
			$tempId=$row['id'];
			$ndate=sdate_fmt($row['ndate']);
			$title_html=htmlspecialchars($row['title']);
			$subtitle_html=htmlspecialchars($row['subtitle']);
			$username_html=htmlspecialchars($row['username']);
			$content=$row['content'];
			$content=html_entity_decode($content, ENT_QUOTES);
			
			echo "
				<tr>
					<td class='newsbox2'><h3><a>$ndate: $title_html</a><span style='font-size:small;float:right'>by $username_html</span></h3></td>
				</tr>
				<tr>
					<td class='newsbox2' style='padding-bottom:10px'>
						$content
					</td>
				</tr>
			";
		}
		
		echo "</table>";
		
		
	}
	else {
		$problem="No news to display.";
	}
	
	//If we display only one news, a link allow the user to display other ones
	if ($id!=-1) {
		echo "<div id='quicklinks'> [ <a href='news.php'>Display all news</a> ]</div>";
	}
	//Else we display an history bar
	else {
		//Display Navigation Bar
		$query="SELECT COUNT(id) AS total FROM news";
		$total=0;
		if ($result=@mysql_query($query,$GLOBALS["dbh"])) {
			$row=@mysql_fetch_array($result);
			$total=$row['total'];
		}
		
		if ($total!=0 && config("news_per_page")<=$total) {
			echo "<div id='quicklinks'> [ ";
			
			$count=ceil($total/$newsCount);
			for($i=0;$i<$count;$i++) {
				$ofs_start=$i*$newsCount+1;
				$ofs_stop=($i+1)*$newsCount;
				$ofs_stop=($ofs_stop>$total) ? $total : $ofs_stop;
				$start=$i*$newsCount;
				if ($offset+1>=$ofs_start && $offset+1<=$ofs_stop) {
					echo $ofs_start . "-" . $ofs_stop;
				}
				else {
					echo "<a href=\"news.php?ofs=$start\">" . $ofs_start . "-" . $ofs_stop . "</a>";
				}
				
				if ($i!=$count-1) {
					echo  " | ";
				}
			}
			
			echo "]</div>";
		}
	}

	include("footer.inc");
?>
