<?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"); 

include("include/professor_por_nucleo_variables.php");

$mode = postvalue("mode");

if(!isLogged())
{ 
	return;
}
if(!CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
	return;
}

$cipherer = new RunnerCipherer($strTableName);


include('include/xtempl.php');
$xt = new Xtempl();

$layout = new TLayout("detailspreview","FusionOrange","MobileOrange");
$layout->blocks["bare"] = array();
$layout->containers["dcount"] = array();

$layout->containers["dcount"][] = array("name"=>"detailspreviewheader","block"=>"","substyle"=>1);


$layout->containers["dcount"][] = array("name"=>"detailspreviewdetailsfount","block"=>"","substyle"=>1);


$layout->containers["dcount"][] = array("name"=>"detailspreviewdispfirst","block"=>"display_first","substyle"=>1);


$layout->skins["dcount"] = "empty";
$layout->blocks["bare"][] = "dcount";
$layout->containers["detailspreviewgrid"] = array();

$layout->containers["detailspreviewgrid"][] = array("name"=>"detailspreviewfields","block"=>"details_data","substyle"=>1);


$layout->skins["detailspreviewgrid"] = "grid";
$layout->blocks["bare"][] = "detailspreviewgrid";$page_layouts["professor_por_nucleo_detailspreview"] = $layout;


$recordsCounter = 0;

//	process masterkey value
$mastertable = postvalue("mastertable");
$masterKeys = my_json_decode(postvalue("masterKeys"));
if($mastertable != "")
{
	$_SESSION[$strTableName."_mastertable"]=$mastertable;
//	copy keys to session
	$i = 1;
	if(is_array($masterKeys) && count($masterKeys) > 0)
	{
		while(array_key_exists ("masterkey".$i, $masterKeys))
		{
			$_SESSION[$strTableName."_masterkey".$i] = $masterKeys["masterkey".$i];
			$i++;
		}
	}
	if(isset($_SESSION[$strTableName."_masterkey".$i]))
		unset($_SESSION[$strTableName."_masterkey".$i]);
}
else
	$mastertable = $_SESSION[$strTableName."_mastertable"];

//$strSQL = $gstrSQL;

if($mastertable == "nucleo")
{
	$where = "";
		$where .= GetFullFieldName("idnucleo", $strTableName, false)."=".make_db_value("idnucleo",$_SESSION[$strTableName."_masterkey1"]);
}

$str = SecuritySQL("Search");
if(strlen($str))
	$where.=" and ".$str;
$strSQL = $gQuery->gSQLWhere($where);

$strSQL.=" ".$gstrOrderBy;

$rowcount = $gQuery->gSQLRowCount($where);

$xt->assign("row_count",$rowcount);
if($rowcount) {
	$xt->assign("details_data",true);
	$rs = db_query($strSQL,$conn);

	$display_count = 10;
	if($mode == "inline")
		$display_count*=2;
	if($rowcount>$display_count+2)
	{
		$xt->assign("display_first",true);
		$xt->assign("display_count",$display_count);
	}
	else
		$display_count = $rowcount;

	$rowinfo = array();
	
	$data = $cipherer->DecryptFetchedArray($rs);
	require_once getabspath('classes/controls/ViewControlsContainer.php');
	$pSet = new ProjectSettings($strTableName, PAGE_LIST);
	$viewContainer = new ViewControlsContainer($pSet, PAGE_LIST);
	while($data && $recordsCounter<$display_count) {
		$recordsCounter++;
		$row = array();
		$keylink = "";
		$keylink.="&key1=".htmlspecialchars(rawurlencode(@$data["id"]));

	
	//	nome - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("nome", $data, $keylink);
			$row["nome_value"] = $value;
	//	email - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("email", $data, $keylink);
			$row["email_value"] = $value;
	//	cpf - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("cpf", $data, $keylink);
			$row["cpf_value"] = $value;
	//	telefone - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("telefone", $data, $keylink);
			$row["telefone_value"] = $value;
	//	celular - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("celular", $data, $keylink);
			$row["celular_value"] = $value;
	//	operadora - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("operadora", $data, $keylink);
			$row["operadora_value"] = $value;
	//	bairro - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("bairro", $data, $keylink);
			$row["bairro_value"] = $value;
	//	regiao - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("regiao", $data, $keylink);
			$row["regiao_value"] = $value;
	//	cidade - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("cidade", $data, $keylink);
			$row["cidade_value"] = $value;
	//	estado - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("estado", $data, $keylink);
			$row["estado_value"] = $value;
	//	grupo - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("grupo", $data, $keylink);
			$row["grupo_value"] = $value;
	//	curso - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("curso", $data, $keylink);
			$row["curso_value"] = $value;
	//	materias - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("materias", $data, $keylink);
			$row["materias_value"] = $value;
	//	manha - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("manha", $data, $keylink);
			$row["manha_value"] = $value;
	//	tarde - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("tarde", $data, $keylink);
			$row["tarde_value"] = $value;
	//	noite - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("noite", $data, $keylink);
			$row["noite_value"] = $value;
	//	aulas - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("aulas", $data, $keylink);
			$row["aulas_value"] = $value;
	//	aulas_onde - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("aulas_onde", $data, $keylink);
			$row["aulas_onde_value"] = $value;
	//	bolsista - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("bolsista", $data, $keylink);
			$row["bolsista_value"] = $value;
	//	motivos - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("motivos", $data, $keylink);
			$row["motivos_value"] = $value;
	//	ano - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("ano", $data, $keylink);
			$row["ano_value"] = $value;
	//	historico - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("historico", $data, $keylink);
			$row["historico_value"] = $value;
	//	atualizado - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("atualizado", $data, $keylink);
			$row["atualizado_value"] = $value;
	//	em_quantos_nucleos - Custom
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("em_quantos_nucleos", $data, $keylink);
			$row["em_quantos_nucleos_value"] = $value;
		$rowinfo[] = $row;
		$data = $cipherer->DecryptFetchedArray($rs);
	}
	$xt->assign_loopsection("details_row",$rowinfo);
}
$returnJSON = array("success" => true);
$xt->load_template("professor_por_nucleo_detailspreview.htm");
$returnJSON["body"] = $xt->fetch_loaded();

if($mode!="inline"){
	$returnJSON["counter"] = postvalue("counter");
	$layout = GetPageLayout(GoodFieldName($strTableName), 'detailspreview');
	if($layout)
	{
		$rtl = $xt->getReadingOrder() == 'RTL' ? 'RTL' : '';
		$returnJSON["style"] = "styles/".$layout->style."/style".$rtl.".css";
		$returnJSON["pageStyle"] = "pagestyles/".$layout->name.$rtl.".css";
		$returnJSON["layout"] = $layout->style." page-".$layout->name.".css";
	}	
}	

echo "<textarea>".htmlspecialchars(my_json_encode($returnJSON))."</textarea>";
?>