<?php

class TreeHelper {

	public static function get_tree_from_flat( &$cechy_used_tree ) {
		$tree_to_rm = array();// to remove from tree
		$tree_flat = array();// wskazniki na $tree
		// 1st pass - create parent => childrens struct
		foreach ($cechy_used_tree as $k_id => $v_parent_id) {
			if (!array_key_exists($v_parent_id, $tree_flat)) {
				$tree_flat[ $v_parent_id ] = array();
			}
			$tree_flat[ $v_parent_id ] [$k_id] = null;
		}//end foreach

		foreach ($tree_flat as $k_parent => $v_childrens) {
			foreach ($v_childrens as $k_child_id => $v_child_arr) {
				if (array_key_exists($k_child_id, $tree_flat)) {
					$tree_flat[ $k_parent ][ $k_child_id ] =& $tree_flat[ $k_child_id ];
					$tree_to_rm []= $k_child_id;
				}
			}//end foreach
		}//end foreach

		$tree = array();
		foreach ($tree_flat as $k_id => $v_tree) {
			if (!in_array($k_id, $tree_to_rm)) {
				$tree[ $k_id ] =& $tree_flat[ $k_id ];
			}
		}//end foreach
		return $tree;
	}

	public static function build_tree_flat( $sql_table, &$cechy_used_tree, $rec_count = 0 ) {
		if ($rec_count > 20) {// rec limit
			echo'Error L' . __LINE__ . ' ('.$rec_count.') limit petli - prawdopodobnie bledna struktura danych ';
			echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">tree: ';print_r( TreeHelper::get_tree_from_flat($cechy_used_tree) );echo'</pre>';
			return false;
		}

		$get_parents_id = array();
		foreach ($cechy_used_tree as $k_cecha_id => $v_parent_id) {
			if (!$v_parent_id) {
				$get_parents_id []= $k_cecha_id;
			}
		}//end foreach
		if (!empty($get_parents_id)) {
			$do_zmiana = false;
			$sql = "select
					t1.`ID`
					, t1.`PARENT_ID`
				from `".$sql_table."` as t1
				where
					t1.`ID` in (".implode(",", $get_parents_id).")
			";
			$res = DB::query( $sql );
			while ($r = DB::fetch( $res )) {
				$cechy_used_tree[ $r->ID ] = $r->PARENT_ID;
				if ($r->PARENT_ID > 0) {
					$cechy_used_tree[ $r->PARENT_ID ] = null;
					$do_zmiana = true;
				}
			}//while
			if (!$do_zmiana) {
				return true;
			}
		} else {
			return true;
		}
		return TreeHelper::build_tree_flat( $sql_table, $cechy_used_tree, $rec_count + 1 );
	}

	public static function get_childrens( $sql_table, $id_proces ) {
		$ret = array();
		$sql = "select
				t1.*
			from `".$sql_table."` as t1
			where
				t1.`PARENT_ID`='".$id_proces."'
		";
		$res = DB::query( $sql );
		while ($r = DB::fetch( $res )) {
			$ret[ $r->ID ] = $r;
		}//while
		return $ret;
	}

	public static function get_all_parents( $sql_table, $id, $parent_id_key = 'PARENT_ID' ) {
		$parents = array();
		self::get_all_parents_rec($sql_table, $id, $parents, $parent_id_key);
		return $parents;
	}

	public static function get_all_parents_rec( $sql_table, $id, &$parents, $parent_id_key ) {
		$sql = "select t.* from `".$sql_table."` as t where t.`ID`='".$id."'";
		$res = DB::query( $sql );
		if ($r = DB::fetch( $res )) {
			$parents[$r->ID] = $r;
			self::get_all_parents_rec($sql_table, $r->$parent_id_key, $parents, $parent_id_key);
		}
	}

}
