|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace extend; |
| 4 | + |
| 5 | +/** |
| 6 | + * Tree 分类扩展 |
| 7 | + * |
| 8 | + */ |
| 9 | +class Category |
| 10 | +{ |
| 11 | + //原始的分类数据 |
| 12 | + private $rawList = array(); |
| 13 | + //格式化后的分类 |
| 14 | + private $formatList = array(); |
| 15 | + //格式化的字符 |
| 16 | + private $icon = array('│', '├', '└'); |
| 17 | + //字段映射,分类id,上级分类pid,分类名称title,格式化后分类名称fulltitle |
| 18 | + private $field = array(); |
| 19 | + |
| 20 | + public function __construct($field = array()) |
| 21 | + { |
| 22 | + $this->field['id'] = isset($field['0']) ? $field['0'] : 'id'; |
| 23 | + $this->field['pid'] = isset($field['1']) ? $field['1'] : 'pid'; |
| 24 | + $this->field['title'] = isset($field['2']) ? $field['2'] : 'title'; |
| 25 | + $this->field['fulltitle'] = isset($field['3']) ? $field['3'] : 'fulltitle'; |
| 26 | + } |
| 27 | + |
| 28 | + public function getChild($pid, $data = array()) |
| 29 | + { |
| 30 | + $childs = array(); |
| 31 | + if (empty($data)) { |
| 32 | + $data = $this->rawList; |
| 33 | + } |
| 34 | + foreach ($data as $Category) { |
| 35 | + if ($Category[$this->field['pid']] == $pid) { |
| 36 | + $childs[] = $Category; |
| 37 | + } |
| 38 | + } |
| 39 | + return $childs; |
| 40 | + } |
| 41 | + |
| 42 | + public function getTree($data, $id = 0) |
| 43 | + { |
| 44 | + //数据为空,则返回 |
| 45 | + if (empty($data)) { |
| 46 | + return false; |
| 47 | + } |
| 48 | + |
| 49 | + $this->rawList = array(); |
| 50 | + $this->formatList = array(); |
| 51 | + $this->rawList = $data; |
| 52 | + $this->_searchList($id); |
| 53 | + return $this->formatList; |
| 54 | + } |
| 55 | + |
| 56 | + //获取当前分类的路径 |
| 57 | + public function getPath($data, $id) |
| 58 | + { |
| 59 | + $this->rawList = $data; |
| 60 | + while (1) { |
| 61 | + $id = $this->_getPid($id); |
| 62 | + if ($id == 0) { |
| 63 | + break; |
| 64 | + } |
| 65 | + } |
| 66 | + return array_reverse($this->formatList); |
| 67 | + } |
| 68 | + |
| 69 | + private function _searchList($id = 0, $space = "") |
| 70 | + { |
| 71 | + //下级分类的数组 |
| 72 | + $childs = $this->getChild($id); |
| 73 | + //如果没下级分类,结束递归 |
| 74 | + if (!($n = count($childs))) { |
| 75 | + return; |
| 76 | + } |
| 77 | + $cnt = 1; |
| 78 | + //循环所有的下级分类 |
| 79 | + for ($i = 0; $i < $n; $i++) { |
| 80 | + $pre = ""; |
| 81 | + $pad = ""; |
| 82 | + if ($n == $cnt) { |
| 83 | + $pre = $this->icon[2]; |
| 84 | + } else { |
| 85 | + $pre = $this->icon[1]; |
| 86 | + $pad = $space ? $this->icon[0] : ""; |
| 87 | + } |
| 88 | + $childs[$i][$this->field['fulltitle']] = ($space ? $space . $pre : "") . $childs[$i][$this->field['title']]; |
| 89 | + $this->formatList[] = $childs[$i]; |
| 90 | + //递归下一级分类 |
| 91 | + $this->_searchList($childs[$i][$this->field['id']], $space . $pad . " "); |
| 92 | + $cnt++; |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + //通过当前id获取pid |
| 97 | + private function _getPid($id) |
| 98 | + { |
| 99 | + foreach ($this->rawList as $key => $value) { |
| 100 | + if ($this->rawList[$key][$this->field['id']] == $id) { |
| 101 | + $this->formatList[] = $this->rawList[$key]; |
| 102 | + return $this->rawList[$key][$this->field['pid']]; |
| 103 | + } |
| 104 | + } |
| 105 | + return 0; |
| 106 | + } |
| 107 | +} |
0 commit comments