<?php

include("_config/addCfg.php");
include("_includes/addInc.php");

// trace visits
$userTrailItem->updateActionOld('reportExportOk');
$userTrailItem->updateEntityOld('report', $R['idReport']);

// the id of the report
$idReport=$R['idReport'];


///////////////////////////////////////////////////////////////////////////////////////////////////
// GET REPORT DATA ////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
$reportFieldsSQL="SELECT * FROM `$DBTReports` WHERE id='{$idReport}'";
$reportFields=$db->GetRow($reportFieldsSQL);
// check report
if ($db->ErrorNo()) {
	writeLog("Could not get report content.", 'events', 10);
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// PREPARE FIELDS AND CONFIRMATION CODE ///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// the report code
$alterSalt="= some stuff = REPORTS = !!! unique string !!!";
$reportKey=md5($alterSalt.$reportFields['name'].$reportFields['description'].$reportFields['format'].$reportFields['source'].$reportFields['columns'].$reportFields['filters'].$reportFields['customFilters'].$reportFields['customColumns'].$reportFields['columnsOrder'].$reportFields['sorting'].$reportFields['pivotTables'].$reportFields['summary']);

///////////////////////////////////////////////////////////////////////////////////////////////////
// CREATE XML CONTENT TO BE EXPORTED //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
$startXml='<?xml version="1.0" encoding="UTF-8"?><report></report>';
$report=simplexml_load_string($startXml);
$report['key']=$reportKey;
$report->name=$reportFields['name'];
$report->description=$reportFields['description'];
$report->fileNameTemplate=$reportFields['fileNameTemplate'];
$report->format=$reportFields['format'];
$report->extension=$reportFields['extension'];
$report->style=$reportFields['style'];
$report->source=$reportFields['source'];
$report->columns=$reportFields['columns'];
$report->filters=$reportFields['filters'];
$report->customFilters=$reportFields['customFilters'];
$report->customColumns=$reportFields['customColumns'];
$report->columnsOrder=$reportFields['columnsOrder'];
$report->sorting=$reportFields['sorting'];
$report->pivotTables=$reportFields['pivotTables'];
$report->summary=$reportFields['summary'];

$reportContent=$report->asXML();

///////////////////////////////////////////////////////////////////////////////////////////////////
// DOWNLOAD FILE //////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

// send the download headers...
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=\"".\Engine\File::cleanName($reportFields['name']).".trt\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".strlen($reportContent));

echo $reportContent;
exit;

?>