TreeRepository.php 5.9 KB

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