Advertisement
dm8

bm_categories_accordion.php v1.0.3 20120511

dm8
May 11th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. /*
  4.   $Id: bm_categories_accordion.php v1.0.3 20120511
  5.   $ModifiedFrom: bm_categories_accordion.php v1.0.2 20120331 Kymation $
  6.   $Loc: catalog/includes/modules/boxes/ $
  7.  
  8.   osCommerce, Open Source E-Commerce Solutions
  9.   http://www.oscommerce.com
  10.  
  11.   Copyright (c) 2012 osCommerce
  12.  
  13.   Released under the GNU General Public License
  14.  */
  15.  
  16. class category_node {
  17.  
  18.   public $id;
  19.   public $name;
  20.   public $cpath;
  21.   public $parent;
  22.   public $depth;
  23.   public $children;
  24.  
  25.   function __construct($_id, $_name, $_cpath, $_parent = 0, $_depth = 0, $_children = array()) {
  26.     $this->id = $_id;
  27.     $this->name = $_name;
  28.     $this->cpath = $_cpath;
  29.     $this->parent = $_parent;
  30.     $this->depth = $_depth;
  31.     $this->children = $_children;
  32.   }
  33.  
  34. }
  35.  
  36. class bm_categories_accordion {
  37.  
  38.   public $code = 'bm_categories_accordion';
  39.   public $group = 'boxes';
  40.   public $title;
  41.   public $description;
  42.   public $sort_order;
  43.   public $enabled = false;
  44.  
  45.   /**
  46.    * $menu Will hold the top-most category nodes, to be used with Queue logic (FIFO).
  47.    * A perfectionist would have made this a linked list...
  48.    * @var type array
  49.    */
  50.   public $menu = array();
  51.  
  52.   /**
  53.    * $allNodesById Will hold all categories, indexed by category id.
  54.    * @var type array
  55.    */
  56.   public $allNodesById = array();
  57.  
  58.   function bm_categories_accordion() {
  59.     global $PHP_SELF;
  60.  
  61.     $this->title = MODULE_BOXES_CATEGORIES_ACCORDION_TITLE;
  62.     $this->description = MODULE_BOXES_CATEGORIES_ACCORDION_DESCRIPTION;
  63.  
  64.     if (defined('MODULE_BOXES_CATEGORIES_ACCORDION_STATUS')) {
  65.       $this->sort_order = MODULE_BOXES_CATEGORIES_ACCORDION_SORT_ORDER;
  66.       $this->enabled = (MODULE_BOXES_CATEGORIES_ACCORDION_STATUS == 'True');
  67.  
  68.       $this->group = ((MODULE_BOXES_CATEGORIES_ACCORDION_CONTENT_PLACEMENT == 'Left Column')
  69.                       ? 'boxes_column_left'
  70.                       : 'boxes_column_right');
  71.     }
  72.  
  73.     // Include the function that is used to add icons in the Admin
  74.     if ($PHP_SELF == 'modules.php') {
  75.       include_once( DIR_WS_FUNCTIONS . 'modules/boxes/icon_selector.php' );
  76.     }
  77.   }
  78.  
  79.   function makeAccordion() {
  80.     global $cPath, $cPath_array;
  81.  
  82.     reset($this->menu);
  83.     $active = 'false'; // used to specify which accordion node is active (see js), zeroindexed.
  84.     $node_index = 0; // see $active
  85.     $return_string = '';
  86.     foreach ($this->menu as $node) {
  87.  
  88.       $return_string .= $this->makeTopMenu($node);
  89.       $return_string .= PHP_EOL . '<div>' . PHP_EOL;
  90.       $return_string .= $this->makeInnerMenu($node);
  91.       $return_string .= PHP_EOL . '</div>' . PHP_EOL;
  92.  
  93.       //identify 'open' accordion position, if any.
  94.       if (tep_not_null($cPath) && $cPath_array[0] == $node->id) {
  95.         $active = $node_index;
  96.       }
  97.  
  98.       //increase node index counter
  99.       $node_index++;
  100.     }
  101.  
  102.     //add the script to trigger the creation of the accordion.
  103.     $return_string .= PHP_EOL .
  104.             '  <script type="text/javascript">' . PHP_EOL .
  105.             '    $(function() {' . PHP_EOL .
  106.             '      $( "#categoriesMenu" ).accordion({' . PHP_EOL .
  107.             '        autoHeight: false,' . PHP_EOL .
  108.             '        icons: {' . PHP_EOL .
  109.             "          'header': 'ui-icon-" . MODULE_BOXES_CATEGORIES_ACCORDION_ICON . "'," . PHP_EOL .
  110.             "          'headerSelected': 'ui-icon-" . MODULE_BOXES_CATEGORIES_ACCORDION_ICON_SELECTED . "'" . PHP_EOL .
  111.             '        },' . PHP_EOL .
  112.             '        active: ' . $active . PHP_EOL .
  113.             '      });' . PHP_EOL .
  114.             '    });' . PHP_EOL .
  115.             '  </script>' . PHP_EOL;
  116.  
  117.     return $return_string;
  118.   }
  119.  
  120.   /**
  121.    * Constucts the top elements of the menu (top-most categories)
  122.    * @param type $node an instance of category_node
  123.    * @return type String
  124.    */
  125.   function makeTopMenu($node) {
  126.     return PHP_EOL . '<h3' .
  127.             ' class="ui-priority-primary"' . // Optional: this makes top menu elements be bold.
  128.             '>' .
  129.             '<a href="#">' .
  130.             $node->name .
  131.             '</a>' .
  132.             '</h3>' .
  133.             PHP_EOL;
  134.   }
  135.  
  136.   /**
  137.    * Constructs the content of the menu.
  138.    * Recursively handles 'currently opened' categories, to show sub-categories with depth > 2
  139.    * @param type $parent an instance of category_node
  140.    * @return type String
  141.    */
  142.   function makeInnerMenu($parent) {
  143.     global $cPath, $cPath_array;
  144.     $return_string = '';
  145.  
  146.     //reset($parent->children);
  147.     foreach ($parent->children as $node) {
  148.       $indent = ($node->depth * 0.5 ) - 1;
  149.  
  150.       $return_string .= PHP_EOL .
  151.               '<div style="margin: 0 -15px 0 ' . $indent . 'em; border-top: 1px solid #cccccc; ' .
  152.               ////NOTE!! modify as needed, default was: padding-top:5px; padding-bottom: 5px;>"
  153.               ////TODO: move these off to a stylesheet, and use something like: class="category_' . $node->depth . '"'
  154.               'padding-top:0.2em; padding-bottom: 0.2em;">' .
  155.               PHP_EOL .
  156.               '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $node->cpath) . '">';
  157.  
  158.       if (isset($cPath_array) && in_array($node->id, $cPath_array)) {
  159.         $return_string .= '<strong>';
  160.       }
  161.  
  162.       // display category name
  163.       $return_string .= $node->name;
  164.  
  165.       if (isset($cPath_array) && in_array($node->id, $cPath_array)) {
  166.         $return_string .= '</strong>';
  167.       }
  168.       $return_string .= '</a>';
  169.  
  170.       if (SHOW_COUNTS == 'true') {
  171.         $products_in_category = tep_count_products_in_category($node->id);
  172.         if ($products_in_category > 0) {
  173.           $return_string .= ' <span class="nowrap">(' . $products_in_category . ')</span> ';
  174.         }
  175.       }
  176.  
  177.       if (!empty($node->children) || tep_has_category_subcategories($node->id)) {
  178.         $return_string .= ' <span class="nowrap">-&gt;</span> ';
  179.       }
  180.  
  181.       $return_string .= '</div>' . PHP_EOL;
  182.  
  183.       if (!empty($node->children)) {
  184.         $return_string .= $this->makeInnerMenu($node);
  185.       }
  186.     }
  187.  
  188.     return $return_string;
  189.   }
  190.  
  191.   /**
  192.    * Populates $this->menu and $this->allNodesById
  193.    * @global type $languages_id
  194.    * @global type $cPath
  195.    * @global type $cPath_array
  196.    * @return string
  197.    */
  198.   function getData() {
  199.     global $languages_id, $cPath, $cPath_array;
  200.  
  201.     //get the 'top' category elements.
  202.     $query =
  203.             "select
  204.           c.categories_id,
  205.           cd.categories_name
  206.         from
  207.           " . TABLE_CATEGORIES . " c
  208.           join " . TABLE_CATEGORIES_DESCRIPTION . " cd
  209.            on cd.categories_id = c.categories_id
  210.         where
  211.           c.parent_id = '0'
  212.           and cd.language_id='" . (int) $languages_id . "'
  213.         order by
  214.           sort_order,
  215.           cd.categories_name";
  216.     $res = tep_db_query($query);
  217.  
  218.     while ($row = tep_db_fetch_array($res)) {
  219.       $node = new category_node(
  220.                       $row['categories_id'], //id
  221.                       $row['categories_name'], // name
  222.                       $row['categories_id'], // cpath, of the top categories === the category id
  223.                       0, //parent
  224.                       0, //depth
  225.                       array() // no children
  226.       );
  227.  
  228.       $this->menu[] = $node;
  229.       $this->allNodesById[$row['categories_id']] = $node;
  230.     }
  231.  
  232.     //iterate over the top nodes and load their direct children (categories of depth 1)
  233.     reset($this->menu);
  234.     foreach ($this->menu as $parent_node) {
  235.       $parent_id = $parent_node->id;
  236.  
  237.       $this->addChildren($parent_id, $parent_id, 1);
  238.     }
  239.  
  240.     //handle cPath, accomodate the potential of this cPath being in a depth higher than 1
  241.     if (tep_not_null($cPath)) {
  242.       reset($cPath_array);
  243.       while (list($key, $parent_id) = each($cPath_array)) {
  244.         //skip if children already populated
  245.         if (!empty($this->allNodesById[$parent_id]->children)) {
  246.           continue;
  247.         }
  248.         //discover the depth
  249.         $depth = $key + 1;
  250.         $this->addChildren($parent_id, $this->allNodesById[$parent_id]->cpath, $depth);
  251.       }
  252.     }
  253.  
  254.     //construct the div of the categories meny accordion here.
  255.     $data = '<div id="categoriesMenu">' . PHP_EOL;
  256.     $data .= $this->makeAccordion() . PHP_EOL;
  257.     $data .= '</div>' . PHP_EOL;
  258.  
  259.     return $data;
  260.   }
  261.  
  262.   /**
  263.    * Adds children to parent nodes.
  264.    * @global type $languages_id
  265.    * @param type $parent_id
  266.    * @param type $parent_cpath
  267.    * @param type $depth
  268.    */
  269.   function addChildren($parent_id, $parent_cpath, $depth) {
  270.     global $languages_id;
  271.     $query =
  272.             "select
  273.              c.categories_id,
  274.              cd.categories_name
  275.            from
  276.              " . TABLE_CATEGORIES . " c
  277.              join " . TABLE_CATEGORIES_DESCRIPTION . " cd
  278.                on (c.categories_id = cd.categories_id)
  279.            where
  280.              c.parent_id = '" . (int) $parent_id . "'
  281.              and cd.language_id = '" . (int) $languages_id . "'
  282.            order by
  283.              sort_order,
  284.              cd.categories_name";
  285.     $res = tep_db_query($query);
  286.     while ($row = tep_db_fetch_array($res)) {
  287.       $child = new category_node(
  288.                       $row['categories_id'], //id
  289.                       $row['categories_name'], // name
  290.                       $parent_cpath . '_' . $row['categories_id'], // cpath, parentId_childId
  291.                       $parent_id, //parent
  292.                       $depth, //depth
  293.                       array() // no children
  294.       );
  295.       $this->allNodesById[$parent_id]->children[] = $child; // append this child to its parent
  296.       $this->allNodesById[$row['categories_id']] = $child; /// add it to the general pool
  297.     }
  298.   }
  299.  
  300.   function execute() {
  301.     global $SID, $oscTemplate;
  302.  
  303.     if ((USE_CACHE == 'true') && empty($SID)) {
  304.       $output = tep_cache_categories_accordion_box();
  305.     } else {
  306.       $output = $this->getData();
  307.     }
  308.  
  309.     $oscTemplate->addBlock($output, $this->group);
  310.   }
  311.  
  312.   function isEnabled() {
  313.     return $this->enabled;
  314.   }
  315.  
  316.   function check() {
  317.     return defined('MODULE_BOXES_CATEGORIES_ACCORDION_STATUS');
  318.   }
  319.  
  320.   function install() {
  321.     tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Categories Module', 'MODULE_BOXES_CATEGORIES_ACCORDION_STATUS', 'True', 'Do you want to add the module to your shop?', '6', '1', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  322.     tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_CATEGORIES_ACCORDION_CONTENT_PLACEMENT', 'Left Column', 'Should the module be loaded in the left or right column?', '6', '2', 'tep_cfg_select_option(array(\'Left Column\', \'Right Column\'), ', now())");
  323.     tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_CATEGORIES_ACCORDION_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '3', now())");
  324.     tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Selected Icon', 'MODULE_BOXES_CATEGORIES_ACCORDION_ICON_SELECTED', 'minus', 'Select the icon to use for the selected tab.', '6', '5', 'tep_cfg_pull_down_icon(', now())");
  325.     tep_db_query("insert into " . TABLE_CONFIGURATION . " (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Unselected Icon', 'MODULE_BOXES_CATEGORIES_ACCORDION_ICON', 'plus', 'Select the icon to use for the unselected tabs.', '6', '4', 'tep_cfg_pull_down_icon(', now())");
  326.   }
  327.  
  328.   function remove() {
  329.     tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
  330.   }
  331.  
  332.   function keys() {
  333.     return array(
  334.         'MODULE_BOXES_CATEGORIES_ACCORDION_STATUS',
  335.         'MODULE_BOXES_CATEGORIES_ACCORDION_CONTENT_PLACEMENT',
  336.         'MODULE_BOXES_CATEGORIES_ACCORDION_SORT_ORDER',
  337.         'MODULE_BOXES_CATEGORIES_ACCORDION_ICON_SELECTED',
  338.         'MODULE_BOXES_CATEGORIES_ACCORDION_ICON'
  339.     );
  340.   }
  341.  
  342. }
  343.  
  344. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement