<?php
namespace authruletree;
class Authruletree {
    
    public function menutree($authrule){
        return $this->sort($authrule);
    }

    public function sort($authrule,$pid=0,$level=0){

        static $arr=array();
        foreach ($authrule as $k => $v) {
            if($v['pid']==$pid){
                $v['level']=$level;
                $arr[]=$v;
                $this->sort($authrule,$v['id'],$level+1);
            }
        }

        return $arr;
    }

    //获取子栏目id

   public function childrenids($id,$obj){
        $data=$obj->field('id,pid')->select();
        return $this->_childrenids($data,$id);
   }

   private function _childrenids($data,$id){
        static $arr=array();
        foreach ($data as $k => $v) {
            if($v['pid']==$id){
                $arr[]=$v['id'];
                $this->_childrenids($data,$v['id']);
            }
        }

        return $arr;
   }

   //处理栏目排序
   public function cateSort($data,$obj){
    foreach ($data as $k => $v) {
        $obj->update(['id'=>$k,'listorder'=>$v]);
    }
   }

  

   
}