TreeHelper.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. }//end foreach
  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. }//end foreach
  20. }//end foreach
  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. }//end foreach
  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. }//end foreach
  41. if (!empty($get_parents_id)) {
  42. $do_zmiana = false;
  43. $sql = "select
  44. t1.`ID`
  45. , t1.`PARENT_ID`
  46. from `".$sql_table."` as t1
  47. where
  48. t1.`ID` in (".implode(",", $get_parents_id).")
  49. ";
  50. $res = DB::query( $sql );
  51. while ($r = DB::fetch( $res )) {
  52. $cechy_used_tree[ $r->ID ] = $r->PARENT_ID;
  53. if ($r->PARENT_ID > 0) {
  54. $cechy_used_tree[ $r->PARENT_ID ] = null;
  55. $do_zmiana = true;
  56. }
  57. }//while
  58. if (!$do_zmiana) {
  59. return true;
  60. }
  61. } else {
  62. return true;
  63. }
  64. return TreeHelper::build_tree_flat( $sql_table, $cechy_used_tree, $rec_count + 1 );
  65. }
  66. public static function get_childrens( $sql_table, $id_proces ) {
  67. $ret = array();
  68. $sql = "select
  69. t1.*
  70. from `".$sql_table."` as t1
  71. where
  72. t1.`PARENT_ID`='".$id_proces."'
  73. ";
  74. $res = DB::query( $sql );
  75. while ($r = DB::fetch( $res )) {
  76. $ret[ $r->ID ] = $r;
  77. }//while
  78. return $ret;
  79. }
  80. public static function get_all_parents( $sql_table, $id, $parent_id_key = 'PARENT_ID' ) {
  81. $parents = array();
  82. self::get_all_parents_rec($sql_table, $id, $parents, $parent_id_key);
  83. return $parents;
  84. }
  85. public static function get_all_parents_rec( $sql_table, $id, &$parents, $parent_id_key ) {
  86. $sql = "select t.* from `".$sql_table."` as t where t.`ID`='".$id."'";
  87. $res = DB::query( $sql );
  88. if ($r = DB::fetch( $res )) {
  89. $parents[$r->ID] = $r;
  90. self::get_all_parents_rec($sql_table, $r->$parent_id_key, $parents, $parent_id_key);
  91. }
  92. }
  93. }