<?php
include($O__libPath . "/functions/server/O__tableMaker.php");

// Load settings
$O__fileData = O__XMLdata($O__DBpath . "/pageTypes/" . $O__PT . "/configs/settings.xml", 0, 0);
$O__pathData = $O__DBpath . "/pageTypes/" . $O__PT . "/data";
unset($O__fileData);

// Basic vars
$O__output = "";
$O__totalValue = 0;
		
function O__createOutput(){
	global $O__DBpath, $O__pathData, $O__PT, $O__URLpath, $O__output, $O__totalValue;

	// Load raw data
	$O__fileData = O__XMLdata($O__pathData . "/" . $O__URLpath[1] . ".xml", 0, 0);
	$O__title = $O__fileData->title;
	$O__dataset = $O__fileData->dataset;
	$O__delimited = (string)$O__fileData->delimited;
	$O__dataAction = $O__fileData->dataAction;
	$O__columnSource = (int)$O__fileData->columnSource;
	$O__columnTarget = (int)$O__fileData->columnTarget;
	$O__datasetCombinedRaw = "";
	$O__outputDatasetCombinedRaw = "";
	$O__outputDatasetUniqueLabelEach = "";

	// "temporary fix" for tab delimiter
	if ($O__delimited == "\\t"){ $O__delimited = "\t"; }
	
	// Raw dataset preparation
	$O__datasetRow = explode("\n",$O__dataset); // create data rows
	$O__datasetRowTotal = count($O__datasetRow); // total amount of rows
	$O__datasetColLabels = explode($O__delimited,$O__datasetRow[0]); // get column labels
	$O__datasetColTotal = count($O__datasetColLabels); // get total # of columns

	// Raw dataset array creation
	for($i=1;$i<$O__datasetRowTotal;$i++){ // Go through each row, after the label row
		$O__datasetCol = explode($O__delimited,$O__datasetRow[$i]); // Get column data from row
		
		if (count($O__datasetCol) > 1){ // If there's column data					
			for($j=0;$j<$O__datasetColTotal;$j++){ $O__datasetTable[$i][$j] = $O__datasetCol[$j]; } // Build column data
		}
	}
	
	// Column labels: Get all possible values
	for($i=1,$j=0;$i<$O__datasetRowTotal;$i++, $j++){ // Go through each row
		$O__columnSourceAll[$j] = $O__datasetTable[$i][$O__columnSource];
	}
	$O__columnSourceAll = array_values(array_unique($O__columnSourceAll, SORT_REGULAR)); // Remove duplicates

	// Reorder data in accordance to all unique values in column
	for($i=0;$i<count($O__columnSourceAll);$i++){ // Go through each unique column value
		for($j=1;$j<$O__datasetRowTotal;$j++){ // Go through each row of the raw dataset
			if ($O__datasetTable[$j][$O__columnSource] == $O__columnSourceAll[$i]){ // If the column label in the row matches the source column label
				$O__datasetRowProcessed[$j] = $O__datasetRow[$j];
			}
		}		
	}
	$O__datasetRowProcessed = array_values($O__datasetRowProcessed);

	// Dataset: Combined Dataset (UX)
	for($i=1;$i<count($O__datasetRowProcessed);$i++){ // Go through each row
		$O__datasetCol = explode($O__delimited,$O__datasetRowProcessed[$i]); // Get column data from each row									
		for($j=0;$j<$O__datasetColTotal;$j++){ // Build column data
			$O__datasetTableProcessed[$i][$j] = $O__datasetCol[$j];
		}				
	}			
	$O__outputDatasetUniqueLabelsAll = "<h3>Unique Labels (All)</h3>" . O__tableMaker($O__datasetColLabels,$O__datasetTableProcessed,"O__table","O__tableLogs",0);

	// Dataset: Original Dataset (UX)
	$O__outputDatasetOriginal = "<h3>Original Dataset</h3>" . O__tableMaker($O__datasetColLabels,$O__datasetTable,"O__table","O__tableLogs",0);			

	// Build combined dataset for EACH unique label	
	for($h=0;$h<count($O__columnSourceAll);$h++){ // Go through each unique label
		
		// Create dataset for this unique label
		unset($O__datasetTableProcessed); // Erase/reset array for each label
		for($i=0;$i<count($O__datasetRowProcessed);$i++){ // Go through each dataset row
			$O__datasetCol = explode($O__delimited,$O__datasetRowProcessed[$i]); // Get column data from row		
			if ($O__datasetCol[$O__columnSource] == $O__columnSourceAll[$h]){ // if row matches currently selected label, include it in processed dataset for this label
				for($j=0; $j<count($O__datasetCol);$j++){ $O__datasetTableProcessed[$i][$j] = $O__datasetCol[$j]; }
			}			
		}			
		$O__datasetTableProcessed = array_values($O__datasetTableProcessed);

		// Create last row with the totals
		$O__totalCount = 0;
		$O__totalDatasetTableProcessedCount = count($O__datasetTableProcessed);
		for($j=0; $j<count($O__datasetTableProcessed); $j++){ $O__totalCount += (int)$O__datasetTableProcessed[$j][$O__columnTarget]; } // Get total for target column
		for($j=0; $j<$O__columnTarget; $j++){ $O__datasetTableProcessed[$O__totalDatasetTableProcessedCount][$j] = ""; } // Last row: Fill in column blanks
		$O__datasetTableProcessed[$O__totalDatasetTableProcessedCount][$O__columnTarget] = $O__totalCount; // Last row
		
		// Dataset: Combined (Summaries)
		$O__datasetCombinedRaw .= $O__columnSourceAll[$h] . $O__delimited . $O__totalCount . "\r\n"; // raw
		$O__datasetTableSummary[$h][0] = $O__columnSourceAll[$h]; // UX table
		$O__datasetTableSummary[$h][1] = $O__totalCount; // UX table
		
		// Dataset: Unique Value 'EACH' (UX)
		$O__outputDatasetUniqueLabelEach .= "<h3>Unique Label '" . $O__columnSourceAll[$h] . "'</h3>" . O__tableMaker($O__datasetColLabels,$O__datasetTableProcessed,"O__table","O__tableLogs",0);			
	}
			
	// Dataset: Combined Raw (UX)
	$O__outputDatasetCombinedRaw .= "<h3>Combined (Raw)</h3><form><textarea style=\"width:98%;\" rows=\"25\" name=\"O__dataset\" id=\"O__bodyCopy\" class=\"O__itemInfo\" >" . trim($O__datasetColLabels[$O__columnSource]) . $O__delimited . trim($O__datasetColLabels[$O__columnTarget]) . "\r\n" . $O__datasetCombinedRaw . "\r\n</textarea></form>";
	
	// Dataset: Combined (UX)
	$O__outputDatasetCombined = "<h3>Combined</h3>" . O__tableMaker(array($O__datasetColLabels[$O__columnSource],$O__datasetColLabels[$O__columnTarget]),$O__datasetTableSummary,"O__table","O__tableLogs",0);
	
	// Assemble output
	$O__output = "<h2>Data Parser</h2>";
	$O__output .="<p><b>Now viewing:</b> <a href=\"/" . $O__PT . "/" . $O__URLpath[1]  . "/edit\">Edit '" . $O__URLpath[1] . "'</a></p>";
	$O__output .= O__sysMsg("Instructions", "Count the values in column " . $O__columnTarget . " ('" . trim($O__datasetColLabels[$O__columnTarget]) . "') for each matching label in column " . $O__columnSource . " ('" . trim($O__datasetColLabels[$O__columnSource]) . "'). <i>(Reminder: Counting starts with 0, not 1.)</i>", "O__sysMsgNeutral");
	$O__output .= $O__outputDatasetCombinedRaw;
	$O__output .= $O__outputDatasetCombined;
	$O__output .= $O__outputDatasetUniqueLabelsAll;
	$O__output .= $O__outputDatasetUniqueLabelEach;
	$O__output .= $O__outputDatasetOriginal;

	return $O__output;
}

$O__output = O__createOutput();
?>
