<?php
/************************************************************************/
/* Maximus CMS: Web Portal System                                       */
/* ==============================                                       */
/*                                                                      */
/* Copyright (c) 2002 - 2006                                            */
/* http://www.php-maximus.fr                                            */
/* dev@php-maximus.org                                                  */
/*                                                                      */
/* 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.       */
/************************************************************************/
/* Based on PHP-NUKE: Web Portal System                                 */
/************************************************************************/
defined('MAXIMUS') or Header("Location: ../../index.php");

$module_name = "Top";
get_lang($module_name);

incoh( 'Top', 'html',  "".CACHE_VTOP."", '1' );

if ($multilingual == 1) {
	$queryalang = "WHERE (`alanguage`='$currentlang' OR `alanguage`='')"; /* top stories */
	$querya1lang = "WHERE (`alanguage`='$currentlang' OR `alanguage`='') AND"; /* top stories */
	$queryslang = "WHERE `slanguage`='$currentlang' "; /* top section articles */
	$queryplang = "WHERE `planguage`='$currentlang' "; /* top polls */
	$queryrlang = "WHERE `rlanguage`='$currentlang' "; /* top reviews */
} else {
	$queryalang = "";
	$querya1lang = "WHERE";
	$queryslang = "";
	$queryplang = "";
	$queryrlang = "";
}

Max_OpenTable();
echo "<center><font class='title'><b>"._TOPWELCOME." $sitename!</b></font></center>";
Max_CloseTable();
br('1');
Max_OpenTable();

/* Top 10 read stories */
$result = $GLOBALS['db']->sql_query("SELECT `sid`, `title`, `counter` FROM `".TABLE_STORIES."` $queryalang ORDER BY `counter` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._READSTORIES."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row = $GLOBALS['db']->sql_fetchrow($result)) {
		$sid = $GLOBALS['db']->XMFnum($row['sid']);
		$title = stripslashes(check_html($row['title'], "nohtml"));
		$counter = $GLOBALS['db']->XMFnum($row['counter']);
		if($counter>0) {
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=News&amp;file=article&amp;sid=$sid\">$title</a> - ($counter "._READS.")<br />\n";
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 most voted stories */

$result2 = $GLOBALS['db']->sql_query("SELECT `sid`, `title`, `ratings` FROM `".TABLE_STORIES."` $querya1lang `score`!='0' ORDER BY `ratings` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result2) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._MOSTVOTEDSTORIES."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row2 = $GLOBALS['db']->sql_fetchrow($result2)) {
		$sid     = $GLOBALS['db']->XMFnum($row2['sid']);
		$title   = stripslashes(check_html($row2['title'], "nohtml"));
		$ratings = $GLOBALS['db']->XMFnum($row2['ratings']);
		if($ratings>0) {
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=News&amp;file=article&amp;sid=$sid\">$title</a> - ($ratings "._LVOTES.")<br />\n";
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 best rated stories */

$result3 = $GLOBALS['db']->sql_query("SELECT `sid`, `title`, `score`, `ratings` FROM `".TABLE_STORIES."` $querya1lang `score`!='0' ORDER BY ratings+score DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result3) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._BESTRATEDSTORIES."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row3 = $GLOBALS['db']->sql_fetchrow($result3)) {
		$sid     = $GLOBALS['db']->XMFnum($row3['sid']);
		$title   = stripslashes(check_html($row3['title'], "nohtml"));
		$score   = $GLOBALS['db']->XMFnum($row3['score']);
		$ratings = $GLOBALS['db']->XMFnum($row3['ratings']);
		if($score>0) {
			$rate = substr($score / $ratings, 0, 4);
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=News&amp;file=article&amp;sid=$sid\">$title</a> - ($rate "._POINTS.")<br />\n";
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 commented stories */

if ($articlecomm == 1) {
	$result4 = $GLOBALS['db']->sql_query("SELECT `sid`, `title`, `comments` FROM `".TABLE_STORIES."` $queryalang ORDER BY `comments` DESC LIMIT 0,$top");
	if ($GLOBALS['db']->sql_numrows($result4) > 0) {
		echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
		."<font class=\"option\"><b>$top "._COMMENTEDSTORIES."</b></font><br /><br /><font class=\"content\">\n";
		$lugar=1;
		while ($row4 = $GLOBALS['db']->sql_fetchrow($result4)) {
			$sid = $GLOBALS['db']->XMFnum($row4['sid']);
			$title = stripslashes(check_html($row4['title'], "nohtml"));
			$comments = $GLOBALS['db']->XMFnum($row4['comments']);
			if($comments>0) {
				echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=News&amp;file=article&amp;sid=$sid\">$title</a> - ($comments "._COMMENTS.")<br />\n";
				$lugar++;
			}
		}
		echo "</font></td></tr></table>\n";
		br('1');
	}
}

/* Top 10 categories */

$result5 = $GLOBALS['db']->sql_query("SELECT `catid`, `title`, `counter` FROM `".TABLE_STORIES_CAT."` ORDER BY `counter` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result5) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._ACTIVECAT."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row5 = $GLOBALS['db']->sql_fetchrow($result5)) {
		$catid = $GLOBALS['db']->XMFnum($row5['catid']);
		$title = stripslashes(check_html($row5['title'], "nohtml"));
		$counter = $GLOBALS['db']->XMFnum($row5['counter']);
		if($counter>0) {
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=News&amp;file=categories&amp;op=newindex&amp;catid=$catid\">$title</a> - ($counter "._HITS.")<br />\n";
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 articles in special sections */

$result6 = $GLOBALS['db']->sql_query("SELECT `artid`, `secid`, `title`, `content`, `counter` FROM `".TABLE_SECCONT."` $queryslang ORDER BY `counter` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result6) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._READSECTION."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row6 = $GLOBALS['db']->sql_fetchrow($result6)) {
		$artid   = $GLOBALS['db']->XMFnum($row6['artid']);
		$secid   = $GLOBALS['db']->XMFnum($row6['secid']);
		$title   = stripslashes(check_html($row6['title'], "nohtml"));
		$content = stripslashes($row6['content']);
		$counter = $GLOBALS['db']->XMFnum($row6['counter']);
		echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=Sections&amp;op=viewarticle&amp;artid=$artid\">$title</a> - ($counter "._READS.")\n";
		br('1');
		$lugar++;
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 users submitters */

$result7 = $GLOBALS['db']->sql_query("SELECT `username`, `counter` FROM `".TABLE_USERS."` WHERE `counter` > '0' ORDER BY `counter` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result7) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._NEWSSUBMITTERS."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row7 = $GLOBALS['db']->sql_fetchrow($result7)) {
		$uname   = stripslashes($row7['username']);
		$counter = $GLOBALS['db']->XMFnum($row7['counter']);
		if($counter>0) {
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=Your_Account&amp;op=userinfo&amp;username=$uname\">$uname</a> - ($counter "._NEWSSENT.")\n";
			br('1');
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 Polls */

$result8 = $GLOBALS['db']->sql_query("select * from `".TABLE_POLL_DESC."` $queryplang");
if ($GLOBALS['db']->sql_numrows($result8)>0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._VOTEDPOLLS."</b></font><br /><br /><font class=\"content\">\n";
	$lugar = 1;
	$result9 = $GLOBALS['db']->sql_query("SELECT `pollID`, `pollTitle`, `timeStamp`, `voters` FROM `".TABLE_POLL_DESC."` $queryplang order by `voters` DESC limit 0,$top");
	$counter = 0;
	while($row9 = $GLOBALS['db']->sql_fetchrow($result9)) {
		$resultArray[$counter] = array($row9['pollID'], $row9['pollTitle'], $row9['timeStamp'], $row9['voters']);
		$counter++;
	}
	for ($count = 0; $count < count($resultArray); $count++) {
		$id = $resultArray[$count][0];
		$pollTitle = $resultArray[$count][1];
		$voters = $resultArray[$count][3];
		for($i = 0; $i < 12; $i++) {
			$result10 = $GLOBALS['db']->sql_query("SELECT `optionCount` FROM `".TABLE_POLL_DATA."` WHERE (`pollID`='$id') AND (`voteID`='$i')");
			$row10       = $GLOBALS['db']->sql_fetchrow($result10);
			$optionCount = $row10['optionCount'];
			$sum         = (int)$sum+$optionCount;
		}
		echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=Surveys&amp;pollID=$id\">$pollTitle</a> - ($sum "._LVOTES.")<br />\n";
		$lugar++;
		$sum = 0;
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 authors */

$result11 = $GLOBALS['db']->sql_query("SELECT `aid`, `counter` FROM `".TABLE_AUTHORS."` ORDER BY `counter` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result11) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._MOSTACTIVEAUTHORS."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row11 = $GLOBALS['db']->sql_fetchrow($result11)) {
		$aid = stripslashes($row11['aid']);
		$counter = $GLOBALS['db']->XMFnum($row11['counter']);
		if($counter>0) {
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=Search&amp;author=$aid\">$aid</a> - ($counter "._NEWSPUBLISHED.")<br />\n";
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 reviews */

$result12 = $GLOBALS['db']->sql_query("SELECT `id`, `title`, `hits` FROM `".TABLE_REVIEWS."` $queryrlang ORDER BY `hits` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result12) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._READREVIEWS."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row12 = $GLOBALS['db']->sql_fetchrow($result12)) {
		$id    = $GLOBALS['db']->XMFnum($row12['id']);
		$title = stripslashes(check_html($row12['title'], "nohtml"));
		$hits  = $GLOBALS['db']->XMFnum($row12['hits']);
		if($hits>0) {
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=Reviews&amp;op=showcontent&amp;id=$id\">$title</a> - ($hits "._READS.")\n";
			br('1');
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n";
	br('1');
}

/* Top 10 downloads */

$result13 = $GLOBALS['db']->sql_query("SELECT `lid`, `cid`, `title`, `hits` FROM `".TABLE_NSNGD_DOWNLOADS."` ORDER BY `hits` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result13) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._DOWNLOADEDFILES."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row13 = $GLOBALS['db']->sql_fetchrow($result13)) {
		$lid   = $GLOBALS['db']->XMFnum($row13['lid']);
		$cid   = $GLOBALS['db']->XMFnum($row13['cid']);
		$title = stripslashes(check_html($row13['title'], "nohtml"));
		$hits  = $GLOBALS['db']->XMFnum($row13['hits']);
		if($hits>0) {
			$row_res = $GLOBALS['db']->sql_fetchrow($GLOBALS['db']->sql_query("SELECT `title` FROM `".TABLE_NSNGD_CATEGORIES."` WHERE `cid`='$cid'"));
			$ctitle = stripslashes(check_html($row_res['title'], "nohtml"));
			$utitle = ereg_replace(" ", "_", $title);
echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=Downloads&amp;op=getit&amp;lid=$lid\">$title</a> ("._CATEGORY.": $ctitle) - ($hits "._LDOWNLOADS.")\n";
br('1');
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n\n";
	br('1');
}

/* Top 10 Pages in Content */

$result14 = $GLOBALS['db']->sql_query("SELECT `pid`, `title`, `counter` FROM `".TABLE_PAGES."` WHERE `active`='1' ORDER BY `counter` DESC LIMIT 0,$top");
if ($GLOBALS['db']->sql_numrows($result14) > 0) {
	echo "<table border=\"0\" cellpadding=\"10\" width=\"100%\"><tr><td align=\"left\">\n"
	."<font class=\"option\"><b>$top "._MOSTREADPAGES."</b></font><br /><br /><font class=\"content\">\n";
	$lugar=1;
	while ($row14 = $GLOBALS['db']->sql_fetchrow($result14)) {
		$pid     = $GLOBALS['db']->XMFnum($row14['pid']);
		$title   = stripslashes(check_html($row14['title'], "nohtml"));
		$counter = $GLOBALS['db']->XMFnum($row14['counter']);
		if($counter>0) {
			echo "<strong><big>&middot;</big></strong>&nbsp;$lugar: <a href=\"modules.php?name=Content&amp;pa=showpage&amp;pid=$pid\">$title</a> ($counter "._READS.")<br />\n";
			$lugar++;
		}
	}
	echo "</font></td></tr></table>\n\n";
}

Max_CloseTable();
incof( 'Top', 'html',  "".CACHE_VTOP."", '1' );