<?php

// IMPORT DES BIBLIOTHEQUES
require_once("../../base/php/startSession.php");
require_once("../../base/php/classDatabase.php");
require_once("../../base/php/Functions.php" );
require_once("../../base/php/WebAppLog.php");

// DECLARATION DES VARIABLES
$compress64 = true;
$database = new Database("iCTI");
$schema = "";

// RECUPERATION DES PARAMETRES HTTP
$tableName = getPOST("tableName");
$firstCall = getPOST("firstCall");
$updatedOids = getPOST("updatedOids");

// ECRITURE DE LA REPONSE HTTP
$httpString = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
$httpString .= "\n<HTTP_SERVICE>";
if ($tableName != "") {
	$httpString .= getRecords($database, $tableName, $firstCall, $updatedOids);
}
$httpString .= "\n</HTTP_SERVICE>";
if ($compress64) {
	$httpString = compress64($httpString);
}
echo "<CTICONTENT>$httpString</CTICONTENT>";

// DEFINITIONS DE FONCTION


/**
 *
 * @param Database $db
 * @param string $tableName
 * @return string
 */
function getRecords($db, $tableName, $firstCall=0, $updatedOids="") {
	
	if ($tableName == "encours") {
		return getRecords_encours($db, $tableName, $firstCall, $updatedOids);
	}
	
	
}
/**
 *
 * @param Database $db
 * @param string $tableName
 * @return string
 */
function getRecords_encours($db, $tableName, $firstCall=0, $updatedOids="") {
	$httpString = '';
	
	$httpString_tmp = "";

    /** Informations d'autentification communes */
    $dbuser = $db->user;
    $dbpassword = $db->password;

    $sqlcmd = "
		DROP TABLE IF EXISTS w_database;
		CREATE TEMP TABLE w_database AS
		SELECT 
			t_finess.oid,
			replace(t_finess_database.dbname,'icti_','') AS code,
			t_finess.texte,
			dbname,
			dbhost,
			dbport,
			'0'::text AS disabled
		FROM base.t_finess
		JOIN base.t_finess_database ON finess_id = t_finess.oid
		JOIN dblink('host=' || dbhost || ' dbname=postgres port=' || dbport || ' user=$dbuser password=$dbpassword', 
			'SELECT ''1''::text AS ok 
			FROM pg_database
			WHERE datname = ''' || dbname || ''' ') 
			AS dblink(ok text) ON ok='1'
		ORDER BY 4
		;

		DROP TABLE IF EXISTS w_encours_status;
		CREATE TEMP TABLE w_encours_status AS
		SELECT 
			oid,
			code,
			texte,
			encoursactive,
			dateencours,
			encoursnow
		FROM w_database
		JOIN dblink('host=' || dbhost || ' dbname=' || dbname || ' port=' || dbport || ' user=$dbuser password=$dbpassword', 
			'SELECT ''1''::text AS ok 
			FROM pg_tables 
			WHERE schemaname = ''activite'' AND tablename= ''t_divers''') 
			AS dblink(ok text) ON ok='1'
		JOIN dblink('host=' || dbhost || ' dbname=' || dbname || ' port=' || dbport || ' user=$dbuser password=$dbpassword', 
			'SELECT 
				COALESCE(MAX(CASE WHEN code = ''ACT_DAT_FIN'' THEN valeur ELSE '''' END),'''') AS actdatefin
			FROM activite.t_divers
			WHERE code IN (''ACT_DAT_FIN'')') 
			AS dblinkactive(actdatefin text) ON actdatefin = ''
		JOIN dblink('host=' || dbhost || ' dbname=' || dbname || ' port=' || dbport || ' user=$dbuser password=$dbpassword', 
			'SELECT 
				MAX(CASE WHEN code = ''ENCOURSACTIVE'' THEN valeur ELSE '''' END) AS encoursactive,
				MAX(CASE WHEN code = ''DATEENCOURS'' THEN lpad(valeur,2,''0'') ELSE '''' END) AS dateencours,
				MAX(CASE WHEN code = ''ENCOURSNOW'' THEN valeur ELSE '''' END) AS encoursnow
			FROM activite.t_divers
			WHERE code IN (''ENCOURSACTIVE'',''DATEENCOURS'',''ENCOURSNOW'')
			') 
			AS dblink_data(encoursactive text, dateencours text, encoursnow text) ON encoursactive='1'
		;
		INSERT INTO w_encours_status
		SELECT 0,
			'***',
			'Modifier tous les environnements',
			'1',
			MIN(dateencours),
			MAX(encoursnow)
		FROM w_encours_status
		WHERE encoursactive='1';
		SELECT * 
		FROM w_encours_status
		WHERE encoursactive='1'" ;

	if ($updatedOids != "") {
		$sqlcmd.= "AND
				oid in ($updatedOids) ";
	}
	$sqlcmd.= "
			ORDER BY 2";
	
	
	$result = $db->exec($sqlcmd);
	
	if ($result != false) {
		// lignes
		$ok = TRUE;
		while ($ok == TRUE) {
			$ok = FALSE;
			$record = $db->nextRecordAssoc();
			if ($record != FALSE) {
				$ok = TRUE;
				$httpString_tmp .= '\r\n<RECORD ';
				$httpString_tmp .= 'oid="' . $record['oid'] . '" ';
				$httpString_tmp .= 'code="' . toHTML(trim($record['code'])) . '" ';
				$httpString_tmp .= 'texte="' . toHTML(trim($record['texte'])) . '" ';
				$httpString_tmp .= 'encoursactive="' . toHTML(trim($record['encoursactive'])) . '" ';
				$httpString_tmp .= 'dateencours="' . toHTML(trim($record['dateencours'])) . '" ';
				$httpString_tmp .= 'encoursnow="' . toHTML(trim($record['encoursnow'])) . '" ';
				$httpString_tmp .= ' />';
				if (strlen($httpString_tmp) > 64000) {
					$httpString .= $httpString_tmp;
					$httpString_tmp = "";
				}
			}
		}
	}
	$httpString .= $httpString_tmp;
	
	if ($firstCall != "0") {
		
	}
	
	return $httpString;
}


?>