<?php namespace Phpcmf\Library;
class Tree {
private $data;
private $result_array;
private $menu_icon = [
' ',
' ├ ',
' └ '
];
private $icon = [
' │ ',
' ├ ',
' └ '
]; private $arr; private $nbsp = " ";
private $deep = 1;
private $count = 1;
private $ret;
public function html_icon() {
$this->nbsp = '<span class="tree-icon"></span>';
$this->icon = [
'<span class="tree-icon">│</span>',
'<span class="tree-icon">├</span>',
'<span class="tree-icon">└</span>'
];
return $this;
}
public function init($arr) {
$this->ret = '';
$this->data = $arr;
$this->result = [];
return $this;
}
public function get($data) {
$this->data = $data;
$this->result = [];
$this->create(0);
return $this->result_array;
}
private function _data($data) {
$this->ret = '';
$this->data = $data;
$this->deep = 1;
return $this;
}
private function get_child($k_id) {
$arrays = [];
if (is_array($this->data)) {
foreach($this->data as $id => $a) {
$a['pid'] == $k_id && $arrays[$id] = $a;
}
}
$this->deep++;
return $arrays;
}
public function create($k_id = 0, $adds = '') {
if ($this->deep > 5000) {
return; }
$number = 1;
$child = $this->get_child($k_id);
if (is_array($child)) {
$total = dr_count($child);
foreach($child as $id => $a) {
$j = $k = '';
if ($number == $total){
$j.= $this->menu_icon[2];
} else {
$j.= $this->menu_icon[1];
$k = $adds ? $this->menu_icon[0] : '';
}
$a['spacer'] = $adds ? $adds.$j : '';
$this->result_array[] = $a;
$this->create($a['id'], $adds.$k.' ');
$number++;
}
}
$this->deep = 1;
}
private function _have($list, $item){
return(strpos(',,'.$list.',', ','.$item.','));
}
private function _linkage_tree_result($myid, $str, $sid = 0, $adds = '') {
if ($this->deep > 5000) {
return $this->ret; }
$number = 1;
$mychild = $this->get_child($myid);
if (is_array($mychild)) {
$total = count($mychild);
foreach ($mychild 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("\$this->ret.= \"$str\";");
$number++;
$a['child'] && $this->_linkage_tree_result($id, $str, $sid, $adds.$k.' ');
}
}
return $this->ret;
}
public function select_linkage($data, $id = 0, $str, $default = ' -- ') {
$string = '<select class="form-control" '.$str.'>';
$default && $string.= "<option value='0'>$default</option>";
$tree = [];
if (is_array($data)) {
foreach($data as $t) {
$t['selected'] = $id == $t['id'] ? 'selected' : '';
$tree[$t['id']] = $t;
}
}
$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
$string.= $this->_data($tree)->_linkage_tree_result(0, $str);
$string.= '</select>';
return $string;
}
public function select_category($data, $id = 0, $str, $default = ' -- ', $onlysub = 0, $is_push = 0, $is_first = 0) {
$string = '<select class="form-control" '.$str.'>';
$default && $string.= "<option value='0'>$default</option>";
$tree = [];
$first = 0;
if (is_array($data)) {
foreach($data as $t) {
if ($t['tid'] == 2) {
continue;
}
if ($is_push && $t['tid'] == 0 && !$t['child']) {
continue;
}
if (IS_ADMIN && dr_is_app('cqx') && \Phpcmf\Service::M('content', 'cqx')->is_edit($t['id'])) {
continue;
}
if ($is_push && $t['child'] == 1 && $t['catids']) {
$ispost = 0;
foreach ($t['catids'] as $i) {
if (isset($data[$i]) && $data[$i]['child'] == 0) {
$ispost = 1; break;
}
}
if (!$ispost) {
continue;
}
}
$t['selected'] = (is_array($id) ? in_array($t['id'], $id) : $id == $t['id']) ? 'selected' : '';
$t['html_disabled'] = $onlysub && $t['child'] ? 1 : 0;
$first == 0 && $t['child'] == 0 && $first = $t['id'];
if (isset($t['setting'])) {
unset($t['setting']);
}
$tree[$t['id']] = $t;
}
}
$str = "<option value='\$id' \$selected>\$spacer \$name</option>";
$str2 = "<optgroup label='\$spacer \$name'></optgroup>";
$string.= $this->_data($tree)->_category_tree_result(0, $str, $str2);
$string.= '</select>';
if ($is_first) {
$mark = "value='";
$first2 = (int)substr($string, strpos($string, $mark) + strlen($mark));
$first = $first2 ? $first2 : $first;
}
$data = $is_first ? array($string, $first) : $string;
return $data;
}
private function _category_tree_result($myid, $str, $str2, $sid = 0, $adds = '') {
if ($this->deep > 5000) {
return $this->ret; }
$mychild = $this->get_child($myid);
$number = 1;
if (is_array($mychild)) {
$mytotal = count($mychild);
foreach ($mychild as $id => $a) {
$j = $k = '';
if ($number == $mytotal) {
$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);
$html_disabled ? @eval("\$this->ret.= \"$str2\";") : @eval("\$this->ret.= \"$str\";");
$number++;
$a['child'] && $this->_category_tree_result($id, $str, $str2, $sid, $adds.$k.' ');
}
}
return $this->ret;
}
/**
* 得到树型结构
*
* @param int ID,表示获得这个ID下的所有子级
* @param string 生成树型结构的基本代码,例如:"<option value=\$id \$selected>\$spacer\$name</option>"
* @param int 被选中的ID,比如在做树型下拉框的时候需要用到
* @return string
*/
public function get_tree($myid, $str, $sid = 0, $adds = '', $str_group = '') {
if ($this->deep > 5000) {
return $this->ret; }
$mychild = $this->get_child($myid);
$mytotal = dr_count($mychild);
$number = 1;
if (is_array($mychild)) {
foreach ($mychild as $id => $value) {
$j = $k = '';
if ($number == $mytotal) {
$j.= $this->icon[2];
} else {
$j.= $this->icon[1];
$k = $adds ? $this->icon[0] : '';
}
$spacer = $adds ? $adds.$j : '';
$selected = $id == $sid ? 'selected' : '';
$class = 'dr_catid_'.$value['id'];
$parent = SYS_CAT_ZSHOW ? (!$value['child'] ? '' : '<a href="javascript:void();" class="blue select-cat" childs="'.$value['childids'].'" action="open" catid='.$id.'>[-]</a> ') : '';
@extract($value);
$pid == 0 && $str_group ? @eval("\$nstr = \"$str_group\";") : @eval("\$nstr = \"$str\";");
$this->ret.= $nstr;
$nbsp = $this->nbsp;
$this->get_tree($id, $str, $sid, $adds.$k.$nbsp, $str_group);
$number++;
}
}
return $this->ret;
}
public function get_tree_array($myid, $str = '', $sid = 0, $adds = '', $str_group = '') {
if ($this->deep > 5000) {
return $this->result; }
$mychild = $this->get_child($myid);
$mytotal = dr_count($mychild);
$number = 1;
if (is_array($mychild)) {
foreach ($mychild as $id => $value) {
$j = $k = '';
if ($number == $mytotal) {
$j.= $this->icon[2];
} else {
$j.= $this->icon[1];
$k = $adds ? $this->icon[0] : '';
}
$value['spacer'] = $adds ? $adds.$j : '';
$this->result[$id] = $value;
$nbsp = $this->nbsp;
$this->get_tree_array($id, $str, $sid, $adds.$k.$nbsp, $str_group);
$number++;
}
}
return $this->result;
}
public function get_tree_multi($myid, $str, $sid = 0, $adds = '') {
if ($this->deep > 5000) {
return $this->ret; }
$number = 1;
$mychild = $this->get_child($myid);
if (is_array($mychild)) {
$mytotal = count($mychild);
foreach ($mychild as $id => $a) {
$j = $k = '';
if ($number == $mytotal) {
$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->get_tree_multi($id, $str, $sid, $adds.$k.' ');
$number++;
}
}
return $this->ret;
}
}