<?
////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


class FilterProcess{
	static $controls=array();
	static public function getFilter($filtro){
		array_push(FilterProcess::$controls,$filtro);
		$cadena="";
		$field=$filtro['field'];
		switch((string)$filtro['type']){
			case "text":
				$valor=isset($_REQUEST['fil_'.$field]) ? $_REQUEST['fil_'.$field] : '';
				$cadena="<input type='text' name='fil_".$field."' id='fil_".$field."' class='txt_complete' value='".$valor."' />";
				break;
			case "select":
				$valor=$_REQUEST['fil_'.$field];
				$cadena="<select name='fil_".$field."' id='fil_".$field."' class='select_large'>";
				if(isset($filtro->sql)){
					$q=mysql_query(replaceSpecialParams((string)$filtro->sql)) or die("Error de setup de filtro:<br />".mysql_error());
					$cadena.="<option value='-1'> ------- </option>";
					while($f=mysql_fetch_array($q)){
						$v=$f[0]==$valor ? " selected='selected'" : '';
						if(isset($filtro->sql['lang'])){
							$f[1]=Content::extractLang($f[1],$filtro->sql['lang']);	
						}
						$cadena.="<option value='".$f[0]."'{$v}>".$f[1]."</option>";
					}
				}else if(isset($filtro->list)){
					foreach($filtro->list->option as $option){
						$v=$option['value']==$valor ? " selected='selected'" : '';
						$cadena.="<option value='".$option['value']."'{$v}>".$option."</option>";
					}
				}
				$cadena.="</select>";
				break;
			case "boolean":
				$valor=$_REQUEST['fil_'.$field]=='true' ? "checked='checked'" : '';
				$cadena="<input type='checkbox' name='fil_".$field."' id='fil_".$field."' class='ch_default' {$valor} value='1' />";
				break;
			case "number":
				$valor_1=isset($_REQUEST['fil_'.$field.'_1']) ? $_REQUEST['fil_'.$field.'_1'] : '';
				$valor_2=isset($_REQUEST['fil_'.$field.'_2']) ? $_REQUEST['fil_'.$field.'_2'] : '';
				$valor_op_0=$_REQUEST['fil_'.$field.'_op']=="=" ? " selected='selected'" : '';
				$valor_op_1=$_REQUEST['fil_'.$field.'_op']=="<" ? " selected='selected'" : '';
				$valor_op_2=$_REQUEST['fil_'.$field.'_op']==">" ? " selected='selected'" : '';
				$valor_op_3=$_REQUEST['fil_'.$field.'_op']=="<=" ? " selected='selected'" : '';
				$valor_op_4=$_REQUEST['fil_'.$field.'_op']==">=" ? " selected='selected'" : '';
				$valor_op_5=$_REQUEST['fil_'.$field.'_op']=="<>" ? " selected='selected'" : '';
				$valor_op_6=$_REQUEST['fil_'.$field.'_op']=="rng." ? " selected='selected'" : '';
				$display2=$_REQUEST['fil_'.$field.'_op']=="rng." ? "visible" : "none";
				$cadena=<<<EOT
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
	<tr>
		<td width='10'>
			<select name='fil_{$field}_op' id='fil_{$field}_op' class='select_short' onchange='filter_chOp(this)'>
				<option value='='{$valor_op_0}>=</option>
				<option value='<'{$valor_op_1}>&lt;</option>
				<option value='>'{$valor_op_2}>&gt;</option>
				<option value='<='{$valor_op_3}>&lt;=</option>
				<option value='>='{$valor_op_4}>&gt;=</option>
				<option value='<>'{$valor_op_5}>&lt;&gt;</option>
				<option value='rng.'{$valor_op_6}>rng.</option>
			</select>
		</td>
		<td>
			&nbsp;<input type='text' name='fil_{$field}_1' id='fil_{$field}_1' class='txt_short' value='{$valor_1}' />
		</td>
	</tr>
	<tr id='fil_{$field}_row' style='display:{$display2}'>
		<td>&nbsp;</td>
		<td>
			&nbsp;<input type='text' name='fil_{$field}_2' id='fil_{$field}_2' class='txt_short' value='{$valor_2}' />
		</td>
	</tr>
</table>
EOT;
				break;
			case "date":
				$lnng=$langs[$_SESSION['userlang']][0];
				$valor_1=isset($_REQUEST['fil_'.$field.'_1']) ? $_REQUEST['fil_'.$field.'_1'] : '';
				$valor_2=isset($_REQUEST['fil_'.$field.'_2']) ? $_REQUEST['fil_'.$field.'_2'] : '';
				$valor_op_0=$_REQUEST['fil_'.$field.'_op']=="=" ? " selected='selected'" : '';
				$valor_op_1=$_REQUEST['fil_'.$field.'_op']=="<" ? " selected='selected'" : '';
				$valor_op_2=$_REQUEST['fil_'.$field.'_op']==">" ? " selected='selected'" : '';
				$valor_op_3=$_REQUEST['fil_'.$field.'_op']=="<=" ? " selected='selected'" : '';
				$valor_op_4=$_REQUEST['fil_'.$field.'_op']==">=" ? " selected='selected'" : '';
				$valor_op_5=$_REQUEST['fil_'.$field.'_op']=="<>" ? " selected='selected'" : '';
				$valor_op_6=$_REQUEST['fil_'.$field.'_op']=="rng." ? " selected='selected'" : '';
				$value_1=$valor_1!='' ? date(CAL_DATE_FORMAT,strtotime($valor_1)) : '';
				$value_2=$valor_2!='' ? date(CAL_DATE_FORMAT,strtotime($valor_2)) : '';
				$display2=$_REQUEST['fil_'.$field.'_op']=="rng." ? "visible" : "none";
				$cadena=<<<EOT
<table width='100%' cellpadding='0' cellspacing='0' border='0'>
	<tr>
		<td width='10'>
			<select name='fil_{$field}_op' id='fil_{$field}_op' class='select_short' onchange='filter_chOp(this)'>
				<option value='='{$valor_op_0}>=</option>
				<option value='<'{$valor_op_1}>&lt;</option>
				<option value='>'{$valor_op_2}>&gt;</option>
				<option value='<='{$valor_op_3}>&lt;=</option>
				<option value='>='{$valor_op_4}>&gt;=</option>
				<option value='<>'{$valor_op_5}>&lt;&gt;</option>
				<option value='rng.'{$valor_op_6}>rng.</option>
			</select>
		</td>
		<td>
			&nbsp;<input type='text' name='cal_{$field}_1' id='cal_{$field}_1' class='txt_complete' style='width:64px' value='{$value_1}' />
			<input type='hidden' name='fil_{$field}_1' id='fil_{$field}_1' value='{$valor_1}' />
		</td>
	</tr>
	<tr id='fil_{$field}_row' style='display:{$display2}'>
		<td>&nbsp;</td>
		<td>
			&nbsp;<input type='text' name='cal_{$field}_2' id='cal_{$field}_2' class='txt_complete' style='width:64px' value='{$value_2}' />
			<input type='hidden' name='fil_{$field}_2' id='fil_{$field}_2' value='{$valor_2}' />
		</td>
	</tr>
</table>
<script type='text/javascript'>
$(function() {
	$("#cal_{$field}_1").datepicker("option",$.datepicker.regional['$lnng']);
	$("#cal_{$field}_2").datepicker("option",$.datepicker.regional['$lnng']);
	$("#cal_{$field}_1").datepicker({
		changeMonth: true, changeYear: true, onSelect: function(dateText, inst){
			var yuyu=new Date($(this).datepicker("getDate"));
			var cc=yuyu.getFullYear()+"-"+(yuyu.getMonth()+1)+"-"+yuyu.getDate();
			$("#fil_{$field}_1").val(cc);
		}
	});
	$("#cal_{$field}_2").datepicker({
		changeMonth: true, changeYear: true, onSelect: function(dateText, inst){
			var yuyu=new Date($(this).datepicker("getDate"));
			var cc=yuyu.getFullYear()+"-"+(yuyu.getMonth()+1)+"-"+yuyu.getDate();
			$("#fil_{$field}_2").val(cc);
		}
	});
});
EOT;
				$cadena.="</script>";
				break;
		}
		return $cadena;
	}
	
	static public function getValidationScript($xml){
		$contenido="";
		//$url="var url=''";
		foreach($xml->filter as $control){
			$tipo=(string)$control['type'];
			$campo=(string)$control['field'];
			$error=(string)$control['errortext'];
			switch($tipo){
				case "number":
					$contenido.="if(isNaN(document.getElementById('fil_{$campo}_1').value) || isNaN(document.getElementById('fil_{$campo}_2').value)) errores+='{$error}';".chr(13);
					break;
				case "date":
					$contenido.="if(validaFiltroFecha('{$campo}')==false) errores+='{$error}';".chr(13);
					break;
				case "boolean":
					break;
				default:
			}
		}
		$cadena=<<<EOT
		<script type='text/javascript'>
			function validaFiltros(){
				var errores='';
				var url = $('#frmFiltros').serialize();
				{$contenido}
				if(errores!=''){
					alert(errores);
				}else{
					//document.getElementById('frmFiltros').submit();
					changeURL(url);
				}
			}
EOT;
		$cadena.="</script>";
		return $cadena;
	}
	
	static public function getFilterInfo($control){
		$obj=new StdClass();
		$tipo=(string)$control['type'];
		$campo=(string)$control['field'];
		$titulo=(string)$control['title'];
		$fieldname=str_replace("___",".",(string)$control['field']);
		$obj->title=$titulo;
		$obj->type=$tipo;
		switch($tipo){
			case "text":
				if(!isset($_REQUEST["fil_$campo"]) || $_REQUEST["fil_$campo"]=='' || $_REQUEST["fil_$campo"]==NULL) continue;
				$obj->value=" contiene el término '" . $_REQUEST["fil_$campo"] . "'";
				break;
			case "boolean":
				if(!isset($_REQUEST["fil_$campo"]) || $_REQUEST["fil_$campo"]!='true') continue;
				$obj->value=BUTTON_YES;
				break;
			case "select":
				if(!isset($_REQUEST["fil_$campo"]) || $_REQUEST["fil_$campo"]=='-1' || $_REQUEST["fil_$campo"]=='') continue;
				if(isset($control->sql)){
					$q=mysql_query(replaceSpecialParams((string)$control->sql)) or die("Error de setup de filtro");
					while($f=mysql_fetch_array($q)){
						if($f[0]==$_REQUEST["fil_$campo"]){
							if(isset($control->sql['lang'])){
								$f[1]=Content::extractLang($f[1],$control->sql['lang']);	
							}
							$obj->value=$f[1];
							break;
						}
					}
				}else if(isset($control->list)){
					foreach($control->list->option as $option){
						$v=$option['value']==$valor ? " selected='selected'" : '';
						$cadena.="<option value='".$option['value']."'{$v}>".$option."</option>";
						if($option['value']==$_REQUEST["fil_$campo"]){
							$obj->value=$option['value'];
							break;
						}
					}
				}
				break;
			case "number":
			case "date":
				if($_REQUEST["fil_{$campo}_op"]=='rng.'){
					if($_REQUEST["fil_{$campo}_1"]=='' || $_REQUEST["fil_{$campo}_2"]=='') continue;
				}else{
					if($_REQUEST["fil_{$campo}_1"]=='') continue;
				}
				$valor_1=isset($_REQUEST['fil_'.$campo.'_1']) ? $_REQUEST['fil_'.$campo.'_1'] : '';
				$valor_2=isset($_REQUEST['fil_'.$campo.'_2']) ? $_REQUEST['fil_'.$campo.'_2'] : '';
				switch($_REQUEST['fil_'.$campo.'_op']){
					case "=":
						$obj->value=" igual a $valor_1";
						break;
					case "<":
						$obj->value=" menor que $valor_1";
						break;
					case ">":
						$obj->value=" mayor que $valor_1";
						break;
					case "<=":
						$obj->value=" menor o igual que $valor_1";
						break;
					case ">=":
						$obj->value=" mayor o igual que $valor_1";
						break;
					case "<>":
						$obj->value=" distinto de $valor_1";
						break;
					case "rng.":
						$obj->value=" entre $valor_1 y $valor_2";
						break;
				}
				break;
		}
		return $obj;
	}
	
	static public function getResetScript($xml){
		$contenido="";
		$url="var url=''";
		foreach($xml->filter as $control){
			$tipo=(string)$control['type'];
			$campo=(string)$control['field'];
			switch($tipo){
				case "text":
					$contenido.="document.getElementById('fil_{$campo}').value=''".chr(13);
					$url.="+'fil_{$campo}=&'";
					break;
				case "boolean":
					$contenido.="document.getElementById('fil_{$campo}').checked=false".chr(13);
					$url.="+'fil_{$campo}=&'";
					break;
				case "select":
					$contenido.="document.getElementById('fil_{$campo}').selectedIndex=0".chr(13);
					$url.="+'fil_{$campo}=&'";
					break;
				case "number":
				case "date":
					$contenido.="document.getElementById('fil_{$campo}_op').selectedIndex=0".chr(13);
					$contenido.="document.getElementById('fil_{$campo}_1').value=''".chr(13);
					$contenido.="document.getElementById('fil_{$campo}_2').value=''".chr(13);
					$url.="+'fil_{$campo}_op=&'";
					$url.="+'fil_{$campo}_1=&'";
					$url.="+'fil_{$campo}_2=&'";
					break;
			}
		}
		$cadena=<<<EOT
		<script type='text/javascript'>
			function resetFiltros(){
				{$url}
				{$contenido}
				//document.getElementById('frmFiltros').submit();
				changeURL(url);
			}
EOT;
		$cadena.="</script>";
		return $cadena;
	}
	
	static public function parse($xml){
		$cadena="";
		foreach($xml->filter as $control){
			$tipo=(string)$control['type'];
			$campo=(string)$control['field'];
			$fieldname=str_replace("___",".",(string)$control['field']);
			switch($tipo){
				case "text":
					if(!isset($_REQUEST["fil_$campo"]) || $_REQUEST["fil_$campo"]=='' || $_REQUEST["fil_$campo"]==NULL) continue;
					$cadena.=" $fieldname LIKE " . DB_CONN_GET_NAMES . " '%" . mysql_real_escape_string($_REQUEST["fil_$campo"]) . "%' AND ";
					break;
				case "boolean":
					if(!isset($_REQUEST["fil_$campo"]) || $_REQUEST["fil_$campo"]!='true') continue;
					$cadena.=" $fieldname=true AND ";
					break;
				case "select":
					if(!isset($_REQUEST["fil_$campo"]) || $_REQUEST["fil_$campo"]=='-1' || $_REQUEST["fil_$campo"]=='') continue;
					$valor=$_REQUEST["fil_$campo"];
					if(is_numeric($valor)){
						$cadena.=" $fieldname=" . mysql_real_escape_string($valor) . " AND ";
					}else{
						$cadena.=" $fieldname=" . DB_CONN_GET_NAMES . " '" . mysql_real_escape_string($valor) . "' AND ";
					}
					break;
				case "number":
					if($_REQUEST["fil_{$campo}_op"]=='rng.'){
						if($_REQUEST["fil_{$campo}_1"]=='' || $_REQUEST["fil_{$campo}_2"]=='') continue;
						$cadena.=" $fieldname BETWEEN ".$_REQUEST["fil_{$campo}_1"]." AND ".$_REQUEST["fil_{$campo}_2"]." AND ";
					}else{
						if($_REQUEST["fil_{$campo}_1"]=='') continue;
						$cadena.=" $fieldname" . $_REQUEST["fil_{$campo}_op"] . $_REQUEST["fil_{$campo}_1"] . " AND ";
					}
					break;
				case "date":
					if($_REQUEST["fil_{$campo}_op"]=='rng.'){
						if($_REQUEST["fil_{$campo}_1"]=='' || $_REQUEST["fil_{$campo}_2"]=='') continue;
						$cadena.=" DATE($fieldname) BETWEEN '".$_REQUEST["fil_{$campo}_1"]."' AND '".$_REQUEST["fil_{$campo}_2"]."' AND ";
					}else{
						if($_REQUEST["fil_{$campo}_1"]=='') continue;
						$cadena.=" DATE($fieldname)" . $_REQUEST["fil_{$campo}_op"] . "'" . $_REQUEST["fil_{$campo}_1"] . "' AND ";
					}
					break;
			}
		}
		return $cadena;
	}
	
	static public function getQuery($sql,$xml){
		$clausula_filtro=FilterProcess::parse($xml);
		return $clausula_filtro;
	}
}
?>