Advertisement
abushyk

purecssmenu.php

May 3rd, 2015
450
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.50 KB | None | 0 0
  1. <?php
  2. /**
  3.  * PureCSS menu class
  4.  * @author Kondin Dmitriy <kondin@etown.ru> http://www.sitebill.ru
  5.  */
  6. class PureCSS_Menu extends Structure_Manager {
  7.     /**
  8.      * Constructor
  9.      */
  10.     function __construct() {
  11.         $this->SiteBill();
  12.     }
  13.  
  14.     /**
  15.      * Get menu
  16.      * @param
  17.      * @return
  18.      */
  19.     function get_menu ($menues=array()) {
  20.         $DBC=DBC::getInstance();
  21.         $structure=$this->loadCategoryStructure();
  22.        
  23.         $add_points=array();
  24.         $additional_menues=array();
  25.         $ids==array();
  26.        
  27.         if(!empty($menues)){
  28.             foreach($menues as $m){
  29.                 if($m['id']==0){
  30.                     $m['href']='http://'.$_SERVER['HTTP_HOST'].'/'.SITEBILL_MAIN_URL.'/';
  31.                 }else{
  32.                     if(isset($_SESSION['_site_cache']['nave_menu'][$m['id']])){
  33.                         $add_points[$m['id']]=$_SESSION['_site_cache']['nave_menu'][$m['id']];
  34.                     }else{
  35.                         $ids[]=$m['id'];
  36.                     }
  37.                 }
  38.        
  39.        
  40.                 $additional_menues[$m['id']]=$m;
  41.             }
  42.        
  43.             if(!empty($ids)){
  44.                 $query='SELECT * FROM '.DB_PREFIX.'_menu_structure WHERE menu_id IN ('.implode(',', $ids).') ORDER BY sort_order';
  45.                 $stmt=$DBC->query($query);
  46.                 if($stmt){
  47.                     while($ar=$DBC->fetch($stmt)){
  48.                         $ar['url']=trim($ar['url']);
  49.                         if($ar['url']!='' && 0!==strpos($ar['url'], 'http:')){
  50.                             $ar['url'] = trim($ar['url'], '/');
  51.                         }
  52.                         $add_points[$ar['menu_id']][]=$ar;
  53.                         $_SESSION['_site_cache']['nave_menu'][$ar['menu_id']]=$add_points[$ar['menu_id']];
  54.                     }
  55.                 }
  56.             }
  57.        
  58.         }
  59.        
  60.         if(!empty($additional_menues)){
  61.             $behind=array();
  62.             $after=array();
  63.             foreach ($additional_menues as $k=>$ad_menu){
  64.                 if($ad_menu['position']=='behind'){
  65.                     $behind[]=$ad_menu;
  66.                 }else{
  67.                     $after[]=$ad_menu;
  68.                 }
  69.        
  70.        
  71.             }
  72.             if(!empty($behind)){
  73.                 $behind=array_reverse($behind);
  74.             }
  75.             $additional_menues=array_merge($behind, $after);
  76.             foreach ($additional_menues as $ad_menu){
  77.                 $k=$ad_menu['id'];
  78.                 $indexes=array_keys($structure['catalog']);
  79.                 sort($indexes);
  80.                 $last_index=end($indexes)+1;
  81.                 if($ad_menu['position']=='behind'){
  82.                     array_unshift($structure['childs'][0], $last_index);
  83.                 }else{
  84.                     $structure['childs'][0][]=$last_index;
  85.                 }
  86.                 if(isset($ad_menu['href']) && $ad_menu['href']!=''){
  87.                     $href=trim($ad_menu['href'], '/');
  88.                 }else{
  89.                     $href='#';
  90.                 }
  91.                 $structure['catalog'][$last_index]=array('url'=>$href, 'name'=>$ad_menu['title']);
  92.        
  93.                 if(isset($add_points[$k]) && !empty($add_points[$k])){
  94.                     $current_index=$last_index+1;
  95.                     foreach ($add_points[$k] as $ad_point){
  96.                         $structure['catalog'][$current_index]=array('url'=>$ad_point['url'], 'name'=>$ad_point['name']);
  97.                         $structure['childs'][$last_index][]=$current_index;
  98.                         $current_index++;
  99.                     }
  100.                 }
  101.             }
  102.        
  103.         }
  104.        
  105.         //return Realty_Menu_Decorator::getMenu($structure);
  106.        
  107.        
  108.        
  109.         $category_structure = $structure;
  110.         $level = 1;
  111.  
  112.  
  113.         $rs = '<ul class="nav navbar-nav">';
  114.  
  115.         foreach ( $category_structure['childs'][0] as $item_id => $categoryID ) {
  116.             if ( isset($category_structure['childs'][$categoryID]) && count($category_structure['childs'][$categoryID]) > 0 ) {
  117.                 $name = '<span>'.$category_structure['catalog'][$categoryID]['name'].'</span>';
  118.             } else {
  119.                 $name = '<span>'.$category_structure['catalog'][$categoryID]['name'].'</span>';
  120.             }
  121.  
  122.             if($category_structure['catalog'][$categoryID]['url']!=''){
  123.                 if ( preg_match('/^http/', $category_structure['catalog'][$categoryID]['url']) ) {
  124.                     $rs .= '<li class="dropdown active"><a class="dropdown-toggle" data-toggle="dropdown'.($category_structure['catalog'][$categoryID]['current']==1 ? ' current' : '').'" href="'.$category_structure['catalog'][$categoryID]['url'].'">'.$name.'<span class="caret"></span></a>';
  125.                 } else {
  126.                     $rs .= '<li class="dropdown active"><a class="dropdown-toggle" data-toggle="dropdown'.($category_structure['catalog'][$categoryID]['current']==1 ? ' current' : '').'" href="'.SITEBILL_MAIN_URL.'/'.$category_structure['catalog'][$categoryID]['url'].'">'.$name.'<span class="caret"></span></a>';
  127.                 }
  128.             }else{
  129.                 $rs .= '<li class="dropdown active"><a class="dropdown-toggle" data-toggle="dropdown'.($category_structure['catalog'][$categoryID]['current']==1 ? ' current' : '').'" href="'.SITEBILL_MAIN_URL.'/topic'.$categoryID.'.html">'.$name.'<span class="caret"></span></a>';
  130.             }
  131.  
  132.             $rs .= $this->getChildNodes($categoryID, $category_structure, $level + 1, $current_category_id=0);
  133.             $rs .= '</li>';
  134.         }
  135.         $rs .= '</ul>';
  136.  
  137.      
  138.         return $rs;
  139.     }
  140.  
  141.     /**
  142.      * Get child nodes
  143.      * @param $categoryID
  144.      * @param $category_structure
  145.      * @param $level
  146.      * @param $current_category_id
  147.      */
  148.     function getChildNodes($categoryID, $category_structure, $level, $current_category_id) {
  149.         $rs='';
  150.         if ( !is_array($category_structure['childs'][$categoryID]) ) {
  151.             return '';
  152.         }
  153.         $rs .= '<ul class="dropdown-menu">';
  154.         foreach ( $category_structure['childs'][$categoryID] as $child_id ) {
  155.             if ( !empty($category_structure['childs'][$child_id]) AND count($category_structure['childs'][$child_id]) > 0 ) {
  156.                 $name = '<span>'.$category_structure['catalog'][$child_id]['name'].'</span>';
  157.             } else {
  158.                 $name = $category_structure['catalog'][$child_id]['name'];
  159.             }
  160.  
  161.             if($category_structure['catalog'][$child_id]['url']!=''){
  162.                 $rs .= '<li><a '.($category_structure['catalog'][$child_id]['current']==1 ? ' current' : '').'" href="'.SITEBILL_MAIN_URL.'/'.$category_structure['catalog'][$child_id]['url'].'">'.$name.'</a>';
  163.             }else{
  164.                 $rs .= '<li><a '.($category_structure['catalog'][$child_id]['current']==1 ? ' current' : '').'" href="'.SITEBILL_MAIN_URL.'/topic'.$child_id.'.html">'.$name.'</a>';
  165.             }
  166.             if ( !empty($category_structure['childs'][$child_id]) AND count($category_structure['childs'][$child_id]) > 0 ) {
  167.                 $rs .= $this->getChildNodes($child_id, $category_structure, $level + 1, $current_category_id);
  168.             }
  169.             $rs .= '</li>';
  170.         }
  171.         $rs .= '</ul>';
  172.         return $rs;
  173.     }
  174.  
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement