<?php

///////////////////////////////////////////////////////////////////////////////////////////////////
// DOWNLOAD REPORT ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['downloadReport']) {

	// report dir / file
	$reportDir=$MODULE_REL_PATH.$CFG['paths']['generatedReports'].$idReportGenerating."/";

	// generates the name of the file to be downloaded
	$reportFile=$reportOutput['report']['reportName'];

	// the download file
	$reportDownloadFile=$reportDir.$reportOutput['report']['fileName'];

	// read the file
	if (is_file($reportDownloadFile)) {

		$userTrailItem->updateActionOld('reportGenerating_reportDownload');

		// CREATE ZIP FILE FOR DOWNLOAD -----------------------------------------------------------
		if ($R['format']=='zip') {

			$zipFileName=\Engine\File::getName($reportDownloadFile).'.zip';
			$zipFileNameWithPath=\Engine\File::getPath($reportDownloadFile).$zipFileName;

			$zip=new ZipArchive();

			if (!$zip->open($zipFileNameWithPath, ZIPARCHIVE::CREATE)) {
				writeLog('There is a problem with `ZipArchive::open` PHP lib.', 'events', 8);
				$showMessage['error']=$_LANG['reportsGeneration']['viewReport']['zipFileBad'];
				$OK=false;
			}

			if ($OK and !$zip->addFile($reportDownloadFile, \Engine\File::getNameFull($reportFile))) {
				writeLog('There is a problem with `ZipArchive::addFile` PHP lib.', 'events', 8);
				$showMessage['error']=$_LANG['reportsGeneration']['viewReport']['zipFileBad'];
				$OK=false;
			}

			$zip->close();

			$reportDownloadFile=$zipFileNameWithPath;
			$reportFile=\Engine\File::getName($reportFile).'.zip';

		}

		// DOWNLOAD THE FILE ----------------------------------------------------------------------
		if ($OK) {
			// file size
			$reportSize=filesize($reportDownloadFile);

			// Force download window
			header("Content-Type: application/force-download");
			header("Content-Type: application/octet-stream");
			header("Content-Type: application/download");

			// Add file details to download window
			header("Content-Disposition: attachment; filename=\"".$reportFile."\";");
			if ($reportSize) {
				header("Content-Length: ".$reportSize);
			}

			// output the file
			readfile($reportDownloadFile);

			// DELETE THE ZIP FILE --------------------------------------------------------------------
			if ($R['format']=='zip') {
				if (file_exists($reportDownloadFile)) {
					unlink($reportDownloadFile);
				}
			}

			exit;

		}


	}
	// file does not exist
	else {

		$showMessage['error'] = $_LANG['reportsGeneration']['viewReport']['noFileOnServer'];
		writeLog("Report file is missing from the server. (id: ".$idReportGenerating, 'events', 9);
		$OK=0;
	}

}


///////////////////////////////////////////////////////////////////////////////////////////////////
// VIEW REPORT ////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['viewReport']) {

	$reportDir=$MODULE_REL_PATH.$CFG['paths']['generatedReports'].$idReportGenerating."/";
	$reportFile=$reportOutput['report']['fileName'];

	$reportDownloadFile=$reportDir.$reportFile;

	// read the file
	if (is_file($reportDownloadFile)) {

		$userTrailItem->updateActionOld('reportGenerating_reportView');

		// output the file
		readfile($reportDownloadFile);

		exit;

	}
	// file does not exist
	else {
		$showMessage['error'] = $_LANG['reportsGeneration']['viewReport']['noFileOnServer'];
		writeLog("Report file is missing from the server. file: ".$reportDownloadFile, 'events', 9);
		$OK=0;
	}

}



?>