説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

Tree.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Yzncms [ 御宅男工作室 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2018 http://yzncms.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 御宅男 <530765310@qq.com>
  10. // +----------------------------------------------------------------------
  11. // +----------------------------------------------------------------------
  12. // | 通用的树型类,可以生成任何树型结构
  13. // +----------------------------------------------------------------------
  14. namespace util;
  15. class Tree
  16. {
  17. /**
  18. * 生成树型结构所需要的2维数组
  19. * @var array
  20. */
  21. public $arr = [];
  22. protected static $instance;
  23. public $options = [];
  24. /**
  25. * 生成树型结构所需修饰符号,可以换成图片
  26. * @var array
  27. */
  28. public $icon = ['│', '├', '└'];
  29. public $nbsp = "&nbsp;";
  30. public $id = "id";
  31. public $pidname = 'parentid';
  32. public $child = 'child';
  33. public $ret = '';
  34. /**
  35. * 初始化.
  36. *
  37. * @param array $options 参数
  38. *
  39. * @return Tree
  40. */
  41. public static function instance($options = [])
  42. {
  43. if (is_null(self::$instance)) {
  44. self::$instance = new static($options);
  45. }
  46. return self::$instance;
  47. }
  48. /**
  49. * 构造函数,初始化类
  50. * @param array $arr 2维数组,例如:
  51. * array(
  52. * 1 => array('id'=>'1','parentid'=>0,'name'=>'一级栏目一'),
  53. * 2 => array('id'=>'2','parentid'=>0,'name'=>'一级栏目二'),
  54. * 3 => array('id'=>'3','parentid'=>1,'name'=>'二级栏目一'),
  55. * 4 => array('id'=>'4','parentid'=>1,'name'=>'二级栏目二'),
  56. * 5 => array('id'=>'5','parentid'=>2,'name'=>'二级栏目三'),
  57. * 6 => array('id'=>'6','parentid'=>3,'name'=>'三级栏目一'),
  58. * 7 => array('id'=>'7','parentid'=>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. }
  67. if (!is_null($nbsp)) {
  68. $this->nbsp = $nbsp;
  69. }
  70. $this->ret = '';
  71. return $this;
  72. }
  73. /**
  74. * 得到子级数组
  75. * @param int
  76. * @return array
  77. */
  78. public function getChild($myid)
  79. {
  80. $a = $newarr = [];
  81. if (is_array($this->arr)) {
  82. foreach ($this->arr as $id => $a) {
  83. if ($a[$this->pidname] == $myid) {
  84. $newarr[$id] = $a;
  85. }
  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 ($value[$this->pidname] == $myid) {
  104. $newarr[] = $value;
  105. $newarr = array_merge($newarr, $this->getChildren($value['id']));
  106. } elseif ($withself && $value['id'] == $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 = '', $selectedids = '', $disabledids = '', $itemprefix = '', $toptpl = '')
  209. {
  210. if (!$itemtpl) {
  211. $itemtpl = '<option value=@id @selected @disabled>@spacer @title</option>';
  212. }
  213. $ret = '';
  214. $number = 1;
  215. $childs = $this->getChild($myid);
  216. if ($childs) {
  217. $total = count($childs);
  218. foreach ($childs as $value) {
  219. $id = $value['id'];
  220. $j = $k = '';
  221. if ($number == $total) {
  222. $j .= $this->icon[2];
  223. $k = $itemprefix ? $this->nbsp : '';
  224. } else {
  225. $j .= $this->icon[1];
  226. $k = $itemprefix ? $this->icon[0] : '';
  227. }
  228. $spacer = $itemprefix ? $itemprefix . $j : '';
  229. if ('' !== $selectedids) {
  230. $selected = $selectedids && in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  231. $value = array_merge($value, ['selected' => $selected]);
  232. }
  233. if ('' !== $disabledids) {
  234. $disabled = $disabledids && in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  235. $value = array_merge($value, ['disabled' => $disabled]);
  236. }
  237. $value = array_merge($value, ['spacer' => $spacer]);
  238. $value = array_combine(array_map(function ($k) {
  239. return '@' . $k;
  240. }, array_keys($value)), $value);
  241. $nstr = strtr((($value["@{$this->pidname}"] == 0 || $this->getChild($id)) && $toptpl ? $toptpl : $itemtpl), $value);
  242. $ret .= $nstr;
  243. $ret .= $this->getTree($id, $itemtpl, $selectedids, $disabledids, $itemprefix . $k . $this->nbsp, $toptpl);
  244. $number++;
  245. }
  246. }
  247. return $ret;
  248. }
  249. /**
  250. * 树型结构UL
  251. * @param int $myid 表示获得这个ID下的所有子级
  252. * @param string $itemtpl 条目模板 如:"<li value=@id @selected @disabled>@name @childlist</li>"
  253. * @param string $selectedids 选中的ID
  254. * @param string $disabledids 禁用的ID
  255. * @param string $wraptag 子列表包裹标签
  256. * @param string $wrapattr 子列表包裹属性
  257. * @return string
  258. */
  259. public function getTreeUl($myid, $itemtpl, $selectedids = '', $disabledids = '', $wraptag = 'ul', $wrapattr = '')
  260. {
  261. $str = '';
  262. $childs = $this->getChild($myid);
  263. if ($childs) {
  264. foreach ($childs as $value) {
  265. $id = $value['id'];
  266. unset($value['child']);
  267. $selected = $selectedids && in_array($id, (is_array($selectedids) ? $selectedids : explode(',', $selectedids))) ? 'selected' : '';
  268. $disabled = $disabledids && in_array($id, (is_array($disabledids) ? $disabledids : explode(',', $disabledids))) ? 'disabled' : '';
  269. $value = array_merge($value, ['selected' => $selected, 'disabled' => $disabled]);
  270. $value = array_combine(array_map(function ($k) {
  271. return '@' . $k;
  272. }, array_keys($value)), $value);
  273. $nstr = strtr($itemtpl, $value);
  274. $childdata = $this->getTreeUl($id, $itemtpl, $selectedids, $disabledids, $wraptag, $wrapattr);
  275. $childlist = $childdata ? "<{$wraptag} {$wrapattr}>" . $childdata . "</{$wraptag}>" : "";
  276. $str .= strtr($nstr, ['@childlist' => $childlist]);
  277. }
  278. }
  279. return $str;
  280. }
  281. /**
  282. * 将数据集格式化成层次结构
  283. * @param int $pid 父级id
  284. * @param int $max_level 最多返回多少层,0为不限制
  285. * @param int $curr_level 当前层数
  286. * @author 蔡伟明 <314013107@qq.com>
  287. * @return array
  288. */
  289. public function toLayer($pid = 0, $max_level = 0, $curr_level = 0)
  290. {
  291. $trees = [];
  292. $lists = $this->getChild($pid);
  293. foreach ($lists as $key => $value) {
  294. if ($value[$this->pidname] == $pid) {
  295. if ($max_level > 0 && $curr_level == $max_level) {
  296. return $trees;
  297. }
  298. unset($lists[$key]);
  299. $child = $this->toLayer($value[$this->id], $max_level, $curr_level + 1);
  300. if (!empty($child)) {
  301. $value[$this->child] = $child;
  302. }
  303. $trees[$key] = $value;
  304. }
  305. }
  306. return $trees;
  307. }
  308. /**
  309. *
  310. * 获取树状数组
  311. *
  312. * @param string $myid 要查询的ID
  313. * @param string $itemprefix 前缀
  314. *
  315. * @return string
  316. */
  317. public function getTreeArray($myid, $itemprefix = '')
  318. {
  319. $child = $this->getChild($myid);
  320. $n = 0;
  321. $data = [];
  322. $number = 1;
  323. if (is_array($child)) {
  324. $total = count($child);
  325. foreach ($child as $id => $value) {
  326. $j = $k = '';
  327. if ($number == $total) {
  328. $j .= $this->icon[2];
  329. $k = $itemprefix ? $this->nbsp : '';
  330. } else {
  331. $j .= $this->icon[1];
  332. $k = $itemprefix ? $this->icon[0] : '';
  333. }
  334. $spacer = $itemprefix ? $itemprefix . $j : '';
  335. $value['spacer'] = $spacer;
  336. $data[$n] = $value;
  337. $data[$n]['childlist'] = $this->getTreeArray($value[$this->id], $itemprefix . $k . $this->nbsp);
  338. $n++;
  339. $number++;
  340. }
  341. }
  342. return $data;
  343. }
  344. /**
  345. * 将getTreeArray的结果返回为二维数组
  346. * @param array $data
  347. * @return array
  348. */
  349. public function getTreeList($data = [], $field = 'name')
  350. {
  351. $arr = [];
  352. foreach ($data as $k => $v) {
  353. $childlist = $v['childlist'] ?? [];
  354. unset($v['childlist']);
  355. $v[$field] = $v['spacer'] . ' ' . $v[$field];
  356. $v['haschild'] = $childlist ? 1 : 0;
  357. if ($v[$this->id]) {
  358. $arr[] = $v;
  359. }
  360. if ($childlist) {
  361. $arr = array_merge($arr, $this->getTreeList($childlist, $field));
  362. }
  363. }
  364. return $arr;
  365. }
  366. }