Tree.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <?php
  2. namespace app\extend\fast;
  3. /**
  4. * 通用的树型类
  5. * @author XiaoYao <476552238li@gmail.com>
  6. */
  7. class Tree
  8. {
  9. protected static $instance;
  10. //默认配置
  11. protected $config = [];
  12. public $options = [];
  13. /**
  14. * 生成树型结构所需要的2维数组
  15. * @var array
  16. */
  17. public $arr = [];
  18. /**
  19. * 生成树型结构所需修饰符号,可以换成图片
  20. * @var array
  21. */
  22. public $icon = array('│', '├', '└');
  23. public $nbsp = " ";
  24. public $pidname = 'pid';
  25. public function __construct($options = [])
  26. {
  27. if ($config = config('tree')) {
  28. $this->options = array_merge($this->config, $config);
  29. }
  30. $this->options = array_merge($this->config, $options);
  31. }
  32. /**
  33. * 初始化
  34. * @access public
  35. * @param array $options 参数
  36. * @return Tree
  37. */
  38. public static function instance($options = [])
  39. {
  40. if (is_null(self::$instance)) {
  41. self::$instance = new static($options);
  42. }
  43. return self::$instance;
  44. }
  45. /**
  46. * 初始化方法
  47. * @param array $arr 2维数组,例如:
  48. * array(
  49. * 1 => array('id'=>'1','pid'=>0,'name'=>'一级栏目一'),
  50. * 2 => array('id'=>'2','pid'=>0,'name'=>'一级栏目二'),
  51. * 3 => array('id'=>'3','pid'=>1,'name'=>'二级栏目一'),
  52. * 4 => array('id'=>'4','pid'=>1,'name'=>'二级栏目二'),
  53. * 5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'),
  54. * 6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'),
  55. * 7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二')
  56. * )
  57. * @param string $pidname 父字段名称
  58. * @param string $nbsp 空格占位符
  59. * @return Tree
  60. */
  61. public function init($arr = [], $pidname = null, $nbsp = null)
  62. {
  63. $this->arr = $arr;
  64. if (!is_null($pidname)) {
  65. $this->pidname = $pidname;
  66. }
  67. if (!is_null($nbsp)) {
  68. $this->nbsp = $nbsp;
  69. }
  70. return $this;
  71. }
  72. /**
  73. * 得到子级数组
  74. * @param int
  75. * @return array
  76. */
  77. public function getChild($myid)
  78. {
  79. $newarr = [];
  80. foreach ($this->arr as $value) {
  81. if (!isset($value['id'])) {
  82. continue;
  83. }
  84. if ($value[$this->pidname] == $myid) {
  85. $newarr[$value['id']] = $value;
  86. }
  87. }
  88. return $newarr;
  89. }
  90. /**
  91. * 读取指定节点的所有孩子节点
  92. * @param int $myid 节点ID
  93. * @param boolean $withself 是否包含自身
  94. * @return array
  95. */
  96. public function getChildren($myid, $withself = false)
  97. {
  98. $newarr = [];
  99. foreach ($this->arr as $value) {
  100. if (!isset($value['id'])) {
  101. continue;
  102. }
  103. if ((string)$value[$this->pidname] == (string)$myid) {
  104. $newarr[] = $value;
  105. $newarr = array_merge($newarr, $this->getChildren($value['id']));
  106. } elseif ($withself && (string)$value['id'] == (string)$myid) {
  107. $newarr[] = $value;
  108. }
  109. }
  110. return $newarr;
  111. }
  112. /**
  113. * 读取指定节点的所有孩子节点ID
  114. * @param int $myid 节点ID
  115. * @param boolean $withself 是否包含自身
  116. * @return array
  117. */
  118. public function getChildrenIds($myid, $withself = false)
  119. {
  120. $childrenlist = $this->getChildren($myid, $withself);
  121. $childrenids = [];
  122. foreach ($childrenlist as $k => $v) {
  123. $childrenids[] = $v['id'];
  124. }
  125. return $childrenids;
  126. }
  127. /**
  128. * 得到当前位置父辈数组
  129. * @param int
  130. * @return array
  131. */
  132. public function getParent($myid)
  133. {
  134. $pid = 0;
  135. $newarr = [];
  136. foreach ($this->arr as $value) {
  137. if (!isset($value['id'])) {
  138. continue;
  139. }
  140. if ($value['id'] == $myid) {
  141. $pid = $value[$this->pidname];
  142. break;
  143. }
  144. }
  145. if ($pid) {
  146. foreach ($this->arr as $value) {
  147. if ($value['id'] == $pid) {
  148. $newarr[] = $value;
  149. break;
  150. }
  151. }
  152. }
  153. return $newarr;
  154. }
  155. /**
  156. * 得到当前位置所有父辈数组
  157. * @param int
  158. * @param bool $withself 是否包含自己
  159. * @return array
  160. */
  161. public function getParents($myid, $withself = false)
  162. {
  163. $pid = 0;
  164. $newarr = [];
  165. foreach ($this->arr as $value) {
  166. if (!isset($value['id'])) {
  167. continue;
  168. }
  169. if ($value['id'] == $myid) {
  170. if ($withself) {
  171. $newarr[] = $value;
  172. }
  173. $pid = $value[$this->pidname];
  174. break;
  175. }
  176. }
  177. if ($pid) {
  178. $arr = $this->getParents($pid, true);
  179. $newarr = array_merge($arr, $newarr);
  180. }
  181. return $newarr;
  182. }
  183. /**
  184. * 读取指定节点所有父类节点ID
  185. * @param int $myid
  186. * @param boolean $withself
  187. * @return array
  188. */
  189. public function getParentsIds($myid, $withself = false)
  190. {
  191. $parentlist = $this->getParents($myid, $withself);
  192. $parentsids = [];
  193. foreach ($parentlist as $k => $v) {
  194. $parentsids[] = $v['id'];
  195. }
  196. return $parentsids;
  197. }
  198. /**
  199. * 树型结构Option
  200. * @param int $myid 表示获得这个ID下的所有子级
  201. * @param string $itemtpl 条目模板 如:"<option value=@id @selected @disabled>@spacer@name</option>"
  202. * @param mixed $selectedids 被选中的ID,比如在做树型下拉框的时候需要用到
  203. * @param mixed $disabledids 被禁用的ID,比如在做树型下拉框的时候需要用到
  204. * @param string $itemprefix 每一项前缀
  205. * @param string $toptpl 顶级栏目的模板
  206. * @return string
  207. */
  208. public function getTree($myid, $itemtpl = "<option value=@id @selected @disabled>@spacer@name</option>", $selectedids = '', $disabledids = '', $itemprefix = '', $toptpl = '')
  209. {
  210. $ret = '';
  211. $number = 1;
  212. $childs = $this->getChild($myid);
  213. if ($childs) {
  214. $total = count($childs);
  215. foreach ($childs as $value) {
  216. $id = $value['id'];
  217. $j = $k = '';
  218. if ($number == $total) {
  219. $j .= $this->icon[2];
  220. $k = $itemprefix ? $this->nbsp : '';
  221. } else {
  222. $j .= $this->icon[1];
  223. $k = $itemprefix ? $this->icon[0] : '';
  224. }
  225. $spacer = $itemprefix ? $itemprefix . $j : '';
  226. $selected = $selectedids && in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  227. $disabled = $disabledids && in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  228. $value = array_merge($value, array('selected' => $selected, 'disabled' => $disabled, 'spacer' => $spacer));
  229. $value = array_combine(array_map(function ($k) {
  230. return '@' . $k;
  231. }, array_keys($value)), $value);
  232. $nstr = strtr((($value["@{$this->pidname}"] == 0 || $this->getChild($id)) && $toptpl ? $toptpl : $itemtpl), $value);
  233. $ret .= $nstr;
  234. $ret .= $this->getTree($id, $itemtpl, $selectedids, $disabledids, $itemprefix . $k . $this->nbsp, $toptpl);
  235. $number++;
  236. }
  237. }
  238. return $ret;
  239. }
  240. /**
  241. * 树型结构UL
  242. * @param int $myid 表示获得这个ID下的所有子级
  243. * @param string $itemtpl 条目模板 如:"<li value=@id @selected @disabled>@name @childlist</li>"
  244. * @param string $selectedids 选中的ID
  245. * @param string $disabledids 禁用的ID
  246. * @param string $wraptag 子列表包裹标签
  247. * @param string $wrapattr 子列表包裹属性
  248. * @return string
  249. */
  250. public function getTreeUl($myid, $itemtpl, $selectedids = '', $disabledids = '', $wraptag = 'ul', $wrapattr = '')
  251. {
  252. $str = '';
  253. $childs = $this->getChild($myid);
  254. if ($childs) {
  255. foreach ($childs as $value) {
  256. $id = $value['id'];
  257. unset($value['child']);
  258. $selected = $selectedids && in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  259. $disabled = $disabledids && in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  260. $value = array_merge($value, array('selected' => $selected, 'disabled' => $disabled));
  261. $value = array_combine(array_map(function ($k) {
  262. return '@' . $k;
  263. }, array_keys($value)), $value);
  264. $nstr = strtr($itemtpl, $value);
  265. $childdata = $this->getTreeUl($id, $itemtpl, $selectedids, $disabledids, $wraptag, $wrapattr);
  266. $childlist = $childdata ? "<{$wraptag} {$wrapattr}>" . $childdata . "</{$wraptag}>" : "";
  267. $str .= strtr($nstr, array('@childlist' => $childlist));
  268. }
  269. }
  270. return $str;
  271. }
  272. /**
  273. * 菜单数据
  274. * @param int $myid
  275. * @param string $itemtpl
  276. * @param mixed $selectedids
  277. * @param mixed $disabledids
  278. * @param string $wraptag
  279. * @param string $wrapattr
  280. * @param int $deeplevel
  281. * @return string
  282. */
  283. public function getTreeMenu($myid, $itemtpl, $selectedids = '', $disabledids = '', $wraptag = 'ul', $wrapattr = '', $deeplevel = 0)
  284. {
  285. $str = '';
  286. $childs = $this->getChild($myid);
  287. if ($childs) {
  288. foreach ($childs as $value) {
  289. $id = $value['id'];
  290. unset($value['child']);
  291. $selected = in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  292. $disabled = in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  293. $value = array_merge($value, array('selected' => $selected, 'disabled' => $disabled));
  294. $value = array_combine(array_map(function ($k) {
  295. return '@' . $k;
  296. }, array_keys($value)), $value);
  297. $bakvalue = array_intersect_key($value, array_flip(['@url', '@caret', '@class']));
  298. $value = array_diff_key($value, $bakvalue);
  299. $nstr = strtr($itemtpl, $value);
  300. $value = array_merge($value, $bakvalue);
  301. $childdata = $this->getTreeMenu($id, $itemtpl, $selectedids, $disabledids, $wraptag, $wrapattr, $deeplevel + 1);
  302. $childlist = $childdata ? "<{$wraptag} {$wrapattr}>" . $childdata . "</{$wraptag}>" : "";
  303. $childlist = strtr($childlist, array('@class' => $childdata ? 'last' : ''));
  304. $value = array(
  305. '@childlist' => $childlist,
  306. '@url' => $childdata || !isset($value['@url']) ? "javascript:;" : $value['@url'],
  307. '@addtabs' => $childdata || !isset($value['@url']) ? "" : (stripos($value['@url'], "?") !== false ? "&" : "?") . "ref=addtabs",
  308. '@caret' => ($childdata && (!isset($value['@badge']) || !$value['@badge']) ? '<i class="fa fa-angle-left"></i>' : ''),
  309. '@badge' => isset($value['@badge']) ? $value['@badge'] : '',
  310. '@class' => ($selected ? ' active' : '') . ($disabled ? ' disabled' : '') . ($childdata ? ' treeview' . (config('fastadmin.show_submenu') ? ' treeview-open' : '') : ''),
  311. );
  312. $str .= strtr($nstr, $value);
  313. }
  314. }
  315. return $str;
  316. }
  317. /**
  318. * 特殊
  319. * @param integer $myid 要查询的ID
  320. * @param string $itemtpl1 第一种HTML代码方式
  321. * @param string $itemtpl2 第二种HTML代码方式
  322. * @param mixed $selectedids 默认选中
  323. * @param mixed $disabledids 禁用
  324. * @param string $itemprefix 前缀
  325. * @return string
  326. */
  327. public function getTreeSpecial($myid, $itemtpl1, $itemtpl2, $selectedids = 0, $disabledids = 0, $itemprefix = '')
  328. {
  329. $ret = '';
  330. $number = 1;
  331. $childs = $this->getChild($myid);
  332. if ($childs) {
  333. $total = count($childs);
  334. foreach ($childs as $id => $value) {
  335. $j = $k = '';
  336. if ($number == $total) {
  337. $j .= $this->icon[2];
  338. $k = $itemprefix ? $this->nbsp : '';
  339. } else {
  340. $j .= $this->icon[1];
  341. $k = $itemprefix ? $this->icon[0] : '';
  342. }
  343. $spacer = $itemprefix ? $itemprefix . $j : '';
  344. $selected = $selectedids && in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  345. $disabled = $disabledids && in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  346. $value = array_merge($value, array('selected' => $selected, 'disabled' => $disabled, 'spacer' => $spacer));
  347. $value = array_combine(array_map(function ($k) {
  348. return '@' . $k;
  349. }, array_keys($value)), $value);
  350. $nstr = strtr(!isset($value['@disabled']) || !$value['@disabled'] ? $itemtpl1 : $itemtpl2, $value);
  351. $ret .= $nstr;
  352. $ret .= $this->getTreeSpecial($id, $itemtpl1, $itemtpl2, $selectedids, $disabledids, $itemprefix . $k . $this->nbsp);
  353. $number++;
  354. }
  355. }
  356. return $ret;
  357. }
  358. /**
  359. *
  360. * 获取树状数组
  361. * @param string $myid 要查询的ID
  362. * @param string $itemprefix 前缀
  363. * @return array
  364. */
  365. public function getTreeArray($myid, $itemprefix = '')
  366. {
  367. $childs = $this->getChild($myid);
  368. $n = 0;
  369. $data = [];
  370. $number = 1;
  371. if ($childs) {
  372. $total = count($childs);
  373. foreach ($childs as $id => $value) {
  374. $j = $k = '';
  375. if ($number == $total) {
  376. $j .= $this->icon[2];
  377. $k = $itemprefix ? $this->nbsp : '';
  378. } else {
  379. $j .= $this->icon[1];
  380. $k = $itemprefix ? $this->icon[0] : '';
  381. }
  382. $spacer = $itemprefix ? $itemprefix . $j : '';
  383. $value['spacer'] = $spacer;
  384. $data[$n] = $value;
  385. $data[$n]['childlist'] = $this->getTreeArray($id, $itemprefix . $k . $this->nbsp);
  386. $n++;
  387. $number++;
  388. }
  389. }
  390. return $data;
  391. }
  392. public function getTreeArray1($myid, $itemprefix = '')
  393. {
  394. $childs = $this->getChild($myid);
  395. $n = 0;
  396. $data = [];
  397. $number = 1;
  398. if ($childs) {
  399. $total = count($childs);
  400. foreach ($childs as $id => $value) {
  401. $j = $k = '';
  402. if ($number == $total) {
  403. $j .= $this->icon[2];
  404. $k = $itemprefix ? $this->nbsp : '';
  405. } else {
  406. $j .= $this->icon[1];
  407. $k = $itemprefix ? $this->icon[0] : '';
  408. }
  409. $spacer = $itemprefix ? $itemprefix . $j : '';
  410. $value['spacer'] = $spacer;
  411. $data[$n] = $value;
  412. $data[$n]['childlist'] = $this->getTreeArray1($id, $itemprefix . $k . $this->nbsp);
  413. // 去掉空的子数组
  414. if(!$data[$n]['childlist']){
  415. unset($data[$n]['childlist']);
  416. }
  417. $n++;
  418. $number++;
  419. }
  420. }
  421. return $data;
  422. }
  423. /**
  424. * 将getTreeArray的结果返回为二维数组
  425. * @param array $data
  426. * @param string $field
  427. * @return array
  428. */
  429. public function getTreeList($data = [], $field = 'name')
  430. {
  431. $arr = [];
  432. foreach ($data as $k => $v) {
  433. $childlist = isset($v['childlist']) ? $v['childlist'] : [];
  434. unset($v['childlist']);
  435. $v[$field] = $v['spacer'] . ' ' . $v[$field];
  436. $v['haschild'] = $childlist ? 1 : 0;
  437. if ($v['id']) {
  438. $arr[] = $v;
  439. }
  440. if ($childlist) {
  441. $arr = array_merge($arr, $this->getTreeList($childlist, $field));
  442. }
  443. }
  444. return $arr;
  445. }
  446. }