<?php
require_once("../../base/php/startSession.php");
require_once("../../base/php//classDatabase.php");

$liste_id = @$_POST["liste_id"];
if ($liste_id == "") {
	$liste_id = @$_GET["liste_id"];
}

$table_id = @$_POST["table_id"];
if ($table_id == "") {
	$table_id = @$_GET["table_id"];
}


$httpString = "";
$httpString = $httpString . "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$httpString = $httpString . "\n<HTTP_SERVICE liste_id=\"$liste_id\" table_id=\"$table_id\" >";

$httpString = $httpString . getListe($liste_id,  $table_id);

$httpString = $httpString . "\n</HTTP_SERVICE>";
 

$httpString = gzcompress($httpString,9);
$httpString = "_c_" . base64_encode($httpString); 

echo "<CTICONTENT>$httpString</CTICONTENT>";



// recherche des variables de session
function getListe($liste_id, $table_id) {

	$httpString = "";

    $database = new Database("iCTI");
	
	$select_cmd = "";
	

	$result  =  $database->exec("SELECT t_listes.oid as oid, t_listes.code as code, t_listes.texte as texte, t_listes.is_cti as is_cti, t_listes.table_id as table_id, t_listes_tables.texte, t_listes_tables.select_cmd, t_listes.show_omit as show_omit  FROM pmsi.t_listes, pmsi.t_listes_tables WHERE t_listes.oid = $liste_id AND t_listes.table_id = t_listes_tables.oid");
	

	if ($result != false) {
	
		$record = $database->nextRecordInto();
		if ($record != FALSE) {
		
			$oid = $record[0];
			$code = trim($record[1]);
			$code = str_replace("<", "&lt;", $code);
			$code = str_replace(">", "&gt;", $code);
			$code = str_replace('"', "&quot;", $code);
			$texte = trim($record[2]);
			$texte = str_replace("<", "&lt;", $texte);
			$texte = str_replace(">", "&gt;", $texte);
			$texte = str_replace('"', "&quot;", $texte);
			$texte = str_replace('’', "'", $texte);
			$is_cti = trim($record[3]);
			

			$table_id = $record[4];
			$table_texte = trim($record[5]);
			$select_cmd = trim($record[6]);
			$show_omit = trim($record[7]);
			if ($show_omit == "") {
				$show_omit = "1";
			}
		
			$httpString = $httpString . "\r\n<LISTE oid=\"$oid\" code=\"$code\" texte=\"$texte\" is_cti=\"$is_cti\" show_omit=\"$show_omit\"  table_id=\"$table_id\" table_texte=\"$table_texte\" >";
		
		
			$XMLrow = "";
			
			$liste_codes = array();
		
			
			$result  =  $database->exec($select_cmd);
			
			if ($result != false) {
			
				$columnCount = $database->columnCount();
				
				for ($i = 0; $i < $columnCount; $i++) {
					$fieldName = pg_field_name($result, $i);
					
					$XMLrow = $XMLrow . "<COLUMN name=\"$fieldName\" />";
					
				}
		
			
				// lignes 
				$ok = TRUE;

				while ($ok == TRUE) {
					$ok = FALSE;
				
					$record = $database->nextRecordInto();
												

					if ($record != FALSE) {
						$ok = TRUE;
						
						$r++;
						
						$row = "<RECORD";
							
						$i = 0;
						for ($i = 0; $i < $columnCount; $i++) {
							$fieldName = pg_field_name($result, $i);
							$value = trim($record[$i]);
							$value = str_replace("&", "&amp;", $value);
							$value = str_replace("<", "&lt;", $value);
							$value = str_replace(">", "&gt;", $value);
							$value = str_replace('"', "&quot;", $value);
							$value = str_replace('’', "'", $value);

							$row = "$row $fieldName=\"$value\"";
							
						}
						
						$row = "$row />";
						$XMLrow = $XMLrow . "\n$row";

						if (strlen($XMLrow) > 64000) {
							$httpString = $httpString . $XMLrow;
							$XMLrow = "";
						}
							
						
					}
				}
				
				$httpString = $httpString . $XMLrow;
				$XMLrow = "";
				
			
			}
			
			
			// OIDs déjà choisis
			$result  =  $database->exec("SELECT to_id FROM pmsi.t_listes_contenu WHERE liste_id = $liste_id");
			$XMLrow = "";
			
			if ($result != false) {
			
				// lignes 
				$ok = TRUE;

				while ($ok == TRUE) {
					$ok = FALSE;
				
					$record = $database->nextRecordInto();
												

					if ($record != FALSE) {
						$ok = TRUE;
						
						$oid = trim($record[0]);
						
						$row = "<OID oid=\"$oid\" />";
							
						
						$XMLrow = $XMLrow . "\n$row";

						if (strlen($XMLrow) > 64000) {
							$httpString = $httpString . $XMLrow;
							$XMLrow = "";
						}
							
						
					}
				}
				
				$httpString = $httpString . $XMLrow;
				
				
			
			}
			
			
			$httpString = $httpString . "\r\n</LISTE>";
			
		
		}
		
		
	
	}

		

	
	return $httpString;
	
	

}
    
?>