<?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_once("../include/functions/import.php");
verify_login_admin();
$adminurl = $config['adminurl'];

// DELETE 
if($_REQUEST['delete']=="1")
{
	$DDID = intval($_REQUEST['DID']);
	if($DDID > 0)
	{
		$deletedoc = $DDID;
		$docpath = $config['docdir']."/".$deletedoc.".pdf";
		@chmod($docpath, 0777);
			
		if (file_exists($docpath))
		{
			@unlink($docpath);
		}		

		$deletefrom[] = "docs";
		$deletefrom[] = "docs_comments";
		$deletefrom[] = "docs_favorited";
		$deletefrom[] = "docs_reports";

		for($j=0;$j < count($deletefrom);$j++)
		{
			$query = "DELETE FROM ".$deletefrom[$j]." WHERE DID='$deletedoc'";
			$conn->Execute($query);
		}

		$tempthumbs = $config[thumbdir]."/".$deletedoc.".jpg";
		if(file_exists($tempthumbs)) 
		{
			@unlink($tempthumbs);
		}
		
		$message = "Document Successfully Deleted.";
		Stemplate::assign('message',$message);
	}
}
// DELETE

//MATURE
if($_POST['msub']=="1")
{
	$MDID = $_POST['MDID'];
	$mval = $_POST['mval'];
	if($mval == "0")
	{
		$mval2 = "1";
	}
	else
	{
		$mval2 = "0";
	}
	$sql="UPDATE docs SET mature='".intval($mval2)."' WHERE DID='".mysql_real_escape_string($MDID)."'";
	$conn->Execute($sql);
}
//MATURE

//ACTIVE
if($_POST['asub']=="1")
{
	$ADID = $_POST['ADID'];
	$aval = $_POST['aval'];
	if($aval == "0")
	{
		$aval2 = "1";
	}
	else
	{
		$aval2 = "0";
	}
	$sql="UPDATE docs SET active='".intval($aval2)."' WHERE DID='".mysql_real_escape_string($ADID)."'";
	$conn->Execute($sql);
}
//ACTIVE

//DELETE REPORT
if($_POST['rsub']=="1")
{
	$RRID = $_POST['RRID'];
	$sql="DELETE FROM docs_reports WHERE RID='".mysql_real_escape_string($RRID)."'";
	$conn->Execute($sql);
}
//DELETE REPORT

if($_REQUEST['sortby']=="title")
{
	$sortby = "title";
	$sort =" order by A.title";
	$add1 = "&sortby=title";
}
elseif($_REQUEST['sortby']=="reason")
{
	$sortby = "reason";
	$sort =" order by reason";
	$add1 = "&sortby=reason";
}
elseif($_REQUEST['sortby']=="remarks")
{
	$sortby = "remarks";
	$sort =" order by remarks";
	$add1 = "&sortby=remarks";
}
elseif($_REQUEST['sortby']=="username")
{
	$sortby = "username";
	$sort =" order by B.username";
	$add1 = "&sortby=username";
}
elseif($_REQUEST['sortby']=="time")
{
	$sortby = "time";
	$sort =" order by time";
	$add1 = "&sortby=time";
}
elseif($_REQUEST['sortby']=="mature")
{
	$sortby = "mature";
	$sort =" order by A.mature";
	$add1 = "&sortby=mature";
}
elseif($_REQUEST['sortby']=="featured")
{
	$sortby = "featured";
	$sort =" order by A.featured";
	$add1 = "&sortby=featured";
}
else
{
	$sortby = "DID";
	$sort =" order by A.DID";
	$add1 = "&sortby=DID";
}

if($_REQUEST['sorthow']=="desc")
{
	$sorthow ="desc";
	$add1 .= "&sorthow=desc";
}
else
{
	$sorthow ="asc";
	$add1 .= "&sorthow=asc";
}

//Search
$fromid = intval($_REQUEST['fromid']);
$toid = intval($_REQUEST['toid']);
$title = cleanit($_REQUEST['title']);
$remarks = cleanit($_REQUEST['remarks']);
$username = cleanit($_REQUEST['username']);
$mature = cleanit($_REQUEST['mature']);
$active = cleanit($_REQUEST['active']);
if($_POST['submitform'] == "1" || ($_REQUEST['fromid']!="" || $toid>0 || $title!="" || $remarks!="" || $username!="" || $mature!="" || $active!=""))
{
	if($fromid > 0)
	{
		$addtosql = "AND A.DID>='".mysql_real_escape_string($fromid)."'";
		Stemplate::assign('fromid',$fromid);
	}
	else
	{
		$addtosql = "AND A.DID>'".mysql_real_escape_string($fromid)."'";
	}
	if($toid > 0)
	{
		$addtosql .= "AND A.DID<='".mysql_real_escape_string($toid)."'";
		Stemplate::assign('toid',$toid);
	}
	if($title != "")
	{
		$addtosql .= "AND A.title like'%".mysql_real_escape_string($title)."%'";
		Stemplate::assign('title',$title);
	}
	if($remarks != "")
	{
		$addtosql .= "AND remarks like'%".mysql_real_escape_string($remarks)."%'";
		Stemplate::assign('remarks',$remarks);
	}
	if($username != "")
	{
		$addtosql .= "AND B.username like'%".mysql_real_escape_string($username)."%'";
		Stemplate::assign('username',$username);
	}
	if($mature != "")
	{
		$addtosql .= "AND A.mature='1'";
		Stemplate::assign('mature',$mature);
	}
	if($active != "")
	{
		$addtosql .= "AND A.active='1'";
		Stemplate::assign('active',$active);
	}
	Stemplate::assign('search',"1");
}
//Search End

$page = intval($_REQUEST['page']);
if($page=="")
{
	$page = "1";
}
$currentpage = $page;

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

$queryselected = "select A.DID,B.username,C.RID from docs A, members B, docs_reports C WHERE C.USERID=B.USERID AND A.DID=C.DID AND A.active='1' $addtosql $sort $sorthow limit $config[maximum_results]";
$query2 = "select A.*,B.username,C.* from docs A, members B, docs_reports C WHERE C.USERID=B.USERID AND A.DID=C.DID AND A.active='1' $addtosql $sort $sorthow limit $pagingstart, $config[items_per_page]";
$executequeryselected = $conn->Execute($queryselected);
$totaldocs = $executequeryselected->rowcount();	
if ($totaldocs > 0)
{
	if($totaldocs<=$config[maximum_results])
	{
		$total = $totaldocs;
	}
	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);	
	$results = $executequery2->getrows();
	$beginning=$pagingstart+1;
	$ending=$pagingstart+$executequery2->recordcount();
	$pagelinks="";
	$k=1;
	$theprevpage=$currentpage-1;
	$thenextpage=$currentpage+1;
	if ($currentpage > 0)
	{	
		if($currentpage > 1) 
		{
			$pagelinks.="<a href='$adminurl/docs_reported.php?page=1$add1' title='first page'>First</a>&nbsp;";
			$pagelinks.="<a href='$adminurl/docs_reported.php?page=$theprevpage$add1'>Previous</a>&nbsp;";
		};
		$counter=0;
		$lowercount = $currentpage-5;
		if ($lowercount <= 0) $lowercount = 1;
		while ($lowercount < $currentpage)
		{
			$pagelinks.="<a href='$adminurl/docs_reported.php?page=$lowercount$add1'>$lowercount</a>&nbsp;";
			$lowercount++;
			$counter++;
		}
		$pagelinks.=$currentpage."&nbsp;";
		$uppercounter = $currentpage+1;
		while (($uppercounter < $currentpage+10-$counter) && ($uppercounter<=$toppage))
		{
			$pagelinks.="<a href='$adminurl/docs_reported.php?page=$uppercounter$add1'>$uppercounter</a>&nbsp;";
			$uppercounter++;
		}
		if($currentpage < $toppage) 
		{
			$pagelinks.="<a href='$adminurl/docs_reported.php?page=$thenextpage$add1'>Next</a>&nbsp;";
			$pagelinks.="<a href='$adminurl/docs_reported.php?page=$toppage$add1' title='last page'>Last</a>&nbsp;";
		};
	}
}
else
{
	$error = "There are no reported documents.";
}

$mainmenu = "4";
$submenu = "1";
Stemplate::assign('mainmenu',$mainmenu);
Stemplate::assign('submenu',$submenu);
Stemplate::assign('sorthow',$sorthow);
Stemplate::assign('sortby',$sortby);
Stemplate::assign('currentpage',$currentpage);
STemplate::display("administrator/global_header.tpl");
STemplate::assign('beginning',$beginning);
STemplate::assign('ending',$ending);
STemplate::assign('pagelinks',$pagelinks);
STemplate::assign('total',$total+0);
STemplate::assign('results',$results);
Stemplate::assign('error',$error);
STemplate::display("administrator/docs_reported.tpl");
STemplate::display("administrator/global_footer.tpl");
?>