<?php
@ini_set("display_errors","1");
@ini_set("display_startup_errors","1");

include("include/dbcommon.php");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 


$mainTable = postvalue('mainTable');
$fullMainTable = GetTableByShort($mainTable);
$pageType = postvalue('pageType');

if (!checkTableName($mainTable))
	exit(0);
include("include/".$mainTable."_variables.php");

$gSettings = new ProjectSettings($fullMainTable, $pageType);

$mainField = postvalue('mainField');
$linkFieldName = postvalue('linkField');

$strDataSourceTable = $gSettings->getOriginalTableName();
$strLoginTable = "senuc_login";
if ($strDataSourceTable != $strLoginTable)
{
	if(!isLogged()) { 
		return;	
	}
	if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Edit") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Add") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search")) { return;	}
}
else 
{
	$checkResult = true;
	if($mainField=="username")
		$checkResult=false;

	if($mainField=="senha")
		$checkResult=false;

	if($mainField=="email")
		$checkResult=false;

	if($checkResult)
	{
		if(!isLogged()) { return;}
		if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Edit") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Add") && !CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search")) { return;	}
	}
}
$autoCompleteFields = array();

if($strTableName == "senuc_aluno" && $mainField == "cpf"){
	$autoCompleteFields[] = array('masterF'=>"nome", 'lookupF'=>"nome");
	$autoCompleteFields[] = array('masterF'=>"endereco", 'lookupF'=>"email");
	$autoCompleteFields[] = array('masterF'=>"email", 'lookupF'=>"email");
	$autoCompleteFields[] = array('masterF'=>"rg", 'lookupF'=>"cpf");
	$autoCompleteFields[] = array('masterF'=>"telefone", 'lookupF'=>"telefone");
	$autoCompleteFields[] = array('masterF'=>"celular", 'lookupF'=>"celular");
	$autoCompleteFields[] = array('masterF'=>"operadora", 'lookupF'=>"operadora");
	$autoCompleteFields[] = array('masterF'=>"cidade", 'lookupF'=>"cidade");
	$autoCompleteFields[] = array('masterF'=>"estado", 'lookupF'=>"estado");
	$autoCompleteFields[] = array('masterF'=>"etnia", 'lookupF'=>"etnia");
	$autoCompleteFields[] = array('masterF'=>"nascimento", 'lookupF'=>"nascimento");
	$autoCompleteFields[] = array('masterF'=>"nota_cidadania", 'lookupF'=>"nota_cidadania");
	$autoCompleteFields[] = array('masterF'=>"renda", 'lookupF'=>"renda_total");
	$autoCompleteFields[] = array('masterF'=>"preparar_para", 'lookupF'=>"como_conheceu");
$lookupTable = "associados";	
}
if($strTableName == "senuc_coordenador" && $mainField == "cpf"){
	$autoCompleteFields[] = array('masterF'=>"nome", 'lookupF'=>"nome");
	$autoCompleteFields[] = array('masterF'=>"estado", 'lookupF'=>"estado");
	$autoCompleteFields[] = array('masterF'=>"cidade", 'lookupF'=>"cidade");
	$autoCompleteFields[] = array('masterF'=>"endereco", 'lookupF'=>"logradouro");
	$autoCompleteFields[] = array('masterF'=>"email", 'lookupF'=>"email");
	$autoCompleteFields[] = array('masterF'=>"telefone", 'lookupF'=>"telefone");
	$autoCompleteFields[] = array('masterF'=>"celular", 'lookupF'=>"celular");
	$autoCompleteFields[] = array('masterF'=>"operadora", 'lookupF'=>"operadora");
	$autoCompleteFields[] = array('masterF'=>"nascimento", 'lookupF'=>"nascimento");
	$autoCompleteFields[] = array('masterF'=>"etnia", 'lookupF'=>"etnia");
	$autoCompleteFields[] = array('masterF'=>"renda", 'lookupF'=>"renda");
$lookupTable = "associados";	
}
if($strTableName == "senuc_professores" && $mainField == "cpf"){
	$autoCompleteFields[] = array('masterF'=>"nome", 'lookupF'=>"nome");
	$autoCompleteFields[] = array('masterF'=>"email", 'lookupF'=>"email");
	$autoCompleteFields[] = array('masterF'=>"telefone", 'lookupF'=>"telefone");
	$autoCompleteFields[] = array('masterF'=>"celular", 'lookupF'=>"celular");
	$autoCompleteFields[] = array('masterF'=>"operadora", 'lookupF'=>"operadora");
	$autoCompleteFields[] = array('masterF'=>"nascimento", 'lookupF'=>"nascimento");
	$autoCompleteFields[] = array('masterF'=>"etnia", 'lookupF'=>"etnia");
	$autoCompleteFields[] = array('masterF'=>"renda", 'lookupF'=>"renda");
	$autoCompleteFields[] = array('masterF'=>"estado", 'lookupF'=>"estado");
	$autoCompleteFields[] = array('masterF'=>"cidade", 'lookupF'=>"cidade");
$lookupTable = "associados";	
}
if($strTableName == "senuc_descomplica" && $mainField == "cpf"){
	$autoCompleteFields[] = array('masterF'=>"id_aluno", 'lookupF'=>"nome");
$lookupTable = "senuc_aluno";	
}

$nLookupType = $gSettings->getLookupType($mainField);
$cipherer = new RunnerCipherer($nLookupType == LT_QUERY ? $lookupTable : $fullMainTable);
$linkFieldVal = postvalue('linkFieldVal');
$linkFieldVal = $cipherer->MakeDBValue($nLookupType == LT_QUERY ? $linkFieldName : $mainField, $linkFieldVal, "", "", true);
$strLookupWhere = GetLWWhere($mainField, $pageType, $strTableName);
if($nLookupType == LT_QUERY)
{
	$lookupSettings = new ProjectSettings($lookupTable, $pageType);
	$lookupQueryObj = $lookupSettings->getSQLQuery();
	$lookupQueryObj->ReplaceFieldsWithDummies($lookupSettings->getBinaryFieldsIndices());
	$strWhere = whereAdd($lookupQueryObj->m_where->toSql($lookupQueryObj), 
		GetFullFieldName($linkFieldName, $lookupTable, false).'='.$linkFieldVal);
	$strWhere = whereAdd($strWhere, $strLookupWhere);
	$strSQL = $lookupQueryObj->toSql(whereAdd($lookupQueryObj->m_where->toSql($lookupQueryObj), $strWhere));
}else{
	$strSQL = 'SELECT ';
	for($i=0; $i<count($autoCompleteFields); $i++){
		$strSQL .= AddFieldWrappers($autoCompleteFields[$i]['lookupF']).', ';
	}
	$strSQL = substr($strSQL, 0, strlen($strSQL)-2);
	
	$strSQL .= " FROM ".AddTableWrappers($lookupTable);
	$linkFieldName = $cipherer->GetLookupFieldName(AddFieldWrappers($linkFieldName), $strTableName, $mainField);
	$strWhere = $linkFieldName.'='.$linkFieldVal;
	$strWhere = whereAdd($strWhere, $strLookupWhere);
	$strSQL .= " WHERE ".$strWhere;
}

$rs = db_query($strSQL, $conn);	
if($nLookupType == LT_QUERY)
	$row =  $cipherer->DecryptFetchedArray($rs);
else 
	$row = db_fetch_array($rs);
db_close($conn);
if(is_null($row))
	$row = array($mainField=>'');
	
echo "<textarea>".htmlspecialchars(my_json_encode(array('success'=>true, 'data'=>$row)))."</textarea>";
?>