<?php

		if ( !isset($_POST["json"]) ){ die("WTF?!"); }

		$JSON = $_POST["json"];
		$arr = json_decode($JSON, true);
		
		if ($arr["func"] == "insertBoxen") {
		 messwerteZuExcel($arr);
		} elseif ($arr["func"] == "aa") { 
			print("nüscht"); 
		}	else {
			die("falscher input ...\n");
		} 
	#} else {
	#	print("Kein CLI mehr ...\n");
	#}

	require 'includes/boxpos.php';


	function messwerteZuExcel($arr){

		$inputFile = '';
		$dir = './xlsx_tmp';
		switch($arr["anzahlBoxen"]){
			case 1: $inputFile  = 'testfiles/1.xlsx'; break;
			case 2: $inputFile  = 'testfiles/2.xlsx'; break;
			case 3: $inputFile  = 'testfiles/3.xlsx'; break;
			case 4: $inputFile  = 'testfiles/4.xlsx'; break;
			case 5: $inputFile  = 'testfiles/5.xlsx'; break;
			case 6: $inputFile  = 'testfiles/6.xlsx'; break;
			case 7: $inputFile  = 'testfiles/7.xlsx'; break;
			case 8: $inputFile  = 'testfiles/8.xlsx'; break;
			case 9: $inputFile  = 'testfiles/9.xlsx'; break;
			case 10: $inputFile = 'testfiles/10.xlsx'; break;
			case 11: $inputFile = 'testfiles/11.xlsx'; break;
			case 12: $inputFile = 'testfiles/12.xlsx'; break;
			case 13: $inputFile = 'testfiles/13.xlsx'; break;
			case 14: $inputFile = 'testfiles/14.xlsx'; break;
			case 15: $inputFile = 'testfiles/15.xlsx'; break;
		}

		// Unzip
		$zip = new ZipArchive();
		$zip->open($inputFile);
		if ($zip->extractTo($dir)){
			$sheets = array_diff(scandir($dir ."/xl/worksheets", 0), array('_rels', '..', '.'));
			print("DEBUG: $inputFile extracted to $dir | NrOfBoxes: ". $arr["anzahlBoxen"] ."\n");
		} else {
			die("Fehler beim entpacken des Templates.");
		}

		exec("rm -Rf $dir/*");

	}


	exit;

	$inputFile = 'testfiles/13.xlsx';
	$dir = './xlsx_tmp';
	$rmcmd = "rm -Rf $dir/*";

	// Unzip
	$zip = new ZipArchive();
	$zip->open($inputFile);
	if ($zip->extractTo($dir)){
		$sheets = array_diff(scandir($dir ."/xl/worksheets", 0), array('_rels', '..', '.'));
		global $nrOfBoxes;
		$nrOfBoxes = count($sheets) / 2;
		print("DEBUG: $inputFile extracted to $dir | NrOfBoxes: $nrOfBoxes\n");
	}

	switch ($nrOfBoxes){
		case 15:
			writeBoxStrings($boxPos15["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos15["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos15["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos15["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos15["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos15["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos15["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos15["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			writeBoxStrings($boxPos15["box9"], $box9Messwerte); writeBoxMesswerte(9, $box9Messwerte);
			writeBoxStrings($boxPos15["box10"], $box10Messwerte); writeBoxMesswerte(10, $box10Messwerte);
			writeBoxStrings($boxPos15["box11"], $box11Messwerte); writeBoxMesswerte(11, $box11Messwerte);
			writeBoxStrings($boxPos15["box12"], $box12Messwerte); writeBoxMesswerte(12, $box12Messwerte);
			writeBoxStrings($boxPos15["box13"], $box13Messwerte); writeBoxMesswerte(13, $box13Messwerte);
			writeBoxStrings($boxPos15["box14"], $box14Messwerte); writeBoxMesswerte(14, $box14Messwerte);
			writeBoxStrings($boxPos15["box15"], $box15Messwerte); writeBoxMesswerte(15, $box15Messwerte);
			break;
		case 14:
			writeBoxStrings($boxPos14["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos14["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos14["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos14["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos14["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos14["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos14["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos14["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			writeBoxStrings($boxPos14["box9"], $box9Messwerte); writeBoxMesswerte(9, $box9Messwerte);
			writeBoxStrings($boxPos14["box10"], $box10Messwerte); writeBoxMesswerte(10, $box10Messwerte);
			writeBoxStrings($boxPos14["box11"], $box11Messwerte); writeBoxMesswerte(11, $box11Messwerte);
			writeBoxStrings($boxPos14["box12"], $box12Messwerte); writeBoxMesswerte(12, $box12Messwerte);
			writeBoxStrings($boxPos14["box13"], $box13Messwerte); writeBoxMesswerte(13, $box13Messwerte);
			writeBoxStrings($boxPos14["box14"], $box14Messwerte); writeBoxMesswerte(14, $box14Messwerte);
			break;
		case 13:
			writeBoxStrings($boxPos13["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos13["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos13["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos13["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos13["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos13["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos13["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos13["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			writeBoxStrings($boxPos13["box9"], $box9Messwerte); writeBoxMesswerte(9, $box9Messwerte);
			writeBoxStrings($boxPos13["box10"], $box10Messwerte); writeBoxMesswerte(10, $box10Messwerte);
			writeBoxStrings($boxPos13["box11"], $box11Messwerte); writeBoxMesswerte(11, $box11Messwerte);
			writeBoxStrings($boxPos13["box12"], $box12Messwerte); writeBoxMesswerte(12, $box12Messwerte);
			writeBoxStrings($boxPos13["box13"], $box13Messwerte); writeBoxMesswerte(13, $box13Messwerte);
			break;
		case 12:
			writeBoxStrings($boxPos12["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos12["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos12["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos12["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos12["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos12["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos12["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos12["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			writeBoxStrings($boxPos12["box9"], $box9Messwerte); writeBoxMesswerte(9, $box9Messwerte);
			writeBoxStrings($boxPos12["box10"], $box10Messwerte); writeBoxMesswerte(10, $box10Messwerte);
			writeBoxStrings($boxPos12["box11"], $box11Messwerte); writeBoxMesswerte(11, $box11Messwerte);
			writeBoxStrings($boxPos12["box12"], $box12Messwerte); writeBoxMesswerte(12, $box12Messwerte);
			break;
		case 11:
			writeBoxStrings($boxPos11["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos11["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos11["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos11["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos11["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos11["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos11["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos11["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			writeBoxStrings($boxPos11["box9"], $box9Messwerte); writeBoxMesswerte(9, $box9Messwerte);
			writeBoxStrings($boxPos11["box10"], $box10Messwerte); writeBoxMesswerte(10, $box10Messwerte);
			writeBoxStrings($boxPos11["box11"], $box11Messwerte); writeBoxMesswerte(11, $box11Messwerte);
			break;
		case 10:
			writeBoxStrings($boxPos10["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos10["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos10["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos10["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos10["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos10["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos10["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos10["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			writeBoxStrings($boxPos10["box9"], $box9Messwerte); writeBoxMesswerte(9, $box9Messwerte);
			writeBoxStrings($boxPos10["box10"], $box10Messwerte); writeBoxMesswerte(10, $box10Messwerte);
			break;
		case 9:
			writeBoxStrings($boxPos9["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos9["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos9["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos9["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos9["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos9["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos9["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos9["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			writeBoxStrings($boxPos9["box9"], $box9Messwerte); writeBoxMesswerte(9, $box9Messwerte);
			break;
		case 8:
			writeBoxStrings($boxPos8["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos8["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos8["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos8["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos8["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos8["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos8["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			writeBoxStrings($boxPos8["box8"], $box8Messwerte); writeBoxMesswerte(8, $box8Messwerte);
			break;
		case 7:
			writeBoxStrings($boxPos7["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos7["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos7["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos7["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos7["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos7["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			writeBoxStrings($boxPos7["box7"], $box7Messwerte); writeBoxMesswerte(7, $box7Messwerte);
			break;
		case 6:
			writeBoxStrings($boxPos6["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos6["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos6["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos6["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos6["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			writeBoxStrings($boxPos6["box6"], $box6Messwerte); writeBoxMesswerte(6, $box6Messwerte);
			break;
		case 5:
			writeBoxStrings($boxPos5["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos5["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos5["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos5["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			writeBoxStrings($boxPos5["box5"], $box5Messwerte); writeBoxMesswerte(5, $box5Messwerte);
			break;
		case 4:
			writeBoxStrings($boxPos4["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos4["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos4["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			writeBoxStrings($boxPos4["box4"], $box4Messwerte); writeBoxMesswerte(4, $box4Messwerte);
			break;
		case 3:
			writeBoxStrings($boxPos3["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos3["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			writeBoxStrings($boxPos3["box3"], $box3Messwerte); writeBoxMesswerte(3, $box3Messwerte);
			break;
		case 2:
			writeBoxStrings($boxPos2["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			writeBoxStrings($boxPos2["box2"], $box2Messwerte); writeBoxMesswerte(2, $box2Messwerte);
			break;
		case 1:
			writeBoxStrings($boxPos1["box1"], $box1Messwerte); writeBoxMesswerte(1, $box1Messwerte);
			break;
	}



	include("includes/createExcelFile.php");

	function writeBoxMesswerte($boxNr, $messwerte){
		global $dir;		
		$sheet = simplexml_load_file( $dir ."/xl/worksheets/sheet$boxNr.xml" );

		for ( $i=2; $i<=10; $i++) {
			$sheet->sheetData->row[$i]->c[1]->v  = $messwerte[$i - 1][3];			# OL1
			$sheet->sheetData->row[$i]->c[2]->v  = $messwerte[$i - 1][4];			# Ol2
			$sheet->sheetData->row[$i]->c[3]->v  = $messwerte[$i - 1][5];			# OW1
			$sheet->sheetData->row[$i]->c[4]->v  = $messwerte[$i - 1][6];			# OW2
			$sheet->sheetData->row[$i]->c[5]->v  = $messwerte[$i - 1][7];			# R1
			$sheet->sheetData->row[$i]->c[6]->v  = $messwerte[$i - 1][8];			# R2
			$sheet->sheetData->row[$i]->c[7]->v  = $messwerte[$i - 1][9];			# R3
			$sheet->sheetData->row[$i]->c[8]->v  = $messwerte[$i - 1][10];		# R4
			$sheet->sheetData->row[$i]->c[15]->v = $messwerte[$i - 1][11];		# LW
			$sheet->sheetData->row[$i]->c[16]->v = $messwerte[$i - 1][12];		# CW
			$sheet->sheetData->row[$i]->c[17]->v = $messwerte[$i - 1][13];		# IL1
			$sheet->sheetData->row[$i]->c[18]->v = $messwerte[$i - 1][14];		# IL2
			$sheet->sheetData->row[$i]->c[19]->v = $messwerte[$i - 1][15];		# IW1
			$sheet->sheetData->row[$i]->c[20]->v = $messwerte[$i - 1][16];		# IW2
		}

		$sheet->asXML( $dir ."/xl/worksheets/sheet$boxNr.xml" );

		#        0     1           2            3       4       5       6      7     8     9    10    11    12     13     14       15     16
		# array("", "1-0100", "GW101050100", 429.52, 429.52, 310.62, 310.62, 3.71, 3.71, 3.71, 3.71, 1234, 987, 422.25, 422.25, 303.30, 303.30)

	}


	function writeBoxStrings($strPos, $messwerte){
		$strings = simplexml_load_file('xlsx_tmp/xl/sharedStrings.xml');
		$strings->si[$strPos[0]]->t = $messwerte[1][1];
		$strings->si[$strPos[1]]->t = $messwerte[2][1];
		$strings->si[$strPos[2]]->t = $messwerte[3][1];
		$strings->si[$strPos[3]]->t = $messwerte[4][1];
		$strings->si[$strPos[4]]->t = $messwerte[5][1];
		$strings->si[$strPos[5]]->t = $messwerte[6][1];
		$strings->si[$strPos[6]]->t = $messwerte[7][1];
		$strings->si[$strPos[7]]->t = $messwerte[8][1];
		$strings->si[$strPos[8]]->t = $messwerte[9][1];
		$strings->si[$strPos[9]]->t = $messwerte[1][2];
		$strings->si[$strPos[10]]->t = $messwerte[2][2];
		$strings->si[$strPos[11]]->t = $messwerte[3][2];
		$strings->si[$strPos[12]]->t = $messwerte[4][2];
		$strings->si[$strPos[13]]->t = $messwerte[5][2];
		$strings->si[$strPos[14]]->t = $messwerte[6][2];
		$strings->si[$strPos[15]]->t = $messwerte[7][2];
		$strings->si[$strPos[16]]->t = $messwerte[8][2];
		$strings->si[$strPos[17]]->t = $messwerte[9][2];
		$strings->asXML('xlsx_tmp/xl/sharedStrings.xml');
	}


	function getString($v, $strings){
		$s  = array();
		$si = $strings->si[(int) $v];
		// Register & alias the default namespace or you'll get empty results in the xpath query
		$si->registerXPathNamespace('n', 'http://schemas.openxmlformats.org/spreadsheetml/2006/main');
		// Cat together all of the 't' (text?) node values
		foreach($si->xpath('.//n:t') as $t) {
		    $s[] = (string) $t;
		}
		return implode($s);
	}


?> 
