<?php


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

//Get Variables
$getsearchfor = htmlentities(strip_tags($_REQUEST[searchfor]));
$searchfor = escape($getsearchfor);
$getsortby = htmlentities(strip_tags($_REQUEST[sortby]));
$sortby = escape($getsortby);
$getpage = htmlentities(strip_tags($_REQUEST[page]));
$page = escape($getpage);
$getsearchterm = htmlentities(strip_tags($_REQUEST[searchterm]));
$searchterm = escape($getsearchterm);

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 ($getsearchfor == "videos" || $getsearchfor == "members" || $getsearchfor == "series" || $getsearchfor == "channels")
{
	STemplate::assign('getsearchfor',$getsearchfor);
	
	if ($getsearchfor =="videos")
	{
	$queryselected = "SELECT count(*) as total from videos WHERE public='1' AND active='1' AND (title like '%".mysql_real_escape_string($searchterm)."%' OR description like '%".mysql_real_escape_string($searchterm)."%' OR tags like '%".mysql_real_escape_string($searchterm)."%') order by rating $sort limit $config[maximum_results]";
	$query2 = "SELECT * from videos WHERE public='1' AND active='1' AND (title like '%".mysql_real_escape_string($searchterm)."%' OR description like '%".mysql_real_escape_string($searchterm)."%' OR tags like '%".mysql_real_escape_string($searchterm)."%') order by rating $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[660]";
	$mainmenu = "1";
	}
	elseif ($getsearchfor =="members")
	{
	$queryselected = "SELECT count(*) as total from members WHERE status='1' AND username like '%".mysql_real_escape_string($searchterm)."%' order by lastlogin $sort limit $config[maximum_results]";
	$query2 = "SELECT * from members WHERE status='1' AND username like '%".mysql_real_escape_string($searchterm)."%' order by lastlogin $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[661]";
	$mainmenu = "3";
	}
	elseif ($getsearchfor =="series")
	{
	$queryselected = "SELECT count(*) as total from videos_series WHERE name like '%".mysql_real_escape_string($searchterm)."%' order by rating $sort limit $config[maximum_results]";
	$query2 = "SELECT * from videos_series WHERE name like '%".mysql_real_escape_string($searchterm)."%' order by rating $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[662]";
	$mainmenu = "2";
	}
	elseif ($getsearchfor =="channels")
	{
	$queryselected = "SELECT count(*) as total from channels WHERE name like '%".mysql_real_escape_string($searchterm)."%' order by rating $sort limit $config[maximum_results]";
	$query2 = "SELECT * from channels WHERE name like '%".mysql_real_escape_string($searchterm)."%' order by rating $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = "$lang[663]";
	$mainmenu = "4";
	}
	
	$queryvcount = "SELECT count(*) as total from videos WHERE public='1' AND active='1' AND (title like '%".mysql_real_escape_string($searchterm)."%' OR description like '%".mysql_real_escape_string($searchterm)."%' OR tags like '%".mysql_real_escape_string($searchterm)."%') order by rating $sort limit $config[maximum_results]";
	$executevcount = $conn->Execute($queryvcount);
	$totalvcount = $executevcount->fields['total'];
	STemplate::assign('totalvcount',$totalvcount);
	
	$querymcount = "SELECT count(*) as total from members WHERE status='1' AND username like '%".mysql_real_escape_string($searchterm)."%' order by lastlogin $sort limit $config[maximum_results]";
	$executemcount = $conn->Execute($querymcount);
	$totalmcount = $executemcount->fields['total'];
	STemplate::assign('totalmcount',$totalmcount);
	
	$queryscount = "SELECT count(*) as total from videos_series WHERE name like '%".mysql_real_escape_string($searchterm)."%' order by rating $sort limit $config[maximum_results]";
	$executescount = $conn->Execute($queryscount);
	$totalscount = $executescount->fields['total'];
	STemplate::assign('totalscount',$totalscount);
	
	$queryccount = "SELECT count(*) as total from channels WHERE name like '%".mysql_real_escape_string($searchterm)."%' order by rating $sort limit $config[maximum_results]";
	$executeccount = $conn->Execute($queryccount);
	$totalccount = $executeccount->fields['total'];
	STemplate::assign('totalccount',$totalccount);
}

STemplate::assign('showtitle',$showtitle);

if ($searchterm != "")
{
	STemplate::assign('pagetitle',$searchterm." $lang[664] - ".$showtitle);
}
else
{
	STemplate::assign('pagetitle',$showtitle);
}

STemplate::assign('mainmenu',$mainmenu);

$executequeryselected = $conn->Execute($queryselected);

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

if($executequeryselected->fields['total']<=$config[maximum_results])
{
	$total = $executequeryselected->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/search/$searchfor/$sortby/1/$searchterm' title='first page'>$lang[611]</a></li>";
	$pagelinks.="<li class='page_separator'>...</li>";
	$pagelinks.="<li><a href='$thebaseurl/search/$searchfor/$sortby/$theprevpage/$searchterm'>&laquo; $lang[612]</a></li>";
	};
	
	$counter=0;
	
	$lowercount = $currentpage-5;
	if ($lowercount <= 0) $lowercount = 1;
	
	while ($lowercount < $currentpage)
	{
		$pagelinks.="<li><a href='$thebaseurl/search/$searchfor/$sortby/$lowercount/$searchterm'>$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/search/$searchfor/$sortby/$uppercounter/$searchterm'>$uppercounter</a></li>";
		$uppercounter++;
	}
	
	if($currentpage < $toppage) 
	{
	
	$pagelinks.="<li><a href='$thebaseurl/search/$searchfor/$sortby/$thenextpage/$searchterm'>$lang[613] &raquo;</a></li>";
	$pagelinks.="<li class='page_separator'>...</li>";
	$pagelinks.="<li><a href='$thebaseurl/search/$searchfor/$sortby/$toppage/$searchterm' title='last page'>$lang[614]</a></li>";
	};
	
	$pagelinks.="</ul>";

}
}
else
{
	$error = "$lang[665]";
	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('search.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>