icon();
}
// 释放变量
public function __destruct()
{
unset($this->data);
unset($this->cache);
unset($this->ret);
unset($this->icon);
unset($this->result_array);
unset($this->nbsp_str);
unset($this->nbsp);
unset($this->result);
}
/**
* 设置html标签
*/
public function html_icon() {
$this->nbsp_str = '';
$this->icon = [
$this->nbsp_str,
'├ ',
'└ '
];
return $this;
}
/**
* 设置普通标签
*/
public function icon() {
$this->nbsp_str = ' ';
$this->icon = [
$this->nbsp_str,
'├ ',
'└ '
];
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;
}
// 设置数据
protected function _data($data) {
$this->ret = '';
$this->data = $data;
$this->deep = 1;
return $this;
}
// 设置缓存
public function cache($is) {
$this->cache = $is;
return $this;
}
/**
* 得到子级数组
* @param int
* @return array
*/
protected function get_child($k_id) {
$arrays = [];
/*
if ($k_id && isset($this->data[$k_id]) && $this->data[$k_id] && $this->data[$k_id]['next_ids']) {
if (is_array($this->data[$k_id]['next_ids'])) {
foreach ($this->data[$k_id]['next_ids'] as $id) {
$arrays[$id] = $this->data[$id];
}
}
}
if (!$k_id && is_array($this->data)) {
foreach($this->data as $id => $a) {
if ($a['pid'] == $k_id) {
$arrays[$id] = $a;
}
}
}
*/
if (is_array($this->data)) {
foreach($this->data as $id => $a) {
if ($a['pid'] == $k_id) {
$arrays[$id] = $a;
}
}
}
$this->deep++;
return $arrays;
}
/**
* 得到树型数组
*/
public function create($k_id = 0, $adds = '') {
if ($this->deep > 5000) {
return; // 防止死循环
}
$child = $this->get_child($k_id); // 获取子数据
$number = 1;
if (is_array($child)) {
$total = dr_count($child);
foreach($child as $id => $a) {
$k = $adds ? $this->nbsp : '';
$j = $number == $total ? $this->icon[2] : $this->icon[1];
$a['spacer'] = $this->_get_spacer($adds ? $adds.$j : '');
$this->result_array[] = $a;
$this->create($a['id'], $adds.$k.$this->nbsp);
$number++;
}
}
$this->deep = 1;
}
// 替换空格填充符号
protected function _get_spacer($str) {
$num = substr_count($str, $this->nbsp) * 2;
if ($num) {
$str = str_replace($this->nbsp, '', $str);
for ($i = 0; $i < $num; $i ++) {
$str = $this->nbsp_str.$str;
}
}
return $str;
}
// 替换逗号
protected function _have($list, $item){
return(strpos(',,'.$list.',', ','.$item.','));
}
/**
* 用于栏目选择框
*
* @param integer $myid 要查询的ID
* @param string $str HTML代码方式
* @param integer $sid 默认选中
* @param integer $adds 前缀
*/
protected 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 => $phpcmf_a) {
$j = $k = '';
if ($number == $total) {
$j.= $this->icon[2];
} else {
$j.= $this->icon[1];
$k = $adds ? $this->icon[0] : '';
}
$phpcmf_a['spacer'] = $this->_get_spacer($adds ? $adds.$j : '');
$this->ret.= dr_rp_param_var($str, $phpcmf_a);
$number++;
// 如果有下级菜单就递归
$phpcmf_a['child'] && $this->_linkage_tree_result($id, $str, $sid, $adds.$k.$this->nbsp);
}
}
return $this->ret;
}
// 联动菜单选择
public function select_linkage($data, $id = 0, $str = '', $default = ' -- ') {
$string = '';
unset($this->ret);
unset($this->data);
if (!IS_DEV) {
\Phpcmf\Service::L('cache')->set_data($name, $string, 3600);
}
return $string;
}
public function ismain($v) {
$this->ismain = $v;
return $this;
}
/**
* 栏目选择
*
* @param array $data 栏目数据
* @param intval/array $id 被选中的ID
* @param string $str 属性
* @param string $default 默认选项
* @param intval $onlysub 只可选择子栏目
* @param intval $is_push 是否验证权限
* @param intval $is_first 是否返回第一个可用栏目id
* @return string
*/
public function select_category($data, $id = 0, $str = '', $default = ' -- ', $onlysub = 0, $is_push = 0, $is_first = 0) {
$dir = 'tree';
if (isset($data[0]) && dr_is_module($data[0])) {
$mid = $data[0];
$data = \Phpcmf\Service::L('category', 'module')->get_category($mid);
} else {
$mid = 'share';
}
$name = 'tree_cache_'.$mid.SITE_ID.md5(dr_array2string($data).$this->ismain.$mid.$str.$default.$onlysub.$is_push.$is_first);
if (IS_ADMIN) {
$name.= 'admin'.md5(\Phpcmf\Service::C()->admin ? dr_array2string(\Phpcmf\Service::C()->admin['roleid']) : '1');
} else {
$name.= md5(\Phpcmf\Service::C()->member ? dr_array2string(\Phpcmf\Service::C()->member['authid']) : '2');
}
$string = CI_DEBUG ? '' : \Phpcmf\Service::L('cache')->get_file($name, $dir);
if (!$string) {
if (dr_count($data) > 30) {
$string = '