<?php

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

include("loginCheckPoint.php");
include_once("authorizations.php");
require_once("demosettings.php");
include_once 'write_log.php';
include("parseUtils.php");
//require_once("sparql.php");
$mysqli = sqlCreateConnection("searchPerson");

//include_once 'searchWord.php';
if ($_REQUEST["concept"] == "org"){	
	
	$name = $mysqli->real_escape_string($_REQUEST["name"]);
	$sql = "SELECT DISTINCT name FROM organizational_identity WHERE NAME LIKE LOWER('%$name%')";
	$res = $mysqli->query($sql);
	
	if (!empty($res)){					
		while ($row = $res->fetch_object()) {
			$name = $row->name;
			$q = "SELECT COUNT(id) as id FROM organizational_identity WHERE name='$name'";
			$rs = $mysqli->query($q);
			if (!empty($rs)){
				$r = $rs->fetch_object(); 
				$response["rows"][] = array("id" => $r->id, "name" => $row->name);
			}
		}	
	}			
}else{
	$searchPerson = $_REQUEST["searchPerson"];
	$p = getPersonVariations($mysqli, $searchPerson);
	
	foreach ($p  as $k => $v){
		$response["rows"][] = array("id" => $v, "name" => $k);
	}				
}
sqlClose($mysqli, 'searchPerson');
ob_clean();
echo json_encode($response);
