<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.7 ( www.phpsugar.com )
// +------------------------------------------------------------------------+
// | PHP Melody IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.phpsugar.com or if you have received
// | this software from someone who is not a representative of
// | phpSugar, you are involved in an illegal activity.
// | ---
// | In such case, please contact: support@phpsugar.com.
// +------------------------------------------------------------------------+
// | Developed by: phpSugar (www.phpsugar.com) / support@phpsugar.com
// | Copyright: (c) 2004-2011 PhpSugar.com. All rights reserved.
// +------------------------------------------------------------------------+
session_start();
require('config.php');
require_once('include/functions.php');
require_once('include/user_functions.php');
require_once('include/islogged.php');

//	Reset chart?
$now = time();
$span = ($config['chart_days'] * (3600 * 24))/4;
if($span > 0)
{
	if($config['chart_last_reset'] < ($now - $span))
	{
		reset_chart();
	}
}

$cats = trim($_GET['c']);
if ( ! preg_match('/(^[a-zA-Z0-9_-]+)$/i', $cats))
{
	$cats = '';
}
$act = trim($_GET['do']);

$categories_list = categories_dropdown(array('options_only' => true, 'select_all_option' => false, 'value_attr_db_col' => 'tag', 'selected' => $cats));
$categories_list = preg_replace('/value="(.*?)"/', 'value="'. _URL .'/topvideos.'. _FEXT .'?c=$1"', $categories_list);

if($cats != '') {
	$catid = get_catid($cats);
	$cat_name = get_catname($cats);
	if( $config['views_from'] == 1 )
		$sql = "select uniq_id, artist, video_title, yt_views, site_views, added FROM pm_videos WHERE category LIKE '%$catid%' ORDER BY yt_views DESC LIMIT 50" ; 
	else
	{
		$sql = "SELECT uniq_id, artist, video_title, yt_views, site_views, added 
			 FROM pm_videos 
			 WHERE category LIKE '$catid' 
			 OR category LIKE '$catid,%' 
			 OR category LIKE '%,$catid' 
			 OR category LIKE '%,$catid,%' 
			 ORDER BY site_views DESC LIMIT 50" ; 
	}
} 
elseif($act != '' && $act == 'recent') {
	$videos = get_chart(50, 'advanced');
}
else {
	if( $config['views_from'] == 1 )
		$sql = "select uniq_id, artist, video_title, yt_views, site_views, added FROM pm_videos ORDER BY yt_views DESC LIMIT 50" ; 
	else
		$sql = "select uniq_id, artist, video_title, yt_views, site_views, added FROM pm_videos ORDER BY site_views DESC LIMIT 50" ; 

}
if($act == '')
{
$result = mysql_query($sql); 
			$item = '';
			$j = 1;
			$alt = 2;
			while ($row = mysql_fetch_array($result)) 
			{ 
			$col = ($alt % 2) ? 'row1' : 'row2';
			$alt++;
			$item .= "
			  <tr>
				<td align=\"center\" class=\"$col\">".$j.".</td>
				<td align=\"center\" class=\"$col\" width=\"10\"><a href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\"><img src=\"".show_thumb($row['uniq_id'])."\" alt=\"".$row['video_title']."\" class=\"tinythumb\" width=\"72\" height=\"53\" align=\"left\" border=\"1\" /></a></td>
				<td class=\"$col\">".$row['artist']."</td>
<td class=\"$col\"><a href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\">".$row['video_title']."</a></td>
				<td class=\"$col\">";
			$item .= ( $config['views_from'] == 1 ) ? pm_number_format($row['yt_views']) : pm_number_format($row['site_views']);
			$item .= "</td>
			  </tr>";
			$j++;
			}
if(mysql_num_rows($result) == 0) {
$item .= '
	<tr>
	<td colspan = "5" align="center">
	'.$lang['top_videos_msg1'].'
	</td>
	</tr>
';
}
}
elseif($act == 'recent') {
	$item = '';
	$alt = 2;
	$j = 1;
	foreach($videos as $uniq_id => $video)
	{
		$col = ($alt % 2) ? 'row1' : 'row2';
		$alt++;
		$item .= "
			<tr>
				<td align=\"center\" class=\"$col\">".$j.".</td>
				<td align=\"center\" class=\"$col\" width=\"10\"><a href=\"".makevideolink($video['uniq_id'], $video['artist'], $video['video_title'])."\"><img src=\"".show_thumb($video['uniq_id'])."\" alt=\"".$video['video_title']."\" class=\"tinythumb\" width=\"72\" height=\"53\" align=\"left\" border=\"1\" /></a></td>
				<td class=\"$col\">".$video['artist']."</td>
<td class=\"$col\"><a href=\"".makevideolink($video['uniq_id'], $video['artist'], $video['video_title'])."\">".$video['video_title']."</a></td>
				<td class=\"$col\">";
			$item .= pm_number_format($video['views']);
			$item .= "</td>
			  </tr>";
			$j++;
	}
	if(count($videos) == 0)
	{
		$item .= '
		<tr>
		<td colspan = "5" align="center">
		'.$lang['top_videos_msg2'].'
		</td>
		</tr>
		';
	}

}

$count_last_days = count_days($config['chart_last_reset'], time());

if($config['chart_days'] == 0) 
{
	if($count_last_days >= 0 && $count_last_days <= 1)
	{
		$smarty->assign('chart_days', $lang['yesterdays_top']);
	}
	else
	{
		$smarty->assign('chart_days', sprintf($lang['top_videos_last_x_days'], $count_last_days));
	}
} 
else 
{
	if(($count_last_days >= 0 && $count_last_days <= 1) && $config['chart_days'] <= 1)
	{
		$smarty->assign('chart_days', $lang['yesterdays_top']);
	}
	else
	{
		$smarty->assign('chart_days', sprintf($lang['top_videos_last_x_days'], $config['chart_days']));
	}
}
// define meta tags & common variables
$meta_title = sprintf($lang['top_m_videos_from'], _SITENAME);
$meta_description = '';
// end
$smarty->assign('cat_name', $cat_name);
$smarty->assign('results', $item);
$smarty->assign('categories_list', $categories_list);
// --- DEFAULT SYSTEM FILES - DO NOT REMOVE --- //
$smarty->assign('meta_title', $meta_title);
$smarty->assign('meta_description', $meta_description);
$smarty->assign('template_dir', $template_f);
$smarty->display('top_videos.tpl');
?>