<?php
/**
*	此文件是流程模块【home_nav.首页导航】对应控制器接口文件。
*/ 
class mode_aqyk_home_navClassAction extends inputAction{
	
	/**
	*	重写函数：保存前处理，主要用于判断是否可以保存
	*	$table String 对应表名
	*	$arr Array 表单参数
	*	$id Int 对应表上记录Id 0添加时，大于0修改时
	*	$addbo Boolean 是否添加时
	*	return array('msg'=>'错误提示内容','rows'=> array()) 可返回空字符串，或者数组 rows 是可同时保存到数据库上数组
	*/
	protected function savebefore($table, $arr, $id, $addbo){
		
	}
	
	/**
	*	重写函数：保存后处理，主要保存其他表数据
	*	$table String 对应表名
	*	$arr Array 表单参数
	*	$id Int 对应表上记录Id
	*	$addbo Boolean 是否添加时
	*/	
	protected function saveafter($table, $arr, $id, $addbo){
		
	}
	
	private function getselectdatas($rows,&$barr, $pid=NULL, $level=0)
    {
        foreach($rows as $k=>$rs){
            if($rs['parent_id']==$pid){
                $str  = '';
                for($i=0;$i<$level;$i++)$str.='&nbsp;&nbsp;&nbsp;';
                if($str!='')$str.='├';
                $name = ''.$str.''.$rs['title'].'';
                $barr[] = array(
                    'name'  => $name,
                    'value' => $rs['id'],
                );
                $this->getselectdatas($rows, $barr, $rs['id'], $level+1);
            }
        }
    }

    public function getselectdata($lx=NULL)
    {
        $rows = m('aqyk_home_nav')->getall('1=1','`id`,title,parent_id');
        $barr = array();
        if($lx==NULL)$barr[] = array(
            'value' => NULL,
            'name'  => '最顶级',
        );
        $this->getselectdatas($rows, $barr, NULL, 0);
        return $barr;
    }

    public function home_navdata()
    {
        return $this->getselectdata();
    }

    public function storeafter($table, $rows)
    {
        $barr = array();
        $this->gettreedata($rows, $barr);

        return array(
            'rows' => $barr
        );
    }

    //树形结构
    public function gettreedata($rows, &$barr, $pid=NULL, $level=1)
    {
        foreach($rows as $k=>$rs){
            if($rs['parent_id']==$pid){
                $rs['level'] 	= $level;
                $rs['stotal'] 	= $this->gettreetotal($rows, $rs['id']);
                $barr[] = $rs;
                $this->gettreedata($rows, $barr, $rs['id'], $level+1);
            }
        }
    }
    public function gettreetotal($rows, $pid)
    {
        $stotal = 0;
        foreach($rows as $k=>$rs){
            if($rs['parent_id']==$pid){
                $stotal++;
            }
        }
        return $stotal;
    }
	
	public function categoryList()
    {
        $admin = m('admin')->getone("id=$this->adminid");
        $comid = $admin['comid'];
        $data = m('aqyk_home_nav')->getall("comid=$comid");
        if (is_array($data)){
            $arr = $this->getLevel($data);
            $result = $this->getChilds($arr);
        }else{
            $result = [];
        }
        return $result;
    }
    /**
     * 传递一个父级分类ID返回所有子分类
     * @param $cate
     * @param $pid
     * @return array
     */
    private function getChilds($data, $pid=null)
    {
        $arr = array();
        foreach ($data as $v) {
            if ($v['parent_id']==$pid){
                $nbsp = '&nbsp;&nbsp;&nbsp;&nbsp;';
                $res = str_repeat($nbsp,$v['level']-1);
                $value = array("name"=>$res.$v['title'],"value"=>$v['id']);
                $arr[] = $value;
                $arr = array_merge($arr, $this->getChilds($v['child'], $v['id']));
            }
        }
        return $arr;
    }

    /**
     * 传递一个父级分类ID返回所有子分类
     * @param $cate
     * @param $pid
     * @return array
     */
    private function getLevel($data, $pid=null,$level = 0)
    {
        $_arr = [];
        foreach ($data as $v) {
            if ($v['parent_id'] == $pid) {
                $arr = $v;
                $arr['level'] = $level+1;
                $arr['child'] = $this->getLevel($data, $v['id'],$arr['level']);
                $_arr[] = $arr;
            }
        }
        return $_arr;
    }
}	
			