<?php


session_start();
include("include/config.php");
include("include/functions/import.php");
$thebaseurl = $config['baseurl'];

$mainmenu = "4";
STemplate::assign('mainmenu',$mainmenu);


//Get Variables
$getsortby = htmlentities(strip_tags($_REQUEST[sortby]));
$sortby = escape($getsortby);
$getfilter = htmlentities(strip_tags($_REQUEST[filter]));
$filter = escape($getfilter);
$getpage = htmlentities(strip_tags($_REQUEST[page]));
$page = escape($getpage);

if ($sortby=="a")
{
	$sort = "asc";
}
else
{
	$sortby = "z";
	$sort = "desc";
}

if($page=="")
{
	$page = "1";
}
$currentpage = $page;

if ($page >=2)
{
	$pagingstart = ($page-1)*$config[items_per_page];
}
else
{
	$pagingstart = "0";
}

if ($filter =="0")
{
	$query1 = "SELECT count(*) as total from channels order by time_added $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels order by time_added $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[514]";
	$showseotitle = "$lang[175]";
	$submenu = "0";
}
elseif ($filter =="1")
{
	$query1 = "SELECT count(*) as total from channels order by subscriptions $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels order by subscriptions $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[515]";
	$showseotitle = "$lang[183]";
	$submenu = "1";
}
elseif ($filter =="2")
{
	$query1 = "SELECT count(*) as total from channels order by viewcount $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels order by viewcount $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[516]";
	$showseotitle = "$lang[188]";
	$submenu = "2";
}
elseif ($filter =="3")
{
	$query1 = "SELECT count(*) as total from channels order by rating $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels order by rating $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[517]";
	$showseotitle = "$lang[179]";
	$submenu = "3";
}
elseif ($filter =="4")
{
	$query1 = "SELECT count(*) as total from channels order by commentcount $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels order by commentcount $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[518]";
	$showseotitle = "$lang[187]";
	$submenu = "4";
}

elseif ($filter =="5")
{
	$query1 = "SELECT count(*) as total from channels WHERE featured='1' order by time_added $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels WHERE featured='1' order by time_added $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[221]";
	$showseotitle = "$lang[62]";
	$submenu = "5";
}

else
{
	$query1 = "SELECT count(*) as total from channels order by time_added $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels order by time_added $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[514]";
	$showseotitle = "$lang[175]";
	$submenu = "0";
}
STemplate::assign('showtitle',$showtitle);
STemplate::assign('pagetitle',$showtitle);
STemplate::assign('showseotitle',$showseotitle);
STemplate::assign('filter',$filter);
STemplate::assign('submenu',$submenu);

$executequery1 = $conn->Execute($query1);

$totalvideos = $executequery1->fields['total'];
if ($totalvideos > 0)
{

if($executequery1->fields['total']<=$config[maximum_results])
{
	$total = $executequery1->fields['total'];
}
else
{
	$total = $config[maximum_results];
}

$toppage = ceil($total/$config[items_per_page]);
if($toppage==0)
{
	$xpage=$toppage+1;
}
else
{
	$xpage = $toppage;
}


$executequery2 = $conn->Execute($query2);
$users = $executequery2->getrows();
$beginning=$pagingstart+1;
$ending=$pagingstart+$executequery2->recordcount();

$pagelinks="";
$k=1;
$theprevpage=$currentpage-1;
$thenextpage=$currentpage+1;

if ($currentpage > 0)
{
	$pagelinks.="<ul>";
	
	if($currentpage > 1) 
	{
	$pagelinks.="<li><a href='$thebaseurl/browse/channels/$sortby/$filter/1/$showseotitle' title='first page'>$lang[611]</a></li>";
	$pagelinks.="<li class='page_separator'>...</li>";
	$pagelinks.="<li><a href='$thebaseurl/browse/channels/$sortby/$filter/$theprevpage/$showseotitle'>&laquo; $lang[612]</a></li>";
	};
	
	$counter=0;
	
	$lowercount = $currentpage-5;
	if ($lowercount <= 0) $lowercount = 1;
	
	while ($lowercount < $currentpage)
	{
		$pagelinks.="<li><a href='$thebaseurl/browse/channels/$sortby/$filter/$lowercount/$showseotitle'>$lowercount</a></li>";
		$lowercount++;
		$counter++;
	}
	
	$pagelinks.="<li class='page_current'>$currentpage</li>";
	
	$uppercounter = $currentpage+1;
	
	while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
	{
		$pagelinks.="<li><a href='$thebaseurl/browse/channels/$sortby/$filter/$uppercounter/$showseotitle'>$uppercounter</a></li>";
		$uppercounter++;
	}
	
	if($currentpage < $toppage) 
	{
	
	$pagelinks.="<li><a href='$thebaseurl/browse/channels/$sortby/$filter/$thenextpage/$showseotitle'>$lang[613] &raquo;</a></li>";
	$pagelinks.="<li class='page_separator'>...</li>";
	$pagelinks.="<li><a href='$thebaseurl/browse/channels/$sortby/$filter/$toppage/$showseotitle' title='last page'>$lang[614]</a></li>";
	};
	
	$pagelinks.="</ul>";

}
}
else
{
	$error = "$lang[519]";
	STemplate::assign('error',$error);
}

//TEMPLATES BEGIN
STemplate::assign('beginning',$beginning);
STemplate::assign('ending',$ending);
STemplate::assign('pagelinks',$pagelinks);
STemplate::assign('total',$total);
STemplate::assign('listvideos',$users);
STemplate::display('header.tpl');
STemplate::display('channels.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>