Author Topic: showing all parent-level categories in menu  (Read 5511 times)

druider

  • Beginner
  • *
  • Posts: 2
showing all parent-level categories in menu
« on: August 03, 2007, 11:44:49 am »
Hello All!
When I click on the third-level category menu displays only one parent category. Is it possible to modify ps_product_caterory class to display all parent-level categories using link-list menu?
Second thing is to keeping active not only current parent category but all the categorys in path (like '-' in dTree).
Thanks for reply.

druider

  • Beginner
  • *
  • Posts: 2
Re: showing all parent-level categories in menu
« Reply #1 on: October 26, 2007, 13:48:58 pm »
I have found some solution, here is the code (ps_product_category.php):

Code: [Select]
function get_category_tree( $category_id=0,
$links_css_class="mainlevel",
$list_css_class="mm123",
$highlighted_style="text-decoration:none;" ) {
global $sess;

$categories = ps_product_category::getCategoryTreeArray();

// Copy the Array into an Array with auto_incrementing Indexes
$key = array_keys($categories);
$size = sizeOf($key);
$category_tmp = Array();
for ($i=0; $i<$size; $i++)
$category_tmp[$i] = &$categories[$key[$i]];

$html = "";
/** FIRST STEP
    * Order the Category Array and build a Tree of it
    **/
$nrows = count( $category_tmp );

$id_list = array();
$row_list = array();
$depth_list = array();

$children = array();
$parent_ids = array();

for($k = 0 ; $k < $nrows ; $k++) {
$parent_ids[$k] = $category_tmp[$k]['category_parent_id'];
}

for($n = 0 ; $n < $nrows ; $n++)
if($category_tmp[$n]["category_parent_id"] == 0)
{ array_push($id_list,$category_tmp[$n]["category_child_id"]);
array_push($row_list,$n);
array_push($depth_list,0);
}

$loop_count = 0;
while(count($id_list) < $nrows) {
if( $loop_count > $nrows )
break;
$id_temp = array();
$row_temp = array();
$depth_temp = array();
for($i = 0 ; $i < count($id_list) ; $i++) {
$id = $id_list[$i];
$row = $row_list[$i];
$depth = $depth_list[$i];
array_push($id_temp,$id);
array_push($row_temp,$row);
array_push($depth_temp,$depth);

$pattern = '/\b'.$id.'\b/';
$children = preg_grep( $pattern, $parent_ids );

foreach($children as $key => $value) {
if( array_search($category_tmp[$key]["category_child_id"],$id_list) == NULL) {
array_push($id_temp,$category_tmp[$key]["category_child_id"]);
array_push($row_temp,$key);
array_push($depth_temp,$depth + 1);
}
}
}
$id_list = $id_temp;
$row_list = $row_temp;
$depth_list = $depth_temp;
$loop_count++;
}

/** SECOND STEP
* Find out if we have subcategories to display
**/
$allowed_subcategories = Array();
if( !empty( $categories[$category_id]["category_parent_id"] ) ) {
// Find the Root Category of this category
$root = $categories[$category_id];
$allowed_subcategories[] = $categories[$category_id]["category_parent_id"];
// Loop through the Tree up to the root
while( !empty( $root["category_parent_id"] )) {

$allowed_subcategories[] = $categories[$root["category_child_id"]]["category_child_id"];

$root = $categories[$root["category_parent_id"]];
}
}
// Fix the empty Array Fields
if( $nrows < count( $row_list ) ) {
$nrows = count( $row_list );
}

// Find heigher levels of actual node
$position1=-1;
$position2=-1;
for($kk = 0 ; $kk < $nrows ; $kk++) {
  if($category_id == $category_tmp[$row_list[$kk]]["category_child_id"]) {
    for ($kk2 = 0 ; $kk2 < $nrows ; $kk2++) {
  if($category_tmp[$row_list[$kk]]["category_parent_id"]==$category_tmp[$row_list[$kk2]]["category_child_id"])
{
$position1=$kk2; 
for ($kk3 = 0 ; $kk3 < $nrows ; $kk3++) {
      if($category_tmp[$row_list[$kk2]]["category_parent_id"]==$category_tmp[$row_list[$kk3]]["category_child_id"])
    {
$position2=$kk3;
break;
}
      else
        $position2=-1;
        }
break;
}
  else
    $position1=-1;
    }
  }


}
// Now show the categories

for($n = 0 ; $n < $nrows ; $n++) {


if( !isset( $row_list[$n] ) || !isset( $category_tmp[$row_list[$n]]["category_child_id"] ) )
continue;
if(( $category_id == $category_tmp[$row_list[$n]]["category_child_id"] )||($n==$position1) ||($n==$position2)) {
$style = $highlighted_style;
}
else
$style = "";

$allowed = false;
if( $depth_list[$n] > 0 ) {
// Subcategory!
if( isset( $root ) && in_array( $category_tmp[$row_list[$n]]["category_child_id"], $allowed_subcategories )
|| $category_tmp[$row_list[$n]]["category_parent_id"] == $category_id
|| $category_tmp[$row_list[$n]]["category_parent_id"] == @$categories[$category_id]["category_parent_id"]) {
$allowed = true;

}
}
else
$allowed = true;
/* can use this part of code to show all 2-level positions for other categories, but it's very static
my menu heve two main level categories so here are two ranges (<27) (>27) - it's the number of node on category list
if your menu isn't static this idea isnt good

if($position2==0 && $category_tmp[$row_list[$n]]["category_parent_id"]<27) {
  if ($depth_list[$n]==1) $allowed=true;
}
if($position2==27 && $category_tmp[$row_list[$n]]["category_parent_id"]>27) {
  if ($depth_list[$n]==1) $allowed=true;
}
*/
$append = "";
if( $allowed ) {
if( $depth_list[$n] == 0 )
{
if( $style == $highlighted_style ) {//menu (0-level)
$append = 'id="active_menu0"'; // id for active element
}
}

if( $depth_list[$n] == 1 )//1-level submenu
{
$css_class = "sublevel";
if( $style == $highlighted_style ) {
$append = 'id="active_menu1"'; // id for active element
}
}
else
$css_class = $links_css_class;

if( $depth_list[$n] == 2 )// 2-level submenu
{
$css_class = "sublevel2";
if( $style == $highlighted_style ) {
$append = 'id="active_menu2"';
}
}

$catname = shopMakeHtmlSafe( $category_tmp[$row_list[$n]]["category_name"] );

$html .= '
          <a title="'.$catname.'" style="display:block;'.$style.'" class="'. $css_class .'" href="'. $sess->url(URL."index.php?page=shop.browse&amp;category_id=".$category_tmp[$row_list[$n]]["category_child_id"]) .'" '.$append.'>'
. str_repeat("",$depth_list[$n]) . $catname //. str_repeat("&nbsp;&nbsp;&nbsp;",$depth_list[$n]) . $catname
//. ps_product_category::products_in_category( $category_tmp[$row_list[$n]]["category_child_id"] )
.'</a>';
}
}
$html = ereg_replace('&amp;','&',$html);
return $html;
}

athe

  • Beginner
  • *
  • Posts: 2
Re: showing all parent-level categories in menu
« Reply #2 on: February 04, 2008, 20:25:30 pm »
Hi,
I have the same problem.
You write we have to modify ps_product_category.php file. Pls, can you tell me which part of the code is new, which rows from your code i have to put in the file and at which row's number?
roman

jimjimmiejames

  • Beginner
  • *
  • Posts: 18
Re: showing all parent-level categories in menu
« Reply #3 on: May 11, 2009, 20:53:12 pm »
Doesn't work here unfortunately!

jagerfly

  • Beginner
  • *
  • Posts: 9
Re: showing all parent-level categories in menu
« Reply #4 on: August 27, 2009, 22:41:03 pm »
With me it dont work, i have J1.0.14 and VM 1.1.3