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

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

require_once("include/service_variables.php");

$mode = postvalue("mode");

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

require_once("classes/searchclause.php");

$cipherer = new RunnerCipherer($strTableName);

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





$layout = new TLayout("detailspreview_bootstrap", "OfficeOffice", "MobileOffice");
$layout->version = 3;
	$layout->bootstrapTheme = "default";
		$layout->customCssPageName = "service_detailspreview";
$layout->blocks["bare"] = array();
$layout->containers["dcount"] = array();
$layout->container_properties["dcount"] = array(  );
$layout->containers["dcount"][] = array("name"=>"bsdetailspreviewcount",
	"block"=>"", "substyle"=>1  );

$layout->skins["dcount"] = "";

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

$layout->skins["detailspreviewgrid"] = "";

$layout->blocks["bare"][] = "detailspreviewgrid";
$page_layouts["service_detailspreview"] = $layout;




$recordsCounter = 0;

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

$params = array();
$params['id'] = 1;
$params['xt'] = &$xt;
$params['tName'] = $strTableName;
$params['pageType'] = "detailspreview";
$pageObject = new DetailsPreview($params);

$pSet = new ProjectSettings($strTableName, PAGE_LIST);


$whereClauses = array();
if($mastertable == "service_type")
{
	$formattedValue = make_db_value("service_service_type_id",$_SESSION[$sessionPrefix."_masterkey1"]);
	if( $formattedValue == "null" )
		$whereClauses[] = $pageObject->getFieldSQLDecrypt("service_service_type_id") . " is null";
	else
		$whereClauses[] = $pageObject->getFieldSQLDecrypt("service_service_type_id") . "=" . $formattedValue;
}

$whereClauses[] = SecuritySQL("Search", $strTableName);
$query = $pSet->getSQLQuery();

$strSQL = $query->buildSQL_default( $whereClauses );
$rowcount = $pageObject->connection->getFetchedRowsNumber( $strSQL );

$strSQL .= $pageObject->getOrderByClause();

$xt->assign("row_count",$rowcount);
if($rowcount) 
{
	$xt->assign("details_data",true);

	$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();
	
	require_once getabspath('classes/controls/ViewControlsContainer.php');
	$viewContainer = new ViewControlsContainer($pSet, PAGE_LIST);
	$viewContainer->isDetailsPreview = true;

	$b = true;
	$qResult = $pageObject->connection->query( $strSQL );
	$data = $cipherer->DecryptFetchedArray( $qResult->fetchAssoc() );
	while($data && $recordsCounter<$display_count) {
		$recordsCounter++;
		$row = array();
		$keylink = "";
		$keylink.="&key1=".runner_htmlspecialchars(rawurlencode(@$data["service_id"]));
	
	
	//	service_id - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_id", $data, $keylink);
			$row["service_id_value"] = $value;
			$format = $pSet->getViewFormat("service_id");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_id")))
				$class = ' rnr-field-number';
			$row["service_id_class"] = $class;
	//	service_service_type_id - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_service_type_id", $data, $keylink);
			$row["service_service_type_id_value"] = $value;
			$format = $pSet->getViewFormat("service_service_type_id");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_service_type_id")))
				$class = ' rnr-field-number';
			$row["service_service_type_id_class"] = $class;
	//	service_title_ar - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_title_ar", $data, $keylink);
			$row["service_title_ar_value"] = $value;
			$format = $pSet->getViewFormat("service_title_ar");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_title_ar")))
				$class = ' rnr-field-number';
			$row["service_title_ar_class"] = $class;
	//	service_title_en - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_title_en", $data, $keylink);
			$row["service_title_en_value"] = $value;
			$format = $pSet->getViewFormat("service_title_en");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_title_en")))
				$class = ' rnr-field-number';
			$row["service_title_en_class"] = $class;
	//	service_description_ar - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_description_ar", $data, $keylink);
			$row["service_description_ar_value"] = $value;
			$format = $pSet->getViewFormat("service_description_ar");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_description_ar")))
				$class = ' rnr-field-number';
			$row["service_description_ar_class"] = $class;
	//	service_description_en - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_description_en", $data, $keylink);
			$row["service_description_en_value"] = $value;
			$format = $pSet->getViewFormat("service_description_en");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_description_en")))
				$class = ' rnr-field-number';
			$row["service_description_en_class"] = $class;
	//	service_price - Number
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_price", $data, $keylink);
			$row["service_price_value"] = $value;
			$format = $pSet->getViewFormat("service_price");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_price")))
				$class = ' rnr-field-number';
			$row["service_price_class"] = $class;
	//	service_price_min - Number
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_price_min", $data, $keylink);
			$row["service_price_min_value"] = $value;
			$format = $pSet->getViewFormat("service_price_min");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_price_min")))
				$class = ' rnr-field-number';
			$row["service_price_min_class"] = $class;
	//	service_shipping_cost - Number
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_shipping_cost", $data, $keylink);
			$row["service_shipping_cost_value"] = $value;
			$format = $pSet->getViewFormat("service_shipping_cost");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_shipping_cost")))
				$class = ' rnr-field-number';
			$row["service_shipping_cost_class"] = $class;
	//	service_period - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_period", $data, $keylink);
			$row["service_period_value"] = $value;
			$format = $pSet->getViewFormat("service_period");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_period")))
				$class = ' rnr-field-number';
			$row["service_period_class"] = $class;
	//	service_status - 
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_status", $data, $keylink);
			$row["service_status_value"] = $value;
			$format = $pSet->getViewFormat("service_status");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_status")))
				$class = ' rnr-field-number';
			$row["service_status_class"] = $class;
	//	service_active - Checkbox
			$viewContainer->recId = $recordsCounter;
		    $value = $viewContainer->showDBValue("service_active", $data, $keylink);
			$row["service_active_value"] = $value;
			$format = $pSet->getViewFormat("service_active");
			$class = "rnr-field-text";
			if($format==FORMAT_FILE) 
				$class = ' rnr-field-file'; 
			if($format==FORMAT_AUDIO)
				$class = ' rnr-field-audio';
			if($format==FORMAT_CHECKBOX)
				$class = ' rnr-field-checkbox';
			if($format==FORMAT_NUMBER || IsNumberType($pSet->getFieldType("service_active")))
				$class = ' rnr-field-number';
			$row["service_active_class"] = $class;
		$rowinfo[] = $row;
		if ($b) {
			$rowinfo2[] = $row;
			$b = false;
		}
		$data = $cipherer->DecryptFetchedArray( $qResult->fetchAssoc() );
	}
	$xt->assign_loopsection("details_row",$rowinfo);
	$xt->assign_loopsection("details_row_header",$rowinfo2); // assign class for header
}
$returnJSON = array("success" => true);
$xt->load_template(GetTemplateName("service", "detailspreview"));
$returnJSON["body"] = $xt->fetch_loaded();

if($mode!="inline")
{
	$returnJSON["counter"] = postvalue("counter");
	$layout = GetPageLayout(GoodFieldName($strTableName), 'detailspreview');
	if($layout)
	{
		foreach($layout->getCSSFiles(isRTL(), mobileDeviceDetected() && $layout->version != BOOTSTRAP_LAYOUT) as $css)
		{
			$returnJSON['CSSFiles'][] = $css;
		}
	}	
}	

echo printJSON($returnJSON);
exit();
?>