*/ class clsTopLevelMenu{ var $tree = array(); var $tmpArray = array(); var $html=""; var $aStack = array(); var $sPathToExpand = ""; var $nMaxLevel = 2; var $nStartCategoryID = 68; var $cssClass; var $index; var $menuId; var $tagFilter; var $version; var $source; function __construct($nMaxLevel=2, $cssClass="", $index, $menuId="top-menu", $tagFilter="", $version="2011", $source="menu" ) { $this->nStartCategoryID=category_default_page_id; $this->nMaxLevel = $nMaxLevel; $this->cssClass = $cssClass; $this->index = $index; $this->tagFilter = $tagFilter; if($menuId==""){ $menuId="top-menu"; } $this->menuId = $menuId; $this->version = $version; $this->source= $source; } function parseMenu(){ $db = new clsDBInternetDB(); $tagFilter = ""; if($this->tagFilter!=""){ $tagFilter=" and a.tags like '%".$tagFilter."%'"; } $SQL=""; if($this->source=="shop"){ $page=getNesPage("TemplateShopCategory"); $SQL=" SELECT categories_name as top_menu_name, childcount as child_count, concat('".$page."/"."',seo_name) as seo_name, '' as category_url, category_path, 1 as topmenu, 1 as ver_menu_visible, 0 as system, category_id, categories_name as category_name, category_id_parent FROM shop_category INNER JOIN shop_categories_description ON shop_category.category_id = shop_categories_description.id WHERE shop_category.valid=1 and shop_categories_description.languages_id = ".langid." order by category_position "; }else{ $SQL = " SELECT a.top_menu_name, a.child_count, a.seo_name, a.category_url, a.category_path, a.topmenu, a.ver_menu_visible, a.system, a.category_id, a.category_name, a.category_id_parent FROM directory_categories a WHERE a.category_path like '".$this->nStartCategoryID.".%' and a.category_id != 0 ".$tagFilter." order by a.category_position"; } $dbresult = $db->query($SQL); while ($dbresult && $row = mysql_fetch_array($dbresult)) { $id="tm-".$row['seo_name']; $url = "/" . $row['seo_name'] . "/"; if( $row['category_url']!="" ){ $url = $row['category_url']; } if (!strlen($url)) { $url = "#"; } $menuname = $row['category_name']; if($row['top_menu_name']!=""){ $menuname = $row['top_menu_name']; } $menuname = substr ($menuname, 0, 100); if($row['category_id_parent']==$this->nStartCategoryID && $row['topmenu']!=1){ }elseif( substr_count($row['category_path'], '.') > $this->nMaxLevel+1){ }elseif( substr_count($row['category_path'], '.') > 2 && $row['ver_menu_visible']==0){ }elseif($row['category_id']==$this->nStartCategoryID){ }elseif($row['category_id_parent']==$this->nStartCategoryID && $row['topmenu']==1){ $this->tmpArray[$row['category_id']]['parent_id'] = null; $this->tmpArray[$row['category_id']]['name'] = $menuname ; $this->tmpArray[$row['category_id']]['url'] = $url ; $this->tmpArray[$row['category_id']]['id'] = $id ; $this->tmpArray[$row['category_id']]['children'] = $row['child_count']; }else{ $this->tmpArray[$row['category_id']]['parent_id'] = $row['category_id_parent']; $this->tmpArray[$row['category_id']]['name'] = $menuname ; $this->tmpArray[$row['category_id']]['url'] = $url ; $this->tmpArray[$row['category_id']]['id'] = $id ; $this->tmpArray[$row['category_id']]['children'] = $row['child_count']; } } unset($db); $this->depth($this->tmpArray,0,0,""); $this->displayMenu(); return $this->toString(); } function depth($tmpArray,$parent_id,$level,$path){ reset ($tmpArray); $bFirst=false; $last_key=null; foreach($tmpArray as $key => $value){ if ($value['parent_id'] == $parent_id ){ unset($tmpArray[$key]); unset($this->tmpArray[$key]); $this->tree[$key]['level'] = $level; $this->tree[$key]['parent_id'] = $value['parent_id']; $this->tree[$key]['name'] = $value['name']; $this->tree[$key]['url'] = $value['url']; $this->tree[$key]['id'] = $value['id']; $this->tree[$key]['path'] = $path.$key."."; $this->tree[$key]['children'] = $value['children']; if(!$bFirst){ $bFirst=true; $this->tree[$key]['first'] = true; }else{ $this->tree[$key]['first'] = false; } if($last_key!=null){ $this->tree[$last_key]['last'] = false; } $last_key=$key; $this->tree[$key]['last'] = true; unset($value); if($key != $parent_id ){ //$this->tree[$key]['children'] = 1; } if($level+1 <= $this->nMaxLevel){ $this->depth($this->tmpArray,$key,$level+1,$path.$key."."); } } } } function response( $code ){ if($code!=""){ $this->html .= $code; } } function sPush( $level, $code ){ array_push($this->aStack, array("level" => $level,"code" => $code)); } function sPop( $level ){ $_level=$level; $out=""; $bLoop=true; while($bLoop){ $aLast=array_pop($this->aStack); $_level=$aLast[level]; if($aLast!=null && $_level>=$level){ $out.=$aLast[code]; }elseif($aLast!=null && $_level<$level){ array_push($this->aStack,$aLast); $bLoop=false; }else{ $bLoop=false; } } return $out; } function displayMenu( ){ reset ($this->tree); $_level = 0; foreach($this->tree as $id => $value){ $name = $value['name']; $level = $value['level']; $parent_id = $value['parent_id']; $first = $value['first']; $last = $value['last']; $path = $value['path']; $url = $value['url']; $children = $value['children']; if($level<=$_level){ $this->response($this->sPop( $level )); }else{ $this->response(""); } $current=false; $ptm=category_default_page_top_menu; if(defined("category_default_page_top_dynamic")){ $ptm=category_default_page_top_dynamic; } if($id==$ptm){ $current=true; } $class=""; $class_prefix=""; if( $first ){ $class= $class_prefix . "first"; $class_prefix=" "; } if( $last ){ $class.=$class_prefix . "last"; $class_prefix=" "; } if( $current ){ $class.=$class_prefix . "current"; $class_prefix=" "; } $class_a = ''; if($children!=0 && $level < ($this->nMaxLevel-1) ){ if($this->version =="2011"){ $class_a = ' class="dir"'; }else{ $class .= ' dir'; } } if($class!=""){ $class=' class="'.$class.'"'; } $this->response('' . $name .""); $this->sPush($level,""); $_parent_id=$parent_id; $_level=$level; } $bLoop=true; while($bLoop){ $aLast=array_pop($this->aStack); if($aLast!=null ){ $this->response($aLast[code]); }else{ $bLoop=false; } } } function toString(){ $class=""; if($this->version=="2011"){ $class=" toplevel-menu "; } return ''; } } ?>