<?php

class IndexPageGenerator {

	// Name of the directory with test results
	var  $testDirectoryName = '../tests/';

	// Constructor
	function IndexPageGenerator() {

	}

	function Generate() {
		echo '<html>' . $this->HtmlHead() . '<body><h1>Event reconstruction quality monitoring</h1>';
		
		$days = $this->getDirFromDir($this->testDirectoryName);
	
		// Loop over days
		foreach($days as $day) {
			echo '<b><a href="javascript:unhide(\''. $day . '\');">' . $day . '</a></b>' . '<br>';
			echo '<div id="' . $day . '" class="hidden">';
			$tests = $this->getDirFromDir($this->testDirectoryName . $day);
			// Loop over tests
			foreach($tests as $test) {
				echo $test . ' : ';
				$testDir = $this->testDirectoryName . $day . '/' . $test . '/';
				// Search for HTML files in current test directory
				$htmlFiles = $this->getFilesFromDir($testDir, 'html');
            // Search for ROOT files in current test directory
            $rootFiles = $this->getFilesFromDir($testDir, 'root');
				
				if (count($htmlFiles) == 0 && count($rootFiles) == 0) {
					echo '<b><small>No results found.</small></b>';
				} else {
					// Loop over all HTML files in current test directory
					foreach($htmlFiles as $htmlFile) {
						$link = $testDir . $htmlFile;
						echo '<small><a href="'. $link . '">' . $htmlFile . '</a></small> ';
					}
               // Loop over all ROOT files in current test directory
					foreach($rootFiles as $rootFile) {
						$link = 'rootio.php?files=' . $testDir . $rootFile;
						echo '<small><a href="'. $link . '">' . $rootFile . '</a></small> ';
					}
				}
				echo '<br>';
			}
			echo '</div>';
  		}
  		
  		echo '</body></html>';
	}

	function getDirFromDir($dir) {
		$files = array();
		if ($handle = opendir($dir)) {
			while (false !== ($file = readdir($handle))) {
				if ($file != "." && $file != "..") {
					if(is_dir($dir.'/'.$file)) {
						$files[] = $file;
					}
				}
			}
			closedir($handle);
		}
		return $files;
	}
	
	function getFilesFromDir($dir, $fileType) {
		$files = array();
		if ($handle = opendir($dir)) {
			while (false !== ($file = readdir($handle))) {
				if(pathinfo($file, PATHINFO_EXTENSION) == $fileType) {
					$files[] = $file;
				}
			}
			closedir($handle);
		}
		return $files;
	}
	
	function HtmlHead() {
		$str = '<head>';
		$str .= '<link rel="stylesheet" type="text/css" href="mystyle.css" />';
	    $str .= '<script type="text/javascript">';
	    $str .= 'function unhide(divID) {';
	    $str .= 'var item = document.getElementById(divID);';
	    $str .= 'if (item) { item.className=(item.className==\'hidden\')?\'unhidden\':\'hidden\'; }}';	    
	    $str .= '</script>';
	    $str .= '</head>';
	    return $str;
	}
}
?>