<?php
/**************************************************************************************************
|
| iDocScript - Document Sharing Script
| http://www.idocscript.com
| webmaster@idocscript.com
|
|**************************************************************************************************
|
| By using this software you agree that you have read and acknowledged our End-User License 
| Agreement available at http://www.idocscript.com/eula.html and to be bound by it.
|
| Copyright (c) 2011 iDocScript.com. All rights reserved.
|**************************************************************************************************/

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

if ($_SESSION['USERID'] != "" && $_SESSION['USERID'] >= 0 && is_numeric($_SESSION['USERID']))
{
	if($_REQUEST['submitdelete']!="")
	{
		$deletedid = $_POST['deletedid'];

		if ($deletedid != "" && $deletedid >= 0 && is_numeric($deletedid))
		{
			$query = "DELETE FROM docs_favorited WHERE DID='".mysql_real_escape_string($deletedid)."' AND USERID='".mysql_real_escape_string($_SESSION['USERID'])."'";
			$conn->Execute($query);
			$message = $lang['608'];
		}
	}
	
	
	$sortby = cleanit($_REQUEST['sortby']);
	$page = cleanit($_REQUEST['page']);

	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";
	}

	$query1 = "SELECT count(*) as total from docs A, docs_favorited B where B.USERID='".mysql_real_escape_string($_SESSION['USERID'])."' AND A.DID=B.DID AND A.public='1' AND A.active='1' order by A.time_added $sort limit $config[maximum_results]";
	$query2 = "SELECT * from docs A, docs_favorited B where B.USERID='".mysql_real_escape_string($_SESSION['USERID'])."' AND A.DID=B.DID AND A.public='1' AND A.active='1' order by A.time_added $sort limit $pagingstart, $config[items_per_page]";
	$showtitle = $lang['481'];
	STemplate::assign('pagetitle',$showtitle);
	STemplate::assign('showtitle',$showtitle);

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

	$totaldocs = $executequery1->fields['total'];
	if ($totaldocs > 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);
		$docs = $executequery2->getrows();
		$beginning=$pagingstart+1;
		$ending=$pagingstart+$executequery2->recordcount();
	
		$pagelinks="";
		$k=1;
		$theprevpage=$currentpage-1;
		$thenextpage=$currentpage+1;
	
		$added = "page=";
		if ($currentpage > 0)
		{
		
			if($currentpage > 1) 
			{
			$pagelinks.="<a href='$thebaseurl/myfav?".$added."1' class='page'>$lang[611]</a>";
			$pagelinks.="<a href='$thebaseurl/myfav?".$added."$theprevpage' class='page'>$lang[612]</a>";
			};
			
			$counter=0;
			
			$lowercount = $currentpage-5;
			if ($lowercount <= 0) $lowercount = 1;
			
			while ($lowercount < $currentpage)
			{
				$pagelinks.="<a href='$thebaseurl/myfav?".$added."$lowercount' class='page'>$lowercount</a>";
				$lowercount++;
				$counter++;
			}
			
			$pagelinks.="<span class='current'>$currentpage</span>";
			
			$uppercounter = $currentpage+1;
			
			while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
			{
				$pagelinks.="<a href='$thebaseurl/myfav?".$added."$uppercounter' class='page'>$uppercounter</a>";
				$uppercounter++;
			}
			
			if($currentpage < $toppage) 
			{
			
			$pagelinks.="<a href='$thebaseurl/myfav?".$added."$thenextpage' class='page'>$lang[613]</a>";
			$pagelinks.="<a href='$thebaseurl/myfav?".$added."$toppage' title='last page' class='page'>$lang[614]</a>";
			}
		}
	}
	else
	{
		$error = $lang['615'];
		STemplate::assign('error',$error);
	}
}
else
{
	$redirect = base64_encode($thebaseurl."/myfav");
	header("Location:$config[baseurl]/login.php?redirect=$redirect");exit;
}

//TEMPLATES BEGIN
STemplate::assign('error',$error);
STemplate::assign('message',$message);
STemplate::assign('beginning',$beginning);
STemplate::assign('ending',$ending);
STemplate::assign('pagelinks',$pagelinks);
STemplate::assign('total',$total);
STemplate::assign('docs',$docs);
STemplate::display('header.tpl');
STemplate::display('myfavoritedocs.tpl');
STemplate::display('footer.tpl');
//TEMPLATES END
?>