TreeRepository.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. /**
  3. * 树形操作仓库.
  4. *
  5. * @author nj
  6. * @date 2021-01-30 10:36:05
  7. */
  8. namespace App\Repositories\Utils;
  9. class TreeRepository
  10. {
  11. private $arr = array();
  12. private $icon = array();
  13. private $ret = '';
  14. public function __construct(
  15. $arr = array()
  16. ) {
  17. $this->arr = $arr;
  18. $this->icon = array(
  19. '│',
  20. '├',
  21. '└'
  22. );
  23. $this->ret = '';
  24. //return is_array($arr);
  25. }
  26. public function getParant(
  27. $myid
  28. ) {
  29. $newarr = array();
  30. if (!isset($this->arr[$myid])) {
  31. return false;
  32. }
  33. $pid = $this->arr[$myid]['parent_id'];
  34. $pid = $this->arr[$pid]['parent_id'];
  35. if (is_array($this->arr)) {
  36. foreach ($this->arr as $id => $a) {
  37. if ($a['parent_id'] == $pid) {
  38. $newarr[$id] = $a;
  39. }
  40. }
  41. }
  42. return $newarr;
  43. }
  44. public function getChild(
  45. $myid
  46. ) {
  47. $newarr = array();
  48. if (is_array($this->arr)) {
  49. foreach ($this->arr as $id => $a) {
  50. if ($a['parent_id'] == $myid) {
  51. $newarr[$id] = $a;
  52. }
  53. }
  54. }
  55. return $newarr ? $newarr : false;
  56. }
  57. public function getPos(
  58. $myid,
  59. &$newarr
  60. ) {
  61. $a = array();
  62. if (!isset($this->arr[$myid])) {
  63. return false;
  64. }
  65. $newarr[] = $this->arr[$myid];
  66. $pid = $this->arr[$myid]['parent_id'];
  67. if (isset($this->arr[$pid])) {
  68. $this->getPos(
  69. $pid,
  70. $newarr
  71. );
  72. }
  73. if (is_array($newarr)) {
  74. krsort($newarr);
  75. foreach ($newarr as $v) {
  76. $a[$v['id']] = $v;
  77. }
  78. }
  79. return $a;
  80. }
  81. public function getTree(
  82. $myid,
  83. $str,
  84. $sid = 0,
  85. $adds = ''
  86. ) {
  87. $nstr = '';
  88. $number = 1;
  89. $child = $this->getChild($myid);
  90. if (is_array($child)) {
  91. $total = count($child);
  92. foreach ($child as $id => $a) {
  93. $j = $k = '';
  94. if ($number == $total) {
  95. $j .= $this->icon[2];
  96. } else {
  97. $j .= $this->icon[1];
  98. $k = $adds ? $this->icon[0] : '';
  99. }
  100. $spacer = $adds ? $adds.$j : '';
  101. $selected = $id == $sid ? 'selected' : '';
  102. @extract($a);
  103. eval("\$nstr = \"$str\";");
  104. $this->ret .= $nstr;
  105. $this->getTree(
  106. $id,
  107. $str,
  108. $sid,
  109. $adds . $k . '&nbsp;'
  110. );
  111. $number++;
  112. }
  113. }
  114. return $this->ret;
  115. }
  116. /**
  117. * +----------------------------------------------------------
  118. * 返回属性节点数组
  119. * +----------------------------------------------------------
  120. * @access public
  121. * @param array $arr 原始数组
  122. * @param string $key_node_id 子级ID名
  123. * @param string $key_parent_id 父级ID名
  124. * @param string $key_childrens 节点名
  125. * @param string $refs 资源
  126. * +----------------------------------------------------------
  127. * @return array
  128. * +----------------------------------------------------------
  129. */
  130. public function tree2AreaArray(
  131. $arr,
  132. $key_node_id,
  133. $key_parent_id = 'parent_id',
  134. $key_childrens = 'childrens',
  135. & $refs = null
  136. ) {
  137. if (empty($arr)) {
  138. $arr = $this->arr;
  139. }
  140. $refs = array();
  141. foreach ($arr as $offset => $row) {
  142. $arr[$offset][$key_childrens] = array();
  143. $refs[$row[$key_node_id]] =& $arr[$offset];
  144. }
  145. $tree = array();
  146. foreach ($arr as $offset => $row) {
  147. $parent_id = $row[$key_parent_id];
  148. if ($parent_id) {
  149. if (!isset($refs[$parent_id])) {
  150. $tree[] =& $arr[$offset];
  151. continue;
  152. }
  153. $parent =& $refs[$parent_id];
  154. $parent[$key_childrens][$arr[$offset][$key_node_id]] = &$arr[$offset];
  155. if (empty($parent[$key_childrens][$arr[$offset][$key_node_id]][$key_childrens])) {
  156. unset($parent[$key_childrens][$arr[$offset][$key_node_id]][$key_childrens]);
  157. }
  158. } else {
  159. $tree[$arr[$offset][$key_node_id]] =& $arr[$offset];
  160. }
  161. }
  162. return $tree;
  163. }
  164. }