<?php

if(!isset($_SESSION)) session_start();
include_once("config.php");
include("loginCheckPoint.php");
include_once("authorizations.php");
require_once("demosettings.php");
//include_once($_SERVER['DOCUMENT_ROOT']. "/iventitymatcherhtm/bsw/utils.php");


$userdetails = fetchUserDetails($_SESSION['username']);
// global $AUTH;
$AUTH = getGroupsAuth();

$userAuth = $AUTH["'" . $userdetails['Group_ID']. "'"]['permissions'];
	
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>IntuScan Station</title>
    <link class="include" rel="stylesheet" type="text/css" href="dist/jquery.jqplot.min.css" />
    <link href="style.css" rel="stylesheet" type="text/css" />
    <!--[if lt IE 9]><script language="javascript" type="text/javascript" src="dist/excanvas.js"></script><![endif]-->
    <script class="include" type="text/javascript" src="dist/jquery.min.js"></script>
    <script class="include" type="text/javascript" src="dist/jquery.jqplot.min.js"></script>
    <script class="include" type="text/javascript" src="dist/plugins/jqplot.canvasTextRenderer.min.js"></script>
    <script class="include" type="text/javascript" src="dist/plugins/jqplot.canvasAxisLabelRenderer.min.js"></script>
    <script type="text/javascript" src="dist/plugins/jqplot.dateAxisRenderer.min.js"></script>
    <script type="text/javascript" src="dist/plugins/jqplot.highlighter.min.js"></script>
    <script type="text/javascript" src="dist/plugins/jqplot.enhancedLegendRenderer.min.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" src="dist/plugins/jqplot.cursor.min.js"></script>	
    <style>
        .chart { height:400px; width:700px; }
    </style>
</head>
 


<script type="text/javascript">

//function drawChart() {
//	  var slice_1 = ['Person', 247];
//	  var slice_2 = ['Organization', 128];
//	  var slice_3 = ['Event', 35];
//	  var slice_4 = ['Date', 47];
//	  var slice_5 = ['Place', 120];
//	  var slice_6 = ['Other', 10];
//	  var series = [slice_1, slice_2, slice_3, slice_4, slice_5, slice_6];
//	  var data = [series];
	 
//	  var options = {
//	    title: 'General',
//	    seriesDefaults: {
//	      renderer: jQuery.jqplot.PieRenderer,
//	      rendererOptions: {	         
//	          showDataLabels: true
//	        }
//	    },		                  
//	    legend: { show:true, location: 'e' }
//	  };	   
//	  $.jqplot('chartDivId', data, options);
//	}

</script>

<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>
      <?php
      	include('general-toolbar.php'); 
      ?>
      <div class="clr"></div>
    </div>
    <div class="clr"></div>
  </div>
  <div class="content">    		
  		<center><select name="GraphSelect" id="GraphSelect" onchange="changeGraph()">  			
			<option value="general" selected="selected">General entities</option>
			<option value="languages">Languages</option>	
			<option value="format">File Format</option>
			<option value="priority">priority</option>
			<option value="PoliticalOrientation">Political Orientation</option>
			<option value="ReligiousAffiliation">Religious Affiliation</option>
		</select>   		
	
      <div id="chartDivId" style="margin-top:20px; margin-left:20px; width:600px; height:300px;"></div></center>       
      <div class="sidebar">
        
      </div>
      <div class="clr"></div>
  </div>
  	<div class="footer">
	
  </div>
</div>

</body>
<script type="text/javascript">
	function changeGraph(){
		
		var div = document.getElementById('chartDivId');
		while(div.hasChildNodes()) {
		    div.removeChild(div.firstChild);
		}
		drawGraph();
		//if (document.getElementById("GraphSelect").value === "languages")
		//	drawLanguagesChart();
		//else if (document.getElementById("GraphSelect").value === "general")
		//	drawGeneralChart();
		//else if (document.getElementById("GraphSelect").value === "format")
		//	drawFormatChart();
		//else if (document.getElementById("GraphSelect").value === "priority")
		//	drawPriorityChart();
		//else if (document.getElementById("GraphSelect").value === "PoliticalOrientation")
		//	drawPoliticalOrientationChart();
		//else if (document.getElementById("GraphSelect").value === "ReligiousAffiliation")
		//	drawReligiousAffiliationChart();
		//else
		//	drawGraph();
	}	
</script>
</html>
<script type="text/javascript">
function drawGraph(){
  var line1=[['23-May-08', 578.55], ['20-Jun-08', 566.5], ['25-Jul-08', 480.88], ['22-Aug-08', 509.84],
      ['26-Sep-08', 454.13], ['24-Oct-08', 379.75], ['21-Nov-08', 303], ['26-Dec-08', 308.56],
      ['23-Jan-09', 299.14], ['20-Feb-09', 346.51], ['20-Mar-09', 325.99], ['24-Apr-09', 386.15]];
  var plot2 = $.jqplot('chart2', [line1], {
    title:'Mouse Cursor Tracking',
    axes:{
      xaxis:{
        renderer:$.jqplot.DateAxisRenderer,
          tickOptions:{
            formatString:'%b&nbsp;%#d'
          }
      },
      yaxis:{
        tickOptions:{
          formatString:'$%.2f'
        }
      }
    },
    highlighter: {
      show: false
    },
    cursor: {
      show: true,
      tooltipLocation:'sw'
    }
  });
});
</script>
<?php
	///////////////
	//General table
	///////////////
	echo "<script type=\"text/javascript\">";
	$tables = array("person_object", "organizational_identity", "date", "place_object");
	$res_arr = array();	
	foreach ($tables as $t){
		if ($userdetails['Group_ID'] == 0) 
			$sql = "SELECT SUM(occurrences) as s FROM $t";
		else{
			$username = $userdetails['Username'];
			$sql = "SELECT SUM(occurrences) as s FROM $t WHERE username='$username'";						
		}
		$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
		$res = $mysqli->query($sql);
		if(!empty($res))
			$row = $res->fetch_object();		
		$res_arr[$t] = $row->s;		
	}	 		
		
	echo "function drawGeneralChart() {";
	$i = 1;
	foreach ($res_arr as $k => $v){
		echo "var slice_$i = ['$k ($v)', $v];";
		$i++;			
	}
	$j = 1;
	echo "var series = [";
	while ($j < $i){
		if ($j == 1)
			echo "slice_$j";
		else 
			echo ",slice_$j";
		$j++;
	}
	echo "];";	 	
	echo "var data = [series];	 
	  var options = {
	    title: 'General',
	    seriesDefaults: {
	      renderer: jQuery.jqplot.PieRenderer,
	      rendererOptions: {	         
	          showDataLabels: true
	        }
	    },		                  
	    legend: { show:true, location: 'e' }
	  };	   
	  $.jqplot('chartDivId', data, options);
	}";	
	///////////////////
	// file languages
	///////////////////
	$languages = array("Arabic", "English", "French", "Indonesian", "Spanish", "Urdu", "Unkown");
	unset($res_arr);	
	
	foreach ($languages as $l){
		if ($userdetails['Group_ID'] == 0) 
			$sql = "SELECT COUNT(languages) AS l FROM files WHERE languages=LOWER('$l')";
		else{
			$username = $userdetails['Username'];
			$sql = "SELECT COUNT(languages) AS l FROM files WHERE languages=LOWER('$l') WHERE username='$username'";								
		}
		//$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
		$res = $mysqli->query($sql);
		if(!empty($res))
			$row = $res->fetch_object();		
		$res_arr[$l] = $row->l;		
	}	 	
	echo "function drawLanguagesChart() {";
	$i = 1;
	foreach ($res_arr as $k => $v){
		echo "var slice_$i = ['$k ($v)', $v];";
		$i++;			
	}
	$j = 1;
	echo "var series = [";
	while ($j < $i){
		if ($j == 1)
			echo "slice_$j";
		else 
			echo ",slice_$j";
		$j++;
	}
	echo "];";	 	
	echo "var data = [series];	 
	  var options = {
	    title: 'Languages',
	    seriesDefaults: {
	      renderer: jQuery.jqplot.PieRenderer,
	      rendererOptions: {	         
	          showDataLabels: true
	        }
	    },		                  
	    legend: { show:true, location: 'e' }
	  };	   
	  $.jqplot('chartDivId', data, options);
	}";	
	
	///////////////////
	// file format
	///////////////////
	$format = array("txt", "pdf", "doc", "docx");
	unset($res_arr);	
	
	foreach ($format as $f){
		if ($userdetails['Group_ID'] == 0) 
			$sql = "SELECT COUNT(file_format) AS f FROM files WHERE file_format=LOWER('$f')";
		else{
			$username = $userdetails['Username'];
			$sql = "SELECT COUNT(file_format) AS f FROM files WHERE file_format=LOWER('$f') WHERE username='$username'";								
		}
		//$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
		$res = $mysqli->query($sql);
		if(!empty($res))
			$row = $res->fetch_object();		
		$res_arr[$f] = $row->f;		
	}	 	
	echo "function drawFormatChart() {";
	$i = 1;
	foreach ($res_arr as $k => $v){
		echo "var slice_$i = ['$k ($v)', $v];";
		$i++;			
	}
	$j = 1;
	echo "var series = [";
	while ($j < $i){
		if ($j == 1)
			echo "slice_$j";
		else 
			echo ",slice_$j";
		$j++;
	}
	echo "];";	 	
	echo "var data = [series];	 
	  var options = {
	    title: 'File format',
	    seriesDefaults: {
	      renderer: jQuery.jqplot.PieRenderer,
	      rendererOptions: {	         
	          showDataLabels: true
	        }
	    },		                  
	    legend: { show:true, location: 'e' }
	  };	   
	  $.jqplot('chartDivId', data, options);
	}";
	
	
	///////////////////
	// Priority
	///////////////////
	$priority = array("High", "Medium", "Low", "None");
	unset($res_arr);	
	
	foreach ($priority as $p){
		if ($p === "None"){
			$sql = "SELECT COUNT(id) AS p FROM files WHERE threat_rating IS NULL";
		}else {
			if ($userdetails['Group_ID'] == 0) 
				$sql = "SELECT COUNT(threat_rating) AS p FROM files WHERE threat_rating=LOWER('$p')";
			else{
				$username = $userdetails['Username'];
				$sql = "SELECT COUNT(threat_rating) AS p FROM files WHERE threat_rating=LOWER('$p') WHERE username='$username'";								
			}
		}		
		//$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
		$res = $mysqli->query($sql);
		if(!empty($res))
			$row = $res->fetch_object();		
		$res_arr[$p] = $row->p;		
	}	 	
	echo "function drawPriorityChart() {";
	$i = 1;
	foreach ($res_arr as $k => $v){
		echo "var slice_$i = ['$k ($v)', $v];";
		$i++;			
	}
	$j = 1;
	echo "var series = [";
	while ($j < $i){
		if ($j == 1)
			echo "slice_$j";
		else 
			echo ",slice_$j";
		$j++;
	}
	echo "];";	 	
	echo "var data = [series];	 
	  var options = {
	    title: 'Priority',
	    seriesDefaults: {
	      renderer: jQuery.jqplot.PieRenderer,
	      rendererOptions: {	         
	          showDataLabels: true
	        }
	    },		                  
	    legend: { show:true, location: 'e' }
	  };	   
	  $.jqplot('chartDivId', data, options);
	}";
	
	
	///////////////////
	// Political Orientation
	///////////////////
	
	$sql = "SELECT DISTINCT ideological_stream AS i FROM files WHERE ideological_stream IS NOT NULL";
	$res = $mysqli->query($sql);
	$i = 0;
	$PoliticalOrientation = array();
	if(!empty($res)){
		while ($row = $res->fetch_object())				
			$PoliticalOrientation[$i++] = $row->i;		
	}
	
	$PoliticalOrientation[$i] = "None";	
		
	unset($res_arr);	
	
	foreach ($PoliticalOrientation as $p){
		if ($p === "None"){
			$sql = "SELECT COUNT(id) AS p FROM files WHERE ideological_stream IS NULL";
		}else {
			if ($userdetails['Group_ID'] == 0) 
				$sql = "SELECT COUNT(ideological_stream) AS p FROM files WHERE ideological_stream=LOWER('$p')";
			else{
				$username = $userdetails['Username'];
				$sql = "SELECT COUNT(ideological_stream) AS p FROM files WHERE ideological_stream=LOWER('$p') WHERE username='$username'";								
			}
		}		
		//$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
		$res = $mysqli->query($sql);
		if(!empty($res))
			$row = $res->fetch_object();		
		$res_arr[$p] = $row->p;		
	}	 	
	echo "function drawPoliticalOrientationChart() {";
	$i = 1;
	foreach ($res_arr as $k => $v){
		echo "var slice_$i = ['$k ($v)', $v];";
		$i++;			
	}
	$j = 1;
	echo "var series = [";
	while ($j < $i){
		if ($j == 1)
			echo "slice_$j";
		else 
			echo ",slice_$j";
		$j++;
	}
	echo "];";	 	
	echo "var data = [series];	 
	  var options = {
	    title: 'Political Orientation',
	    seriesDefaults: {
	      renderer: jQuery.jqplot.PieRenderer,
	      rendererOptions: {	         
	          showDataLabels: true
	        }
	    },		                  
	    legend: { show:true, location: 'e' }
	  };	   
	  $.jqplot('chartDivId', data, options);
	}";
	
	///////////////////
	// Religious Affiliation
	///////////////////
	
	$sql = "SELECT DISTINCT islamic_sect AS i FROM files WHERE islamic_sect IS NOT NULL";
	$res = $mysqli->query($sql);
	$i = 0;
	$PoliticalOrientation = array();
	if(!empty($res)){
		while ($row = $res->fetch_object())				
			$PoliticalOrientation[$i++] = $row->i;		
	}
	
	$PoliticalOrientation[$i] = "None";	
		
	unset($res_arr);	
	
	foreach ($PoliticalOrientation as $p){
		if ($p === "None"){
			$sql = "SELECT COUNT(id) AS p FROM files WHERE islamic_sect IS NULL";
		}else {
			if ($userdetails['Group_ID'] == 0) 
				$sql = "SELECT COUNT(islamic_sect) AS p FROM files WHERE islamic_sect=LOWER('$p')";
			else{
				$username = $userdetails['Username'];
				$sql = "SELECT COUNT(islamic_sect) AS p FROM files WHERE islamic_sect=LOWER('$p') WHERE username='$username'";								
			}
		}		
		//$mysqli = new mysqli($databaseHost, $databaseUser, $databasePass, $databaseName);
		$res = $mysqli->query($sql);
		if(!empty($res))
			$row = $res->fetch_object();		
		$res_arr[$p] = $row->p;		
	}	 	
	echo "function drawReligiousAffiliationChart() {";
	$i = 1;
	foreach ($res_arr as $k => $v){
		echo "var slice_$i = ['$k ($v)', $v];";
		$i++;			
	}
	$j = 1;
	echo "var series = [";
	while ($j < $i){
		if ($j == 1)
			echo "slice_$j";
		else 
			echo ",slice_$j";
		$j++;
	}
	echo "];";	 	
	echo "var data = [series];	 
	  var options = {
	    title: 'Religious Affiliation',
	    seriesDefaults: {
	      renderer: jQuery.jqplot.PieRenderer,
	      rendererOptions: {	         
	          showDataLabels: true
	        }
	    },		                  
	    legend: { show:true, location: 'e' }
	  };	   
	  $.jqplot('chartDivId', data, options);
	}";
	
	echo "changeGraph();";
	echo "</script>";
?>


</html>