<?php

///////////////////////////////////////////////////////////////////////////////////////////////////
// IF "ADD REPORT" AND FIRST TIME ON THIS PAGE - GIVE ACCESS TO CURRENT USER //////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if (!$R['updateIdReport'] and !$S['adminReport']['accessUsers']) {
	$S['adminReport']['accessUsers'][$S['auth']['idUser']]=1;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// DEFINE THE FIELDS //////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

// REPORT CATEGORY ////////////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='select';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['category'];
$newField['attributes']['id']="reportCategoryId";
$newField['attributes']['mandatory']=1;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['name']='idCategory';
$newField['attributes']['value']=$S['adminReport']['idCategory'];
$newField['options']= [''=>$_LANG['reportsManagement']['admin']['general']['chooseCategory']];
$categoriesListSQL="SELECT id, name FROM `$DBTReportsCategories` WHERE !deleted ORDER BY id ASC";
$categoriesListArr=$db->GetAll($categoriesListSQL);
if (!is_array($categoriesListArr)) {
	writeLog("Could not get categories list. SQL: ".$categoriesListSQL, 'events', 10);
} else {
	foreach($categoriesListArr as $cat) {
		$newField['options'][$cat['id']]=$cat['name'];
	}
}

$fields['idCategory']=$newField;


// REPORT NAME ////////////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='text';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['name'];
$newField['attributes']['id']="reportNameId";
$newField['attributes']['mandatory']=1;
$newField['attributes']['maxlength']=100;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['name']='name';
$newField['attributes']['value']=$S['adminReport']['name'];

$fields['name']=$newField;


// REPORT DESCRIPTION /////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='textarea';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['description'];
$newField['attributes']['id']="reportDescriptionId";
$newField['attributes']['mandatory']=0;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['rows']=3;
$newField['attributes']['name']='description';
$newField['attributes']['value']=$S['adminReport']['description'];

$fields['description']=$newField;


// SOURCE /////////////////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='select';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['source'];
$newField['attributes']['id']="reportSourceId";
$newField['attributes']['mandatory']=1;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['name']='source';
$newField['attributes']['value']=$S['adminReport']['source'];
$newField['options']= [''=>$_LANG['reportsManagement']['admin']['general']['chooseSource']];
// get sources list
foreach($CFG['reportsManagement']['adminReport']['sourcesList'] as $source=>$srcDet) {
	if ($srcDet['active']) {
		$newField['options'][ $srcDet['category'] ]['options'][$source]=$_LANG['reports']['source']['name'][$source];
		$newField['options'][ $srcDet['category'] ]['title']=$_LANG['reports']['sourceCategory']['name'][ $srcDet['category'] ];
	}
}

// display some explanatory text here...
if ($S['adminReport']['source']) {
	$newField['details'].="<div id='categoryInfoDiv' class='importantSourceInfo' style='display:none'>".$_LANG['reportsManagement']['admin']['general']['sourceInfo']."</div>";
}

// add something to onchange events
$newField['attributes']['onchange']="
	$('fileNameTemplate').value='';ajaxIframeReload('filtersList', 'general_filtersSelector.php?reportSource='+this.value);
	var repSrc = $('reportSourceId').value;
";
if ($S['adminReport']['source']) {
	$newField['attributes']['onchange'].="$('categoryInfoDiv').show();";
}



$fields['source']=$newField;


// REPORT STYLE ///////////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='select';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['style'];
$newField['attributes']['id']="reportStyleId";
$newField['attributes']['mandatory']=1;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['name']='style';
if (!$S['adminReport']['style'])
	$S['adminReport']['style']=$CFG['reportsManagement']['adminReport']['general']['defaultStyle'];
$newField['attributes']['value']=$S['adminReport']['style'];

$newField['options']= [];
foreach($CFG['reportsManagement']['adminReport']['general']['pivotStyles'] as $style=>$active) {
	if ($active)
		$newField['options'][$style]=$_LANG['reportsManagement']['reports']['styles'][$style];
}

$fields['style']=$newField;


// REPORT FORMAT //////////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='select';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['format'];
$newField['attributes']['id']="reportFormatId";
$newField['attributes']['mandatory']=1;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['name']='format';
if (!$S['adminReport']['format'])
	$S['adminReport']['format']=$CFG['reportsManagement']['adminReport']['general']['exportFileDefaultFormat'];
$newField['attributes']['value']=$S['adminReport']['format'];

$newField['options']= [];
foreach($CFG['reportsManagement']['adminReport']['general']['exportFileFormats'] as $format=>$active) {
	if ($active)
		$newField['options'][$format]=$_LANG['reportsManagement']['reports']['formats'][$format];
}
// on change...
foreach($CFG['reportsManagement']['adminReport']['general']['exportFileFormats'] as $format=>$active) {
	if ($active) {
		$newField['attributes']['onchange'].="if (this.value=='".$format."') $('reportExtensionId').value='".$CFG['reportsManagement']['adminReport']['general']['defaultFileExtension'][$format]."';";
	}
}
$newField['attributes']['onChange']="";



$fields['format']=$newField;


// REPORT EXTENSION ///////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='text';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['extension'];
$newField['attributes']['id']="reportExtensionId";
$newField['attributes']['mandatory']=1;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['name']='extension';
if (!$S['adminReport']['extension'])
	$S['adminReport']['extension']=$CFG['reportsManagement']['adminReport']['general']['defaultFileExtension'][$S['adminReport']['format']];
$newField['attributes']['value']=$S['adminReport']['extension'];

$fields['extension']=$newField;


// EXECUTION PLATFORM /////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='radio';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['platform'];
$newField['explanations']=$_LANG['reportsManagement']['admin']['general']['platformInfo'];
$newField['attributes']['id']="reportPlatformId";
$newField['attributes']['mandatory']=1;
$newField['attributes']['name']='platform';
if (!$S['adminReport']['platform'])
	$S['adminReport']['platform']=$CFG['reportsManagement']['adminReport']['general']['executionPlaftormDefault'];
$newField['attributes']['value']=$S['adminReport']['platform'];
$newField['radios']= ['cli'=>$_LANG['reportsManagement']['admin']['general']['platform_cli'], 'currentPage'=>$_LANG['reportsManagement']['admin']['general']['platform_currentPage']];

$fields['platform']=$newField;


// FILE NAME TEMPLATE /////////////////////////////////////////////////////////////////////////////
$newField= [];
$newField['type']='text';
$newField['description']=$_LANG['reportsManagement']['admin']['general']['fileNameTplField'];
$newField['explanations']=$_LANG['reportsManagement']['admin']['general']['fileNameTplInfo'];
$newField['attributes']['id']="fileNameTemplate";
$newField['attributes']['mandatory']=0;
$newField['attributes']['maxlength']=100;
$newField['attributes']['style']="width:300px;";
$newField['attributes']['name']='fileNameTemplate';
$newField['attributes']['value']=$S['adminReport']['fileNameTemplate'];

$newField['details']="<div id=\"filtersList\"></div>";

$fields['fileNameTemplate']=$newField;


?>