<?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');

$page = (int) $_GET['page'];
if ( ! $page)
{
	$page = 1;
}
$limit	= 50;
$from 	= $page * $limit - ($limit);
$date 	= $_GET['d'];
$total_videos = 0;

//	count total videos
switch ($date)
{
	default: 
		
		$total_videos = $config['total_videos'];
		
	break;
	
	case 'today':
	case 'yesterday':
	case 'month':
		
		switch ($date)
		{
			case 'today':
				$time = getdate();
				$time_start = mktime(0, 0, 0, $time['mon'], $time['mday'], $time['year']);
				$time_end	= mktime(23, 59, 59, $time['mon'], $time['mday'], $time['year']);
			break;
			
			case 'yesterday':
				$time = getdate();
				$time_start = mktime(0, 0, 0, $time['mon'], $time['mday'] - 1, $time['year']);
				$time_end	= mktime(23, 59, 59, $time['mon'], $time['mday'] - 1, $time['year']);
			break;
			
			case 'month':
				$time = getdate();	
				$days_this_month = (int) date('t', mktime(0,0,0, $time['mon'], 1, $time['year']));
				
				$time_start = mktime(0, 0, 0, $time['mon'], 1, $time['year']);
				$time_end	= mktime(23, 59, 59, $time['mon'], $days_this_month, $time['year']);
			break;
		}
		
		$sql = "SELECT COUNT(*) as total_found 
				FROM pm_videos 
				WHERE added >= '". $time_start ."' 
				  AND added <= '". $time_end ."'";
		$result = @mysql_query($sql);
		
		if ( ! $result)
		{
			$total_videos = 0;
		}
		else
		{
			$row = mysql_fetch_assoc($result);
			mysql_free_result($result);
			
			$total_videos = (int) $row['total_found'];
			unset($row);
		}
		
	break;
}

if ($total_videos > 0)
{
	switch ($date)
	{
		default: 
				
			$sql = "SELECT uniq_id, artist, video_title, yt_views, site_views, added 
					FROM pm_videos 
					ORDER BY added DESC 
					LIMIT ". $from .",". $limit;
					
			
		break;
		
		case 'today':
		case 'yesterday':
		case 'month':
			
			$sql = "SELECT uniq_id, artist, video_title, yt_views, site_views, added 
					FROM pm_videos 
					WHERE added >= '". $time_start ."' 
				  	  AND added <= '". $time_end ."' 
					ORDER BY added DESC 
					LIMIT ". $from .",". $limit;
		break;
	}
	
	$result = mysql_query($sql);
	
	//	html output
	$item 	= '';
	$j 		= 1;
	$alt 	= 0;
	
	while ($row = mysql_fetch_assoc($result))
	{
		$href = makevideolink($row['uniq_id'], $row['artist'], $row['video_title']);
		
		$col = ($alt % 2) ? 'row1' : 'row2';

		$item .= '<tr>';
		$item .= '<td align="center" class="'. $col .'" width="10">';
		$item .= '<a href="'. $href .'">';
		$item .= '<img src="'. show_thumb($row['uniq_id']) .'" alt="'. $row['video_title'] .'" ';
		$item .= ' class="tinythumb" width="53" height="40" align="left" border="1" />';
		$item .= '</a>';
		$item .= '</td>';
		$item .= '<td class="'. $col .'" width="250">';
		$item .= $row['artist'];
		$imte .= '</td>';
		$item .= '<td class="'. $col .'">';
		$item .= '<a href="'. $href .'">';
		$item .= $row['video_title'];
		$item .= '</a>';
		$item .= '</td>';
		$item .= '<td class="'. $col .'">';
		$item .= time_since($row['added']) .' '. $lang['ago'];
		$item .= '</td>';
		$item .= '</tr>';
		
		$alt++;
		$j++;
	}
}
else
{
	$empty_results = '1';
}

//	generate smart pagination
$pagination = '';

if ($total_videos > $limit)
{
	$filename = 'newvideos.php';
	
	if (_SEOMOD)
	{
		$filename = 'newvideos.html';
	}

	$extra = '';
	if ($date != '')
	{
		$extra = '&d='. $date;
	}
	
	$pagination = generate_smart_pagination($page, $total_videos, $limit, 1, $filename, $extra);
}

// define meta tags & common variables
$meta_title = sprintf($lang['nv_page_title'], _SITENAME);
$meta_description = '';
// end
$smarty->assign('empty_results', $empty_results);
$smarty->assign('cat_name', $cat_name);
$smarty->assign('results', $item);
$smarty->assign('categories_list', $categories_list);
$smarty->assign('pagination', $pagination); 

// --- 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('new_videos.tpl');
?>