123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- <?php
- namespace tree;
- class Tree
- {
-
- public $arr = [];
-
- public $icon = ['│', '├', '└'];
- public $nbsp = " ";
- private $str = '';
-
- public $ret = '';
-
- public function init($arr = [])
- {
- $this->arr = $arr;
- $this->ret = '';
- return is_array($arr);
- }
-
- public function getParent($myId)
- {
- $newArr = [];
- if (!isset($this->arr[$myId]))
- return false;
- $pid = $this->arr[$myId]['parent_id'];
- $pid = $this->arr[$pid]['parent_id'];
- if (is_array($this->arr)) {
- foreach ($this->arr as $id => $a) {
- if ($a['parent_id'] == $pid)
- $newArr[$id] = $a;
- }
- }
- return $newArr;
- }
-
- public function getChild($myId)
- {
- $newArr = [];
- if (is_array($this->arr)) {
- foreach ($this->arr as $id => $a) {
- if ($a['parent_id'] == $myId) {
- $newArr[$id] = $a;
- }
- }
- }
- return $newArr ? $newArr : false;
- }
-
- public function getPosition($myId, &$newArr)
- {
- $a = [];
- if (!isset($this->arr[$myId]))
- return false;
- $newArr[] = $this->arr[$myId];
- $pid = $this->arr[$myId]['parent_id'];
- if (isset($this->arr[$pid])) {
- $this->getPosition($pid, $newArr);
- }
- if (is_array($newArr)) {
- krsort($newArr);
- foreach ($newArr as $v) {
- $a[$v['id']] = $v;
- }
- }
- return $a;
- }
-
- public function getTree($myId, $str, $sid = 0, $adds = '', $str_group = '')
- {
- $number = 1;
-
- $child = $this->getChild($myId);
- if (is_array($child)) {
- $total = count($child);
- foreach ($child as $key => $value) {
- $j = $k = '';
- if ($number == $total) {
- $j .= $this->icon[2];
- } else {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds . $j : '';
- $selected = $value['id'] == $sid ? 'selected' : '';
- $id = 0;
- $nstr = '';
- $parentId = $value['parent_id'];
- @extract($value);
- $parentId == 0 && $str_group ? eval("\$nstr = \"$str_group\";") : eval("\$nstr = \"$str\";");
- $this->ret .= $nstr;
- $nbsp = $this->nbsp;
- $this->getTree($id, $str, $sid, $adds . $k . $nbsp, $str_group);
- $number++;
- }
- }
- return $this->ret;
- }
-
- public function getTreeArray($myId, $maxLevel = 0, $level = 1)
- {
- $returnArray = [];
-
- $children = $this->getChild($myId);
- if (is_array($children)) {
- foreach ($children as $child) {
- $child['_level'] = $level;
- $returnArray[$child['id']] = $child;
- if ($maxLevel === 0 || ($maxLevel !== 0 && $maxLevel > $level)) {
- $mLevel = $level + 1;
- $returnArray[$child['id']]["children"] = $this->getTreeArray($child['id'], $maxLevel, $mLevel);
- }
- }
- }
- return $returnArray;
- }
-
- public function getTreeMulti($myId, $str, $sid = 0, $adds = '')
- {
- $number = 1;
- $child = $this->getChild($myId);
- if (is_array($child)) {
- $total = count($child);
- foreach ($child as $id => $a) {
- $j = $k = '';
- if ($number == $total) {
- $j .= $this->icon[2];
- } else {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds . $j : '';
- $selected = $this->have($sid, $id) ? 'selected' : '';
- @extract($a);
- eval("\$nstr = \"$str\";");
- $this->ret .= $nstr;
- $this->getTreeMulti($id, $str, $sid, $adds . $k . ' ');
- $number++;
- }
- }
- return $this->ret;
- }
-
- public function getTreeCategory($myId, $str, $str2, $sid = 0, $adds = '')
- {
- $number = 1;
- $child = $this->getChild($myId);
- if (is_array($child)) {
- $total = count($child);
- foreach ($child as $id => $a) {
- $j = $k = '';
- if ($number == $total) {
- $j .= $this->icon[2];
- } else {
- $j .= $this->icon[1];
- $k = $adds ? $this->icon[0] : '';
- }
- $spacer = $adds ? $adds . $j : '';
- $selected = $this->have($sid, $id) ? 'selected' : '';
- @extract($a);
- if (empty($html_disabled)) {
- eval("\$nstr = \"$str\";");
- } else {
- eval("\$nstr = \"$str2\";");
- }
- $this->ret .= $nstr;
- $this->getTreeCategory($id, $str, $str2, $sid, $adds . $k . ' ');
- $number++;
- }
- }
- return $this->ret;
- }
-
- function getTreeView($myId, $effected_id = 'example', $str = "<span class='file'>\$name</span>", $str2 = "<span class='folder'>\$name</span>", $showlevel = 0, $style = 'filetree ', $currentlevel = 1, $recursion = FALSE)
- {
- $child = $this->getChild($myId);
- if (!defined('EFFECTED_INIT')) {
- $effected = ' id="' . $effected_id . '"';
- define('EFFECTED_INIT', 1);
- } else {
- $effected = '';
- }
- $placeholder = '<ul><li><span class="placeholder"></span></li></ul>';
- if (!$recursion)
- $this->str .= '<ul' . $effected . ' class="' . $style . '">';
- foreach ($child as $id => $a) {
- @extract($a);
- if ($showlevel > 0 && $showlevel == $currentlevel && $this->getChild($id))
- $folder = 'hasChildren';
- $floder_status = isset($folder) ? ' class="' . $folder . '"' : '';
- $this->str .= $recursion ? '<ul><li' . $floder_status . ' id=\'' . $id . '\'>' : '<li' . $floder_status . ' id=\'' . $id . '\'>';
- $recursion = FALSE;
-
- if ($child == 1) {
- eval("\$nstr = \"$str2\";");
- $this->str .= $nstr;
- if ($showlevel == 0 || ($showlevel > 0 && $showlevel > $currentlevel)) {
- $this->getTreeView($id, $effected_id, $str, $str2, $showlevel, $style, $currentlevel + 1, TRUE);
- } elseif ($showlevel > 0 && $showlevel == $currentlevel) {
- $this->str .= $placeholder;
- }
- } else {
- eval("\$nstr = \"$str\";");
- $this->str .= $nstr;
- }
- $this->str .= $recursion ? '</li></ul>' : '</li>';
- }
- if (!$recursion)
- $this->str .= '</ul>';
- return $this->str;
- }
-
- function getTreeViewMenu($myId, $effected_id = 'example', $str = "<span class='file'>\$name</span>", $str2 = "<span class='folder'>\$name</span>", $showlevel = 0, $ul_class = "", $li_class = "", $style = 'filetree ', $currentlevel = 1, $recursion = FALSE, $dropdown = 'hasChild')
- {
- $child = $this->getChild($myId);
- if (!defined('EFFECTED_INIT')) {
- $effected = ' id="' . $effected_id . '"';
- define('EFFECTED_INIT', 1);
- } else {
- $effected = '';
- }
- $placeholder = '<ul><li><span class="placeholder"></span></li></ul>';
- if (!$recursion) {
- $this->str .= '<ul' . $effected . ' class="' . $style . '">';
- }
- foreach ($child as $id => $a) {
- @extract($a);
- if ($showlevel > 0 && is_array($this->getChild($a['id']))) {
- $floder_status = " class='$dropdown $li_class'";
- } else {
- $floder_status = " class='$li_class'";;
- }
- $this->str .= $recursion ? "<ul class='$ul_class'><li $floder_status id= 'menu-item-$id'>" : "<li $floder_status id= 'menu-item-$id'>";
- $recursion = FALSE;
-
- if ($this->getChild($a['id'])) {
- eval("\$nstr = \"$str2\";");
- $this->str .= $nstr;
- if ($showlevel == 0 || ($showlevel > 0 && $showlevel > $currentlevel)) {
- $this->getTreeViewMenu($a['id'], $effected_id, $str, $str2, $showlevel, $ul_class, $li_class, $style, $currentlevel + 1, TRUE);
- } elseif ($showlevel > 0 && $showlevel == $currentlevel) {
-
- }
- } else {
- eval("\$nstr = \"$str\";");
- $this->str .= $nstr;
- }
- $this->str .= $recursion ? '</li></ul>' : '</li>';
- }
- if (!$recursion)
- $this->str .= '</ul>';
- return $this->str;
- }
-
- public function createSubJson($myId, $str = '')
- {
- $sub_cats = $this->getChild($myId);
- $n = 0;
- if (is_array($sub_cats))
- foreach ($sub_cats as $c) {
- $data[$n]['id'] = iconv(CHARSET, 'utf-8', $c['catid']);
- if ($this->getChild($c['catid'])) {
- $data[$n]['liclass'] = 'hasChildren';
- $data[$n]['children'] = [['text' => ' ', 'classes' => 'placeholder']];
- $data[$n]['classes'] = 'folder';
- $data[$n]['text'] = iconv(CHARSET, 'utf-8', $c['catname']);
- } else {
- if ($str) {
- @extract(array_iconv($c, CHARSET, 'utf-8'));
- eval("\$data[$n]['text'] = \"$str\";");
- } else {
- $data[$n]['text'] = iconv(CHARSET, 'utf-8', $c['catname']);
- }
- }
- $n++;
- }
- return json_encode($data);
- }
- private function have($list, $item)
- {
- return (strpos(',,' . $list . ',', ',' . $item . ','));
- }
- }
|