gadlol

osCommerce tep_draw_categories_tree by John

Jul 14th, 2014
475
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //tep_draw_categories_tree BY JOHN BAROUNIS
  2.  
  3.   function tep_draw_categories_tree($drawExpanded=false,$root_id = 0,$mainUlClass='',$submenuUlClass='submenu'){
  4.     global $languages_id,$cPath_array;
  5.        
  6.     //GET ALL CATEGORIES
  7.     $categories_query = tep_db_query("select c.categories_id, cd.categories_name, c.parent_id from " . TABLE_CATEGORIES . " c, " . TABLE_CATEGORIES_DESCRIPTION . " cd where c.categories_id = cd.categories_id and cd.language_id='" . (int)$languages_id ."' order by sort_order, cd.categories_name");
  8.    
  9.     $items = array();
  10.    
  11.     while ($categories = tep_db_fetch_array($categories_query))  {
  12.         $items[$categories['categories_id']] = array('name' => $categories['categories_name'], 'parent_id' => $categories['parent_id'], 'id' => $categories['categories_id']);
  13.     }
  14.    
  15.     $citems=count($items);
  16.    
  17.     if($citems<=0) return '';
  18.     elseif($citems==1) $children[] = $items; //in case we have one category item without subcategories, rare but possible
  19.     else foreach( $items as $item ) $children[$item['parent_id']][] = $item;
  20.        
  21.         // loop will be false if the root has no children (i.e., an empty categories!)
  22.         $loop = !empty( $children[$root_id] );
  23.  
  24.         $parent = $root_id;
  25.         $parent_stack = array();
  26.     $html=array();//store html code
  27.         $stack=array();//helper array so to know the current level
  28.    
  29.     $pic=''; //products_in_category string
  30.    
  31.         $html[]='<ul class="'.$mainUlClass.'">';
  32.         while ( $loop && ( ( $option = each( $children[$parent] ) ) || ( $parent > $root_id ) ) ){
  33.  
  34.             if ( $option === false ){
  35.  
  36.                 $parent = array_pop( $parent_stack );
  37.                
  38.                 $html[] = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 ) . '</ul>';
  39.                 $html[] = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 ) . '</li>';
  40.                
  41.                 array_pop( $stack );
  42.                
  43.             }elseif ( !empty( $children[$option['value']['id']] ) ){
  44.            
  45.                 $tab = str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 );
  46.                 $stack[]=$option['value']['id'];
  47.                
  48.                 $rt=$root_id>0 ? $root_id.'_' : '';
  49.  
  50.                 $cpath_new=count($stack)<=0 ? 'cPath='.$rt.$option['value']['id'] : 'cPath='.$rt.implode('_',$stack);        
  51.                    
  52.           $html[]=$tab.'<li><a href="'.tep_href_link(FILENAME_DEFAULT, $cpath_new).'">';
  53.        
  54.           if (SHOW_COUNTS == 'true') { //THIS SHOULD BE CHANGED SO NOT TO USE tep_count_products_in_category WHICH IS RECURSIVE
  55.             $products_in_category = tep_count_products_in_category($option['value']['id']);
  56.             if ($products_in_category > 0) {
  57.               $pic='&nbsp;(' . $products_in_category . ')';
  58.             }
  59.           }
  60.          
  61.           $sm=0;        
  62.          
  63.           if((isset($cPath_array) && in_array($option['value']['id'], $cPath_array))){
  64.                    
  65.             $sm=1;
  66.          
  67.             $html[]='<strong>'.stripslashes($option['value']['name']).'-&gt;'.$pic.'</strong>';
  68.            
  69.           }else{
  70.          
  71.             $html[]=stripslashes($option['value']['name']).'-&gt;'.$pic;
  72.            
  73.           }
  74.      
  75.           $html[]='</a>';
  76.  
  77.                   $html[] = $tab . "\t" . '<ul class="'.$submenuUlClass.'" style="'.($sm!==1 && !$drawExpanded ?'display:none;':'').'">';
  78.  
  79.                 $parent_stack[]=$option['value']['parent_id'];
  80.                 $parent = $option['value']['id'];
  81.  
  82.             }else{
  83.                
  84.         $rt=$root_id>0 ? $root_id.'_' : '';
  85.  
  86.                 $cpath_new= count($stack)<=0 ? 'cPath='.$rt.$option['value']['id'] : 'cPath='.$rt.implode('_',$stack).'_'.$option['value']['id'];
  87.            
  88.                 $html[]=str_repeat( "\t", ( count( $parent_stack ) + 1 ) * 2 - 1 ).'<li><a href="'.tep_href_link(FILENAME_DEFAULT, $cpath_new).'" >';
  89.                
  90.         if (SHOW_COUNTS == 'true') { //THIS SHOULD BE CHANGED SO NOT TO USE tep_count_products_in_category WHICH IS RECURSIVE
  91.           $products_in_category = tep_count_products_in_category($option['value']['id']);
  92.           if ($products_in_category > 0) {
  93.             $pic='&nbsp;(' . $products_in_category . ')';
  94.           }
  95.         }
  96.                
  97.                 if (isset($cPath_array) && in_array($option['value']['id'], $cPath_array)) {
  98.  
  99.           $html[]='<strong>'.stripslashes($option['value']['name']).$pic.'</strong>';
  100.  
  101.         }else{
  102.  
  103.           $html[]=stripslashes($option['value']['name']).$pic;
  104.  
  105.         }
  106.                
  107.                 $html[]='</a></li>';
  108.                
  109.             }
  110.                
  111.         }
  112.     $html[]='</ul>';
  113.         echo implode( "\r\n", $html );
  114.  
  115.   }
  116.  
  117. //tep_draw_categories_tree BY JOHN BAROUNIS
RAW Paste Data