Tree.php 16 KB

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