<?php

function OBOWIAZKI_GRAPH() {

	// items:
	$activeWorkers = array();
	$zasobyStanowiska = array();
	$procesy = array();
	// conections:
	$procesToStanowisko = array();
	$workerToStanowisko = array();

	{// fetch activeWorkers, workerToStanowisko
		$db = DB::getDB();
		$sql = "select u.`ID`
				, u.`ADM_ACCOUNT`
				, u.`ADM_NAME`  -- imie i nazwisko
				, u.`EMPLOYEE_TYPE`  -- EMPLOYEE_TYPE (ldap:employeeType)
				, up.`ID_ZASOB`
			from `ADMIN_USERS` as u
				left join `CRM_AUTH_PROFILE` as up on (up.`REMOTE_ID`=u.`ID` and up.`REMOTE_TABLE`='ADMIN_USERS' and up.`A_STATUS` in('WAITING', 'NORMAL'))
			where u.`A_STATUS` in('WAITING', 'NORMAL')
				and u.`EMPLOYEE_TYPE`='Pracownik'
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			if ($r->ID_ZASOB > 0) {
				$workerToStanowisko[$r->ID][] = $r->ID_ZASOB;
			}
			unset($r->ID_ZASOB);
			$activeWorkers[$r->ID] = $r;
		}
	}// fetch activeWorkers, workerToStanowisko

	{// fetch zasobyStanowiska
		$db = DB::getDB();
		$sql = "select z.`ID`
				, z.`DESC`
			from `CRM_LISTA_ZASOBOW` as z
			where z.`A_STATUS` in('WAITING', 'NORMAL')
				and z.`TYPE` in('STANOWISKO')
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			$zasobyStanowiska[$r->ID] = $r;
		}
	}// fetch zasobyStanowiska

	{// fetch procesy
		$db = DB::getDB();
		$sql = "select p.`ID`
				, p.`DESC`
			from `CRM_PROCES` as p
			where p.`A_STATUS` in('WAITING', 'NORMAL')
				and p.`TYPE` in('PROCES_INIT')
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			$procesy[$r->ID] = $r;
		}
	}// fetch procesy

	{// fetch procesToStanowisko
		$db = DB::getDB();
		$sql = "select w.`ID_PROCES`
				, w.`ID_ZASOB`
			from `CRM_WSKAZNIK` as w
				join `CRM_PROCES` as p on(p.`ID`=w.`ID_PROCES`)
				join `CRM_LISTA_ZASOBOW` as z on(z.`ID`=w.`ID_ZASOB`)
			where w.`A_STATUS` in('WAITING', 'NORMAL')
				and p.`A_STATUS` in('WAITING', 'NORMAL')
				and z.`A_STATUS` in('WAITING', 'NORMAL')
		";
		$res = $db->query($sql);
		while ($r = $db->fetch($res)) {
			$procesToStanowisko[$r->ID_ZASOB][] = $r->ID_PROCES;
		}
	}// fetch procesToStanowisko

	if(V::get('DBG', '', $_GET)) {
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">activeWorkers (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($activeWorkers);echo'</pre>';
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">workerToStanowisko (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($workerToStanowisko);echo'</pre>';
		echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;text-align:left;">procesy('.count($procesy).') (' . __CLASS__ . '::' . __FUNCTION__ . ':' . __LINE__ . '): ';print_r($procesy);echo'</pre>';
	}

	?>
<link rel="stylesheet" href="stuff/joint-js/joint.css" />

<div id="obowiazki_graph" style="border:1px solid #ddd;max-height:600px;overflow:auto;"></div>

<script src="stuff/joint-js/joint.min.js"></script>
<!--
<script src="stuff/joint-js/lodash.min.js"></script>
<script src="stuff/joint-js/backbone-min.js"></script>
<script src="stuff/joint-js/geometry.min.js"></script>
<script src="stuff/joint-js/vectorizer.min.js"></script>
<script src="stuff/joint-js/joint.clean.min.js"></script>
-->
<script>
<?php
	$boxHeight = 20;
?>
var graph = new joint.dia.Graph;

var paper = new joint.dia.Paper({
    el: $('#obowiazki_graph'),
    width: 1000,
    height: 5000,
    model: graph,
    gridSize: 1
});

var workers = {};
<?php $i = 0; foreach ($activeWorkers as $worker) : $i++; ?>
	var worker = new joint.shapes.basic.Rect({
    position: { x: 100, y: <?php echo 10 + ($i * ($boxHeight + 5)); ?> },
    size: { width: 100, height: <?php echo $boxHeight; ?> },
    attrs: { rect: { fill: 'blue' }, text: { text: '<?php echo $worker->ID; ?>', fill: 'white' } }
	});
	workers[<?php echo $worker->ID; ?>] = worker;
<?php endforeach; ?>

var stanowiska = {};
<?php $i = 0; foreach ($zasobyStanowiska as $zasob) : $i++; ?>
	var stanowisko = new joint.shapes.basic.Rect({
    position: { x: 100 + 400, y: <?php echo 10 + ($i * ($boxHeight + 5)); ?> },
    size: { width: 100, height: <?php echo $boxHeight; ?> },
    attrs: { rect: { fill: 'green' }, text: { text: '<?php echo $zasob->ID; ?>', fill: 'white' } }
	});
	stanowiska[<?php echo $zasob->ID; ?>] = stanowisko;
<?php endforeach; ?>

var procesy = {};
<?php $i = 0; foreach ($procesy as $proces) : $i++; ?>
	var proces = new joint.shapes.basic.Rect({
    position: { x: 100 + 800, y: <?php echo 10 + ($i * ($boxHeight + 5)); ?> },
    size: { width: 100, height: <?php echo $boxHeight; ?> },
    attrs: { rect: { fill: 'red' }, text: { text: '<?php echo $proces->ID; ?>', fill: 'white' } }
	});
	procesy[<?php echo $proces->ID; ?>] = proces;
<?php endforeach; ?>

var workerToStanowisko = [];
<?php $i = 0; foreach ($workerToStanowisko as $workerId => $zasobyIds) : $i++; ?>
	<?php foreach ($zasobyIds as $zasobId) : $i++; ?>
		if (workers[<?php echo $workerId; ?>] && 'id' in workers[<?php echo $workerId; ?>]
				&& stanowiska[<?php echo $zasobId; ?>] && 'id' in stanowiska[<?php echo $zasobId; ?>]
			) {
			var link = new joint.dia.Link({
					source: { id: workers[<?php echo $workerId; ?>].id },
					target: { id: stanowiska[<?php echo $zasobId; ?>].id }
			});
			workerToStanowisko.push(link);
		}
	<?php endforeach; ?>
<?php endforeach; ?>

var procesToStanowisko = [];
<?php $i = 0; foreach ($procesToStanowisko as $stanowiskoId => $procesyIds) : $i++; ?>
	<?php foreach ($procesyIds as $procesId) : $i++; ?>
		if (procesy[<?php echo $procesId; ?>] && 'id' in procesy[<?php echo $procesId; ?>]
				&& stanowiska[<?php echo $stanowiskoId; ?>] && 'id' in stanowiska[<?php echo $stanowiskoId; ?>]
			) {
			var link = new joint.dia.Link({
					source: { id: stanowiska[<?php echo $stanowiskoId; ?>].id },
					target: { id: procesy[<?php echo $procesId; ?>].id }
			});
			procesToStanowisko.push(link);
		}
	<?php endforeach; ?>
<?php endforeach; ?>

graph.addCells(workers);
graph.addCells(stanowiska);
graph.addCells(procesy);
graph.addCells(workerToStanowisko);
graph.addCells(procesToStanowisko);

/*
var rect = new joint.shapes.basic.Rect({
    position: { x: 100, y: 30 },
    size: { width: 100, height: 30 },
    attrs: { rect: { fill: 'blue' }, text: { text: 'my box', fill: 'white' } }
});

var rect2 = rect.clone();
rect2.translate(300);

var link = new joint.dia.Link({
    source: { id: rect.id },
    target: { id: rect2.id }
});

graph.addCells([rect, rect2]);
graph.addCells([link]);
*/
</script>
	<?php
}

