thinkphp框架onethink后台支持三级菜单,_php技巧_脚本之家
分类:高并发

复制代码 代码如下:

复制代码 代码如下:

onethink后台支持三级菜单,CMS管理后台1.0版本本身不支持三级菜单,稍加改动,即可支持无限级分类

复制代码 代码如下:

/** 
* 组合模式 

* 将对象组合成树形结构以表示"部分-整体"的层次结构,使得客户对单个对象和复合对象的使用具有一致性 
*/ 
abstract class MenuComponent 

public function add($component){}
public function remove($component){}
public function getName(){}
public function getUrl(){}
public function display(){}
}
class Menu extends MenuComponent
{
private $_items = array();
private $_name = null;
public function __construct($name)
{
$this->_name = $name;
}
public function add($component)
{
$this->_items[] = $component;
}
public function remove($component)
{
$key = array_search($component,$this->_items);
if($key !== false) unset($this->_items[$key]);
}
public function display()
{
echo "-- ".$this->_name." ---------
";
foreach($this->_items as $item)
{
$item->display();
}
}
}
class Item extends MenuComponent
{
private $_name = null;
private $_url = null;
public function __construct($name,$url)
{
$this->_name = $name;
$this->_url = $url;
}
public function display()
{
echo $this->_name."#".$this->_url."
";
}
}
class Client
{
private $_menu = null;
public function __construct($menu)
{
$this->_menu = $menu;
}
public function setMenu($menu)
{
$this->_menu = $menu;
}
public function displayMenu()
{
$this->_menu->display();
}
}
// 实例一下
// 创建menu
$subMenu1 = new Menu("sub menu1");
$subMenu2 = new Menu("sub menu2");
$subMenu3 = new Menu("sub menu3");
$item1 = new Item("163","www.163.com");
$item2 = new Item("sina","www.sina.com");
$subMenu1->add($item1);
$subMenu1->add($item2);
$item3 = new Item("baidu","www.baidu.com");
$item4 = new Item("google","www.google.com");
$subMenu2->add($item3);
$subMenu2->add($item4);
$allMenu = new Menu("All Menu");
$allMenu->add($subMenu1);
$allMenu->add($subMenu2);
$allMenu->add($subMenu3);
$objClient = new Client($allMenu);
$objClient->displayMenu();
$objClient->setMenu($subMenu2);
$objClient->displayMenu();

_name = $name; } public function add { $this->_items[] = $component; } public function remove { $key = array_search($component,$this->_items); if unset; } public function display() { echo "-- ".$this->_name." ---------
"; foreach($this->_items as $item) { $item->display(); } } } class Item extends MenuComponent { private $_name = null; private $_url = null; public function __construct { $this->_name = $name; $this->_url = $url; } public function display() { echo $this->_name."#".$this->_url."
"; } } class Client { private $_menu = null; public function __construct { $this->_menu = $menu; } public function setMenu { $this->_menu = $menu; } public function displayMenu() { $this->_menu->display(); } } // 实例一下 // 创建menu $subMenu1 = new Menu; $subMenu2 = new Menu; $subMenu3 = new Menu; $item1 = new Item; $item2 = new Item("sina","www.sina.com"); $subMenu1->add; $subMenu1->add; $item3 = new Item("baidu","www.baidu.com"); $item4 = new Item("google","www.google.com"); $subMenu2->add; $subMenu2->add; $allMenu = new Menu; $allMenu->add; $allMenu->add; $allMenu->add; $objClient = new Client; $objClient->displayMenu(); $objClient->setMenu; $objClient->displayMenu();

/**
 * 获取控制器菜单数组,二级菜单元素位于一级菜单的'_child'元素中
 * @author 朱亚杰  <xcoolcc@gmail.com>
 */
final public function getMenus($controller=CONTROLLER_NAME){
    // $menus  =   session('ADMIN_MENU_LIST'.$controller);
 
    $flag=false;//是否开启menu三级菜单
 
    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(); //设置子节点
 
        //高亮主菜单
        $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'];
 
            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 ){
                    $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');
                    $third_urlss=array();
 
                    if(!empty($second_urls) && $flag){
                    foreach($second_urls as $key=>$v){
 
                        $wheres          =   array();
                        $wheres['pid']   =   $key;
                        $wheres['hide']  =   0;
                        if(!C('DEVELOP_MODE')){ // 是否开发者模式
                            $wheres['is_dev']    =   0;
                        }
                        $third_urls[] = M('Menu')->where($wheres)->getField('id,url');
                        foreach($third_urls as $vv){
                             foreach($vv as $key=>$vs){
                                 $third_urlss[$key]=$vs;
                             }
                        }
                    }
                    }
 
                    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;
                        }
                        if(!empty($third_urlss) && $flag){
                            foreach ($third_urlss as $key=>$to_check_urlss) {
                                if( stripos($to_check_urlss,MODULE_NAME)!==0 ){
                                    $rules = MODULE_NAME.'/'.$to_check_urlss;
                                }else{
                                    $rules = $to_check_urlss;
                                }
                                if($this->checkRule($rules, AuthRuleModel::RULE_URL,null))
 
                                    echo $rules."<br/>";
                                    $to_check_urlsss[] = $to_check_urlss;
                            }
                        }
 
                    }
 
                    // 按照分组生成子菜单树
                    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(!empty($menus['child'][$g]) && $flag){
                            foreach($menuList as $key=>$v){
                                $maps['pid'] =   $v['id'];
                                $menuLists = M('Menu')->where($maps)->field('id,pid,title,url,tip')->order('sort asc')->select();
                                $menus['child'][$g][$key]['ttt'] = list_to_tree($menuLists, 'id', 'pid', 'operater', $v['id']);
                            }
                        }
                    }
                    if($menus['child'] === array()){
                        //$this->error('主菜单下缺少子菜单,请去系统=》后台菜单管理里添加');
                    }
                }
            }
        }
        // session('ADMIN_MENU_LIST'.$controller,$menus);
    }
    return $menus;
}

 $oldSetting = libxml_use_internal_errors( true );   
libxml_clear_errors();  
/**
 * 
 * -+-----------------------------------
 * |PHP5 Framework - 2011
 * |Web Site: www.iblue.cc
 * |E-mail: mejinke@gmail.com
 * |Date: 2012-10-12
 * -+-----------------------------------
 * 
 * @desc HTML解析器
 * @author jingke
 */   
class XF_HtmlDom  
{  
    private $_xpath = null;  
    private $_nodePath = '';  

然后Public/base.html

    public function __construct($xpath = null, $nodePath = '')  
    {  
        $this->_xpath = $xpath;  
        $this->_nodePath = $nodePath;  
    }  

<!-- 边栏 -->
<div class="sidebar">
    <!-- 子导航 -->
    <block name="sidebar">
        <div class="sidebar-collapse" id="sidebar-collapse">
            <i class="fa fa-angle-double-left"><<</i>
        </div>
        <div id="subnav" class="subnav">
            <notempty name="_extra_menu">
                {// 动态扩展菜单 //}
                {:extra_menu($_extra_menu,$__MENU__)}
            </notempty>
            <volist name="__MENU__.child" id="sub_menu">
                <!-- 子导航 -->
                <notempty name="sub_menu">
                    <notempty name="key"><h3><i class="icon icon-unfold"></i>{$key}</h3></notempty>
                    <ul class="side-sub-menu">
                        <volist name="sub_menu" id="menu">
                            <li>
                                <a class="item" href="{$menu.url|U}">{$menu.title}</a>
                            </li>
                        </volist>
                       <notempty name="menu.ttt">
                         <volist name="menu.ttt" id="menus">
                            <li>
                                <a class="item" href="{$menus.url|U}">{$menus.title}</a>
                            </li>
                         </volist>
                       </notempty>
                    </ul>
                </notempty>
                <!-- /子导航 -->
            </volist>
        </div>
    </block>
    <!-- /子导航 -->
</div>

    public function loadHtml($url)  
    {  
        ini_set('user_agent', 'Mozilla/5.0 (Linux; U; Android 2.1; en-us; Nexus One Build/ERD62) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17 –Nexus');  
        $content = '';  
        if(strpos(strtolower($url), 'http')===false)  
        {  
            $content = file_get_contents($url);  
        }  
        else 
        {  
            $ch = curl_init();   
            $user_agent = "Baiduspider+(+)";  
            $user_agent1='Mozilla/5.0 (Windows NT 5.1; rv:6.0) Gecko/20100101 Firefox/6.0';  
            curl_setopt($ch, CURLOPT_URL, $url);   
            curl_setopt($ch, CURLOPT_HEADER, false);   
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
            curl_setopt($ch, CURLOPT_REFERER, $url);  
            curl_setopt($ch, CURLOPT_USERAGENT, $user_agent1);  
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);  
            $content =curl_exec($ch);   
            curl_close($ch);  
        }  

        $html = new DOMDocument();   
        $html->loadHtml($content);   
        $this->_xpath = new DOMXPath( $html );   
        //return $this; 

    }  

    public function find($query, $index = null)  
    {  
        if($this->_nodePath == '')  
            $this->_nodePath = '//'; 
        else 
            $this->_nodePath .= '/';  

        $nodes = $this->_xpath->query($this->_nodePath.$query);  
        //echo $nodes->item(0)->getNodePath();exit; 

          
        if ($index == null && !is_numeric($index))   
        {   
            $tmp = array();  
            foreach ($nodes as $node)   
            {  
                $tmp[] = new XF_HtmlDom($this->_xpath, $node->getNodePath());  
            }  
            return $tmp;  
        }  
        return new XF_HtmlDom($this->_xpath,$this->_xpath->query($this->_nodePath.$query)->item($index)->getNodePath());  
    }  

    /**
     * 获取内容
     */ 
    public function text()  
    {  
        if ($this->_nodePath != '' && $this->_xpath != null )   
            return $this->_xpath->query($this->_nodePath)->item(0)->textContent;  
        else 
            return false;  
    }  

    /**
     * 获取属性值
     */ 
    public function getAttribute($name)  
    {  
        if ($this->_nodePath != '' && $this->_xpath != null )   
            return $this->_xpath->query($this->_nodePath)->item(0)->getAttribute($name);  
        else 
            return false;  
    }  

    public function __get($name)  
    {  
        if($name == 'innertext')  
            return $this->text();  
        else 
            return $this->getAttribute($name);  
    }    
}    
$xp = new xf_HtmlDom();  
$xp->loadHtml(''); 
$rows = $xp->find("td[@id='baidu']/a", 0)->innertext;  
print_r($rows); 

本文由10bet手机官网发布于高并发,转载请注明出处:thinkphp框架onethink后台支持三级菜单,_php技巧_脚本之家

上一篇:清除空格,php压缩HTML函数轻松实现压缩html 下一篇:没有了
猜你喜欢
热门排行
精彩图文