<?php
ini_set("display_errors","1");
ini_set("display_startup_errors","1");
header("Expires: Thu, 01 Jan 1970 00:00:01 GMT"); 
set_magic_quotes_runtime(0);

include("include/dbcommon.php");
include("include/pf_camara_variables.php");



include('libs/Smarty.class.php');
$smarty = new Smarty();



$conn=db_connect();

//	process reqest data, fill session variables

if(!count($_POST) && !count($_GET))
{
	$sess_unset = array();
	foreach($_SESSION as $key=>$value)
		if(substr($key,0,strlen($strTableName)+1)==$strTableName."_" &&
			strpos(substr($key,strlen($strTableName)+1),"_")===false)
			$sess_unset[] = $key;
	foreach($sess_unset as $key)
		unset($_SESSION[$key]);
}

if(@$_REQUEST["a"]=="showall")
	$_SESSION[$strTableName."_search"]=0;
else if(@$_REQUEST["a"]=="search")
{
	$_SESSION[$strTableName."_searchfield"]=postvalue("SearchField");
	$_SESSION[$strTableName."_searchoption"]=postvalue("SearchOption");
	$_SESSION[$strTableName."_searchfor"]=postvalue("SearchFor");
	if(postvalue("SearchFor")!="" || postvalue("SearchOption")=='Empty')
		$_SESSION[$strTableName."_search"]=1;
	else
		$_SESSION[$strTableName."_search"]=0;
	$_SESSION[$strTableName."_pagenumber"]=1;
}
else if(@$_REQUEST["a"]=="advsearch")
{
	$_SESSION[$strTableName."_asearchnot"]=array();
	$_SESSION[$strTableName."_asearchopt"]=array();
	$_SESSION[$strTableName."_asearchfor"]=array();
	$_SESSION[$strTableName."_asearchfor2"]=array();
	$tosearch=0;
	$asearchfield = postvalue("asearchfield");
	$_SESSION[$strTableName."_asearchtype"] = postvalue("type");
	if(!$_SESSION[$strTableName."_asearchtype"])
		$_SESSION[$strTableName."_asearchtype"]="and";
	foreach($asearchfield as $field)
	{
		$gfield=GoodFieldName($field);
		$asopt=postvalue("asearchopt_".$gfield);
		$value1=postvalue("value_".$gfield);
		$type=postvalue("type_".$gfield);
		$value2=postvalue("value1_".$gfield);
		$not=postvalue("not_".$gfield);
		if($value1 || $asopt=='Empty')
		{
			$tosearch=1;
			$_SESSION[$strTableName."_asearchopt"][$field]=$asopt;
			if(!is_array($value1))
				$_SESSION[$strTableName."_asearchfor"][$field]=$value1;
			else
				$_SESSION[$strTableName."_asearchfor"][$field]=combinevalues($value1);
			$_SESSION[$strTableName."_asearchfortype"][$field]=$type;
			if($value2)
				$_SESSION[$strTableName."_asearchfor2"][$field]=$value2;
			$_SESSION[$strTableName."_asearchnot"][$field]=($not=="on");
		}
	}
	if($tosearch)
		$_SESSION[$strTableName."_search"]=2;
	else
		$_SESSION[$strTableName."_search"]=0;
	$_SESSION[$strTableName."_pagenumber"]=1;
}



if(@$_REQUEST["orderby"])
	$_SESSION[$strTableName."_orderby"]=@$_REQUEST["orderby"];

if(@$_REQUEST["pagesize"])
{
	$_SESSION[$strTableName."_pagesize"]=@$_REQUEST["pagesize"];
	$_SESSION[$strTableName."_pagenumber"]=1;
}

if(@$_REQUEST["goto"])
	$_SESSION[$strTableName."_pagenumber"]=@$_REQUEST["goto"];


//	process reqest data - end

$includes="";

if ($useAJAX) {
$includes.="<script language=\"JavaScript\" src=\"include/jquery.js\"></script>\r\n";
$includes.="<script language=\"JavaScript\" src=\"include/ajaxsuggest.js\"></script>\r\n";
}
$includes.="<script language=\"JavaScript\" src=\"include/jsfunctions.js\">".
"</script>\n".
"<script type=\"text/javascript\">".
"\nvar bSelected=false;".
"\nvar TEXT_FIRST = \""."Primero"."\";".
"\nvar TEXT_PREVIOUS = \""."Anterior"."\";".
"\nvar TEXT_NEXT = \""."Siguiente"."\";".
"\nvar TEXT_LAST = \""."Último"."\";";
if ($useAJAX) {
$includes.="\nvar SUGGEST_TABLE = \"pf_camara_searchsuggest.php\";".
"\nvar MASTER_PREVIEW_TABLE = \"pf_camara_masterpreview.php\";";
}
$includes.="\n</script>";
if ($useAJAX) {
$includes.="<div id=\"search_suggest\"></div>".
"<div id=\"master_details\"></div>";
}

$smarty->assign("includes",$includes);
$smarty->assign("useAJAX",$useAJAX);


//	process session variables
//	order by
$strOrderBy="";
$order_ind=-1;

$smarty->assign("order_dir_id_camara","a");
$smarty->assign("order_dir_nombre_camara","a");
$smarty->assign("order_dir_estado_actual","a");
$smarty->assign("order_dir_tipo_camara","a");
$smarty->assign("order_dir_proceso_actual","a");
$smarty->assign("order_dir_posicion_puerta","a");
$smarty->assign("order_dir_x","a");
$smarty->assign("order_dir_y","a");
$smarty->assign("order_dir_w","a");
$smarty->assign("order_dir_h","a");
$smarty->assign("order_dir_maxpallet","a");

if(@$_SESSION[$strTableName."_orderby"])
{
	$order_field=substr($_SESSION[$strTableName."_orderby"],1);
	$order_dir=substr($_SESSION[$strTableName."_orderby"],0,1);
	$order_ind=GetFieldIndex($order_field);

	$smarty->assign("order_dir_id_camara","a");
	if($order_field=="id_camara")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_id_camara","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_id_camara","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_nombre_camara","a");
	if($order_field=="nombre_camara")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_nombre_camara","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_nombre_camara","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_estado_actual","a");
	if($order_field=="estado_actual")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_estado_actual","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_estado_actual","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_tipo_camara","a");
	if($order_field=="tipo_camara")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_tipo_camara","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_tipo_camara","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_proceso_actual","a");
	if($order_field=="proceso_actual")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_proceso_actual","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_proceso_actual","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_posicion_puerta","a");
	if($order_field=="posicion_puerta")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_posicion_puerta","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_posicion_puerta","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_x","a");
	if($order_field=="x")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_x","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_x","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_y","a");
	if($order_field=="y")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_y","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_y","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_w","a");
	if($order_field=="w")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_w","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_w","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_h","a");
	if($order_field=="h")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_h","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_h","<img src=\"images/".$img.".gif\" border=0>");
	}
	$smarty->assign("order_dir_maxpallet","a");
	if($order_field=="maxpallet")
	{
		if($order_dir=="a")
		{
			$smarty->assign("order_dir_maxpallet","d");
			$img="up";
		}
		else
			$img="down";
		$smarty->assign("order_image_maxpallet","<img src=\"images/".$img.".gif\" border=0>");
	}

	if($order_ind)
	{
		if($order_dir=="a")
			$strOrderBy="order by ".($order_ind)." asc";
		else 
			$strOrderBy="order by ".($order_ind)." desc";
	}
}
if(!$strOrderBy)
	$strOrderBy=$gstrOrderBy;

//	page number
$mypage=(integer)$_SESSION[$strTableName."_pagenumber"];
if(!$mypage)
	$mypage=1;

//	page size
$PageSize=(integer)$_SESSION[$strTableName."_pagesize"];
if(!$PageSize)
	$PageSize=$gPageSize;

	$smarty->assign("rpp10_selected",($PageSize==10)?"selected":"");
	$smarty->assign("rpp20_selected",($PageSize==20)?"selected":"");
	$smarty->assign("rpp30_selected",($PageSize==30)?"selected":"");
	$smarty->assign("rpp50_selected",($PageSize==50)?"selected":"");
	$smarty->assign("rpp100_selected",($PageSize==100)?"selected":"");
	$smarty->assign("rpp500_selected",($PageSize==500)?"selected":"");

// delete record
if (@$_REQUEST["mdelete"])
{
	foreach(@$_REQUEST["mdelete"] as $ind)
	{
		$keys=array();
		$keys["id_camara"]=refine($_REQUEST["mdelete1"][$ind-1]);
		$strSQL="delete from ".AddTableWrappers($strOriginalTableName)." where ".KeyWhere($keys);
		$retval=true;
		$where = substr($strSQL,strlen("delete from ".AddTableWrappers($strOriginalTableName)." where "));
		if(function_exists("BeforeDelete"))
			$retval = BeforeDelete($where);
		if($retval)
		{
					LogInfo($strSQL);
			db_exec($strSQL,$conn);
			if(function_exists("AfterDelete"))
				AfterDelete();
		}
	}
	if(function_exists("AfterMassDelete"))
		AfterMassDelete();
}

//	make sql "select" string

$strSQL = $gstrSQL;

//	add search params

if(@$_SESSION[$strTableName."_search"]==1)
//	 regular search
{  
	$strSearchFor=trim($_SESSION[$strTableName."_searchfor"]);
	$strSearchOption=trim($_SESSION[$strTableName."_searchoption"]);
	if(@$_SESSION[$strTableName."_searchfield"])
	{
		$strSearchField = $_SESSION[$strTableName."_searchfield"];
		if($where = StrWhere($strSearchField, $strSearchFor, $strSearchOption, ""))
			$strSQL = AddWhere($strSQL,$where);
		else
			$strSQL = AddWhere($strSQL,"1=0");
	}
	else
	{
		$strWhere = "1=0";
		if($where=StrWhere("id_camara", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("nombre_camara", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("estado_actual", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("tipo_camara", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("proceso_actual", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("posicion_puerta", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("x", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("y", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("w", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("h", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		if($where=StrWhere("maxpallet", $strSearchFor, $strSearchOption, ""))
			$strWhere .= " or ".$where;
		$strSQL = AddWhere($strSQL,$strWhere);
	}
}
else if(@$_SESSION[$strTableName."_search"]==2)
//	 advanced search
{
	$sWhere="";
	foreach(@$_SESSION[$strTableName."_asearchfor"] as $f => $sfor)
		{
			$strSearchFor=trim($sfor);
			$strSearchFor2="";
			$type=@$_SESSION[$strTableName."_asearchfortype"][$f];
			if(array_key_exists($f,@$_SESSION[$strTableName."_asearchfor2"]))
				$strSearchFor2=trim(@$_SESSION[$strTableName."_asearchfor2"][$f]);
			if($strSearchFor!="" || true)
			{
				if (!$sWhere) 
				{
					if($_SESSION[$strTableName."_asearchtype"]=="and")
						$sWhere="1=1";
					else
						$sWhere="1=0";
				}
				$strSearchOption=trim($_SESSION[$strTableName."_asearchopt"][$f]);
				if($where=StrWhereAdv($f, $strSearchFor, $strSearchOption, $strSearchFor2,$type))
				{
					if($_SESSION[$strTableName."_asearchnot"][$f])
						$where="not (".$where.")";
					if($_SESSION[$strTableName."_asearchtype"]=="and")
	   					$sWhere .= " and ".$where;
					else
	   					$sWhere .= " or ".$where;
				}
			}
		}
		$strSQL = AddWhere($strSQL,$sWhere);
	}






//	order by
$strSQL.=" ".trim($strOrderBy);

//	save SQL for use in "Export" and "Printer-friendly" pages

$_SESSION[$strTableName."_sql"] = $strSQL;

LogInfo($strSQL);


//	select and display records
if(CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
//	 Pagination:
	$numrows=GetRowCount($strSQL);
	if(!$numrows)
	{
		$smarty->assign("rowsfound",false);
		$smarty->assign("message","No se hallaron registros");
	}
	else
	{
		$smarty->assign("rowsfound",true);
		$smarty->assign("records_found",$numrows);
		$maxRecords = $numrows;
		$maxpages=ceil($maxRecords/$PageSize);
		if($mypage > $maxpages)
			$mypage = $maxpages;
		if($mypage<1) 
			$mypage=1;
		$maxrecs=$PageSize;
		$smarty->assign("page",$mypage);
		$smarty->assign("maxpages",$maxpages);

//	write pagination
$smarty->assign("pagination","<script language=\"JavaScript\">WritePagination(".$mypage.",".$maxpages.");
		function GotoPage(nPageNumber)
		{
			window.location='pf_camara_list.php?goto='+nPageNumber;
		}
</script>");
		
		$strSQL = AddTop($strSQL, $mypage*$PageSize);
	}
	$rs=db_query($strSQL,$conn);
	db_pageseek($rs,$PageSize,$mypage);

//	hide colunm headers if needed
	$recordsonpage=$numrows-($mypage-1)*$PageSize;
	if($recordsonpage>$PageSize)
	$recordsonpage=$PageSize;
	if($recordsonpage>=1)
		$smarty->assign("column1show",true);
	else
		$smarty->assign("column1show",false);



//	fill $rowinfo array
	$rowinfo = array();
	$shade=false;
	$recno=1;
	$editlink="";
	$copylink="";

	while($data=db_fetch_array($rs))
	{
		if(function_exists("BeforeProcessRowList"))
		{
			if(!BeforeProcessRowList($data))
				continue;
		}
		break;
	}

	while($data && $recno<=$PageSize)
	{
		$row=array();
		if(!$shade)
		{
			$row["shadeclass"]='class="shade"';
			$row["shadeclassname"]="shade";
			$shade=true;
		}
		else
		{
			$row["shadeclass"]='class="shadeless"';
			$row["shadeclassname"]="shadeless";
			$shade=false;
		}
		for($col=1;$data && $recno<=$PageSize && $col<=1;$col++)
		{


//	key fields
			$row[$col."id1"]=htmlspecialchars($data["id_camara"]);
			$row[$col."recno"] = $recno;
			$recno++;
//	detail tables
//	edit page link
			$editlink="";
			$editlink.="editid1=".htmlspecialchars(urlencode($data["id_camara"]));
			$row[$col."editlink"]=$editlink;

			$copylink="";
			$copylink.="copyid1=".htmlspecialchars(urlencode($data["id_camara"]));
			$row[$col."copylink"]=$copylink;
			$keylink="";
			$keylink.="&key1=".htmlspecialchars(urlencode($data["id_camara"]));


//	id_camara - 
			$value="";
				$value = ProcessLargeText(GetData($data,"id_camara", ""),"field=id%5Fcamara".$keylink,"",MODE_LIST);
			$row[$col."id_camara_value"]=$value;

//	nombre_camara - 
			$value="";
				$value = ProcessLargeText(GetData($data,"nombre_camara", ""),"field=nombre%5Fcamara".$keylink,"",MODE_LIST);
			$row[$col."nombre_camara_value"]=$value;

//	estado_actual - 
			$value="";
				$value = ProcessLargeText(GetData($data,"estado_actual", ""),"field=estado%5Factual".$keylink,"",MODE_LIST);
			$row[$col."estado_actual_value"]=$value;

//	tipo_camara - 
			$value="";
				$value = ProcessLargeText(GetData($data,"tipo_camara", ""),"field=tipo%5Fcamara".$keylink,"",MODE_LIST);
			$row[$col."tipo_camara_value"]=$value;

//	proceso_actual - 
			$value="";
				$value = ProcessLargeText(GetData($data,"proceso_actual", ""),"field=proceso%5Factual".$keylink,"",MODE_LIST);
			$row[$col."proceso_actual_value"]=$value;

//	posicion_puerta - 
			$value="";
				$value = ProcessLargeText(GetData($data,"posicion_puerta", ""),"field=posicion%5Fpuerta".$keylink,"",MODE_LIST);
			$row[$col."posicion_puerta_value"]=$value;

//	x - 
			$value="";
				$value = ProcessLargeText(GetData($data,"x", ""),"field=x".$keylink,"",MODE_LIST);
			$row[$col."x_value"]=$value;

//	y - 
			$value="";
				$value = ProcessLargeText(GetData($data,"y", ""),"field=y".$keylink,"",MODE_LIST);
			$row[$col."y_value"]=$value;

//	w - 
			$value="";
				$value = ProcessLargeText(GetData($data,"w", ""),"field=w".$keylink,"",MODE_LIST);
			$row[$col."w_value"]=$value;

//	h - 
			$value="";
				$value = ProcessLargeText(GetData($data,"h", ""),"field=h".$keylink,"",MODE_LIST);
			$row[$col."h_value"]=$value;

//	maxpallet - 
			$value="";
				$value = ProcessLargeText(GetData($data,"maxpallet", ""),"field=maxpallet".$keylink,"",MODE_LIST);
			$row[$col."maxpallet_value"]=$value;
			$row[$col."show"]=true;
			if(function_exists("BeforeMoveNextList"))
				BeforeMoveNextList($data,$row,$col);
			while($data=db_fetch_array($rs))
			{
				if(function_exists("BeforeProcessRowList"))
				{
					if(!BeforeProcessRowList($data))
						continue;
				}
				break;
			}
		}
		$rowinfo[]=$row;
	}
	$smarty->assign("rowinfo",$rowinfo);

}


if(CheckSecurity(@$_SESSION["_".$strTableName."_OwnerID"],"Search"))
{
	if($_SESSION[$strTableName."_search"]==1)
	{
		$onload = "onLoad=\"if(document.getElementById('SearchFor')) document.getElementById('ctlSearchFor').focus();\"";
		$smarty->assign("onload",$onload);
//	fill in search variables
	//	field selection
		if(@$_SESSION[$strTableName."_searchfield"]=="id_camara")
			$smarty->assign("search_id_camara","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="nombre_camara")
			$smarty->assign("search_nombre_camara","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="estado_actual")
			$smarty->assign("search_estado_actual","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="tipo_camara")
			$smarty->assign("search_tipo_camara","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="proceso_actual")
			$smarty->assign("search_proceso_actual","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="posicion_puerta")
			$smarty->assign("search_posicion_puerta","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="x")
			$smarty->assign("search_x","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="y")
			$smarty->assign("search_y","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="w")
			$smarty->assign("search_w","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="h")
			$smarty->assign("search_h","selected");
		if(@$_SESSION[$strTableName."_searchfield"]=="maxpallet")
			$smarty->assign("search_maxpallet","selected");
	// search type selection
		if(@$_SESSION[$strTableName."_searchoption"]=="Contains")
			$smarty->assign("search_contains_option_selected","selected");		
		if(@$_SESSION[$strTableName."_searchoption"]=="Equals")
			$smarty->assign("search_equals_option_selected","selected");		
		if(@$_SESSION[$strTableName."_searchoption"]=="Starts with ...")
			$smarty->assign("search_startswith_option_selected","selected");		
		if(@$_SESSION[$strTableName."_searchoption"]=="More than ...")
			$smarty->assign("search_more_option_selected","selected");		
		if(@$_SESSION[$strTableName."_searchoption"]=="Less than ...")
			$smarty->assign("search_less_option_selected","selected");		
		if(@$_SESSION[$strTableName."_searchoption"]=="Equal or more than ...")
			$smarty->assign("search_equalormore_option_selected","selected");		
		if(@$_SESSION[$strTableName."_searchoption"]=="Equal or less than ...")
			$smarty->assign("search_equalorless_option_selected","selected");		
		if(@$_SESSION[$strTableName."_searchoption"]=="Empty")
			$smarty->assign("search_empty_option_selected","selected");		

		$smarty->assign("search_searchfor","value=\"".htmlspecialchars(@$_SESSION[$strTableName."_searchfor"])."\"");
	}
}




$smarty->assign("displayheader","<script language=\"JavaScript\">DisplayHeader();</script>");




$strSQL=$_SESSION[$strTableName."_sql"];

$templatefile = "pf_camara_list.htm";
if(function_exists("BeforeShowList"))
	BeforeShowList($smarty,$templatefile);

$smarty->display($templatefile);

