<?php
// +------------------------------------------------------------------------+
// | PHP Melody version 1.6.6 ( 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);
if($span > 0)
{
	if($config['chart_last_reset'] < ($now - $span))
	{
		reset_chart();
	}
}

//	generate categories list;

$sql = "SELECT * FROM pm_categories WHERE parent_id=0 ORDER BY position ASC";
$result = mysql_query($sql);
while($row = mysql_fetch_assoc($result))
{
	$categories_list[$row['tag']] = $row['name'];
	
	$subcats = mysql_query("SELECT * FROM pm_categories WHERE parent_id=".$row['id']." AND parent_id !=0 ORDER BY position ASC");
	while($r = mysql_fetch_array($subcats)) 
	{
		$categories_list[$r['tag']] = "&raquo; ".$r['name'];
	}
}

// Filtering by category
//$cats = magicSlashes($_GET['c']);
$cats = trim($_GET['c']);
$act = trim($_GET['do']);
$cats = str_replace(array("%", "<", ">", " "), '', $cats);
$cats = secure_sql($cats);
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 24" ; 
	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 24" ; 
	}
} 
elseif($act != '' && $act == 'recent') {
	$videos = get_chart(24, '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 24" ; 
	else
		$sql = "select uniq_id, artist, video_title, yt_views, site_views, added FROM pm_videos ORDER BY site_views DESC LIMIT 24" ; 

}
if($act == '')
{
$result = mysql_query($sql); 
			$item = '';
			$j = 1;
			$alt = 2;
			while ($row = mysql_fetch_array($result)) 
			{ 
			$col = ($alt % 2) ? 'row1' : 'row2';
			$alt++; 
			$item .= "<li  class=\"$col column\"><span class=\"no\" >".$j."</span><span class=\"view-count\" >";
			$item .= ( $config['views_from'] == 1 ) ? pm_number_format($row['yt_views']) : pm_number_format($row['site_views']);
			$item .= "</span><a title=\"". fewchars($row['video_title'], 20) ."\" class=\"row-img\" href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\"><img src=\"".show_thumb($row['uniq_id'])."\" alt=\"".$row['video_title']."\" /></a><div class=\"top-list-info-box\"><span>". fewchars($row['artist'], 20) ."</span><span><a href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\">". fewchars($row['video_title'], 20) ." .</a></span>";
			$item .= "</div></li>";
			$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 .= "<li  class=\"$col column\"><span class=\"no\" >".$j."</span><span class=\"view-count\" >";
			$item .= ( $config['views_from'] == 1 ) ? pm_number_format($row['yt_views']) : pm_number_format($row['site_views']);
			$item .= "</span><a title=\"". fewchars($row['video_title'], 20) ."\" class=\"row-img\" href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\"><img src=\"".show_thumb($row['uniq_id'])."\" alt=\"".$row['video_title']."\" /></a><div class=\"top-list-info-box\"><span>". fewchars($row['artist'], 20) ."</span><span><a href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\">". fewchars($row['video_title'], 20) ."</a></span>";
			$item .= "</div></li>";	
				$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');
?>