Tree.php 16 KB

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