<?php
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
/**
* phpShop dTree menu
* @license GNU/GPL License : http://www.gnu.org/copyleft/gpl.html
* @author dTree mos menu created by Winfred van Kuijk <winfred@vankuijk.net>
* @author Soeren Eberhardt
* @ Uses dTree Javascript: http://www.destroydrop.com/javascripts/tree/
* @version $Id: product.folders.php,v 1.4 2005/01/27 19:34:03 soeren_nb Exp $
*
* CATEGORY TREE
*/
mm_showMyFileName( __FILE__ );

    /* dTree API, default value
    * change to fit your needs **/
    $useSelection =  'true';
    $useLines =  'true';
    $useCookies = 'false';
    $useIcons =  'true';
    $useStatusText =  'false';
    $closeSameLevel =  'false';
    $root_label = "";
    // if all folders should be open, we will ignore the closeSameLevel
    $openAll =  'false';
    if ( $openAll == "true" ) { $closeSameLevel = "false"; }
    
    
    $menu_htmlcode = "";
    
        // what should be used as the base of the tree?
        // ( could be *first* menu item, *site* name, *module*, *menu* name or *text* )
        $base = "first";
        
        
        // in case *text* should be the base node, what text should be displayed?
        $basetext =  "";
        
        $category_id = 0;
        
        // select menu items from database
        $query  = "SELECT category_id,category_name,category_parent_id FROM #__pshop_category, #__pshop_category_xref ";
        $query .= "WHERE #__pshop_category.category_publish='Y' AND ";
        $query .= "#__pshop_category.category_id=#__pshop_category_xref.category_child_id ";
        $query .= "ORDER BY category_parent_id, list_order, category_name ASC";

        $database->setQuery( $query );
        $res = $database->query();
        // how many menu items in this menu?
        $row = $database->getNumRows($res);
        if ($row) {
          if ($row == 0) { continue; }
        }
        
        $rows = $database->loadObjectList();
        echo $database->getErrorMsg();
        
        $first_row = @$rows[0];
        
        // create a unique tree identifier, in case multiple dtrees are used 
        // (max one per module)
        $tree = "d".time()."_".$first_row->category_id;
        
        
        // start creating the content
        // create left aligned table, load the CSS stylesheet and dTree code
        $menu_htmlcode .= "<br /><table border=\"0\" align=\"center\" cellspacing=\"1\" cellpadding=\"0\" width=\"90%\"><tr><td align=\"left\">\n";
        $menu_htmlcode .= "<link rel=\"stylesheet\" href=\"$mosConfig_live_site/modules/dtree/dtree.css\" type=\"text/css\" />\n";
        $menu_htmlcode .= "<script type=\"text/javascript\" src=\"$mosConfig_live_site/modules/dtree/dtree.js\"></script>\n";
        $menu_htmlcode .= "<script type=\"text/javascript\">\n";
        
        // create the tree, using the unique name
        // pass the live_site parameter on so dTree can find the icons
        $menu_htmlcode .= "$tree = new dTree('$tree',\"$mosConfig_live_site\");\n";
        
        // pass on the dTree API parameters
        $menu_htmlcode .= "$tree.config.useSelection=".$useSelection.";\n";
        $menu_htmlcode .= "$tree.config.useLines=".$useLines.";\n";
        $menu_htmlcode .= "$tree.config.useIcons=".$useIcons.";\n";
        $menu_htmlcode .= "$tree.config.useCookies=".$useCookies.";\n";
        $menu_htmlcode .= "$tree.config.useStatusText=".$useStatusText.";\n";
        $menu_htmlcode .= "$tree.config.closeSameLevel=".$closeSameLevel.";\n";
        
        $basename = $root_label;
        
        // what is the ID of this node?
        $baseid = $first_row->category_parent_id;
        // create the link (if not a menu item, no link [could be: to entry page of site])
        $baselink = ( $base == "first") ? $_SERVER['PHP_SELF']."?option=com_phpshop" : "";
        // remember which item is open, normally $Itemid
        // except when we want the first item (e.g. Home) to be the base;
        // in that case we have to pretend all remaining items belong to "Home"
        $openid = $category_id;
       
        // it could be that we are displaying e.g. mainmenu in this dtree, 
        // but item in usermenu is selected, 
        // so: for the rest of this module track if this menu contains the selected item
        // Default value: first node (=baseid), but not selected
        $opento = $baseid;
        $opento_selected = "false";
        // what do you know... the first node was selected
        if ($baseid == $openid) { $opento_selected = "true"; }
        $target = "";
        
        // create the first node, parent is always -1
        $menu_htmlcode .= "$tree.add(\"$baseid\",\"-1\",\"$basename\",\"$baselink\",\"\",\"$target\");\n";
        
        // process each of the nodes
        foreach ($rows as $row) {
        
            // get name and link (just to save space in the code later on)
            $name = $row->category_name;
            $url = $_SERVER['PHP_SELF'].'?option=com_phpshop&page=product.product_category_form&category_id='.$row->category_id;
            $menu_htmlcode .= "$tree.add(\"".$row->category_id."\",\"".$row->category_parent_id."\",\"$name\",\"$url\",\"\",\"$target\");\n";
            // if this node is the selected node
            if ($row->category_id == $openid) { 
                $opento = $openid; 
                $opento_selected = "true"; 
            }
            $q = "SELECT #__pshop_product.product_name,#__pshop_product.product_id FROM #__pshop_product, #__pshop_product_category_xref ";
            $q .= "WHERE #__pshop_product.product_id=#__pshop_product_category_xref.product_id ";
            $q .= "AND #__pshop_product_category_xref.category_id='".$row->category_id."' ";
            $q .= "ORDER BY #__pshop_product.product_name";
            $database->setQuery( $q );
            $products = $database->loadObjectList();
            foreach( $products as $product ) {
              // get name and link (just to save space in the code later on)
              $name = $product->product_name;
              $url = $_SERVER['PHP_SELF'].'?option=com_phpshop&page=product.product_form&product_id='.$product->product_id;
              $menu_htmlcode .= "$tree.add(\"".$product->product_id."\",\"".$row->category_id."\",\"$name\",\"$url\",\"\",\"$target\");\n";
            }

              
        }
        
        $menu_htmlcode .= "document.write($tree);\n";
        $menu_htmlcode .= $openAll == "true" ? "$tree.openAll();\n" : "$tree.closeAll();\n";
        $menu_htmlcode .= "$tree.openTo(\"$opento\",\"$opento_selected\");\n";
        $menu_htmlcode .= "</script>\n";
        $menu_htmlcode .= "</td></tr></table>\n";
        
        echo $menu_htmlcode;


/************* END OF CATEGORY TREE ******************************
*********************************************************
*/
?>
