<?php

// empty session data for now
unset($S['adminReport']);


///////////////////////////////////////////////////////////////////////////////////////////////////
// GETS REPORT DETAILS ////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
$reportDetailsSQL="SELECT * FROM `$DBTReports` WHERE id='".$R['updateIdReport']."'";
$reportDetails=$db->GetRow($reportDetailsSQL);

// write log / save report id into session - for updating in the end
if (!$reportDetails) {
	if (!is_array($reportDetails)) {
		writeLog("Could not get report details. SQL: ".$reportDetailsSQL, 'events', 10);
	}
	$S['adminReport']['updateIdReport']=0;
} else {
	$S['adminReport']['updateIdReport']=$R['updateIdReport'];
}

// ASSOCIATE EVERY REPORT FIELDS //////////////////////////////////////////////////////////////////
if ($S['adminReport']['updateIdReport']) {

	$S['adminReport']['idCategory']=$reportDetails['idCategory'];
	$S['adminReport']['source']=$reportDetails['source'];
	$S['adminReport']['name']=$reportDetails['name'];
	$S['adminReport']['fileNameTemplate']=$reportDetails['fileNameTemplate'];
	$S['adminReport']['description']=$reportDetails['description'];
	$S['adminReport']['platform']=$reportDetails['platform'];
	$S['adminReport']['format']=$reportDetails['format'];
	$S['adminReport']['extension']=$reportDetails['extension'];
	$S['adminReport']['style']=$reportDetails['style'];

	// columns and filters
	$S['adminReport']['filters']=unserialize($reportDetails['filters']);
	if (!$S['adminReport']['filters']) $S['adminReport']['filters']= [];
	$S['adminReport']['customFilters']=unserialize($reportDetails['customFilters']);
	if (!$S['adminReport']['customFilters']) $S['adminReport']['customFilters']= [];
	$S['adminReport']['columns']=unserialize($reportDetails['columns']);
	if (!$S['adminReport']['columns']) $S['adminReport']['columns']= [];
	$S['adminReport']['columnsOrder']=unserialize($reportDetails['columnsOrder']);
	if (!$S['adminReport']['columnsOrder']) $S['adminReport']['columnsOrder']= [];
	// sorting/grouping/pivots
	$S['adminReport']['sorting']=unserialize($reportDetails['sorting']);
	if (!$S['adminReport']['sorting']) $S['adminReport']['sorting']= [];
	$S['adminReport']['pivots']=unserialize($reportDetails['pivotTables']);
	if (!$S['adminReport']['pivots']) $S['adminReport']['pivots']= [];
	// custom columns
	$customColumnsList=unserialize($reportDetails['customColumns']);
	if ($customColumnsList) {
		foreach($customColumnsList as $idx=>$col) {
			if ($col['type']=='remark') {
				$customColumnsList[$idx]['f']='remark:'.$col['rmSrc'].':'.$col['rmVar'];
			}
			elseif ($col['type']=='arrayValue') {
				$customColumnsList[$idx]['f']='arrayValue:'.$col['arrSrc'].':'.$col['arrKey'];
			}
			elseif ($col['type']=='entity') {
				$customColumnsList[$idx]['f']='entity:'.$col['entType'].':'.$col['idEnt'].':'.$col['attr'];
			}
			elseif ($col['type']=='entityValue') {
				$customColumnsList[$idx]['f']='entityValue:'.$col['entType'].':'.$col['idEnt'].':'.$col['attr'];
			}
		}
	}
	$S['adminReport']['customColumns']=$customColumnsList;
	if (!$S['adminReport']['customColumns']) $S['adminReport']['customColumns']= [];

	// extra options
	$S['adminReport']['extraOptions']['emailRecipients']=unserialize($reportDetails['emailRecipients']);
	if ($reportDetails['autoGenerate']) {
		$S['adminReport']['extraOptions']['autoGenerate']=unserialize($reportDetails['autoGenerate']);
		$S['adminReport']['extraOptions']['autoGenerate']['active']=1;
	} else {
		$S['adminReport']['extraOptions']['autoGenerate']= [];
	}
	// report summary
	$S['adminReport']['extraOptions']['reportSummary']= [];
	$S['adminReport']['extraOptions']['reportSummary']['options']=unserialize($reportDetails['summary']);
	if ($S['adminReport']['extraOptions']['reportSummary']['options'])
		$S['adminReport']['extraOptions']['reportSummary']['active']=true;

}



///////////////////////////////////////////////////////////////////////////////////////////////////
// GETS REPORT ACCESS /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
$reportAccessSQL="SELECT idUser FROM `$DBTReportsAccess` WHERE idReport='".$R['updateIdReport']."'";
$reportAccess=$db->GetAll($reportAccessSQL);

// check sql
if (!$reportAccess) {
	if ( !is_array($reportAccess) ) {
		writeLog("Could not get report accesses.", 'events', 10);
	}
}
// ASSOCIATE REPORT ACCESS ////////////////////////////////////////////////////////////////////////
else {
	foreach($reportAccess as $usr) {
		$S['adminReport']['accessUsers'][$usr['idUser']]=1;
	}
}


?>