#!/usr/bin/env php
<?php

mysql_connect('192.168.61.253', 'root', 'kontem11on');
mysql_select_db('SES_USERS2');
mysql_query("set names utf8");

class FindCorelations {

	private $TABLE_REF, $DEPTH;
	public $results = array();
	private $nodesCache = array();
	private $path = array();

	function __construct($TABLE_REF, $DEPTH) {
		$this->TABLE_REF = $TABLE_REF;
		$this->DEPTH = $DEPTH;
	}

	public function findNodes($ID, $relation = "", $end = 0) {
		if (!$this->path) $this->firstNodes[] = $ID;
		if (isset($this->path[$ID])) {
			return false;
		}

		$this->path[$ID] = $relation;
		if (!$relation) $relation = 0;

		if ($end) {
			$this->results[] = $this->path;
			array_pop($this->path);
			return true;
		}

		if (count($this->path) > $this->DEPTH) {
			array_pop($this->path);
			return false;
		}

		$nodes = array();
		if (isset($this->nodesCache[$ID])) {
			$nodes = $this->nodesCache[$ID];
		} else {
			$nodes = array();
//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref join BI_audit_ALL_usable usable on ref.ID2 = usable.ID_BI_audit_ALL where ref.ID1 = '{$ID}'";
//			$query = "select ref.ID2, RELATION, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";
//			$where = "";
//			if ($relation) $where = "and ({$relation} & ref.RELATION_ID) != {$relation}";
//			else $where = "";
			$query = "select ref.ID2, RELATION_ID, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}'";// {$where}";
//echo $query."\n";
			//$query = "select ref.ID2, RELATION, RELATION_ID, END from {$this->TABLE_REF} ref where ref.ID1 = '{$ID}' and ref.RELATION != '{$relation}'";
			if ($result = mysql_query($query)) {
				while ($row = mysql_fetch_object($result)) {
					//if (!in_array($row->ID2, $nodes)) 
			//		$nodes[$row->ID2] = array('relation' => explode(", ", $row->RELATION), 'end' => $row->END);
					$nodes[$row->ID2] = array('relation' => (int) $row->RELATION_ID, 'end' => $row->END);
				}
			}
			if ($nodes) {
				ksort($nodes);
				$this->nodesCache[$ID] = $nodes;
			}
		}

		if ($nodes) {
			foreach ($nodes as $node => $data) {
				//if (!$relation ||array_diff(explode(", ", $relation), explode(", ", $data['relation'])))
//echo $relation . " : " . $data['relation'] . " : " . (($relation & $data['relation'])) . "\n";
				if (!($relation && (($relation & $data['relation']) == $relation))) {
//					echo $relation . " : " . $data['relation'] . " : " . (($relation & $data['relation'])) . "\n";
					$this->findNodes($node, $data['relation'], $data['end']);
//				} else {
//					echo $relation . " : " . $data['relation'] . " : " . ((int) $relation & (int) $data['relation']) . "\n";
				}
			}
		}
		array_pop($this->path);
	}
}



$TABLE = "BI_audit_ALL";
$TABLE_REF = "BI_audit_ALL_ref";

$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";
//$query = "select t.ID from {$TABLE} as t join BI_audit_ENERGA_PRACOWNICY pracownicy on t.REMOTE_ID = pracownicy.ID where pracownicy.source = 'ENERGA' and REMOTE_TABLE = 'BI_audit_ENERGA_PRACOWNICY'";

$result = mysql_query($query);
$test = new FindCorelations($TABLE_REF, 8);
while ($row = mysql_fetch_object($result)) $test->findNodes($row->ID);
$results = $test->results;

$data = base64_encode(gzcompress(json_encode($results)));
echo $data;
