<?php
echo ("
<html>
<head>
	<link rel='stylesheet' type='text/css' href='../CSS/Buttons.css'>
");
require '../2_Projects/FileOperations.php';
$con = mysql_connect($website,$userid,$pw);
if (!$con) {
	die('Could not connect: ' . mysql_error());
}

$curDB = "../CurrentlyOpenedDBName.tmp";
if(file_exists ($curDB)){
	$f = fopen($curDB, "r");
	$dbase   = trim(fgets($f)); // linked database name
	fclose($f);
	mysql_select_db($dbase, $con);
}else{
	$dbase = "None open"; // . " (Select one from File Operations button)";
}
echo ("</head>");

echo ("
	<body>
	<form action='ImportExport.php' method='post'>
	<p align='right'>
		<button class='btnImportExport' type='submit' name='import'>Import</button>");

if($dbase != "None open"){
	echo("
	<button class='btnImportExport' type='submit' name='export'>Export</button>
	<button class='btnDelete' type='submit' name='delete'>-</button>");
}
echo ("</p></form>");

if($dbase != "None open")$star = CurrentlyFetchingData($con);
set_button_to_start(); // init

if($_GET[started] == "yes" || $star){
	foreach( glob('*.lock') as $file )unlink($file);
	foreach( glob('cookies*') as $file )unlink($file);
	set_button_to_stop();
}

echo("
	<form action='AppendData.php?started={$started}' method='post'>
	<p align='right'>");
		if($dbase != "None open"){

			// Data fetch start button to not appear if no sources defined
			$res = mysql_query("SELECT COUNT(source_id) FROM Source",$con);
			while($row = mysql_fetch_array($res))$NumSrc = $row[0];

			// Data fetch start button to not appear if all sources are manual
			$AllManual = true;
			$res = mysql_query("SELECT input_method FROM Fetcher INNER JOIN Dynamic ON Dynamic.fetcher_id = Fetcher.fetcher_id",$con);
			while($row = mysql_fetch_array($res)){
				if($row[0] != "Manual")$AllManual = false;
			}

			if(($NumSrc > 0 && !$AllManual) || $MDF_fetching){
				echo("<button title = '{$titletext}' name='startButton' type='submit' style='	border-style:solid;border-width: 5px;border-color:{$colour}'>{$text}</button> ");
				echo("<input type='hidden' name='started' value='{$started}'/>");
			}
		}
echo("</p></form></body></html>");
exit;

function set_button_to_start(){
	global $started, $colour, $text, $titletext;
	$started = "yes";
	$colour = "green";
	$text = "Start";
	$titletext = "Start data fetching";
}

function set_button_to_stop(){
	global $started, $colour, $text, $titletext;
	$started = "no";
	$colour = "red";
	$text = "Stop";
	$titletext = "Stop data fetching";
}
?>
