#!/usr/bin/env php
<?php
//$_SERVER['SERVER_NAME'] = "biuro.biall-net.pl";
//require("/Library/Server/Web/Data/Sites/Default/dev-bzyk/se-lib/bootstrap.php");
require("config_inc.php"); //generated by build_BI_audit.xml

#require("/Library/Server/Web/Data/Sites/SE-producition-git/SE/se-lib/bootstrap.php");
date_default_timezone_set('Europe/Warsaw');// PHP 5 >= 5.1.0 required by date functions
Lib::loadClass('V');

if (!isset($argv[1])) die("Missing parameter\n");
if ($argv[1] == "-") $data = stream_get_contents(STDIN);
else {
	if (!is_file($argv[1])) die("File {$argv[1]} not found\n");
	$data = @file_get_contents($argv[1]);
	if (!$data) die("Error loading file\n");
}
//preg_match('/.*\.([[:alpha:]]*)$/', $argv[1], $match);
//switch ($match[1]) {
//	case "data":
		$results = @json_decode(gzuncompress(base64_decode($data)), true);
//		break;
//	case "txt":
//		$results = unserialize($data);
//		break;
//	default:
//		die("Unknown file type\n");
//}


if (!$results) die("Error loading data\n");

//echo "Liczba wyników: " . count($results) . "\n";
foreach ($results as $result) echo implode("->", array_keys($result)) . "\n";
//print_r($results);
die();


$TABLE = "BI_audit_ALL";

//$query = "select `DATA` from BI_audit_ALL_results where ID=4";
//$data = DB::getPDO()->fetchvalue($query);
//$results = json_decode(gzuncompress(base64_decode($data)), true);

$xmlRoot = "RelatedFeatureRoot";
$xmlRoot2 = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA";
$xmlElements = "BI_audit_ENERGA_RUM_KONTRAHENCI_POWIAZANIA_row";

$items_results = [];
foreach ($results as $result) {
	$item_results = [];
	foreach ($result as $ID => $rel) {
		$query = "select REMOTE_TABLE, REMOTE_ID from {$TABLE} where ID = '{$ID}'";
		$row = DB::getPDO()->fetchFirst($query);
		$query = "select * from `{$row['REMOTE_TABLE']}` where ID = '{$row['REMOTE_ID']}'";
		$obj = DB::fetch(DB::query($query));
		$item_result = [$row['REMOTE_TABLE'] => (array) $obj];
		if ($rel) $item_result['relation_from'] = $rel;
		$item_results['object'][] = $item_result;
		if ($row['REMOTE_TABLE'] == "BI_audit_ENERGA_RUM_KONTRAHENCI") {
			$query = "select umowy.* from BI_audit_ENERGA_RUM_UMOWY umowy join `CRM__#REF_TABLE__23` ref on umowy.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
			$res = DB::query($query);
			$umowy = [];
			$i = 0;
			while ($obj = DB::fetch($res)) {
				if (++$i > 50) {
					//$umowy[] = "Przekroczono limit 50 rekordów (pominięto " . (mysql_num_rows($res) - 50) . " z " . mysql_num_rows($res) . ")";
					break;
				}
				$umowy[] = (array) $obj;
			}
			if ($umowy) {
				$item_results['deal'] = $umowy;
			}
		} elseif ($row['REMOTE_TABLE'] == "BI_audit_KW_requested_person") {
			$query = "select kw.* from BI_audit_KW_requested kw join `CRM__#REF_TABLE__24` ref on kw.ID = ref.PRIMARY_KEY where ref.REMOTE_PRIMARY_KEY = '{$row['REMOTE_ID']}'";
			$res = DB::query($query);
			$kw = [];
			while ($obj = DB::fetch($res)) {
				$kw[] = (array) $obj;
			}
			if ($kw) {
				$item_results['kw'] = $kw;
			}
		}
	}
	$items_results[$xmlRoot2][$xmlElements][] = $item_results;
}
//print_r($items_results);
$test = V::arrayToXML($items_results, true, $xmlRoot);
echo $test;
