<?php
session_start();

include_once("config.php");
include("sem/sparql.php");
include("sem/queries.php");
include_once("sem/sql.php");
include_once("bsw/utils.php");

$searchtext = $_REQUEST["searchtext"];
$sourcefile = $_REQUEST["sourcefile"];
$ontology = $_REQUEST["ontology"];
$concept = $_REQUEST["concept"];
$lexicon = $_REQUEST["lexicon"];
$casesense = $_REQUEST["casesense"];
$wholeword = $_REQUEST["wholeword"];
$limit = $_REQUEST["limit"];
$page = $_REQUEST["page"];
$rperpage = 2000;//$_REQUEST["rperpage"];
$results = $_REQUEST["results"];
$wlSelected = $_REQUEST["wlSelected"];
$analyze = $_REQUEST["analyze"];
$resultType = $_REQUEST["resultType"];
$analyzeFile = $_REQUEST["analyzeFile"];
$returnRawResults = $_REQUEST["returnRawResults"];

if ($analyzeFile == "true"){
    $filePath = __DIR__.DIRECTORY_SEPARATOR."upload_file".DIRECTORY_SEPARATOR  . $sourcefile;
	$lines = file($filePath, FILE_IGNORE_NEW_LINES);	
	foreach ($lines as $value) {
	    $analysis = getAnalysis($value,$ivEntityMatcherService);
		printAnalysis($analysis,$value);		
	}		
	
	exit();
}


$firstInputId = null;

if ($analyze == "yes"){
    $analysis = getAnalysis($searchtext,$ivEntityMatcherService, $resultType);
	printAnalysis($analysis,$searchtext);
	if (!empty($errormsg)){
		die($errormsg);
        }
        if(!empty($error)){
           echo "<b>" . $error . "</b><br>";
        }
	exit();
}

$reportFile = "upload_file".DIRECTORY_SEPARATOR."search_result.txt";
$fh = fopen($reportFile, 'w');
$reportOutput = "";	

if (empty($searchtext))
    $actionElements = searchListOfEntity(__DIR__.DIRECTORY_SEPARATOR."upload_file" .DIRECTORY_SEPARATOR. $sourcefile,$arr, "sourceFile",$wlSelected,$ivEntityMatcherService);
else	
    $actionElements = searchEntity($searchtext,$arr, "person",$wlSelected,$ivEntityMatcherService);

if (!empty($actionElements))
{	
	$i = 1;
	foreach ($actionElements as $input){
		//count number of matches for eatch input person
		$total = 0;
		foreach ($input['Rules'] as $rule){
			$total += sizeof($rule);
		}
		$name = str_replace("'","\\'",$input['person_input_name']);		
		echo "<b><a href=\"javascript:toggleInputName('" . $name . "',$i)\">" . $input['person_input_name'] . " ($total matches)" . "</a></b><br><br>";		
		$inputDivIndex = $i;
		echo "<div id=div". $i++ . " style=\"display:none\"></div>";		
		echo "<div id=div$i style=\"display:none\">";
			echo "<table class=\"\">";
			foreach ($input['Rules'] as $ruleKey => $rule){
				$i++;
				echo "<tr>";
					echo "<td></td>";
					echo "<td>";					
						echo "<b>". "<a href=\"javascript:toggle(". $i.  ")\"  title=\"" . getRuleToolTip($ruleKey) . "\">" . $ruleKey. " (" .sizeof($rule) ." matches)" . "</a></b>";
						echo "<div id=div". $i++. " style=\"display:none\">";
							echo "<table class=\"\">";	
								foreach ($rule as $p){
									echo "<tr>";
										echo "<td valign=\"top\">";
											echo $p['person_id'];
										echo "</td>";
										echo "<td valign=\"top\">";
											$name = str_replace("'","''",$p['person_name']);
											echo "<a href=\"javascript:analyzeName('" . $name . "',$i, $inputDivIndex)\">" . $p['person_name'] . '</a>';																				
											//echo "<a href=\"javascript:analayzeName($i)\">" . $p['person_name'] . '</a>';
										echo "</td>";
										echo "<td valign=\"top\">";
											echo "<div id=div". $i++. " style=\"display:none\"></div>";																			
										echo "</td>";
									echo "</tr>";
								}
							echo "</table>";
						echo "</div>";
					echo "</td>";
				echo "</tr>";	
			}
			echo "</table>";
		echo "</div>";
	}
}else{	
	$name = str_replace("'","\\'",$searchtext);		
	echo "<b><a href=\"javascript:toggleInputName('" . $name . "',1)\">" . $searchtext .  " (0 matches)"  . "</a></b><br><br>";		
	echo "<div id=div1 style=\"display:none\"></div>";	
	echo "<div id=div2 style=\"display:none\">";
}
	
?>