Forums / All Other Contributions/Addons / Simple Category Tree

Simple Category Tree

Views: 97,118

Results 21 to 40 of 249
11 Feb 2008, 10:50 AM
#21
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Simple Category Tree

I named it differently on my site.

Anyway, add .bak to the auto loader file so it is NOT loaded anymore, then let me know when you are done, I want to check if strict error report is really on

quentinjs:

die(..) worked.

I did have to change the file name in the auto loader as 'simple_categories_generator.php' was simple_categories_tree_generator.php

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

11 Feb 2008, 10:57 AM
#23
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

something is not right here, error report is suppressed for some reason, otherwise you see tons of notices on the site.

What is your cart version? your php version?

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

11 Feb 2008, 11:15 PM
#27
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

A number of minor fixes, now all notices and warnings should be gone:

<?php
/**
 * Simple Category Tree
 * @Version: Beta 1
 * @Authour: yellow1912
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 */ 

define ('SCT_REBUILD_TREE','true');
class simple_categories_generator{
    var $category_tree = array();
    var $parent_open_tag = '';
    var $parent_close_tag = '';
    var $child_open_tag = '';
    var $child_close_tag = '';
    function simple_categories_generator() {}
    function init(){
        if(SCT_REBUILD_TREE != 'false' || count($this->category_tree) == 0){
            global $languages_id, $db;
            $categories_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 c.categories_status=1
                          and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                          order by c.parent_id, c.sort_order, cd.categories_name";
            $categories = $db->Execute($categories_query);

            // reset the tree first
            $this->category_tree = array(); 
            while (!$categories->EOF) {
                $this->category_tree[$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'],
                'parent_id' => $categories->fields['parent_id']);
                $this->category_tree[$categories->fields['categories_id']]['path'][] = $categories->fields['categories_id'];
                $this->category_tree[$categories->fields['parent_id']]['sub_cats'][] = $categories->fields['categories_id'];
                $categories->MoveNext();
            }
            
            // walk through the array and build sub/cPath and other addtional info needed
            foreach($this->category_tree as $key => $value){
                // add sub 'class' for print-out purpose
                $this->category_tree[$key]['sub'] = isset($this->category_tree[$key]['sub_cats']) ? 'has_sub' : 'no_sub';
                // only merge if parent cat is not 0
                if(isset($this->category_tree[$key]['parent_id']) && $this->category_tree[$key]['parent_id'] > 0){
                    if(is_array($this->category_tree[$this->category_tree[$key]['parent_id']]['path']) && count($this->category_tree[$this->category_tree[$key]['parent_id']]['path'])> 0)
                        $this->category_tree[$key]['path'] = array_merge($this->category_tree[$this->category_tree[$key]['parent_id']]['path'],$this->category_tree[$key]['path']);
                }
                $this->category_tree[$key]['cPath'] = isset($this->category_tree[$key]['path']) ? implode('_',$this->category_tree[$key]['path']) : $key;
    
            }
            // for debugging using super global mod
            // $_POST['category_tree'] = $this->category_tree;
        }
    }
    // 9 is a ridiculous level already. If you go deeper than that, you have some problem with performance + structure
    // Max level should be around 3
    function build_category_string($parent_tag = 'div', $child_tag = 'span', $divider = '', $categories_id = 0, $max_level = 9, $include_root = false){
        if(!is_int($categories_id)){
            $temp = explode('_',$categories_id);
            $categories_id = $temp(count($temp)-1);
        }
        
        $result = '';
        // don't check if max_level = 0, since we assume store owners are not crazy enough to do that
        // --> less check = faster
        if(isset($this->category_tree[$categories_id])){
            // 
            $sub = $this->category_tree[$categories_id]['sub'];
            $level = 0;
            $class = "level_$level $sub";
            $this->_build_tags($parent_tag, $child_tag);
            // check if we should include the root or only its branches
            if($include_root && $categories_id > 0){
                $result = sprintf($this->parent_open_tag, $class).sprintf($this->child_open_tag, $class);
                $result .= $this->_build_category_string($categories_id, $level, $max_level, $divider);
                $result .= $this->child_close_tag.$this->parent_close_tag;
            }
            else{
                $result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
            }
            
        }
        return $result;
    }

    function _build_category_string($categories_id, $level, $max_level, $divider){        
        $result = $this->build_category_link($this->category_tree[$categories_id]['name'],$this->category_tree[$categories_id]['cPath']);
        $level++;
        $result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
        return $result;
    }

    function __build_category_string($categories_id, $level, $max_level, $divider){
        $result = '';
        if($level < $max_level){        
            $sub = $this->category_tree[$categories_id]['sub'];
            $class = "level_$level $sub";
            if(isset($this->category_tree[$categories_id]['sub_cats'])){
                    $result .= sprintf($this->parent_open_tag, $class);
                    $count = count($this->category_tree[$categories_id]['sub_cats']);
                    for($i=0; $i < $count; $i++){
                        $sub = $this->category_tree[$this->category_tree[$categories_id]['sub_cats'][$i]]['sub'];
                        $class = "level_$level $sub";
                        $result .= sprintf($this->child_open_tag, $class).$this->_build_category_string($this->category_tree[$categories_id]['sub_cats'][$i],$level,$max_level, $divider).$this->child_close_tag;
                        $result .= ($i < ($count-1)) ? $divider : '';
                    }
                    $result .= $this->parent_close_tag;
                }
        }
        return $result;
    }
    
    function _build_tags($parent_tag, $child_tag){
        if(!empty($parent_tag)){
            $this->parent_open_tag = "<$parent_tag class='%s'>";
            $this->parent_close_tag = "</$parent_tag>";        
        }
        else{
            $this->parent_open_tag = $this->parent_close_tag = '';        
        }
        if(!empty($child_tag)){
            $this->child_open_tag = "<$child_tag class='%s'>";
            $this->child_close_tag = "</$child_tag>";        
        }
        else{
            $this->child_open_tag = $this->child_close_tag = '';
        }
    }
    
    function build_category_link($categories_name, $cPath){
        return '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath) . '">'.$categories_name.'</a>';
    }
}
?>

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

11 Feb 2008, 11:18 PM
#28
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

12 Feb 2008, 2:01 AM
#29
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

There is a bug that would cause fatal error if you attempt to pass a cPath string (instead of number), fixed:

<?php
/**
 * Simple Category Tree
 * @Version: Beta 1
 * @Authour: yellow1912
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 */ 

define ('SCT_REBUILD_TREE','true');
class simple_categories_generator{
    var $category_tree = array();
    var $parent_open_tag = '';
    var $parent_close_tag = '';
    var $child_open_tag = '';
    var $child_close_tag = '';
    function simple_categories_generator() {}
    function init(){
        if(SCT_REBUILD_TREE != 'false' || count($this->category_tree) == 0){
            global $languages_id, $db;
            $categories_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 c.categories_status=1
                          and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                          order by c.parent_id, c.sort_order, cd.categories_name";
            $categories = $db->Execute($categories_query);

            // reset the tree first
            $this->category_tree = array(); 
            while (!$categories->EOF) {
                $this->category_tree[$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'],
                'parent_id' => $categories->fields['parent_id']);
                $this->category_tree[$categories->fields['categories_id']]['path'][] = $categories->fields['categories_id'];
                $this->category_tree[$categories->fields['parent_id']]['sub_cats'][] = $categories->fields['categories_id'];
                $categories->MoveNext();
            }
            
            // walk through the array and build sub/cPath and other addtional info needed
            foreach($this->category_tree as $key => $value){
                // add sub 'class' for print-out purpose
                $this->category_tree[$key]['sub'] = isset($this->category_tree[$key]['sub_cats']) ? 'has_sub' : 'no_sub';
                // only merge if parent cat is not 0
                if(isset($this->category_tree[$key]['parent_id']) && $this->category_tree[$key]['parent_id'] > 0){
                    if(is_array($this->category_tree[$this->category_tree[$key]['parent_id']]['path']) && count($this->category_tree[$this->category_tree[$key]['parent_id']]['path'])> 0)
                        $this->category_tree[$key]['path'] = array_merge($this->category_tree[$this->category_tree[$key]['parent_id']]['path'],$this->category_tree[$key]['path']);
                }
                $this->category_tree[$key]['cPath'] = isset($this->category_tree[$key]['path']) ? implode('_',$this->category_tree[$key]['path']) : $key;
    
            }
            // for debugging using super global mod
            // $_POST['category_tree'] = $this->category_tree;
        }
    }
    // 9 is a ridiculous level already. If you go deeper than that, you have some problem with performance + structure
    // Max level should be around 3
    function build_category_string($parent_tag = 'div', $child_tag = 'span', $divider = '', $categories_id = 0, $max_level = 9, $include_root = false){
        if(!is_int($categories_id)){
            $temp = explode('_',$categories_id);
            $categories_id = $temp[count($temp)-1];
        }
        
        $result = '';
        // don't check if max_level = 0, since we assume store owners are not crazy enough to do that
        // --> less check = faster
        if(isset($this->category_tree[$categories_id])){
            // 
            $sub = $this->category_tree[$categories_id]['sub'];
            $level = 0;
            $class = "level_$level $sub";
            $this->_build_tags($parent_tag, $child_tag);
            // check if we should include the root or only its branches
            if($include_root && $categories_id > 0){
                $result = sprintf($this->parent_open_tag, $class).sprintf($this->child_open_tag, $class);
                $result .= $this->_build_category_string($categories_id, $level, $max_level, $divider);
                $result .= $this->child_close_tag.$this->parent_close_tag;
            }
            else{
                $result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
            }
            
        }
        return $result;
    }

    function _build_category_string($categories_id, $level, $max_level, $divider){        
        $result = $this->build_category_link($this->category_tree[$categories_id]['name'],$this->category_tree[$categories_id]['cPath']);
        $level++;
        $result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
        return $result;
    }

    function __build_category_string($categories_id, $level, $max_level, $divider){
        $result = '';
        if($level < $max_level){        
            $sub = $this->category_tree[$categories_id]['sub'];
            $class = "level_$level $sub";
            if(isset($this->category_tree[$categories_id]['sub_cats'])){
                    $result .= sprintf($this->parent_open_tag, $class);
                    $count = count($this->category_tree[$categories_id]['sub_cats']);
                    for($i=0; $i < $count; $i++){
                        $sub = $this->category_tree[$this->category_tree[$categories_id]['sub_cats'][$i]]['sub'];
                        $class = "level_$level $sub";
                        $result .= sprintf($this->child_open_tag, $class).$this->_build_category_string($this->category_tree[$categories_id]['sub_cats'][$i],$level,$max_level, $divider).$this->child_close_tag;
                        $result .= ($i < ($count-1)) ? $divider : '';
                    }
                    $result .= $this->parent_close_tag;
                }
        }
        return $result;
    }
    
    function _build_tags($parent_tag, $child_tag){
        if(!empty($parent_tag)){
            $this->parent_open_tag = "<$parent_tag class='%s'>";
            $this->parent_close_tag = "</$parent_tag>";        
        }
        else{
            $this->parent_open_tag = $this->parent_close_tag = '';        
        }
        if(!empty($child_tag)){
            $this->child_open_tag = "<$child_tag class='%s'>";
            $this->child_close_tag = "</$child_tag>";        
        }
        else{
            $this->child_open_tag = $this->child_close_tag = '';
        }
    }
    
    function build_category_link($categories_name, $cPath){
        return '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath) . '">'.$categories_name.'</a>';
    }
}
?>

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

12 Feb 2008, 6:02 PM
#30
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

A new feature added in this version, it let you retrieve the deepest children of each and every category, this can come in very handy when you want to list all products belong to a category that has many sub-cats. This feature is in alpha version.

To use: after building the tree, you can call this function, it will be executed only once if you use session:

$_SESSION['category_tree']->build_deepest_level_children();

To get the array contains deepest cat of a certain cat id:

$category_tree = $_SESSION['category_tree']->retrieve_category_tree_array();
// assuming we want cat 1
$deepest_cats = $category_tree[1]['deepest_cats']; // this is an array containing deepest categories ids
<?php
/**
 * Simple Category Tree
 * @Version: Beta 1
 * @Authour: yellow1912
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 */ 

define ('SCT_REBUILD_TREE','false');
class simple_categories_generator{
	var $category_tree = array();
	var $is_deepest_cats_built = false;
	var $parent_open_tag = '';
	var $parent_close_tag = '';
	var $child_open_tag = '';
	var $child_close_tag = '';
	function simple_categories_generator() {}
	function init(){
		if(SCT_REBUILD_TREE != 'false' || count($this->category_tree) == 0){
			global $languages_id, $db;
			$categories_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 c.categories_status=1
						  and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
	                      order by c.parent_id, c.sort_order, cd.categories_name";
			$categories = $db->Execute($categories_query);

			// reset the tree first
			$this->category_tree = array(); 
			$this->is_deepest_cats_built = false;
			while (!$categories->EOF) {
				$this->category_tree[$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'],
				'parent_id' => $categories->fields['parent_id']);
				$this->category_tree[$categories->fields['categories_id']]['path'][] = $categories->fields['categories_id'];
				$this->category_tree[$categories->fields['parent_id']]['sub_cats'][] = $categories->fields['categories_id'];
				$categories->MoveNext();
			}
			
			// walk through the array and build sub/cPath and other addtional info needed
			foreach($this->category_tree as $key => $value){
				// add sub 'class' for print-out purpose
				$this->category_tree[$key]['sub'] = isset($this->category_tree[$key]['sub_cats']) ? 'has_sub' : 'no_sub';
				// only merge if parent cat is not 0
				if(isset($this->category_tree[$key]['parent_id']) && $this->category_tree[$key]['parent_id'] > 0){
					if(is_array($this->category_tree[$this->category_tree[$key]['parent_id']]['path']) && count($this->category_tree[$this->category_tree[$key]['parent_id']]['path'])> 0)
						$this->category_tree[$key]['path'] = array_merge($this->category_tree[$this->category_tree[$key]['parent_id']]['path'],$this->category_tree[$key]['path']);
				}
				$this->category_tree[$key]['cPath'] = isset($this->category_tree[$key]['path']) ? implode('_',$this->category_tree[$key]['path']) : $key;
	
			}
			// for debugging using super global mod
			// $_POST['category_tree'] = $this->category_tree;
		}
	}
	
	function retrieve_category_tree_array(){
		return $this->category_tree;
	}
	// 9 is a ridiculous level already. If you go deeper than that, you have some problem with performance + structure
	// Max level should be around 3
	function build_category_string($parent_tag = 'div', $child_tag = 'span', $divider = '', $categories_id = 0, $max_level = 9, $include_root = false){
		if(!is_int($categories_id)){
			$temp = explode('_',$categories_id);
			$categories_id = end($temp);
		}
		
		$result = '';
		// don't check if max_level = 0, since we assume store owners are not crazy enough to do that
		// --> less check = faster
		if(isset($this->category_tree[$categories_id])){
			// 
			$sub = $this->category_tree[$categories_id]['sub'];
			$level = 0;
			$class = "level_$level $sub";
			$this->_build_tags($parent_tag, $child_tag);
			// check if we should include the root or only its branches
			if($include_root && $categories_id > 0){
				$result = sprintf($this->parent_open_tag, $class).sprintf($this->child_open_tag, $class);
				$result .= $this->_build_category_string($categories_id, $level, $max_level, $divider);
				$result .= $this->child_close_tag.$this->parent_close_tag;
			}
			else{
				$result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
			}
			
		}
		return $result;
	}

	function _build_category_string($categories_id, $level, $max_level, $divider){		
		$result = $this->build_category_link($this->category_tree[$categories_id]['name'],$this->category_tree[$categories_id]['cPath']);
		$level++;
		$result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
		return $result;
	}

	function __build_category_string($categories_id, $level, $max_level, $divider){
		$result = '';
		if($level < $max_level){		
			$sub = $this->category_tree[$categories_id]['sub'];
			$class = "level_$level $sub";
			if(isset($this->category_tree[$categories_id]['sub_cats'])){
					$result .= sprintf($this->parent_open_tag, $class);
					$count = count($this->category_tree[$categories_id]['sub_cats']);
					for($i=0; $i < $count; $i++){
						$sub = $this->category_tree[$this->category_tree[$categories_id]['sub_cats'][$i]]['sub'];
						$class = "level_$level $sub";
						$result .= sprintf($this->child_open_tag, $class).$this->_build_category_string($this->category_tree[$categories_id]['sub_cats'][$i],$level,$max_level, $divider).$this->child_close_tag;
						$result .= ($i < ($count-1)) ? $divider : '';
					}
					$result .= $this->parent_close_tag;
				}
		}
		return $result;
	}
	
	function _build_tags($parent_tag, $child_tag){
		if(!empty($parent_tag)){
			$this->parent_open_tag = "<$parent_tag class='%s'>";
			$this->parent_close_tag = "</$parent_tag>";		
		}
		else{
			$this->parent_open_tag = $this->parent_close_tag = '';		
		}
		if(!empty($child_tag)){
			$this->child_open_tag = "<$child_tag class='%s'>";
			$this->child_close_tag = "</$child_tag>";		
		}
		else{
			$this->child_open_tag = $this->child_close_tag = '';
		}
	}
	
	function build_category_link($categories_name, $cPath){
		return '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath) . '">'.$categories_name.'</a>';
	}
	
	function build_deepest_level_children(){
		if(!$this->is_deepest_cats_built)
		$this->_build_deepest_level_children(0);
		// for debugging using super global mod
		// $_POST['category_tree'] = $this->category_tree;
	}
	
	function _build_deepest_level_children($categories_id){
		$parent_id = isset($this->category_tree[$categories_id]['parent_id']) ? $this->category_tree[$categories_id]['parent_id'] : -1;
		if(isset($this->category_tree[$categories_id]['sub_cats'])){
			foreach($this->category_tree[$categories_id]['sub_cats'] as $sub_cat){
					// we now need to loop thru these cats, and find if they have sub_cats
					$this->_build_deepest_level_children($sub_cat);
			}
		}
		elseif($parent_id > 0){
			$this->category_tree[$parent_id]['deepest_cats'][] = $categories_id;
		}
		
		if($parent_id > 0 && isset($this->category_tree[$categories_id]['deepest_cats']) 
						  && isset($this->category_tree[$parent_id]['deepest_cats'])){
			$this->category_tree[$parent_id]['deepest_cats'] = array_merge($this->category_tree[$parent_id]['deepest_cats'],$this->category_tree[$categories_id]['deepest_cats']);
		}
	}
}
?>

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

12 Feb 2008, 9:37 PM
#31
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

Bug fixes, added new function to retrieve the deepest cats easily:
function retrieve_deepest_cats_array (accept cid and cpath)

<?php
/**
 * Simple Category Tree
 * @Version: Beta 1
 * @Authour: yellow1912
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 */ 

define ('SCT_REBUILD_TREE','true');
class simple_categories_generator{
    var $category_tree = array();
    var $is_deepest_cats_built = false;
    var $parent_open_tag = '';
    var $parent_close_tag = '';
    var $child_open_tag = '';
    var $child_close_tag = '';
    function simple_categories_generator() {}
    function init(){
        if(SCT_REBUILD_TREE != 'false' || count($this->category_tree) == 0){
            global $languages_id, $db;
            $categories_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 c.categories_status=1
                          and cd.language_id = '" . (int)$_SESSION['languages_id'] . "'
                          order by c.parent_id, c.sort_order, cd.categories_name";
            $categories = $db->Execute($categories_query);

            // reset the tree first
            $this->category_tree = array(); 
            $this->is_deepest_cats_built = false;
            while (!$categories->EOF) {
                $this->category_tree[$categories->fields['categories_id']] = array('name' => $categories->fields['categories_name'],
                'parent_id' => $categories->fields['parent_id']);
                $this->category_tree[$categories->fields['categories_id']]['path'][] = $categories->fields['categories_id'];
                $this->category_tree[$categories->fields['parent_id']]['sub_cats'][] = $categories->fields['categories_id'];
                $categories->MoveNext();
            }
            
            // walk through the array and build sub/cPath and other addtional info needed
            foreach($this->category_tree as $key => $value){
                // add sub 'class' for print-out purpose
                $this->category_tree[$key]['sub'] = isset($this->category_tree[$key]['sub_cats']) ? 'has_sub' : 'no_sub';
                // only merge if parent cat is not 0
                if(isset($this->category_tree[$key]['parent_id']) && $this->category_tree[$key]['parent_id'] > 0){
                    if(is_array($this->category_tree[$this->category_tree[$key]['parent_id']]['path']) && count($this->category_tree[$this->category_tree[$key]['parent_id']]['path'])> 0)
                        $this->category_tree[$key]['path'] = array_merge($this->category_tree[$this->category_tree[$key]['parent_id']]['path'],$this->category_tree[$key]['path']);
                }
                $this->category_tree[$key]['cPath'] = isset($this->category_tree[$key]['path']) ? implode('_',$this->category_tree[$key]['path']) : $key;
    
            }
            // for debugging using super global mod
            // $_POST['category_tree'] = $this->category_tree;
        }
    }
    
    function retrieve_categories_tree_array(){
        return $this->category_tree;
    }
    
    function retrieve_deepest_cats_array($categories_id){
        $categories_id = $this->_get_categories_id($categories_id);
        return (isset($this->category_tree[$categories_id]['deepest_cats']) ? $this->category_tree[$categories_id]['deepest_cats'] : array());
    }
    // 9 is a ridiculous level already. If you go deeper than that, you have some problem with performance + structure
    // Max level should be around 3
    function build_category_string($parent_tag = 'div', $child_tag = 'span', $divider = '', $categories_id = 0, $max_level = 9, $include_root = false){
        $categories_id = $this->_get_categories_id($categories_id);        
        $result = '';
        // don't check if max_level = 0, since we assume store owners are not crazy enough to do that
        // --> less check = faster
        if(isset($this->category_tree[$categories_id])){
            // 
            $sub = $this->category_tree[$categories_id]['sub'];
            $level = 0;
            $class = "level_$level $sub";
            $this->_build_tags($parent_tag, $child_tag);
            // check if we should include the root or only its branches
            if($include_root && $categories_id > 0){
                $result = sprintf($this->parent_open_tag, $class).sprintf($this->child_open_tag, $class);
                $result .= $this->_build_category_string($categories_id, $level, $max_level, $divider);
                $result .= $this->child_close_tag.$this->parent_close_tag;
            }
            else{
                $result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
            }
            
        }
        return $result;
    }

    function build_deepest_level_children(){
        if(!$this->is_deepest_cats_built)
        $this->_build_deepest_level_children(0);
        // for debugging using super global mod
        // $_POST['category_tree'] = $this->category_tree;
    }
    
    // The functions below are internal and should not be called.
    function _build_category_string($categories_id, $level, $max_level, $divider){        
        $result = $this->_build_category_link($this->category_tree[$categories_id]['name'],$this->category_tree[$categories_id]['cPath']);
        $level++;
        $result .= $this->__build_category_string($categories_id, $level, $max_level, $divider);
        return $result;
    }

    function __build_category_string($categories_id, $level, $max_level, $divider){
        $result = '';
        if($level < $max_level){        
            $sub = $this->category_tree[$categories_id]['sub'];
            $class = "level_$level $sub";
            if(isset($this->category_tree[$categories_id]['sub_cats'])){
                    $result .= sprintf($this->parent_open_tag, $class);
                    $count = count($this->category_tree[$categories_id]['sub_cats']);
                    for($i=0; $i < $count; $i++){
                        $sub = $this->category_tree[$this->category_tree[$categories_id]['sub_cats'][$i]]['sub'];
                        $class = "level_$level $sub";
                        $result .= sprintf($this->child_open_tag, $class).$this->_build_category_string($this->category_tree[$categories_id]['sub_cats'][$i],$level,$max_level, $divider).$this->child_close_tag;
                        $result .= ($i < ($count-1)) ? $divider : '';
                    }
                    $result .= $this->parent_close_tag;
                }
        }
        return $result;
    }
    
    function _build_tags($parent_tag, $child_tag){
        if(!empty($parent_tag)){
            $this->parent_open_tag = "<$parent_tag class='%s'>";
            $this->parent_close_tag = "</$parent_tag>";        
        }
        else{
            $this->parent_open_tag = $this->parent_close_tag = '';        
        }
        if(!empty($child_tag)){
            $this->child_open_tag = "<$child_tag class='%s'>";
            $this->child_close_tag = "</$child_tag>";        
        }
        else{
            $this->child_open_tag = $this->child_close_tag = '';
        }
    }
    
    function _build_category_link($categories_name, $cPath){
        return '<a href="' . zen_href_link(FILENAME_DEFAULT, 'cPath=' . $cPath) . '">'.$categories_name.'</a>';
    }
    
    function _build_deepest_level_children($categories_id){
        $parent_id = isset($this->category_tree[$categories_id]['parent_id']) ? $this->category_tree[$categories_id]['parent_id'] : -1;
        if(isset($this->category_tree[$categories_id]['sub_cats'])){
            foreach($this->category_tree[$categories_id]['sub_cats'] as $sub_cat){
                    // we now need to loop thru these cats, and find if they have sub_cats
                    $this->_build_deepest_level_children($sub_cat);
            }
        }
        elseif($parent_id > 0){
            $this->category_tree[$parent_id]['deepest_cats'][] = $categories_id;
        }
        
        if($parent_id >= 0 && isset($this->category_tree[$categories_id]['deepest_cats'])){
            if(isset($this->category_tree[$parent_id]['deepest_cats']))
                $this->category_tree[$parent_id]['deepest_cats'] = array_merge($this->category_tree[$parent_id]['deepest_cats'],$this->category_tree[$categories_id]['deepest_cats']);
            else
                $this->category_tree[$parent_id]['deepest_cats'] = $this->category_tree[$categories_id]['deepest_cats'];
        }
    }
    
    function _get_categories_id($categories_id){
        if(!is_int($categories_id)){
            $temp = explode('_',$categories_id);
            $categories_id = end($temp);
        }
        return $categories_id;
    }
}
?>

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

14 Feb 2008, 7:37 AM
#32
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

Code now posted on svn, latest version can be found here:
https://svn.rubikintegration.com/zencart_mods/simple_categories_tree/

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

14 Feb 2008, 7:09 PM
#33
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

Critical bug fixed:
1 missing statement causes the class to append the tree forever if function build_deepest_level_children is used and SCT_REBUILD_TREE set to false.

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

15 Feb 2008, 1:17 AM
#34
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

New version updated:

-> Allow you to always display only first n level of cats, also allow you to go deeper into any branch
--> Add class 'current' to the currently selected cat (for styling purpose)

https://svn.rubikintegration.com/zencart_mods/simple_categories_tree/

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

15 Feb 2008, 2:10 AM
#35
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

Basic documentation added. This is it. Unless there is any bug found, Im not gonna work on this for a while.

GLuck with this module. I started this as something to use on my own site, then build several add-on features that may be useful for others.

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

15 Feb 2008, 10:30 PM
#36
quentinjs avatar

quentinjs

Zen Follower

Join Date:
Mar 2004
Location:
Calgary, Alberta
Posts:
289
Plugin Contributions:
0

Re: Simple Category Tree

I always notice that what one makes for ones self is never quite everyone else would like :-)

If I wanted to show the first 2 levels always, but drill down, what would need to be changed to this code which sits in the tpl_categories.php

  if(isset($_GET['cPath']))
     $content .= $_SESSION['category_tree']->build_category_string('ul', 'li', '', $_GET['cPath'], 2); 
  else
     $content .= $_SESSION['category_tree']->build_category_string('ul', 'li', '', 0, 2);
16 Feb 2008, 12:42 AM
#37
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

And that doesnt work for u? Link to that specific page/site where I can see it?

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

16 Feb 2008, 7:02 AM
#38
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

Since my site needs this function (again _), I added this:
The class is now also in the link
example:

<a 'class="something here" 'href="link here">category name</a>

Before you ask anything, read this:
https://svn.rubikintegration.com/zencart_mods/simple_categories_tree/docs/

New version can be found at:
https://svn.rubikintegration.com/zencart_mods/simple_categories_tree/

Demo:
http://demo.rubikintegration.com/zencart/

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me

16 Feb 2008, 8:32 AM
#39
yellow1912 avatar

yellow1912

Totally Zenned

Join Date:
Oct 2006
Posts:
5,422
Plugin Contributions:
0

Re: Simple Category Tree

Since simple category is an adapted version of css flyout, it should be able to fly as well:
http://demo.rubikintegration.com/zencart/

Certain changes needed to be made tho: the level starts from 0 instead of 1 (blah, my coding habit), and the class names are not the same.

Brief tut:
https://svn.rubikintegration.com/zencart_mods/simple_categories_tree/docs/sample_flyout_menucode.txt

Special thanks to the Jade True, creator of css flyout tree for the code.

I no longer provide installation support on forum for all my modules. However, if there are real bugs with the modules please feel free to contact me