<?php
/**
* File che recupera i valori da mostrare nei suggerimenti dei campi. 
* Viene richiamato dallo script {@link scheda.php} e dalle funzioni di scriptaculous
* 
* @package VFront
* @subpackage RPC
* @author Mario Marcello Verona <marcelloverona@gmail.com>
* @copyright 2007-2010 M.Marcello Verona
* @version 0.96 $Id: rpc.suggest.php 880 2010-12-14 12:43:47Z marciuz $
* @license http://www.gnu.org/licenses/gpl.html GNU Public License
*/

include("./inc/conn.php");
include("./inc/func.comuni.php");


proteggi(1);


echo "<ul>\n";
if(is_array($_REQUEST['dati']) && isset($_REQUEST['t'])){
	
	$TABELLA = $_REQUEST['t'];
	
	list($campo,$valore)=each($_REQUEST['dati']);
	
/*	print_r($campo);
	print_r($valore);*/

	$valore = trim($vmsql->escape($valore));
	
	$sql_campo = "SELECT DISTINCT $campo FROM $TABELLA WHERE $campo LIKE '%".$valore."%' LIMIT 25";
	$query_campo=$vmsql->query($sql_campo);
	
	while ($dati=$vmsql->fetch_row($query_campo)) {
		
	  $testo=trim($dati[0]);
	  ?>
	  <li onMouseOver="this.className='mouse2';" onMouseOut="this.className='mouse1';"><?php echo vf_utf8_encode($testo); ?></li>
	  <?php
	}
	
}
else if(is_array($_REQUEST['dati']) && isset($_REQUEST['id_col'])){
	
	$id_col=(int) $_REQUEST['id_col'];
	
	list($campo,$valore)=each($_REQUEST['dati']);
	
	$valore = trim($vmreg->escape($valore));
	
	$q1=$vmreg->query("SELECT in_default FROM {$db1['frontend']}{$db1['sep']}registro_col WHERE id_reg=$id_col");
	
	if($vmreg->num_rows($q1)==1){
		
		list($sql_def)=$vmreg->fetch_row($q1);
		
		$nome_campo=preg_match("|[\W]+([\w]+) +FROM +|si",$sql_def,$finded);
		
		if(isset($finded[1])){
		
			$sql_campo = preg_replace("!^ ?SELECT[\W]{1}!i","SELECT DISTINCT ",$sql_def)." WHERE {$finded[1]} LIKE '%".$valore."%' LIMIT 25";
			
			
			$query_campo=$vmsql->query($sql_campo);
			
			while ($dati=$vmsql->fetch_row($query_campo)) {
		
			  $id_colonna=trim($dati[0]);
			  $testo=trim($dati[1]);
			  ?>
			  <li id="<?php echo "ac___{$campo}___{$id_colonna}";?>" onMouseOver="this.className='mouse2';" onMouseOut="this.className='mouse1';"><?php echo vf_utf8_encode($testo); ?></li>
			  <?php
			}
		}
	}
	
}
echo "</ul>\n";
?>