<?php

$EASY_AUTHENTICATE=true;
include("_config/addCfg.php");
include("_includes/addInc.php");

// selected pivot
$selectedPivot=$S['adminReport']['pivots']['selectedPivot'];
$reportSource=$S['adminReport']['source'];

///////////////////////////////////////////////////////////////////////////////////////////////////
// SAVE PIVOT DETAILS /////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
// update pivot details
$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['n']=$R['pivotName'];
$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['s']=!!$R['selected'];
$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['v']=!!$R['visible'];
$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['format']=$R['format'];
$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['ext']=$R['extension'];
$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['det']=$R['showDetails'];
$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['style']=$R['style'];


///////////////////////////////////////////////////////////////////////////////////////////////////
// ADD COLUMN TO DESIRED TARGET ZONE //////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['column'] and $R['targetZone']) {

	$OK=1;

	// check if target zone is correct
	if ($R['targetZone']!="rows" and $R['targetZone']!="cols" and $R['targetZone']!="data") {
		$OK=0;
	}

	// ADD COLUMN TO ZONE /////////////////////////////////////////////////////////////////////////
	if ($OK) {

		// add field to desired zone
		if ($R['targetZone']=="rows" or $R['targetZone']=="cols")
			$S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['targetZone']][$R['column']]=1;
		if ($R['targetZone']=="data")
			$S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['targetZone']][$R['column']]=$R['operation'];


		// clean column from other zone rows/cols (not to exist as duplicate)
		if ($R['targetZone']=="cols")
			unset($S['adminReport']['pivots']['pivotsList'][$selectedPivot]['rows'][$R['column']]);
		if ($R['targetZone']=="rows")
			unset($S['adminReport']['pivots']['pivotsList'][$selectedPivot]['cols'][$R['column']]);

	}


}


///////////////////////////////////////////////////////////////////////////////////////////////////
// FIELDS MANAGEMENT - MOVE UP ////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['column'] and $R['operation']=="moveUp") { // if column selected and operation is moveUp
	// if target zone is correct
	if ($R['zone']=="rows" or $R['zone']=="cols" or $R['zone']=="data") {
		// list of fields
		if (is_array($S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']])) {
			// get list of columns
			$fieldsListOriginal=array_keys($S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']]);
			// the chosen column
			$colId=array_search($R['column'], $fieldsListOriginal);
			// move the column up
			if (is_numeric($colId) and $colId>0) {
				interchangeVariables($fieldsListOriginal[$colId], $fieldsListOriginal[$colId-1]);
			}
			// create the new order
			$fieldsListFinal= [];
			foreach($fieldsListOriginal as $colKey) {
				$fieldsListFinal[$colKey]=$S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']][$colKey];
			}
			// set the new order of columns
			unset($S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']]);
			$S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']]=$fieldsListFinal;

		}

	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// FIELDS MANAGEMENT - MOVE DOWN //////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['column'] and $R['operation']=="moveDown") { // if column selected and operation is moveUp
	// if target zone is correct
	if ($R['zone']=="rows" or $R['zone']=="cols" or $R['zone']=="data") {
		// list of fields
		if (is_array($S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']])) {
			// get list of columns
			$fieldsListOriginal=array_keys($S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']]);
			// the chosen column
			$colId=array_search($R['column'], $fieldsListOriginal);
			// move the column up
			if (is_numeric($colId) and $colId<count($fieldsListOriginal)-1 ) {
				interchangeVariables($fieldsListOriginal[$colId], $fieldsListOriginal[$colId+1]);
			}
			// create the new order
			$fieldsListFinal= [];
			foreach($fieldsListOriginal as $colKey) {
				$fieldsListFinal[$colKey]=$S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']][$colKey];
			}
			// set the new order of columns
			unset($S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']]);
			$S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']]=$fieldsListFinal;

		}

	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// FIELDS MANAGEMENT - MOVE FROM ROWS TO COLS /////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['column'] and $R['operation']=="moveToCols" and $R['zone']=="rows") {
	// eliminate from rows
	unset($S['adminReport']['pivots']['pivotsList'][$selectedPivot]['rows'][$R['column']]);
	// add to cols
	$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['cols'][$R['column']]=1;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// FIELDS MANAGEMENT - MOVE FROM COLS TO ROWS /////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['column'] and $R['operation']=="moveToRows" and $R['zone']=="cols") {
	// eliminate from rows
	unset($S['adminReport']['pivots']['pivotsList'][$selectedPivot]['cols'][$R['column']]);
	// add to cols
	$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['rows'][$R['column']]=1;
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// FIELDS MANAGEMENT - DATA FORMULA TYPE //////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['column'] and $R['formula'] and $R['zone']=="data") {
	$S['adminReport']['pivots']['pivotsList'][$selectedPivot]['data'][$R['column']]=$R['formula'];
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// FIELDS MANAGEMENT - DELETE FIELD ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($R['column'] and $R['operation']=="delete") {
	if ($R['zone']=="rows" or $R['zone']=="cols" or $R['zone']=="data") {
		// eliminate from rows
		unset($S['adminReport']['pivots']['pivotsList'][$selectedPivot][$R['zone']][$R['column']]);
	}
}



///////////////////////////////////////////////////////////////////////////////////////////////////
// GET NAMES OF COLUMNS ($columnsTranslations) ////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if ($S['adminReport']['pivots']['pivotsList'][$selectedPivot]['rows'] or $S['adminReport']['pivots']['pivotsList'][$selectedPivot]['cols'] or $S['adminReport']['pivots']['pivotsList'][$selectedPivot]['data']) {

	if ($reportSource) {
		// load the columns configs
		loadReportConfigs($reportSource, 'columns');
		// load the default column names
		@include($MODULE_REL_PATH."_lang/".$S['global']['lang']."/_customLoad/reports/".$reportSource."/columns.lang.php");
	}

	// add normal columns to $columnsTranslations
	$columnsTranslations= [];
	if (is_array($CFG['reportsManagement']['adminReport']['columnsList'][$reportSource])) {
		foreach($CFG['reportsManagement']['adminReport']['columnsList'][$reportSource] as $col=>$colDet) {
			// skip all disabled columns
			if (!$colDet['disabled']) {
				if ($S['adminReport']['columns'][$col]['n']) {
					$columnsTranslations[$col]=$S['adminReport']['columns'][$col]['n'];
				} else {
					$columnsTranslations[$col]=$_LANG['reports']['columnNames'][$reportSource][$col];
				}
			}
		}
	}

	// add custom columns to $columnsTranslations
	if ($S['adminReport']['customColumns']) {
		foreach($S['adminReport']['customColumns'] as $colIdx=>$colDet) {
			$columnsTranslations['CUSTOM_COLUMN_'.$colIdx]=$colDet['n'];
		}
	}
}


###################################################################################################
## SMARTY ASSIGNMENTS #############################################################################
###################################################################################################

// pivot details
$smarty->assign('pivotDetails', $S['adminReport']['pivots']['pivotsList'][ $selectedPivot ]);
$smarty->assign('pivotKey', $selectedPivot);
$smarty->assign('columnsTranslations', $columnsTranslations);
$smarty->assign('defaultFileExtension', $CFG['reportsManagement']['adminReport']['pivotTables']['defaultFileExtension']);
$smarty->assign('pivotTablesExportFileFormats', $CFG['reportsManagement']['adminReport']['pivotTables']['exportFileFormats']);
$smarty->assign('pivotTablesStyles', $CFG['reportsManagement']['adminReport']['pivotTables']['pivotStyles']);


// ------------------------------------------------------------------------------------------------
$smarty->display("index_stepContent_pivotTables_pivotsList_selPivot.tpl");

?>