isGuest()) {
	UI::exitError(getMLText("my_documents"),getMLText("access_denied"));
}
if (!$db->createTemporaryTable("ttstatid")) {
	UI::exitError(getMLText("approval_summary"),getMLText("internal_error_exit"));
}
UI::htmlStartPage(getMLText("approval_summary"));
UI::globalNavigation();
UI::pageNavigation(getMLText("my_documents"), "my_documents");
UI::contentHeading(getMLText("approval_summary"));
UI::contentContainerStart();
// TODO: verificare scadenza
// Get document list for the current user.
$approvalStatus = $user->getApprovalStatus();
// reverse order
$approvalStatus["indstatus"]=array_reverse($approvalStatus["indstatus"],true);
$approvalStatus["grpstatus"]=array_reverse($approvalStatus["grpstatus"],true);
// Create a comma separated list of all the documentIDs whose information is
// required.
$dList = array();
foreach ($approvalStatus["indstatus"] as $st) {
	if (!in_array($st["documentID"], $dList)) {
		$dList[] = $st["documentID"];
	}
}
foreach ($approvalStatus["grpstatus"] as $st) {
	if (!in_array($st["documentID"], $dList)) {
		$dList[] = $st["documentID"];
	}
}
$docCSV = "";
foreach ($dList as $d) {
	$docCSV .= (strlen($docCSV)==0 ? "" : ", ")."'".$d."'";
}
if (strlen($docCSV)>0) {
	$queryStr = "SELECT `tblDocuments`.*, `tblDocumentLocks`.`userID` as `lockUser`, ".
		"`tblDocumentStatus`.*, `tblDocumentStatusLog`.`status`, ".
		"`tblDocumentStatusLog`.`comment` AS `statusComment`, `tblDocumentStatusLog`.`date` as `statusDate`, ".
		"`tblDocumentStatusLog`.`userID`, `oTbl`.`fullName` AS `ownerName`, `sTbl`.`fullName` AS `statusName` ".
		"FROM `tblDocumentStatus` ".
		"LEFT JOIN `tblDocumentStatusLog` USING (`statusID`) ".
		"LEFT JOIN `ttstatid` on `ttstatid`.`maxLogID` = `tblDocumentStatusLog`.`statusLogID` ".
		"LEFT JOIN `tblDocuments` ON `tblDocuments`.`id` = `tblDocumentStatus`.`documentID` ".
		"LEFT JOIN `tblDocumentLocks` ON `tblDocuments`.`id`=`tblDocumentLocks`.`document` ".
		"LEFT JOIN `tblUsers` AS `oTbl` on `oTbl`.`id` = `tblDocuments`.`owner` ".
		"LEFT JOIN `tblUsers` AS `sTbl` on `sTbl`.`id` = `tblDocumentStatusLog`.`userID` ".
		"WHERE `ttstatid`.`maxLogID`=`tblDocumentStatusLog`.`statusLogID` ".
		"AND `tblDocuments`.`id` IN (" . $docCSV . ") ".
		"ORDER BY `statusDate` DESC";
	$resArr = $db->getResultArray($queryStr);
	
	if (is_bool($resArr) && !$resArr) {
		UI::exitError(getMLText("approval_summary"),getMLText("internal_error_exit"));
	}
	// Create an array to hold all of these results, and index the array by
	// document id. This makes it easier to retrieve document ID information
	// later on and saves us having to repeatedly poll the database every time
	// new document information is required.
	$docIdx = array();
	foreach ($resArr as $res) {
	
		// verify expiry
		if ( $res["expires"] && time()>$res["expires"]+24*60*60 ){
			if  ( $res["status"]==S_DRAFT_APP || $res["status"]==S_DRAFT_REV ){
				$res["status"]=S_EXPIRED;
			}
		}
		$docIdx[$res["id"]][$res["version"]] = $res;
	}
}
$iRev = array();	
$printheader = true;
foreach ($approvalStatus["indstatus"] as $st) {
	if (isset($docIdx[$st["documentID"]][$st["version"]])) {
	
		if ($printheader){
			print "
";
			print "\n\n";
			print "| ".getMLText("name")."\n";
			print " | ".getMLText("owner")."\n";
			print " | ".getMLText("status")."\n";
			print " | ".getMLText("version")."\n";
			print " | ".getMLText("last_update")."\n";
			print " | ".getMLText("expires")."\n";
			print " | 
\n\n\n";
			$printheader = false;
		}
	
		print "\n";
		print "| ".$docIdx[$st["documentID"]][$st["version"]]["name"]."";
		print " | ".$docIdx[$st["documentID"]][$st["version"]]["ownerName"]."";
		print " | ".getOverallStatusText($docIdx[$st["documentID"]][$st["version"]]["status"])."";
		print " | ".$st["version"]."";
		print " | ".$st["date"]." ". $docIdx[$st["documentID"]][$st["version"]]["statusName"] ."";
		print " | ".(!$docIdx[$st["documentID"]][$st["version"]]["expires"] ? "-":getReadableDate($docIdx[$st["documentID"]][$st["version"]]["expires"]))."";				
		print " | 
\n";
	}
	if ($st["status"]!=-2) {
		$iRev[] = $st["documentID"];
	}
}
if (!$printheader) {
	echo "\n
\n";
}else{
	printMLText("empty_notify_list");
}
UI::contentContainerEnd();
UI::contentHeading(getMLText("group_approval_summary"));
UI::contentContainerStart();
$printheader = true;
foreach ($approvalStatus["grpstatus"] as $st) {
	if (!in_array($st["documentID"], $iRev) && isset($docIdx[$st["documentID"]][$st["version"]])) {
	
		if ($printheader){
			print "";
			print "\n\n";
			print "| ".getMLText("name")."\n";
			print " | ".getMLText("owner")."\n";
			print " | ".getMLText("status")."\n";
			print " | ".getMLText("version")."\n";
			print " | ".getMLText("last_update")."\n";
			print " | ".getMLText("expires")."\n";
			print " | 
\n\n\n";
			$printheader = false;
		}	
	
		print "\n";
		print "| ".$docIdx[$st["documentID"]][$st["version"]]["name"]."";
		print " | ".$docIdx[$st["documentID"]][$st["version"]]["ownerName"]."";
		print " | ".getOverallStatusText($docIdx[$st["documentID"]][$st["version"]]["status"])."";
		print " | ".$st["version"]."";
		print " | ".$st["date"]." ". $docIdx[$st["documentID"]][$st["version"]]["statusName"] ."";
		print " | ".(!$docIdx[$st["documentID"]][$st["version"]]["expires"] ? "-":getReadableDate($docIdx[$st["documentID"]][$st["version"]]["expires"]))."";				
		print " | 
\n";
	}
}
if (!$printheader) {
	echo "\n
\n";
}else{
	printMLText("empty_notify_list");
}
UI::contentContainerEnd();
UI::htmlEndPage();
?>