Advertisement
dm8

bm_categories_accordion.php v1.0.4 20120518

dm8
May 18th, 2012
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.27 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.   $Id: bm_categories_accordion.php v1.0.4 20120518
  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 = '<div id="categoriesMenu">';
  86.     foreach ($this->menu as $node) {
  87.  
  88.       $return_string .= $this->makeTopMenu($node);
  89.       $return_string .= PHP_EOL . '<div class="menu_content">' . 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.             '</div><!-- #categoriesMenu -->' . PHP_EOL .
  105.             '  <script type="text/javascript">' . PHP_EOL .
  106.             '    $(function() {' . PHP_EOL .
  107.             '      $( "#categoriesMenu" ).accordion({' . PHP_EOL .
  108.             '        autoHeight: false,' . PHP_EOL .
  109.             '        icons: {' . PHP_EOL .
  110.             "          'header': 'ui-icon-" . MODULE_BOXES_CATEGORIES_ACCORDION_ICON . "'," . PHP_EOL .
  111.             "          'headerSelected': 'ui-icon-" . MODULE_BOXES_CATEGORIES_ACCORDION_ICON_SELECTED . "'" . PHP_EOL .
  112.             '        },' . PHP_EOL .
  113.             '        collapsible: true,' . PHP_EOL .
  114.             '        active: ' . $active . PHP_EOL .
  115.             '      });' . PHP_EOL .
  116.             '    });' . PHP_EOL .
  117.             '  </script>' . PHP_EOL;
  118.  
  119.     return $return_string;
  120.   }
  121.  
  122.   /**
  123.    * Constucts the top elements of the menu (top-most categories)
  124.    * @param type $node an instance of category_node
  125.    * @return type String
  126.    */
  127.   function makeTopMenu($node) {
  128.     return PHP_EOL . '<h3' .
  129.             ' class="ui-priority-primary"' . // Optional: this makes top menu elements be bold.
  130.             '>' .
  131.             '<a href="#">' .
  132.             $node->name .
  133.             '</a>' .
  134.             '</h3>' .
  135.             PHP_EOL;
  136.   }
  137.  
  138.   /**
  139.    * Constructs the content of the menu.
  140.    * Recursively handles 'currently opened' categories, to show sub-categories with depth > 2
  141.    * @param type $parent an instance of category_node
  142.    * @return type String
  143.    */
  144.   function makeInnerMenu($parent) {
  145.     global $cPath_array;
  146.     $return_string = '';
  147.  
  148.     //reset($parent->children);
  149.     foreach ($parent->children as $node) {
  150.       //$indent = ($node->depth * 0.5 ) - 1;
  151.  
  152.       $return_string .= PHP_EOL .
  153.               '<div ' .
  154.               ' class="menu_' . $node->depth . '"' .
  155.               '>' .
  156.               PHP_EOL .
  157.               '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $node->cpath) . '">';
  158.  
  159.       if (isset($cPath_array) && in_array($node->id, $cPath_array)) {
  160.         $return_string .= '<strong>';
  161.       }
  162.  
  163.       // display category name
  164.       $return_string .= $node->name;
  165.  
  166.       if (isset($cPath_array) && in_array($node->id, $cPath_array)) {
  167.         $return_string .= '</strong>';
  168.       }
  169.       $return_string .= '</a>';
  170.  
  171.       if (SHOW_COUNTS == 'true') {
  172.         $products_in_category = tep_count_products_in_category($node->id);
  173.         if ($products_in_category > 0) {
  174.           $return_string .= ' <span class="nowrap">(' . $products_in_category . ')</span> ';
  175.         }
  176.       }
  177.  
  178.       if (!empty($node->children) || tep_has_category_subcategories($node->id)) {
  179.         $return_string .= ' <span class="nowrap">-&gt;</span> ';
  180.       }
  181.  
  182.       $return_string .= '</div>' . PHP_EOL;
  183.  
  184.       if (!empty($node->children)) {
  185.         $return_string .= $this->makeInnerMenu($node);
  186.       }
  187.     }
  188.  
  189.     return $return_string;
  190.   }
  191.  
  192.   /**
  193.    * Populates $this->menu and $this->allNodesById
  194.    * @global type $languages_id
  195.    * @global type $cPath
  196.    * @global type $cPath_array
  197.    * @return string
  198.    */
  199.   function getData() {
  200.     global $languages_id, $cPath, $cPath_array;
  201.  
  202.     //get the 'top' category elements.
  203.     $query =
  204.             "select
  205.           c.categories_id,
  206.           cd.categories_name
  207.         from
  208.           " . TABLE_CATEGORIES . " c
  209.           join " . TABLE_CATEGORIES_DESCRIPTION . " cd
  210.            on cd.categories_id = c.categories_id
  211.         where
  212.           c.parent_id = '0'
  213.           and cd.language_id='" . (int) $languages_id . "'
  214.         order by
  215.           sort_order,
  216.           cd.categories_name";
  217.     $res = tep_db_query($query);
  218.  
  219.     while ($row = tep_db_fetch_array($res)) {
  220.       $node = new category_node(
  221.                       $row['categories_id'], //id
  222.                       $row['categories_name'], // name
  223.                       $row['categories_id'], // cpath, of the top categories === the category id
  224.                       0, //parent
  225.                       0, //depth
  226.                       array() // no children
  227.       );
  228.  
  229.       $this->menu[] = $node;
  230.       $this->allNodesById[$row['categories_id']] = $node;
  231.     }
  232.  
  233.     //iterate over the top nodes and load their direct children (categories of depth 1)
  234.     reset($this->menu);
  235.     foreach ($this->menu as $parent_node) {
  236.       $parent_id = $parent_node->id;
  237.  
  238.       $this->addChildren($parent_id, $parent_id, 1);
  239.     }
  240.  
  241.     //handle cPath, accomodate the potential of this cPath being in a depth higher than 1
  242.     if (tep_not_null($cPath)) {
  243.       reset($cPath_array);
  244.       while (list($key, $parent_id) = each($cPath_array)) {
  245.         //skip if children already populated
  246.         if (!empty($this->allNodesById[$parent_id]->children)) {
  247.           continue;
  248.         }
  249.         //discover the depth
  250.         $depth = $key + 1;
  251.         $this->addChildren($parent_id, $this->allNodesById[$parent_id]->cpath, $depth);
  252.       }
  253.     }
  254.  
  255.     //construct the categories meny accordion.
  256.     return $this->makeAccordion();
  257.   }
  258.  
  259.   /**
  260.    * Adds children to parent nodes.
  261.    * @global type $languages_id
  262.    * @param type $parent_id
  263.    * @param type $parent_cpath
  264.    * @param type $depth
  265.    */
  266.   function addChildren($parent_id, $parent_cpath, $depth) {
  267.     global $languages_id;
  268.     $query =
  269.             "select
  270.              c.categories_id,
  271.              cd.categories_name
  272.            from
  273.              " . TABLE_CATEGORIES . " c
  274.              join " . TABLE_CATEGORIES_DESCRIPTION . " cd
  275.                on (c.categories_id = cd.categories_id)
  276.            where
  277.              c.parent_id = '" . (int) $parent_id . "'
  278.              and cd.language_id = '" . (int) $languages_id . "'
  279.            order by
  280.              sort_order,
  281.              cd.categories_name";
  282.     $res = tep_db_query($query);
  283.     while ($row = tep_db_fetch_array($res)) {
  284.       $child = new category_node(
  285.                       $row['categories_id'], //id
  286.                       $row['categories_name'], // name
  287.                       $parent_cpath . '_' . $row['categories_id'], // cpath, parentId_childId
  288.                       $parent_id, //parent
  289.                       $depth, //depth
  290.                       array() // no children
  291.       );
  292.       $this->allNodesById[$parent_id]->children[] = $child; // append this child to its parent
  293.       $this->allNodesById[$row['categories_id']] = $child; /// add it to the general pool
  294.     }
  295.   }
  296.  
  297.   function execute() {
  298.     global $SID, $oscTemplate;
  299.  
  300.     if ((USE_CACHE == 'true') && empty($SID)) {
  301.       $output = tep_cache_categories_accordion_box();
  302.     } else {
  303.       $output = $this->getData();
  304.     }
  305.  
  306.     $oscTemplate->addBlock($output, $this->group);
  307.   }
  308.  
  309.   function isEnabled() {
  310.     return $this->enabled;
  311.   }
  312.  
  313.   function check() {
  314.     return defined('MODULE_BOXES_CATEGORIES_ACCORDION_STATUS');
  315.   }
  316.  
  317.   function install() {
  318.     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())");
  319.     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())");
  320.     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())");
  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 ('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())");
  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 ('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())");
  323.   }
  324.  
  325.   function remove() {
  326.     tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
  327.   }
  328.  
  329.   function keys() {
  330.     return array(
  331.         'MODULE_BOXES_CATEGORIES_ACCORDION_STATUS',
  332.         'MODULE_BOXES_CATEGORIES_ACCORDION_CONTENT_PLACEMENT',
  333.         'MODULE_BOXES_CATEGORIES_ACCORDION_SORT_ORDER',
  334.         'MODULE_BOXES_CATEGORIES_ACCORDION_ICON_SELECTED',
  335.         'MODULE_BOXES_CATEGORIES_ACCORDION_ICON'
  336.     );
  337.   }
  338.  
  339. }
  340.  
  341. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement