<?php
declare (strict_types=1);
namespace library\utils;
// +----------------------------------------------------------------------
// | [ WE CAN DO IT MORE SIMPLE  ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2020 rights reserved.
// +----------------------------------------------------------------------
// | Author: TABLE ME
// +----------------------------------------------------------------------
// | Date: 2020-09-03 21:20
// +----------------------------------------------------------------------


class Dtree {

    public $ret;
    public $arr; // 生成树型结构所需要的2维数组
    public $nbsp = "&nbsp;";
    public $icon = array('│', '├', '└'); // 生成树型结构所需修饰符号，可以换成图片

    public $is_noselect = false;//是否不支持无下一级选项

    /**
     * 构造函数，初始化类
     * @param array 2维数组，例如：
     * array(
     *      1 => array('id'=>'1','pid'=>0,'name'=>'一级栏目一'),
     *      2 => array('id'=>'2','pid'=>0,'name'=>'一级栏目二'),
     *      3 => array('id'=>'3','pid'=>1,'name'=>'二级栏目一'),
     *      4 => array('id'=>'4','pid'=>1,'name'=>'二级栏目二'),
     *      5 => array('id'=>'5','pid'=>2,'name'=>'二级栏目三'),
     *      6 => array('id'=>'6','pid'=>3,'name'=>'三级栏目一'),
     *      7 => array('id'=>'7','pid'=>3,'name'=>'三级栏目二')
     *      )
     */
    public function init($arr = array()) {
        $this->arr = $this->ret = NULL;
        $this->arr = $arr;
        return is_array($arr);
    }

    /**
     * 得到父级数组
     *
     * @param int
     * @return array
     */
    public function get_parent($myid) {
        $newarr = array();
        if(!isset($this->arr[$myid])) return FALSE;
        $pid = $this->arr[$myid]['pid'];
        $pid = $this->arr[$pid]['pid'];
        if (is_array($this->arr)) {
            foreach ($this->arr as $id => $a) {
                if ($a['pid'] == $pid) $newarr[$id] = $a;
            }
        }

        return $newarr;
    }

    /**
     * 得到子级数组
     *
     * @param int
     * @return array
     */
    public function get_child($myid) {

        $a = $newarr = array();

        if (is_array($this->arr)) {
            foreach ($this->arr as $id => $a) {
                if ($a['pid'] == $myid) $newarr[$id] = $a;
            }
        }

        return $newarr ? $newarr : FALSE;
    }

    /**
     * 获取子栏目json
     *
     * Enter description here ...
     * @param unknown_type $myid
     */
    public function creat_sub_json($myid, $str='') {
        $sub_cats = $this->get_child($myid);
        $n = 0;
        if(is_array($sub_cats)) foreach($sub_cats as $c) {
            $data[$n]['id'] = iconv(CHARSET,'utf-8',$c['catid']);
            if($this->get_child($c['catid'])) {
                $data[$n]['liclass'] = 'hasChildren';
                $data[$n]['children'] = array(array('text'=>'&nbsp;','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.','));
    }
}