<?php

if(!isset($_SESSION)) session_start();

include_once("config.php");
include_once("sqlTable.php");
include("loginCheckPoint.php");
include_once("authorizations.php");
require_once("demosettings.php");
include_once("C:/xampp/htdocs/iventitymatcherhtm/bsw/utils.php");

//exec("taskkill /fi \"WindowTitle eq joseki2*\"");
//die("here");
$userdetails = fetchUserDetails($_SESSION['username']);

//global $AUTH;
$AUTH = getGroupsAuth();

$userAuth = $AUTH[$userdetails['Group_ID']]['permissions'];

$canAnalyzeFolder = $userAuth['canAnalyzeFolder'];

$sync = $_REQUEST["sync"];
$clean = $_REQUEST["clean"];
if ($sync == 1)
{
	$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
	if ($userdetails['Group_ID'] == 0)
		$res = $mysqli->query("SELECT * FROM person_object");
	else
		$res = $mysqli->query("SELECT * FROM person_object WHERE username='$userdetails[Username]'");
	$arr_obj = array();
	$r = $res->fetch_object();
	if (!empty($r)){
		$i = 0;
		while ($res_obj = $res->fetch_object())
		{
			$arr_obj[$i++] = $res_obj;
		}

		if (is_dir("c:/tmp"))
			$filepath = "c:/tmp/";
		else
			$filepath = "c:/temp/";
		if ($userdetails['Group_ID'] == 0)
			$filename = "DOCEX Entities DB.tsv";
		else
			$filename = "DOCEX Entities DB-" . $userdetails[Username] .  ".tsv";

		unlink($filepath.$filename);
		foreach ($arr_obj as $obj)
		{
			file_put_contents($filepath.$filename, $obj->id , FILE_APPEND);
			file_put_contents($filepath.$filename, "\t" . $obj->name . "\n", FILE_APPEND);
		}
		if ($userdetails['Group_ID'] == 0)
			deleteWatchList("DOCEX Entities DB","127.0.0.1");
		else
			deleteWatchList("DOCEX Entities DB-" . $userdetails[Username],"127.0.0.1");
		loadList($filepath,$filename,"127.0.0.1");
		//file_put_contents("c:/tmp/DOCEX Entities DB.xml",convertTsvToXml($filepath,$filename));
		sqlClose($mysqli, 'ExtractFinance');
	}
}elseif ($clean == 2){
	if ($userdetails['Group_ID'] == 0){
		//echo "allow deleting all data for administraor";
		createDB($mysqli);
	}
	else
		cleanDBSpecificUser($mysqli, $userdetails[Username]);
	/*$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
	$sql="CREATE TABLE Test(FirstName CHAR(30),LastName CHAR(30),Age INT)";
	$res = $mysqli->query($sql);
	$res = $mysqli->query("select Username from usercake_users");
	while ($r = $res->fetch_object()){
		deleteWatchList("DOCEX Entities DB-".$r->Username,"127.0.0.1");
	}
	deleteWatchList("DOCEX Entities DB","127.0.0.1");
	$res = $mysqli->query("DROP TABLE files");
	$res = $mysqli->query("DROP TABLE person_object");
	$res = $mysqli->query("DROP TABLE date");
	$res = $mysqli->query("DROP TABLE misc_object");
	$res = $mysqli->query("DROP TABLE organizational_identity");
	$res = $mysqli->query("DROP TABLE place_object");

	deleteWatchList("DOCEX Entities DB","127.0.0.1");
	// cmd : taskkill /IM java.exe
	$res= exec('tasklist /v /fo csv | findstr /i "java"');
	if (empty($res)){

	}else{
		$arr = explode(',', $res);
		$pid = $arr[1];
		$res = exec("taskkill /PID " . $pid);
	}*/

}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>IntuScan Station</title>
<meta http-equiv="Content-Type" content="text/html; Charset=UTF-8"  />
<link rel="stylesheet" href="jquery-ui/development-bundle/themes/base/jquery.ui.all.css">
<script src="jquery/jquery.js" type="text/javascript"></script>
<script src="jquery/jquery-ui.custom.js" type="text/javascript"></script>
<script src="jquery/jquery.cookie.js" type="text/javascript"></script>
<script type="text/javascript" src="jquery-ui/js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="jquery-ui/js/jquery-ui-1.8.17.custom.min.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.ui.tabs.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.effects.core.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.effects.blind.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.ui.core.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.ui.widget.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.ui.position.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.ui.autocomplete.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.ui.mouse.js"></script>
<script src="jquery-ui/development-bundle/ui/jquery.ui.sortable.js"></script>
<script type="text/javascript" src="dist/plugins/jqplot.pieRenderer.min.js"></script>
<script type="text/javascript" src="dist/plugins/jqplot.donutRenderer.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){
	  var data = [
	    ['Heavy Industry', 12],['Retail', 9], ['Light Industry', 14],
	    ['Out of home', 16],['Commuting', 7], ['Orientation', 9]
	  ];
	  var plot1 = jQuery.jqplot ('chart1', [data],
	    {
	      seriesDefaults: {
	        // Make this a pie chart.
	        renderer: jQuery.jqplot.PieRenderer,
	        rendererOptions: {
	          // Put data labels on the pie slices.
	          // By default, labels show the percentage of the slice.
	          showDataLabels: true
	        }
	      },
	      legend: { show:true, location: 'e' }
	    }
	  );
	});

function submitform()
{
	document.getElementById("folderPath").value = $("#folder").text();	
  	document.form2.submit();
}

function gotolink()
{
	document.form3.submit();
}

function cleanAll()
{
	if (confirm('Are you sure you want to delete all data?')){
		window.location.href = 'home.php?clean=2';
	}
}

function changeOption()
{	
	var myselect = document.getElementById("intuscanPort");
	switch (myselect.options[myselect.selectedIndex].value){
		case "6000": document.getElementById("folder").innerHTML = "c:\\tmp\\intuview\\input_1"
					break;
		case "6001": document.getElementById("folder").innerHTML = "c:\\tmp\\intuview\\input_2"
					break;	
		case "6002": document.getElementById("folder").innerHTML = "c:\\tmp\\intuview\\input_3"
					break;	
		case "6003": document.getElementById("folder").innerHTML = "c:\\tmp\\intuview\\input_4"
					break;			
	}
}

</script>
<link href="style.css" rel="stylesheet" type="text/css" />

</head>

<body>
<div class="main">
 <div class="top-banner">
	 <a href="home.php"><img src="images/intuscan-img.png" style="border:0;"/></a>
	 <p style="padding:0 10px 10px 10px; float:right;"><b>version 1</b></p>
	 <p style="padding:0 10px 10px 10px; float:right;">Hi, <?php echo $_SESSION["username"] ?></p>
	 <p style="padding:0 10px 10px 10px; float:right;"><a style="color:red;" href="logoff.php">logoff</a></p>
 </div>
  <div class="header">

    <div class="header_block">

      <div class="clr"></div>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  <div class="content">
      <div class="mainbar" id="mainbar">		
	    <div class="article">
			<form action="ExtractFinancialWS.php" method="post" name="form2" id="form2">				
				<label for="name"><b><h3>Analyse Folder</h3></b></label>
				<br>	          	
	          	<b>Input file: </b>	          		          
	          	<br><br>
	          	<label id="folder">c:\tmp\intuview\input_1</label>
	          	<br><br>
	          	<b>Output file: </b>
	          	<br><br>
	          	<input id="outputFile" name="outputFile" type="input" class="text" size="80" value="c:\tmp\intuview\input_1<?php echo date('Y-m-d_H-i-s')?>.tsv"/>
	          	<br>
	          	<input type="checkbox" name="Appendfile" value="true"> Append file if exist
	          	<br><br>
	          	<b>Intuscan Server:</b>
	          	<br><br>
	          	<select id="intuscanPort" name="intuscanPort" onchange="changeOption()">
	          	<option value="6000">Service</option>
	          	<option value="6001">Instance 1</option>
	          	<option value="6002">Instance 2</option>
	          	<option value="6003">Instance 3</option>
	          	</select>
	          	<br><br><br>
           		<input type="button" value="Analyse Folder" name="imageField" id="imageField" src="images/submit.gif" class="send" OnClick="javascript: submitform()"/>
           		<input type="hidden" id="folderPath" name="folderPath" value="c:\tmp\intuview\input_1\">
	        </form>
	    </div>


  </div>
<?php
	if ($clean == 1){
		echo "<script type=\"text/javascript\">";
			echo "cleanAll();";
		echo "</script>";
	}
?>

</div>
<form  action="listFilesPage.php" method="post" name="form3">
	<input type="hidden" name="refresh" value="true"></input>
</form>
</body>

</html>