TreeHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. class TreeHelper {
  3. public static function get_tree_from_flat(&$cechy_used_tree) {
  4. $tree_to_rm = array();// to remove from tree
  5. $tree_flat = array();// wskazniki na $tree
  6. // 1st pass - create parent => childrens struct
  7. foreach ($cechy_used_tree as $k_id => $v_parent_id) {
  8. if (!array_key_exists($v_parent_id, $tree_flat)) {
  9. $tree_flat[$v_parent_id] = array();
  10. }
  11. $tree_flat[$v_parent_id][$k_id] = null;
  12. }
  13. foreach ($tree_flat as $k_parent => $v_childrens) {
  14. foreach ($v_childrens as $k_child_id => $v_child_arr) {
  15. if (array_key_exists($k_child_id, $tree_flat)) {
  16. $tree_flat[$k_parent][$k_child_id] =& $tree_flat[$k_child_id];
  17. $tree_to_rm[] = $k_child_id;
  18. }
  19. }
  20. }
  21. $tree = array();
  22. foreach ($tree_flat as $k_id => $v_tree) {
  23. if (!in_array($k_id, $tree_to_rm)) {
  24. $tree[$k_id] =& $tree_flat[$k_id];
  25. }
  26. }
  27. return $tree;
  28. }
  29. public static function build_tree_flat($sql_table, &$cechy_used_tree, $rec_count = 0) {
  30. if ($rec_count > 20) {// rec limit
  31. echo'Error L' . __LINE__ . ' ('.$rec_count.') limit petli - prawdopodobnie bledna struktura danych ';
  32. 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>';
  33. return false;
  34. }
  35. $get_parents_id = array();
  36. foreach ($cechy_used_tree as $k_cecha_id => $v_parent_id) {
  37. if (!$v_parent_id) {
  38. $get_parents_id[] = $k_cecha_id;
  39. }
  40. }
  41. if (!empty($get_parents_id)) {
  42. $do_zmiana = false;
  43. $db = DB::getDB();
  44. $sql = "select
  45. t1.`ID`
  46. , t1.`PARENT_ID`
  47. from `{$sql_table}` as t1
  48. where
  49. t1.`ID` in (".implode(",", $get_parents_id).")
  50. ";
  51. $res = $db->query($sql);
  52. while ($r = $db->fetch($res)) {
  53. $cechy_used_tree[$r->ID] = $r->PARENT_ID;
  54. if ($r->PARENT_ID > 0) {
  55. $cechy_used_tree[$r->PARENT_ID] = null;
  56. $do_zmiana = true;
  57. }
  58. }
  59. if (!$do_zmiana) {
  60. return true;
  61. }
  62. } else {
  63. return true;
  64. }
  65. return TreeHelper::build_tree_flat($sql_table, $cechy_used_tree, $rec_count + 1);
  66. }
  67. public static function get_childrens($sql_table, $id_proces) {
  68. $ret = array();
  69. $db = DB::getDB();
  70. $sql = "select
  71. t1.*
  72. from `{$sql_table}` as t1
  73. where
  74. t1.`PARENT_ID`='{$id_proces}'
  75. ";
  76. $res = $db->query($sql);
  77. while ($r = $db->fetch($res)) {
  78. $ret[$r->ID] = $r;
  79. }
  80. return $ret;
  81. }
  82. public static function get_all_parents($sql_table, $id, $parent_id_key = 'PARENT_ID') {
  83. $parents = array();
  84. self::get_all_parents_rec($sql_table, $id, $parents, $parent_id_key);
  85. return $parents;
  86. }
  87. public static function get_all_parents_rec($sql_table, $id, &$parents, $parent_id_key) {
  88. $db = DB::getDB();
  89. $sql = "select t.* from `{$sql_table}` as t where t.`ID`='{$id}'";
  90. $res = $db->query($sql);
  91. if ($r = $db->fetch($res)) {
  92. $parents[$r->ID] = $r;
  93. self::get_all_parents_rec($sql_table, $r->$parent_id_key, $parents, $parent_id_key);
  94. }
  95. }
  96. }