<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

//------
//whr: 3
//------

$op = (isset($_GET['op'])) ? inCode($_GET['op']) : "" ;
$id = (isset($_GET['id'])) ? intval($_GET['id']) : 0 ;
$nid = (isset($_GET['nid'])) ? intval($_GET['nid']) : 0 ;
$title = (isset($_GET['title'])) ? mem_urldecode(inCode($_GET['title'])) : "" ;
$ok = (isset($_GET['ok'])) ? inCode($_GET['ok']) : "" ;
$pg = (isset($_GET['pg'])) ? intval($_GET['pg']) : 1 ;	
$rank = (isset($_GET['rank'])) ? intval($_GET['rank']) : 1 ;
$email_mittente = (isset($_POST['email_mittente'])) ? inCode($_POST['email_mittente']) : "" ;
$email_destinatario = (isset($_POST['email_destinatario'])) ? inCode($_POST['email_destinatario']) : "" ;

function newsList($pg) {
	global $dblink,$siteConfig,$page,$tpl;
	
	$ofsppg = (isset($_GET['page'])) ? intval($siteConfig['num_news_page']) : intval($siteConfig['num_news_home']) ;
	$ofsbgn = ($pg*$ofsppg)-$ofsppg;
	
	$tpl_news = array();
	if ($result = $dblink->get_list("SELECT n.*,a.nome AS nomearg,DATE_FORMAT(n.data, '".$siteConfig['timestamp']."') as data_out,(SELECT ROUND(SUM(vote)/COUNT(id)) AS irank FROM memht_ratings WHERE whr=3 AND wid=n.id) AS rank,(SELECT COUNT(id) FROM memht_comments WHERE whr=3 AND wid=n.id AND moderate=0) AS ncomments,a.img FROM memht_news AS n LEFT JOIN memht_argomenti AS a ON n.argomento=a.id WHERE n.enabled=1 ORDER BY n.id DESC LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result as $row) {
			$id = intval($row['id']);
			$argument = intval($row['argomento']);
			$name = outCode($row['nome']);
			$text_home = outCode($row['testo_home']);
			$more = ($row['testo']=="") ? 0 : 1 ;
			$text = outCode($row['testo']);
			$author = outCode($row['autore']);
			$date_orig = $row['data'];
			$date = $row['data_out'];
			$hits = intval($row['hits']);
			$rank = intval($row['rank']);
			$comments = intval($row['ncomments']);
			$img = outCode($row['img']);
			$argname = outCode($row['nomearg']);
			$usecomments = intval($row['usecomments']);
			
			if ($result = $dblink->get_list("SELECT tag FROM memht_tags WHERE whr=3 AND cid=$id ORDER BY tag")) {
				$tags = "";
				foreach ($result as $row) {
					$tags .= "<a href=\"index.php?page=tags&op=list&tag=".mem_urlencode(outCode($row['tag']))."\" title=\"".outCode($row['tag'])."\">".outCode($row['tag'])."</a> ";
				}
			} else {
				$tags = "-";
			}
			
			if (!empty($img) AND file_exists("templates/".$siteConfig['template']."/images/topics/$img.gif")) {
				$img = "<img src='templates/".$siteConfig['template']."/images/topics/$img.gif' border='0' alt='Topic'>";
			} else if (!empty($img) AND file_exists("images/topics/$img.gif")) {
				$img = "<img src='images/topics/$img.gif' border='0' alt='Topic'>";
			} else {
				$img = "<img src='images/topics/empty.gif' border='0' alt='Topic'>";
			}
			
			//
			$cdate = explode(" ",$date_orig);
			$cdate = explode("-",$cdate[0]);
			$day = $cdate[2];
			$month = numToMonth($cdate[1],1);
			$year = $cdate[0];
			
			$tpl_news[] = array(
				"id"=>"$id",
				"title"=>"$name",
				"content"=>"$text_home",
				"more"=>"$more",
				"author"=>"$author",
				"date"=>"$date",
				"day"=>"$day",
				"month"=>"$month",
				"year"=>"$year",
				"hits"=>"$hits",
				"topicimg"=>"$img",
				"usecomments"=>"$usecomments",
				"ncomments"=>"$comments",
				"rank"=>"$rank",
				"tags"=>"$tags",
				"argument"=>"$argname"
			);
		}
		$tpl->assign("tpl_news",$tpl_news);
		
		if (isset($_GET['page'])) {
			//Pages
			include_once("inc/class/paginationSystem.class.php");
			$ps = new paginationSystem();
			$ps->items = $ofsppg;
			$ps->actpg = $pg;
			$ps->query = "SELECT id FROM memht_news WHERE enabled=1";
			$ps->url = "index.php?page=news&pg={{N}}";
			$ps->show();
		} else {
			$num_news = $dblink->get_num("SELECT id FROM memht_news WHERE enabled=1");
			$pages = ceil($num_news/$ofsppg);
			if ($num_news>$ofsppg) {
				echo "<div style='padding:8px 0 8px 8px; font-size:120%;'>";
					echo "&laquo; <a href='index.php?page=news' title='"._READ_OTHER_NEWS_."'><b>"._READ_OTHER_NEWS_."</b></a>";
				echo "</div>";
			}
		}
	} else {
		echo "<div align='center' id='errorText'><b>"._EMPTY_."</b></div>";
	}
}
	
function newsView($id,$title) {
	global $dblink,$siteConfig,$userid,$page;
	
	$id = intval($id);
	$inchits = $dblink->query("UPDATE memht_news SET hits=hits+1 WHERE id=$id OR nome='$title'");

	if ($row_news = $dblink->get_row("SELECT n.*,a.img,DATE_FORMAT(n.data, '".$siteConfig['timestamp']."') as data,(SELECT ROUND(SUM(vote)/COUNT(id)) AS irank FROM memht_ratings WHERE whr=3 AND wid=n.id) AS rank,(SELECT COUNT(id) FROM memht_comments WHERE whr=3 AND wid=n.id AND moderate=0) AS ncomments,a.img FROM memht_news AS n LEFT JOIN memht_argomenti AS a ON n.argomento=a.id WHERE n.enabled=1 AND (n.id=$id OR n.nome='$title')")) {
		$id = intval($row_news['id']);
		$argomento = intval($row_news['argomento']);
		$nome = outCode($row_news['nome']);
		$testo_home = outCode($row_news['testo_home']);
		$testo = outCode($row_news['testo']);
		$autore = outCode($row_news['autore']);
		$data = $row_news['data'];
		$hits = intval($row_news['hits']);
		$rank = intval($row_news['rank']);
		$img = outCode($row_news['img']);
		$usecomments = intval($row_news['usecomments']);
		$language = intval($row_news['language']);
		
		if ($language>0) {
			$row = $dblink->get_row("SELECT * FROM memht_content_languages WHERE id=$language");
			$languageid = intval($row['id']);
			$languagetxt = outCode($row['language']);
		} else {
			$languageid = 0;
			$languagetxt = _GLOBAL_;
		}
		
		if ($img!="" AND file_exists("templates/".$siteConfig['template']."/images/topics/$img.gif")) { $img = "<img src='templates/".$siteConfig['template']."/images/topics/$img.gif' border='0' alt='Topic'>"; }
		else if ($img!="" AND file_exists("images/topics/$img.gif")) { $img = "<img src='images/topics/$img.gif' border='0' alt='Topic'>"; }
		else { $img = "<img src='images/topics/empty.gif' border='0' alt='Topic'>"; }
		
		$contenuto = ($testo!="") ? "$testo_home<br><br>$testo" : "$testo_home" ;
	
		openTable();
			//Content head
			include_once("inc/class/conthead.class.php");
			$ch = new conthead();
			$ch->title = $nome;
			$ch->cdate = $data;
			$ch->author = $autore;
			$ch->hits = $hits;
			$ch->langid = $languageid;
			$ch->langtxt = $languagetxt;
			$ch->prnt_url = "pages/news/print.php?id=$id";
			$ch->pdf_url = "pages/news/pdf.php?id=$id";
			$ch->rss_url = "rss.php?page=news&topic=$argomento";
			$ch->url = $siteConfig['site_url']."/index.php?page=news&op=readNews&id=$id&title=".mem_urlencode($nome);
			$ch->show();
			
			echo "<div style='padding:20px 0; clear:left;'>$contenuto</div>\n";
			
			//Rating
			include_once("inc/class/rating.class.php");
			$rt = new rating();
			$rt->whr = 3;
			$rt->wid = $id;
			$rt->rank = $rank;
			$rt->show();
			
			//Tags
			include_once("inc/class/tags.class.php");
			$tg = new tags();
			$tg->whr = 3;
			$tg->wid = $id;
			$tg->show();
		closeTable();
		
		if ($siteConfig['usecomments']==1 AND $usecomments==1) {
			openTable();
				//Comments
				include_once("inc/class/comments.class.php");
				$cc = new comments();
				$cc->whr = 3;
				$cc->wid = $id;
				$cc->show();
			closeTable();
		}
	} else {
		openTable();
			echo "<div align='center' id='errorText'><b>"._NEWSDONOTEXISTS_."</b></div>";
		closeTable();
	}
}

switch($op) {
	default:
		newsList($pg);
	break;
	
	case "readNews":
		newsView($id,$title);
	break;
}

?>