<?php

Lib::loadClass('ProcesHelper');
Lib::loadClass('UsersHelper');

function GRAPH_VIEW_PROCES() {
	$id_proces = V::get('id_proces', '', $_REQUEST, 'int');
	$id_zasob = V::get('id_zasob', '', $_REQUEST, 'int');

	$db = DB::getDB();

	$ajaxTask = V::get('ajaxTask', '', $_REQUEST);
	if ($ajaxTask == 'getInfo') {
		$id = V::get('id', 0, $_REQUEST, 'int');
		$id_zasob = V::get('id_zasob', '', $_REQUEST, 'int');

		if ($id > 0) {
			$step = $db->get_by_id('CRM_PROCES', $id);
			$cp = new GraphViewProcessParser();
			$tree = $cp->build_proces_init_tree($id);
			$cp->build_tree_flat_info($tree, null);
			$cp->detect_tree_where_to_detail_cut($tree);
			$cp->build_path_zoom_proces_cut($tree, $id);

			?>
			<?php if ($step->TYPE == 'PROCES_INIT') : ?>
				<a href="?FUNCTION_INIT=GRAPH_VIEW_PROCES&id_proces=<?php echo $step->PARENT_ID; ?>&PROCES_INIT_SCAN=1">Widok mapy <?php echo $step->PARENT_ID; ?></a>
			<?php endif; ?>
			<?php if (empty($_REQUEST['RESOURCES'])) : ?>
				<a href="?FUNCTION_INIT=GRAPH_VIEW_PROCES&id_proces=<?php echo $step->PARENT_ID; ?>&PROCES_INIT_SCAN=1&RESOURCES=1&id_zasob=<?php echo $id_zasob; ?>"> | mapa z zasobami</a>
			<?php endif; ?>
			<p>
				<span style="color:red">
					<a href="?FUNCTION_INIT=GRAPH_VIEW_PROCES&id_proces=<?php echo $id; ?>"><?php echo $step->ID; ?></a>
					- <?php echo $step->DESC; ?> <?php echo $step->DESC; ?>
				</span>
				| <a href="?FUNCTION_INIT=GRAPH_VIEW_PROCES&id_proces=<?php echo $id; ?>&RESOURCES=1&id_zasob=<?php echo $id_zasob; ?>"> widok procesu z zasobami </a>
			</p>
			<p><?php echo $step->OPIS; ?></p>
				<?php foreach ($tree->Viev_Zpc[$id]->PATH as $ind=>$id_include) : ?>
					<?php if ($ind == 0) continue; //pierwszy wiersz wyswietlony wyzej ?>
					<?php $step = $db->get_by_id('CRM_PROCES', $id_include); ?>
					<p><b><?php echo $step->ID; ?></a> - <?php echo $step->DESC; ?></b> <font size="-3"><?php echo $step->OPIS; ?></font></p>
				<?php endforeach; ?>
			<?php
		}
		else {//zasob
			$id = V::get('id', 0, $_REQUEST);
			if ($id[0] == 'Z') {
				$zasob = ProcesHelper::getZasobInfo(4975);
				echo "Zasob: [".$zasob->ID."], Typ: ".$zasob->TYPE.", Nazwa: ".$zasob->DESC." (".$zasob->DESC_PL.")<br>".$zasob->OPIS;
				echo "<hr> Nadrzędny: Zasob: [".$zasob->P__ID."] , Typ: ".$zasob->P__TYPE.", Nazwa: ".$zasob->P__DESC." (".$zasob->P__DESC_PL.")<br>".$zasob->P__OPIS;
			}
		}
		exit;
	}

	$graph = new stdClass();
	$graph->htmlID = "graph_proces_id_{$id_proces}";
	$graph->procesTreeFlat = array();
	$graph->procesTreeGoto = array();
	$graph->treeItems = array();

	$graph->elements = new stdClass();
	$graph->elements->nodes = array();
	$graph->elements->edges = array();

	function graph__addNodeID($id, &$graph, $name = null, $path, $parent = null, $type, $once = null) {
		if (!array_key_exists($id, $graph->treeItems)) {
			$graph->treeItems[$id] = true;
			if (empty($name)) $name = $id;
			if (!empty($path)) {
				$name = $name . " " . implode(',', $path);
			}
			$type_init = null;
			if ($type == 'PROCES_INIT') $type_init = 'procesInit';
			else if($type == 'RESOURCE') $type_init = 'RESOURCE';

			$graph->elements->nodes[] = array('data' => array('id' => "{$id}", 'name' => $name, 'type' => $type_init));
		}
		if (!empty($parent)) {
			if (isset($once)) {
				if (!isset($graph->used_elements[$parent][$id]) && !isset($graph->used_elements[$id][$parent])) {
					$graph->elements->edges[] = array('data' => array('source' => $parent, 'target' => $id));
				}
			} else {
				$graph->elements->edges[] = array('data' => array('source' => $parent, 'target' => $id));
			}
			$graph->used_elements[$parent][$id] = 1;
			$graph->used_elements[$id][$parent] = 1;
		}
	}

	$id_zasob = V::get('id_zasob', '', $_REQUEST, 'int');

	$PROCES_INIT_SCAN = V::get('PROCES_INIT_SCAN', '', $_REQUEST, 'int');
	if (!empty($PROCES_INIT_SCAN)) {
		$cp = new GraphViewProcessParser();

		$PROCES_INIT_SCAN_STANOWISKO = V::get('PROCES_INIT_SCAN_STANOWISKO', '', $_REQUEST, 'int');
		$PROCES_INIT_SCAN_USER = V::get('PROCES_INIT_SCAN_USER', '', $_REQUEST, 'int');

		if (!empty($PROCES_INIT_SCAN_STANOWISKO)) {
			$PROCES_INIT_SCAN_STANOWISKA = array($PROCES_INIT_SCAN_STANOWISKO);
			$proces_list_obj = ProcesHelper::get_procesy_by_stanowiska($PROCES_INIT_SCAN_STANOWISKA);
			foreach ($proces_list_obj as $obj) {
				$init[$obj->ID] = $obj->ID;
			}
		} else if(!empty($PROCES_INIT_SCAN_USER))  {
			$proces_list_obj = UsersHelper::get_group_by_user($PROCES_INIT_SCAN_USER, array('SHOW_IN_PERIOD_MARK' => 'YES'));
			foreach ($proces_list_obj as $obj) {
				$init[$obj->ID] = $obj->ID;
			}
		} else if(!empty($id_zasob)) {
			$init = ProcesHelper::get_proces_init_by_zasob_id($id_zasob);
		} else {
			$init = $cp->get_proces_init($id_proces);
		}
		DEBUG_S(3,'lista procesow init',$init,__FILE__,__FUNCTION__,__LINE__);
		foreach ($init as $id_proces) {
			$tree = $cp->build_proces_init_tree($id_proces);
			$cp->build_tree_flat_info($tree, null);
			$cp->detect_tree_where_to_detail_cut($tree, 'nocut');
			$cp->build_path_zoom_proces_cut($tree, $id_proces);
			DEBUG_S(3,'get_proces_init',$init);
			DEBUG_S(3,'tree->childs',$tree->Childs);
			DEBUG_S(3,'tree->Parents',$tree->Parents);
			DEBUG_S(3,'tree->Viev_Zpc',$tree->Viev_Zpc);
			DEBUG_S(3,'tree->Viev_Zpc_Parent',$tree->Viev_Zpc_Parent);
			DEBUG_S(3,'tree->get_resources_by_proceses_id',$tree->get_resources_by_proceses_id);
			graph__addNodeID(0, $graph, $name = null, $path, null);
			foreach ($tree->Viev_Zpc as $id => $obj) {
				graph__addNodeID($id, $graph, $obj->DESC, null, ((isset($obj->PARENT))? $obj->PARENT : null), $tree->Childs[$id]->TYPE);
				foreach ($obj->EXTERNAL_LINK as $link) {
					graph__addNodeID($link, $graph, $tree->Viev_Zpc[$link]->DESC, null, $id, 'PROCES_INIT');
				}
			}
			if (!empty($_REQUEST['RESOURCES'])) {
				$resources = $cp->get_resources_by_proceses_id($tree->Childs, $id_proces, $id_zasob);
				foreach ($resources['PARENT_ID'] as $ID => $DESC) {
					graph__addNodeID("Z_{$ID}", $graph, $DESC, null, null, 'RESOURCE');
				}
				foreach ($resources['ID'] as $ID => $DESC) {
					graph__addNodeID("Z_{$ID}", $graph, $DESC['DESC'], null, $id_proces, 'RESOURCE');
					graph__addNodeID("Z_{$ID}", $graph, $DESC['DESC'], null, "Z_{$DESC['PARENT_ID']}", 'RESOURCE', 'once');
				}
			}
			unset($tree);
		}
	}
	else {
		$cp = new GraphViewProcessParser();
		$tree = $cp->build_proces_init_tree($id_proces);
		$cp->build_tree_flat_info($tree, null);
		$cp->detect_tree_where_to_detail_cut($tree);
		$cp->build_path_zoom_proces_cut($tree, $id_proces);
		DEBUG_S(3,'get_proces_init',$init);
		DEBUG_S(3,'tree->childs',$tree->Childs);
		DEBUG_S(3,'tree->Parents',$tree->Parents);
		DEBUG_S(-3,'tree->Viev_Zpc',$tree->Viev_Zpc);
		DEBUG_S(3,'tree->Viev_Zpc_Parent',$tree->Viev_Zpc_Parent);
		DEBUG_S(3,'tree->Viev_Zpc_Parent_cut',$tree->Viev_Zpc_Parent_cut);
		DEBUG_S(3,'tree->Viev_Zpc_found',$tree->Viev_Zpc_found);
		graph__addNodeID(0, $graph, $name = null, $path, null);
		foreach ($tree->Viev_Zpc as $id => $obj) {
			graph__addNodeID($id, $graph, $obj->DESC, null, ((isset($obj->PARENT))? $obj->PARENT : null), $tree->Childs[$id]->TYPE);
			foreach ($obj->INTERNAL_LINK as $link) {
				graph__addNodeID($id, $graph, $tree->Viev_Zpc[$link]->DESC, null, $link, null);
			}
			foreach ($obj->EXTERNAL_LINK as $link) {
				graph__addNodeID($link, $graph, $tree->Viev_Zpc[$link]->DESC, null, $id, 'PROCES_INIT');
			}
		}
		if (!empty($_REQUEST['RESOURCES'])) {
			$resources = $cp->get_resources_by_proceses_id($tree->Childs, $id_proces, $id_zasob);
			foreach ($resources['PARENT_ID'] as $ID => $DESC) {
				graph__addNodeID("Z_{$ID}", $graph, $DESC, null, null, 'RESOURCE');
			}
			foreach ($resources['ID'] as $ID => $DESC) {
				graph__addNodeID("Z_{$ID}", $graph, $DESC['DESC'], null, $id_proces, 'RESOURCE');
				graph__addNodeID("Z_{$ID}", $graph, $DESC['DESC'], null, "Z_{$DESC['PARENT_ID']}", 'RESOURCE');
			}
		}
	}

	foreach($graph->elements->nodes as $var) {
		$nodes[$var['data']['id']] = $var['data']['id'];
	}
	foreach($graph->elements->edges as $var) {
		if (!isset($nodes[$var['data']['source']])) echo "<br>FAIL1 ".$var['data']['source'];
		if (!isset($nodes[$var['data']['target']])) echo "<br>FAIL2 ".$var['data']['target'];
	}
	DEBUG_S(3,'nodes',$graph->elements->nodes);
	DEBUG_S(3,'edges',$graph->elements->edges);
?>
<style type="text/css">
#<?php echo $graph->htmlID; ?>_wrap {
	position:relative;
	width:1100px;
	margin:0 auto;
	border:1px solid #ccc;
}
#<?php echo $graph->htmlID; ?> {
	height:860px;
	border-bottom:1px solid #ccc;
}
#<?php echo $graph->htmlID; ?>_info {
	padding:3px;
}
#<?php echo $graph->htmlID; ?>_wrap .actions { position:absolute; top:20px; left:-40px; margin:0; padding:0; list-style:none; }
#<?php echo $graph->htmlID; ?>_wrap .actions button { border-radius:0; background:#fff; border:1px solid #ccc; }
</style>
<script src="./stuff/cytoscape.js/arbor.js"></script>
<script src="./stuff/cytoscape.js/cytoscape.js"></script>
<script>
jQuery(loadInfo = function(nodeID){
	$.ajax({
		url: 'index.php?MENU_INIT=GRAPH_VIEW_PROCES&id_proces=<?php echo $id_proces; ?>&ajaxTask=getInfo&id=' + nodeID + '&id_zasob=<?php echo $id_zasob; ?>&HEADER_NOT_INIT=YES',
		type: 'GET',
		dataType: 'text',
		data: '',
		async: true,
		success: function (data) {
			jQuery('#<?php echo $graph->htmlID; ?>_info').html(data);
		},
		error: function (err) {
			console.log('err');
		}
	});
});
jQuery(loadCy = function(){
	jQuery('#<?php echo $graph->htmlID; ?>').cytoscape({
		layout: {
			name: 'arbor'
		},
		style: cytoscape.stylesheet()
			.selector('node')
				.css({
					'shape': 'rectangle',
					'width': '100px', 'height': '8px',
					'font-size': '10px',
					'content': 'data(name)',
					'text-valign': 'center',
					'color': 'white',
					'text-outline-width': 2,
					'text-outline-color': '#888'
				})
			.selector('edge')
				.css({
					'target-arrow-shape': 'triangle'
				})
			.selector(':selected')
				.css({
					'background-color': 'black',
					'line-color': 'black',
					'target-arrow-color': 'black',
					'source-arrow-color': 'black'
				})
			.selector('.faded')
				.css({
					'opacity': 0.25,
					'text-opacity': 0
				})
			.selector('edge.neighborhood')
				.css({
					'background-color': 'blue',
					'color': 'orange'
				})
			.selector('node.procesInit')
				.css({
					'background-color': 'red',
					'text-outline-color': 'red',
					'font-weight': 'bold'
				})
			.selector('node.RESOURCE')
				.css({
					'background-color': 'black',
					'text-outline-color': 'black',
					'font-weight': 'normal','font-size': '8'

				})
				,

		elements: <?php echo json_encode($graph->elements); ?>,

		ready: function(){
			var cy = this;
			window.cy_<?php echo $graph->htmlID; ?> = this;

			cy.elements("node[type = 'procesInit']").addClass('procesInit');
			cy.elements("node[type = 'RESOURCE']").addClass('RESOURCE');
			cy.elements("node[lvl > 1]").hide();

			cy.elements().unselectify();

			cy.on('tap', 'node', function(e){
				//console.log('on tap node', e)
				var node = e.cyTarget;
				var neighborhood = node.neighborhood().add(node);

				var nodeID = node.id();
				loadInfo(nodeID);

				cy.elements().addClass('faded').unselect();
				neighborhood.removeClass('faded');
				node.select();

				cy.edges().removeClass('neighborhood');
				node.connectedEdges().addClass('neighborhood');

				neighborhood.show();

				cy.center(neighborhood);
			});

			cy.on('tap', function(e){
				if (e.cyTarget === cy){
					cy.elements().removeClass('faded');
				}
			});
		},
		showOverlay: false
	});
});

jQuery(document).ready(function(){
	loadCy();

	var parent = $('#<?php echo $graph->htmlID; ?>')
		.parent()
	parent.find('.actions .cy-refresh')
		.on('mousedown touchstart', function(){
			loadCy();

			cy.attr('style', '');
		});
	parent.find('.actions .cy-zoom-plus')
		.on('mousedown touchstart', function(){
			window.cy_<?php echo $graph->htmlID; ?>.zoom(window.cy_<?php echo $graph->htmlID; ?>.zoom() + 0.1);
		});
  parent.find('.actions .cy-zoom-minus')
		.on('mousedown touchstart', function(){
			window.cy_<?php echo $graph->htmlID; ?>.zoom(window.cy_<?php echo $graph->htmlID; ?>.zoom() - 0.1);
		});
});
</script>
<div id="<?php echo $graph->htmlID; ?>_wrap">
	<div id="<?php echo $graph->htmlID; ?>"></div>
	<div id="<?php echo $graph->htmlID; ?>_info"></div>
	<ul class="actions">
		<li><button class="btn cy-refresh"><i title="Refresh" class="ico-refresh"></i></button></li>
		<li><button class="btn cy-zoom-plus"><i title="Zoom +" class="ico-plus"></i></button></li>
		<li><button class="btn cy-zoom-minus"><i title="Zoom -" class="ico-minus"></i></button></li>
	</ul>
</div>

<?php
}

class GraphViewProcessParser {

// var $this->get_proces_init=array();
// [proces_init]  - A
//    [step]      - flat 0
//    [step]      - flat 3 , tree 2 , cut
//     [step]     - flat 0 , tree 0
//     [step]     - flat 1 , tree 1
//       [step]   - flat 1 , tree 1
//         [step] - flat 0 , tree 0
//     [step]     - flat 1 , tree 1
//       [step]   - flat 0 , tree 0

	public function get_proces_init($id_proces) {
		$db = DB::getDB();
		$sql = "select
				p.`ID`
				, p.`DESC`
			from `CRM_PROCES` as p
				left join `_CRM_PROCES_INIT_STATS` as cs on(cs.`ID` = p.`ID`)
			where
				p.`A_STATUS` in('WAITING','NORMAL')
				and p.`TYPE`='PROCES_INIT'
				and (cs.`path` like '%/{$id_proces}/%' or cs.`path` like '%/{$id_proces}'  or cs.`path` like '{$id_proces}/%')
		";
		$res = $db->query($sql);
		while ($h = $db->fetch($res)) {
			$this->get_proces_init[] = $h->ID;
		}
		return $this->get_proces_init;
	}

	public function build_proces_init_tree($id_proces) {
		$db = DB::getDB();
		$tree->Childs[$id_proces]->PARENT = 0;
		$tree->Childs[$id_proces]->SOURCE = 'build_tree_for_proces';
		$tree->Parents[0]->CHILD[$id_proces] = $id_proces;
		$obj = $db->get_by_id('CRM_PROCES', $id_proces);
		$tree->Childs[$id_proces]->TYPE = $obj->TYPE;
		$this->build_tree_for_proces($tree, $id_proces, $id_proces);
		return $tree;
	}

	public function build_tree_for_proces(&$tree, $cursor, $init_once = null) {
		$db = DB::getDB();
		$sqlOr = (!empty($init_once))? " or cp.ID={$init_once} " : "";
		$sql = "select
			cp.`ID`
			, cp.`IF_TRUE_GOTO`
			, cp.`IF_TRUE_GOTO_FLAG`
			, cp.`TYPE`
		from `CRM_PROCES` as cp
		where
			(cp.PARENT_ID={$cursor} {$sqlOr})
			and  cp.`A_STATUS` in('WAITING','NORMAL')
		";
		$res = $db->query($sql);
		while ($h = $db->fetch($res)) {
			if (empty($init_once)) $tree->Childs[$h->ID]->PARENT = $cursor;
			if (empty($init_once)) $tree->Parents[$cursor]->CHILD[$h->ID] = $h->ID;
			$tree->Childs[$h->ID]->TYPE = $h->TYPE;
			$tree->Childs[$h->ID]->SOURCE = 'build_tree_for_proces';
			if ($h->IF_TRUE_GOTO > 0) {
				$tree->Childs[$h->ID]->GOTO_FLAG = $h->IF_TRUE_GOTO_FLAG;
				$tree->Childs[$h->ID]->IF_TRUE_GOTO = $h->IF_TRUE_GOTO;
				$tree->Childs[$h->IF_TRUE_GOTO]->IS_EXECUTED_BY[$h->ID] = $h->ID;
			}
			$this->build_tree_for_proces($tree, $h->ID, null);
		}
	}

	public function get_resources_by_proceses_id($id_proces_obj, $id_proces, $filter_zasob) {
		$db = DB::getDB();
		$filter = "";
		if (is_array($filter_zasob)) {
			$filter = " and cz.ID in (".implode(',', $filter_zasob).") ";
		}
		else if (!empty($filter_zasob)) {
			$filter = " and cz.ID={$filter_zasob}";
		}
		$sql = "select
				cz.ID
				, cz.PARENT_ID
				, cz.`DESC`
				, cz.`TYPE`
				, czp.`DESC` as DESC_czp
				, czp.`TYPE` as TYPE_czp
			from CRM_WSKAZNIK as cw
				left join CRM_LISTA_ZASOBOW as cz on(cz.ID=cw.ID_ZASOB)
				left join CRM_LISTA_ZASOBOW as czp on(czp.ID=cz.PARENT_ID)
			where cw.ID_PROCES in(".implode(',', array_keys($id_proces_obj)).")
				and cw.A_STATUS in ('NORMAL','WAITING')
				and czp.`TYPE` in ('TABELA')
				{$filter}
		";
		$res = $db->query($sql);
		while ($h = $db->fetch($res)) {
			$found['ID'][$h->ID]['PARENT_ID'] = $h->PARENT_ID;
			$found['ID'][$h->ID]['DESC'] = $h->DESC;
			$found['PARENT_ID'][$h->PARENT_ID] = $h->DESC_czp;
		}
		return $found;
	}

	public function build_tree_flat_info(&$tree, $flat) {
		foreach ($tree->Childs as $id => $obj) {
			//sprawdzmy czy nasz parent wymaga wyswietlenia osobnego podkroku w widoku szczegolowego procesu
			foreach ($tree->Parents[$id]->CHILD as $child) {
				$tree->Childs[$id]->CHILD[$child] = count($tree->Parents[$child]->CHILD);
			}
		}
	}

	public function detect_tree_where_to_detail_cut(&$tree, $flag_not_to_cut = null) {
		foreach ($tree->Childs as $id => $obj) {
			//sprawdzmy czy nasz parent wymaga wyswietlenia osobnego podkroku w widoku szczegolowego procesu
			$control = 0;
			foreach ($obj->CHILD as $child) {
				if ($child > 0) $control++;
			}
			if (empty($flag_not_to_cut)) {
				if ($control > 1) {
					//przeciecie jezeli jest wiecej wyjsc niz jedno z zaglebieniami
					$tree->Childs[$id]->TO_DETAIL_CUT_FLAG = true;
				}
				if (isset($tree->Childs[$id]->GOTO_FLAG)) {
					//przeciecie, jezeli jest wyjscie z kroku procesu do innego 
					$tree->Childs[$id]->TO_DETAIL_CUT_FLAG = true;
				}
				if (isset($tree->Childs[$id]->IS_EXECUTED_BY)) {
					//trzeba zbadac tez czy nie jest do niego dowiazywany w jakims miejscu proces
					$tree->Childs[$id]->TO_DETAIL_CUT_FLAG = true;
				}
			} else {
				$tree->Childs[$id]->NO_CUT_FLAG = true;
			}
		}
	}

	public function search_parent_ids_till_cut(&$tree, $init, &$find) {
		//szukam lancuchow do optymalizacji do miejsca flagi CUT, kojarzac od razu linki
		if (isset($tree->Childs[$init]->TO_DETAIL_CUT_FLAG)) {
			$this->zoom_process_links($tree, $init, $init);
		}
		if (!isset($tree->Childs[$init]->TO_DETAIL_CUT_FLAG)) {
			$find[] = $init;
			$this->zoom_process_links($tree, $init, $find[0]);
			foreach($tree->Childs[$init]->CHILD as $id => $count) {
				$this->search_parent_ids_till_cut($tree, $id, $find);
			}
		}
	}

	public function zoom_process_links($tree, $id, $root_id) {
		$db = DB::getDB();
		if (isset($tree->Childs[$id]->GOTO_FLAG)) {
			if (isset($tree->Childs[$tree->Childs[$id]->IF_TRUE_GOTO])) {
				if (isset($tree->Childs[$tree->Childs[$id]->IF_TRUE_GOTO]->SOURCE)) {
					$tree->Viev_Zpc[$root_id]->INTERNAL_LINK[$tree->Childs[$id]->IF_TRUE_GOTO] = $tree->Childs[$id]->IF_TRUE_GOTO;
				} else {
					$tree->Viev_Zpc[$root_id]->EXTERNAL_LINK[$tree->Childs[$id]->IF_TRUE_GOTO] = $tree->Childs[$id]->IF_TRUE_GOTO;
				}
			} else {
				$tree->Viev_Zpc[$root_id]->EXTERNAL_LINK[$tree->Childs[$id]->IF_TRUE_GOTO] = $tree->Childs[$id]->IF_TRUE_GOTO;
				$obj = $db->get_by_id('CRM_PROCES',$tree->Childs[$id]->IF_TRUE_GOTO);
				$tree->Viev_Zpc[$tree->Childs[$id]->IF_TRUE_GOTO]->DESC = "{" . $obj->ID . "}" . V::strShortUtf8($obj->DESC, 50);
				if ($tree->Childs[$id]->GOTO_FLAG == 'GOTO_AND_RETURN') {
					$tree->Viev_Zpc[$tree->Childs[$id]->IF_TRUE_GOTO]->EXTERNAL_LINK[$root_id] = $root_id;
				}
			}
		}
	}

	public function build_path_zoom_proces_cut(&$tree, $init, $path_zpc = array()) {
		$db = DB::getDB();
		foreach ($tree->Childs as $id => $obj) {
			if (!isset($tree->Viev_Zpc_found[$id])) {
				$find = array();
				$this->search_parent_ids_till_cut($tree, $id, $find);
				foreach ($find as $find_id) {
					$tree->Viev_Zpc_found[$find_id] = $find[0];
					if ($find_id != $find[0]) {
						$tree->Viev_Zpc_Parent[$find_id] = $find[0];// TODO: moze byc to z bledem jak nadrzedny bedzie zbitym ciagiem
					}
					else {
						$tree->Viev_Zpc_Parent[$find_id] = $tree->Childs[$find_id]->PARENT;
					}
				}
				$tree->Viev_Zpc[$id]->PATH = $find;
				$obj = $db->get_by_id('CRM_PROCES', $id);
				$tree->Viev_Zpc[$id]->DESC = "{" . $id . "}" . V::strShortUtf8($obj->DESC, 50, '...');
			}
		}

		foreach($tree->Viev_Zpc as $id => $obj) {
			if (!isset($obj->PARENT)) {
				foreach($tree->Viev_Zpc as $id_=>$obj_) {
					if (isset($tree->Childs[$id_]->SOURCE)) {
						if (in_array($tree->Childs[$id]->PARENT, $obj_->PATH)) {
							$tree->Viev_Zpc[$id]->PARENT = $id_;
						}
					}
				}
			}

			if (!isset($tree->Viev_Zpc[$id]->PARENT)) {
				$tree->Viev_Zpc[$id]->PARENT = $tree->Childs[$id]->PARENT;
			}
		}
	}

}
