| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214 |
- <?php
- /**
- * 树形操作仓库.
- *
- * @author nj <nj@gmail.com>
- * @date 2020-01-30 10:36:05
- */
- declare(strict_types=1);
- namespace App\Repositories\Utils;
- class TreeRepository
- {
- private $arr = array();
- private $icon = array();
- private $ret = '';
- public function __construct(
- $arr = array()
- ) {
- $this->arr = $arr;
- $this->icon = array(
- '│',
- '├',
- '└'
- );
- $this->ret = '';
- //return is_array($arr);
- }
- public function getParant(
- $myid
- ) {
- $newarr = array();
- if (!isset($this->arr[$myid])) {
- return false;
- }
- $pid = $this->arr[$myid]['parent_id'];
- $pid = $this->arr[$pid]['parent_id'];
- if (is_array($this->arr)) {
- foreach ($this->arr as $id => $a) {
- if ($a['parent_id'] == $pid) {
- $newarr[$id] = $a;
- }
- }
- }
- return $newarr;
- }
- public function getChild(
- $myid
- ) {
- $newarr = array();
- if (is_array($this->arr)) {
- foreach ($this->arr as $id => $a) {
- if ($a['parent_id'] == $myid) {
- $newarr[$id] = $a;
- }
- }
- }
- return $newarr ? $newarr : false;
- }
- public function getPos(
- $myid,
- &$newarr
- ) {
- $a = array();
- if (!isset($this->arr[$myid])) {
- return false;
- }
- $newarr[] = $this->arr[$myid];
- $pid = $this->arr[$myid]['parent_id'];
- if (isset($this->arr[$pid])) {
- $this->getPos(
- $pid,
- $newarr
- );
- }
- if (is_array($newarr)) {
- krsort($newarr);
- foreach ($newarr as $v) {
- $a[$v['id']] = $v;
- }
- }
- return $a;
- }
- public function getTree(
- $myid,
- $str,
- $sid = 0,
- $adds = ''
- ) {
- $nstr = '';
- $number = 1;
- $child = $this->getChild($myid);
- if (is_array($child)) {
- $total = count($child);
- foreach ($child as $id => $a) {
- $j = $k = '';
- if ($number == $total) {
- $j .= $this->icon[2];
- } else {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds.$j : '';
- $selected = $id == $sid ? 'selected' : '';
- @extract($a);
- eval("\$nstr = \"$str\";");
- $this->ret .= $nstr;
- $this->getTree(
- $id,
- $str,
- $sid,
- $adds . $k . ' '
- );
- $number++;
- }
- }
- return $this->ret;
- }
- /**
- * +----------------------------------------------------------
- * 返回属性节点数组
- * +----------------------------------------------------------
- * @access public
- * @param array $arr 原始数组
- * @param string $key_node_id 子级ID名
- * @param string $key_parent_id 父级ID名
- * @param string $key_childrens 节点名
- * @param string $refs 资源
- * +----------------------------------------------------------
- * @return array
- * +----------------------------------------------------------
- */
- public function tree2AreaArray(
- $arr,
- $key_node_id,
- $key_parent_id = 'parent_id',
- $key_childrens = 'childrens',
- & $refs = null
- ) {
- if (empty($arr)) {
- $arr = $this->arr;
- }
- $refs = array();
- foreach ($arr as $offset => $row) {
- $arr[$offset][$key_childrens] = array();
- $refs[$row[$key_node_id]] =& $arr[$offset];
- }
- $tree = array();
- foreach ($arr as $offset => $row) {
- $parent_id = $row[$key_parent_id];
- if ($parent_id) {
- if (!isset($refs[$parent_id])) {
- $tree[] =& $arr[$offset];
- continue;
- }
- $parent =& $refs[$parent_id];
- $parent[$key_childrens][$arr[$offset][$key_node_id]] = &$arr[$offset];
- if (empty($parent[$key_childrens][$arr[$offset][$key_node_id]][$key_childrens])) {
- unset($parent[$key_childrens][$arr[$offset][$key_node_id]][$key_childrens]);
- }
- } else {
- $tree[$arr[$offset][$key_node_id]] =& $arr[$offset];
- }
- }
- return $tree;
- }
- /**
- * +----------------------------------------------------------
- * 返回属性节点数组
- * +----------------------------------------------------------
- * @access public
- * @param array $arr 原始数组
- * @param string $key_node_id 子级ID名
- * @param string $key_parent_id 父级ID名
- * @param string $key_childrens 节点名
- * @param string $refs 资源
- * +----------------------------------------------------------
- * @return array
- * +----------------------------------------------------------
- */
- public function tree2AreaArray2($arr,$operate,$cell) {
- if (empty($arr)) {
- $arr = $this->arr;
- }
- $dataArr = [];
- foreach ($arr as &$item) {
- // 父菜单
- $dataArr[$item[$operate]] = $item;
- $dataArr[$item[$operate]][$cell] = [];
- foreach ($arr as $item2) {
- // 子菜单
- if ($item['id'] === $item2['parent_id']) {
- $dataArr [$item[$operate]][$cell][$item[$operate].'_'.$item2[$operate]] = $item2;
- unset($item2);
- }
- }
- unset($item);
- }
- return $dataArr;
- }
- }
|