<?php

///////////////////////////////////////////////////////////////////////////////////////////////////
// CLEAN UNNECESSARY SUBTOTALS AND GRANDTOTALS FOR COLS TOTALS ////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

for($i=$displayMatrixDetails['colsTotalsTh']['y'];$i<$displayMatrixDetails['colsTotalsTh']['y']+$displayMatrixDetails['colsTotalsTh']['height'];$i++) {
	$someDeletion=false;
	for($j=0;$j<$displayMatrixDetails['finish']['x'];$j++) {
		if ($pivotDisplayMatrix[$i][$j]['totals']) {
			if ($pivotTotalsDetails['disable']['cols'][ $pivotDisplayMatrix[$i][$j]['totals'] ]) {
				// unset this cell
				unset($pivotDisplayMatrix[$i][$j]);
				$someDeletion=true;
			}
		}
	}
	// if some deletion happened - adjust the height
	if ($someDeletion) {
		// make the finish box smaller (by width)
		$displayMatrixDetails['finish']['height']--;
	}
}



///////////////////////////////////////////////////////////////////////////////////////////////////
// CLEAN UNNECESSARY SUBTOTALS AND GRANDTOTALS FOR ROWS TOTALS ////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////

for($j=$displayMatrixDetails['rowsTotalsTh']['x'];$j<$displayMatrixDetails['rowsTotalsTh']['x']+$displayMatrixDetails['rowsTotalsTh']['width'];$j++) {
	$someDeletion=false;
	for($i=0;$i<$displayMatrixDetails['finish']['y'];$i++) {
		if ($pivotDisplayMatrix[$i][$j]['totals']) {
			if ($pivotTotalsDetails['disable']['rows'][ $pivotDisplayMatrix[$i][$j]['totals'] ]) {
				// unset this cell
				unset($pivotDisplayMatrix[$i][$j]);
				$someDeletion=true;
			}
		}
	}
	// if some deletion happened - adjust the width
	if ($someDeletion) {
		// make the finish box smaller (by width)
		$displayMatrixDetails['finish']['width']--;
	}
}


///////////////////////////////////////////////////////////////////////////////////////////////////
// CLEAN FINISH BOX ///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
unset($pivotDisplayMatrix[$displayMatrixDetails['finish']['y']][$displayMatrixDetails['finish']['x']]);


///////////////////////////////////////////////////////////////////////////////////////////////////
// COLLAPSE ROWS UP (ONLY KEPT ROWS WILL BE DISPLAYED IN THE FIRST ROWS OF C7/C8) /////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
for($i=$displayMatrixDetails['colsTotalsTh']['y'];$i<$displayMatrixDetails['colsTotalsTh']['y']+$displayMatrixDetails['colsTotalsTh']['height'];$i++) {
	// if this row is empty - find the next one to be moved here
	if (!$pivotDisplayMatrix[$i]) {
		for($j=$i+1;$j<$displayMatrixDetails['colsTotalsTh']['y']+$displayMatrixDetails['colsTotalsTh']['height'];$j++) {
			// if one row found
			if ($pivotDisplayMatrix[$j]) {
				// move it to the parent
				$pivotDisplayMatrix[$i]=$pivotDisplayMatrix[$j];
				// unset it here
				$pivotDisplayMatrix[$j]= [];
				// break this loop not to find another row and to overwrite the previous row found
				break;
			}
		}

	}
}

// COUNT VISIBLE ROWS REMAINED ... ----------------------------------------------------------------
$colsTotalsHeight=0;
for($i=$displayMatrixDetails['colsTotalsTh']['y'];$i<$displayMatrixDetails['colsTotalsTh']['y']+$displayMatrixDetails['colsTotalsTh']['height'];$i++) {
	if ($pivotDisplayMatrix[$i]) {
		$colsTotalsHeight++;
	} else {
		unset($pivotDisplayMatrix[$i]);
	}
}

// ... AND UPDATE THE HEIGHT OF SOME ZONES --------------------------------------------------------
$displayMatrixDetails['colsTotalsTh']['height']=$colsTotalsHeight;
// $displayMatrixDetails['finish']['height']=$colsTotalsHeight; // already updated



///////////////////////////////////////////////////////////////////////////////////////////////////
// COLLAPSE ROWS LEFT (ONLY KEPT ROWS WILL BE DISPLAYED IN THE FIRST ROWS OF C3/C6) ///////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
for($j=$displayMatrixDetails['rowsTotalsTh']['x'];$j<$displayMatrixDetails['rowsTotalsTh']['x']+$displayMatrixDetails['rowsTotalsTh']['width'];$j++) {

	// if this col is empty - find the next one to be moved here
	if (!$pivotDisplayMatrix[0][$j]) {
		for($j2=$j+1;$j2<$displayMatrixDetails['rowsTotalsTh']['x']+$displayMatrixDetails['rowsTotalsTh']['width'];$j2++) {
			// if one row found
			if ($pivotDisplayMatrix[0][$j2]) {

				// move this col to the parent
				for($i=0;$i<$displayMatrixDetails['finish']['y'];$i++) {
					// if there is something to move
					if ($pivotDisplayMatrix[$i][$j2]) {
						$pivotDisplayMatrix[$i][$j]=$pivotDisplayMatrix[$i][$j2];
						unset($pivotDisplayMatrix[$i][$j2]);
					}
				}

				break;
			}

		}
	}

}


// COUNT VISIBLE COLS REMAINED ... ----------------------------------------------------------------
$rowsTotalsWidth=0;
for($j=$displayMatrixDetails['rowsTotalsTh']['x'];$j<$displayMatrixDetails['rowsTotalsTh']['x']+$displayMatrixDetails['rowsTotalsTh']['width'];$j++) {
	if ($pivotDisplayMatrix[0][$j]) {
		$rowsTotalsWidth++;
	}
}

// ... AND UPDATE THE HEIGHT OF SOME ZONES --------------------------------------------------------
$displayMatrixDetails['rowsTotalsTh']['width']=$rowsTotalsWidth;
// $displayMatrixDetails['finish']['width']=$rowsTotalsWidth; // already updated




///////////////////////////////////////////////////////////////////////////////////////////////////
// REFRESH THE 'finish' CELL //////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
if (!$displayMatrixDetails['finish']['width'] or !$displayMatrixDetails['finish']['height']) {
	unset( $pivotDisplayMatrix [$displayMatrixDetails['finish']['y']] [$displayMatrixDetails['finish']['x']] );
} else {
	// clean old colspan/rowspan
	unset($pivotDisplayMatrix[$displayMatrixDetails['finish']['y']][$displayMatrixDetails['finish']['x']]['colspan']);
	unset($pivotDisplayMatrix[$displayMatrixDetails['finish']['y']][$displayMatrixDetails['finish']['x']]['rowspan']);

	// re set the region and content (as the cell way unset earlier)
	$pivotDisplayMatrix[$displayMatrixDetails['finish']['y']][$displayMatrixDetails['finish']['x']]['region']="finish";
	$pivotDisplayMatrix[$displayMatrixDetails['finish']['y']][$displayMatrixDetails['finish']['x']]['content']='';

	// calculate again colspan/rowspan
	if ($displayMatrixDetails['finish']['width']>1)
		$pivotDisplayMatrix[$displayMatrixDetails['finish']['y']][$displayMatrixDetails['finish']['x']]['colspan']=$displayMatrixDetails['finish']['width'];
	if ($displayMatrixDetails['finish']['height']>1)
		$pivotDisplayMatrix[$displayMatrixDetails['finish']['y']][$displayMatrixDetails['finish']['x']]['rowspan']=$displayMatrixDetails['finish']['height'];
}


?>