onethink学习之动态扩展菜单

在Admin\View\Public\base.html中有 {:extra_menu($_extra_menu,$__MENU__)}这句调用。
参数$_extra_menu在Admin\Controller\AddonsController.class.php进行指定,代码为:

     $this->assign('_extra_menu',array(
            '已装插件后台'=> D('Addons')->getAdminList(),
        ));

参数$__MENU__在Admin\Controller\AdminController.class.php中进行指定,代码为:

 $this->assign('__MENU__', $this->getMenus());

函数getMenus()的代码为:

     /**
     * 获取控制器菜单数组,二级菜单元素位于一级菜单的'_child'元素中
     * 
     */
    final public function getMenus($controller=CONTROLLER_NAME){

        // $menus  =   session('ADMIN_MENU_LIST'.$controller);
        if(empty($menus)){
            // 获取主菜单
            $where['pid']   =   0;
            $where['hide']  =   0;
            if(!C('DEVELOP_MODE')){ // 是否开发者模式
                $where['is_dev']    =   0;
            }
            $menus['main']  =   M('Menu')->where($where)->order('sort asc')->select();//获得主菜单 


            $menus['child'] = array(); //设置子节点

            //高亮主菜单 获得当前url的id
            $current = M('Menu')->where("url like '%{$controller}/".ACTION_NAME."%'")->field('id')->find();

            if($current){
                $nav = D('Menu')->getPath($current['id']);

                $nav_first_title = $nav[0]['title'];//获得当前url的名称

                foreach ($menus['main'] as $key => $item) {
                    if (!is_array($item) || empty($item['title']) || empty($item['url']) ) {
                        $this->error('控制器基类$menus属性元素配置有误');
                    }
                    if( stripos($item['url'],MODULE_NAME)!==0 ){//如果url中没有模块名,则添加模块名
                        $item['url'] = MODULE_NAME.'/'.$item['url'];
                    }
                    // 判断用户是否有主菜单权限
                    if ( !IS_ROOT && !$this->checkRule($item['url'],AuthRuleModel::RULE_MAIN,null) ) {
                        unset($menus['main'][$key]);
                        continue;//继续循环
                    }

                    // 获取当前主菜单的子菜单项
                    if($item['title'] == $nav_first_title){
                        $menus['main'][$key]['class']='current';
                        //生成child树
                        $groups = M('Menu')->where("pid = {$item['id']}")->distinct(true)->field("`group`")->select();//获得组名称,如扩展

                        if($groups){
                            $groups = array_column($groups, 'group');

                        }else{
                            $groups =   array();
                        }

                        //获取二级分类的合法url
                        $where          =   array();
                        $where['pid']   =   $item['id'];
                        $where['hide']  =   0;
                        if(!C('DEVELOP_MODE')){ // 是否开发者模式
                            $where['is_dev']    =   0;
                        }
                        $second_urls = M('Menu')->where($where)->getField('id,url');//获得某个菜单的所有子菜单

                        if(!IS_ROOT){
                            // 检测菜单权限
                            $to_check_urls = array();
                            foreach ($second_urls as $key=>$to_check_url) {
                                if( stripos($to_check_url,MODULE_NAME)!==0 ){
                                    $rule = MODULE_NAME.'/'.$to_check_url;
                                }else{
                                    $rule = $to_check_url;
                                }
                                if($this->checkRule($rule, AuthRuleModel::RULE_URL,null))
                                    $to_check_urls[] = $to_check_url;
                            }
                        }

                        // 按照分组生成子菜单树

                        foreach ($groups as $g) {
                            $map = array('group'=>$g);
                            if(isset($to_check_urls)){
                                if(empty($to_check_urls)){
                                    // 没有任何权限
                                    continue;
                                }else{
                                    $map['url'] = array('in', $to_check_urls);
                                }
                            }
                            $map['pid'] =   $item['id'];
                            $map['hide']    =   0;
                            if(!C('DEVELOP_MODE')){ // 是否开发者模式
                                $map['is_dev']  =   0;
                            }
                            $menuList = M('Menu')->where($map)->field('id,pid,title,url,tip')->order('sort asc')->select();

                            $menus['child'][$g] = list_to_tree($menuList, 'id', 'pid', 'operater', $item['id']);

                        }
                        if($menus['child'] === array()){
                            //$this->error('主菜单下缺少子菜单,请去系统=》后台菜单管理里添加');
                        }
                    }
                }
            }
            // session('ADMIN_MENU_LIST'.$controller,$menus);
        }

        return $menus;
    }

你可能感兴趣的:(onethink)