programatically add category tree outside magento with jquery tree view 1 comment


programatically add category tree outside magento with jquery tree view

<?php
/**
 * Pragnesh Karia
 * This source file open source made by pragnesh karia
 * contact me : pragnesh.karia@gmail.com
 * Thanks to :
 * http://bassistance.de/jquery-plugins/jquery-plugin-treeview/
 * http://www.mdgart.com/2009/03/15/persistent-drag-and-drop-tree-with-jquery-php-and-mysql-part-1/
 * http://www.mdgart.com/jQuery/Simple-Persistent-Tree/initial_test.php
 *
 * @category   category tree view outside magento (tested v 1.5.1.0)
 * @package    Pragnesh Karia
 * @copyright  Copyright (c) 2011 http://pragneshkaria.com
 * @license
 */
require_once $_SERVER['DOCUMENT_ROOT'] . "/app/Mage.php";
Mage::app('1');
function get_categories() {
    $category = Mage::getModel('catalog/category');
    $tree = $category->getTreeModel();
    $tree->load();
    $ids = $tree->getCollection()->getAllIds();
    $arr = array();
    $arrayCategories = array();
    if ($ids) {
        foreach ($ids as $id) {
            $cat = Mage::getModel('catalog/category');
            $cat->load($id);

          //  if($id!=1){

                $arrayCategories[$id] =
                        array("parent_id" => $cat->getParentId(),
                            "name" => $cat->getName(),
                            "cat_id" => $cat->getId(),
                            "cat_level" => $cat->getLevel(),
                            "cat_url" => $cat->getUrl()
                );
          //  }
        }// for each ends
        return $arrayCategories;
        /*
          echo 'PK<pre>';
          print_r($arrayCategories);
          echo '</pre>';
         */
    }//if ids present
}
$arrayCategories = get_categories();

function createTree($array, $currentParent, $currLevel = 0, $prevLevel = -1) {
    foreach ($array as $categoryId => $category) {
        if ($currentParent == $category['parent_id']) {
            if ($currLevel > $prevLevel)
                echo "<ul id='ul_".$categoryId."'>";
            if ($currLevel == $prevLevel)
                echo " </li> ";

                    $cat_id_hidden = array('1','2');//set category id hidden
                    if(in_array($categoryId,$cat_id_hidden)){
                       // $display = 'none';
                    }
                    else{
                       // $display = 'block';
                    }

            //class="level_'.$category['cat_level'].' "
            echo '<li id="li_'.$categoryId.'">';
            echo '<a id="a_'.$categoryId.'" target="_blank" href="'. $category['cat_url'].'">';
                echo '<span id="span_'.$categoryId.'">'.$category['name'].'</span>';
            echo '</a>';
            //echo '<input type="text" id="m_cat_id_'.$category['cat_id'].'" name="m_cat_id_'.$category['cat_id'].'" value="id = '.$category['cat_id'].' : level = '.$category['cat_level'].' "/>';
            if ($currLevel > $prevLevel) {
                $prevLevel = $currLevel;
            }
            $currLevel++;
            createTree($array, $categoryId, $currLevel, $prevLevel);
            $currLevel--;
        }
    }
    if ($currLevel == $prevLevel)
        echo " </li></ul> ";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Magento jQuery treeview FrontEnd</title>
        <link rel="stylesheet" href="treeview/jquery.treeview.css" />
        <link rel="stylesheet" href="treeview/red-treeview.css" />
        <link rel="stylesheet" href="treeview/screen.css" />
        <script src="treeview/lib/jquery.js" type="text/javascript"></script>
        <script src="treeview/lib/jquery.cookie.js" type="text/javascript"></script>
        <script src="treeview/jquery.treeview.js" type="text/javascript"></script>
        <script type="text/javascript">
            function expand_prags(str){
               // alert(str);
            }
            $(document).ready(function() {
            //to remove default behaviour on category id 1 and 2
            $("#span_1").html('');
            $("#span_1").html('All Categories');
            $("#span_2").html('');
            $("#span_2").html('Pragnesh Karia Root Category');

            $("#a_1").removeAttr("href");
            $("#a_1").removeAttr("target");
            $("#a_2").removeAttr("href");
            $("#a_2").removeAttr("target");

            $("#a_1").attr({'class':'expandable-hitarea','href':''});
            $("#a_2").attr({'class':'expandable-hitarea','href':''});

            });
            $(function() {
                $("#tree").treeview({
                    collapsed: true,
                    animated: "medium",
                    control:".sidetreecontrol",
                    persist: "location"
                });
            })
        </script>
    </head>
    <body>
        <div id="sidetree">
            <div class="treeheader">&nbsp;</div>
            <div class="sidetreecontrol"><a href="?#">Collapse All</a> | <a href="?#">Expand All</a></div>
            <div id="tree">
                <?php createTree($arrayCategories, 0); ?>
            </div>
        </div>
    </body>
</html>

will attaching full code very soon..

Share on Facebook




About Pragnesh Karia

Pragnesh Karia, Open Source Enthusiast, Software Professional, Software Developer, Technical Lead ,Magento, Joomla ,Joomla LMS , Moodel LMS ,PHP ,Mysql, Ajax, Javascript, Jquery, Linux, Fan of Open Sources , Annet Technologies , SEO Analyst , Mootools


One thought on “programatically add category tree outside magento with jquery tree view