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

$cat_name = secure_sql($_GET['cat']);
$page = $_GET['page'];
$sortby = secure_sql($_GET['sortby']);
$order = secure_sql($_GET['order']);

if($sortby == 'views') {
	$sql_sortby = 'site_views';
	$order = 'DESC';
}
elseif($sortby == 'date') {
	$sql_sortby = 'added';
	$order = 'DESC';
}
elseif($sortby == 'rating') {
	$sql_sortby = 'total_value';
	$order = 'DESC';
}
elseif($sortby == 'artist') {
	$sql_sortby = 'artist';
	$order = 'ASC';
}

if(!empty($cat_name)) { 
	$cats = get_catid($cat_name);
	// CHECK IF CATEGORY EXISTS IN DB. OTHERWISE REDIRECT.
	if(empty($cats)){
		header("Location: index."._FEXT);
		exit;
	}

$limit = _BROWSER_PAGE;
$category_name = get_catname($cat_name);
$list_subcats = list_subcategories($cats, '');

} 
else { 
echo $lang['browse_msg1']; 
exit();
}
// Default Page 
if (empty($page) || !is_numeric($page) || $page < 0 ) { $page = 1 ; }
if(is_numeric($cats)){
	
	$resync = 0;
	$category = array();
	$total_items = 0;
	$sql = "SELECT * FROM pm_categories WHERE id = '". $cats ."'"; 
	$result = @mysql_query($sql);
	if ($result)
	{
		$category = mysql_fetch_assoc($result);
		mysql_free_result($result);
	}
	
	$total_items = $category['total_videos'];
	
	if ($total_items < 10)
	{
		$resync = 1;
	}
	
	$total_pages = ceil($total_items / $limit);
	$set_limit = $page * $limit - ($limit);
	
	if($total_items == 0) {
			$problem = $lang['browse_msg2'];
	}
	else
	{
// OUTPUT DATA 
			$sql = "SELECT * FROM pm_videos WHERE category LIKE '%,$cats,%' or category like '%,$cats' or category like '$cats,%' or category='$cats' ORDER BY added DESC LIMIT $set_limit, $limit" ; 
			
			if(isset($sortby) && isset($order)) {
				$sql_sortby	= ($sql_sortby == '') ? "added" : $sql_sortby;
				$order		= ($order == '') ? "DESC" : $order;
				
				$sql = "SELECT * FROM pm_videos WHERE category LIKE '%,$cats,%' or category like '%,$cats' or category like '$cats,%' or category='$cats' ORDER BY ".$sql_sortby." $order LIMIT $set_limit, $limit" ; 
				}
			
			if($sortby == 'rating')
			{
				$sql = "SELECT * FROM pm_videos LEFT JOIN pm_ratings ON (pm_videos.uniq_id = pm_ratings.id) WHERE category LIKE '%,$cats,%' or category like '%,$cats' or category like '$cats,%' or category='$cats' ORDER BY ".$sql_sortby." DESC LIMIT $set_limit, $limit" ;
			}
			
			$result = mysql_query($sql);
			$j = 0; 
			$item = '';
			while ($row = mysql_fetch_array($result)) 
			{ 
			// ** Mark as new ** //
			$sql_date = date('Y-m-d', $row['added']);
			$date_diff = round( abs(strtotime(date('Y-m-d'))-strtotime($sql_date)) / 86400, 0 );
			
			$item .= "<li class=\"item column\"><a 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>";
			$item .= "<div class=\"top-list-info-box\"><span>".fewchars($row['artist'], 15)."</span>";
			$item .= "<span><a href=\"".makevideolink($row['uniq_id'], $row['artist'], $row['video_title'])."\">".fewchars($row['video_title'], 15)."</a></span><span class=\"view-count\">". pm_number_format($row['site_views']) ."  views </span></div>";
			
			$item .= "<div class=\"tag\"></div></li>";
			} 
			$row_count = mysql_num_rows($result);
			mysql_free_result($result);
			
			if ($page == $total_pages && $total_items > 0 && $row_count == 0)
			{
				$resync = 1;
			}
			else if ($row_count == 0 && $page > 1 && $total_items > 0)
			{
				$resync = 1;
			}
			else if (($row_count + $set_limit > $total_items) && $total_items > 0)
			{
				$resync = 1;
			}
	}
// END OUTPUT DATA
	
	if ($resync)
	{
		$sql_t = "SELECT uniq_id FROM pm_videos WHERE category LIKE '%,$cats,%' or category like '%,$cats' or category like '$cats,%' or category='$cats'"; 
		$result_t = @mysql_query($sql_t);
		$total_items = @mysql_num_rows($result_t);
		@mysql_free_result($result_t);
		
		@mysql_query("UPDATE pm_categories SET total_videos = '". $total_items ."' WHERE id = '". $cats ."'");
	}
				
// SHOW LONG PAGINATION 
	$page_count_info = sprintf($lang['page'], $page)." ".$total_pages;
	if($total_pages == 0)
	{
		$page_count_info = $lang['browse_page11'];
	}
	$pag = '';
	$pag_left = '';
		if ($page > 1) // Prev Page 
		{ 
		$prev_page = $page-1 ; 
			if($prev_page == '1') {
				if(_SEOMOD == 1)
				$prev_link = "browse-".$cat_name."-videos-".$prev_page."-".$sortby.".html";
				else
				$prev_link = "category.php?cat=".$cat_name."&page=".$prev_page."&sortby=".$sortby."";
				$pag_left .= "<a href=\"".$prev_link."\" class=\"left\">&laquo; Previous ".$limit." videos"; 
			 }
			else {
	if(_SEOMOD == 1)
	$prev_link = "browse-".$cat_name."-videos-".$prev_page."-".$sortby.".html";
	else
	$prev_link = "category.php?cat=".$cat_name."&page=".$prev_page."&sortby=".$sortby."";
	$pag_left .= "<a href=\"".$prev_link."\" class=\"left\">&laquo; Previous ".$limit." videos"; 
			}
		} 
	   // Page Numbers 
	   if ($page < $total_pages ) 
	   { 
	   $next_page = $page+1 ; 
	   	if(_SEOMOD == 1)
	$next_link = "browse-".$cat_name."-videos-".$next_page."-".$sortby.".html";
	else
	$next_link = "category.php?cat=".$cat_name."&page=".$next_page."&sortby=".$sortby."";
	$pag_right = "<a href=\"".$next_link."\">Next ".$limit." videos &raquo;</a>" ; 

	   }  
	$pag .= "</div>";
/// END SHOW LONG PAGINATION
}
else { 
	header("Location: index."._FEXT);
}

// generate smart pagination
$uri = '';
if(!strstr($_SERVER['REQUEST_URI'], "?"))
{
	$uri = ('' != $_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : 'category.php';
	$uri .= "?";
	
	if($cat_name != '')	$uri .= 'cat='		. $cat_name;
	if($page != '')		$uri .= '&page='	. $page;
	if($sortby != '')	$uri .= '&sortby='	. $sortby;
	if($order != '')	$uri .= '&order='	. $order;
}
else
{
	$uri = $_SERVER['REQUEST_URI'];
}

$uri = explode('?', $uri); 

$filename = ('' != $_SERVER['PHP_SELF']) ? basename($_SERVER['PHP_SELF']) : 'category.php';

if(!is_numeric($_GET['page']))
{
	$uri[1] = str_replace("page=".$_GET['page'], "page=1", $uri[1]);
}

if(_SEOMOD == 1)
{
	$uri[1] = '';
	$filename = $_SERVER['REQUEST_URI'];
	if($filename == '')
	{
		$filename = "browse-" . $cat_name . "-videos-" . $page . "-" . $sortby . ".html";
	}
	else
	{
		if(strpos($filename, "/", 0) !== FALSE)
		{
			$temp = explode("/", $filename);
			$filename = $temp[count($temp)-1];
		}	
	}
}
$pagination = generate_smart_pagination($page, $total_items, $limit, 1, $filename, $uri[1], _SEOMOD);
if($page != 1 && is_numeric($page))
	$meta_title = $category_name." ".$lang['music_videos']." - ". $page_count_info;
else
	$meta_title = $category_name." ".$lang['music_videos'];
$meta_description = $category_name." ".$lang['music_videos']." - "._SITENAME." - ". $page_count_info;

// MAKE SORT BY STICK
if(!empty($cats)) {
	$list_cats = list_categories(0, $cats);
	$smarty->assign('list_categories', $list_cats);
} else {
	$list_cats = list_categories(0, '');
	$smarty->assign('list_categories', $list_cats);
} 
if($config['show_tags'] == 1)
{
	$tag_cloud = tag_cloud(0, $config['tag_cloud_limit'], $config['shuffle_tags']);
	$smarty->assign('tags', $tag_cloud);
	$smarty->assign('show_tags', 1);
}
$smarty->assign('cat_id', $cats);
$smarty->assign('problem', $problem);
$smarty->assign('gv_category_name', $category_name);
$smarty->assign('gv_cat', $cat_name);
$smarty->assign('gv_pagenumber', $page);
$smarty->assign('gv_sortby', $sortby);

$smarty->assign('list_subcats', $list_subcats);
$smarty->assign('pagination', $pagination);

$smarty->assign('page_count_info', $page_count_info);
$smarty->assign('pag_left', $pag_left);
$smarty->assign('pag_right', $pag_right);

$smarty->assign('results', $item);
// --- 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('browse.tpl');
?>