Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- $Id: bm_categories_accordion.php v1.0.3 20120511
- $ModifiedFrom: bm_categories_accordion.php v1.0.2 20120331 Kymation $
- $Loc: catalog/includes/modules/boxes/ $
- osCommerce, Open Source E-Commerce Solutions
- http://www.oscommerce.com
- Copyright (c) 2012 osCommerce
- Released under the GNU General Public License
- */
- class category_node {
- public $id;
- public $name;
- public $cpath;
- public $parent;
- public $depth;
- public $children;
- function __construct($_id, $_name, $_cpath, $_parent = 0, $_depth = 0, $_children = array()) {
- $this->id = $_id;
- $this->name = $_name;
- $this->cpath = $_cpath;
- $this->parent = $_parent;
- $this->depth = $_depth;
- $this->children = $_children;
- }
- }
- class bm_categories_accordion {
- public $code = 'bm_categories_accordion';
- public $group = 'boxes';
- public $title;
- public $description;
- public $sort_order;
- public $enabled = false;
- /**
- * $menu Will hold the top-most category nodes, to be used with Queue logic (FIFO).
- * A perfectionist would have made this a linked list...
- * @var type array
- */
- public $menu = array();
- /**
- * $allNodesById Will hold all categories, indexed by category id.
- * @var type array
- */
- public $allNodesById = array();
- function bm_categories_accordion() {
- global $PHP_SELF;
- $this->title = MODULE_BOXES_CATEGORIES_ACCORDION_TITLE;
- $this->description = MODULE_BOXES_CATEGORIES_ACCORDION_DESCRIPTION;
- if (defined('MODULE_BOXES_CATEGORIES_ACCORDION_STATUS')) {
- $this->sort_order = MODULE_BOXES_CATEGORIES_ACCORDION_SORT_ORDER;
- $this->enabled = (MODULE_BOXES_CATEGORIES_ACCORDION_STATUS == 'True');
- $this->group = ((MODULE_BOXES_CATEGORIES_ACCORDION_CONTENT_PLACEMENT == 'Left Column')
- ? 'boxes_column_left'
- : 'boxes_column_right');
- }
- // Include the function that is used to add icons in the Admin
- if ($PHP_SELF == 'modules.php') {
- include_once( DIR_WS_FUNCTIONS . 'modules/boxes/icon_selector.php' );
- }
- }
- function makeAccordion() {
- global $cPath, $cPath_array;
- reset($this->menu);
- $active = 'false'; // used to specify which accordion node is active (see js), zeroindexed.
- $node_index = 0; // see $active
- $return_string = '';
- foreach ($this->menu as $node) {
- $return_string .= $this->makeTopMenu($node);
- $return_string .= PHP_EOL . '<div>' . PHP_EOL;
- $return_string .= $this->makeInnerMenu($node);
- $return_string .= PHP_EOL . '</div>' . PHP_EOL;
- //identify 'open' accordion position, if any.
- if (tep_not_null($cPath) && $cPath_array[0] == $node->id) {
- $active = $node_index;
- }
- //increase node index counter
- $node_index++;
- }
- //add the script to trigger the creation of the accordion.
- $return_string .= PHP_EOL .
- ' <script type="text/javascript">' . PHP_EOL .
- ' $(function() {' . PHP_EOL .
- ' $( "#categoriesMenu" ).accordion({' . PHP_EOL .
- ' autoHeight: false,' . PHP_EOL .
- ' icons: {' . PHP_EOL .
- " 'header': 'ui-icon-" . MODULE_BOXES_CATEGORIES_ACCORDION_ICON . "'," . PHP_EOL .
- " 'headerSelected': 'ui-icon-" . MODULE_BOXES_CATEGORIES_ACCORDION_ICON_SELECTED . "'" . PHP_EOL .
- ' },' . PHP_EOL .
- ' active: ' . $active . PHP_EOL .
- ' });' . PHP_EOL .
- ' });' . PHP_EOL .
- ' </script>' . PHP_EOL;
- return $return_string;
- }
- /**
- * Constucts the top elements of the menu (top-most categories)
- * @param type $node an instance of category_node
- * @return type String
- */
- function makeTopMenu($node) {
- return PHP_EOL . '<h3' .
- ' class="ui-priority-primary"' . // Optional: this makes top menu elements be bold.
- '>' .
- '<a href="#">' .
- $node->name .
- '</a>' .
- '</h3>' .
- PHP_EOL;
- }
- /**
- * Constructs the content of the menu.
- * Recursively handles 'currently opened' categories, to show sub-categories with depth > 2
- * @param type $parent an instance of category_node
- * @return type String
- */
- function makeInnerMenu($parent) {
- global $cPath, $cPath_array;
- $return_string = '';
- //reset($parent->children);
- foreach ($parent->children as $node) {
- $indent = ($node->depth * 0.5 ) - 1;
- $return_string .= PHP_EOL .
- '<div style="margin: 0 -15px 0 ' . $indent . 'em; border-top: 1px solid #cccccc; ' .
- ////NOTE!! modify as needed, default was: padding-top:5px; padding-bottom: 5px;>"
- ////TODO: move these off to a stylesheet, and use something like: class="category_' . $node->depth . '"'
- 'padding-top:0.2em; padding-bottom: 0.2em;">' .
- PHP_EOL .
- '<a href="' . tep_href_link(FILENAME_DEFAULT, 'cPath=' . $node->cpath) . '">';
- if (isset($cPath_array) && in_array($node->id, $cPath_array)) {
- $return_string .= '<strong>';
- }
- // display category name
- $return_string .= $node->name;
- if (isset($cPath_array) && in_array($node->id, $cPath_array)) {
- $return_string .= '</strong>';
- }
- $return_string .= '</a>';
- if (SHOW_COUNTS == 'true') {
- $products_in_category = tep_count_products_in_category($node->id);
- if ($products_in_category > 0) {
- $return_string .= ' <span class="nowrap">(' . $products_in_category . ')</span> ';
- }
- }
- if (!empty($node->children) || tep_has_category_subcategories($node->id)) {
- $return_string .= ' <span class="nowrap">-></span> ';
- }
- $return_string .= '</div>' . PHP_EOL;
- if (!empty($node->children)) {
- $return_string .= $this->makeInnerMenu($node);
- }
- }
- return $return_string;
- }
- /**
- * Populates $this->menu and $this->allNodesById
- * @global type $languages_id
- * @global type $cPath
- * @global type $cPath_array
- * @return string
- */
- function getData() {
- global $languages_id, $cPath, $cPath_array;
- //get the 'top' category elements.
- $query =
- "select
- c.categories_id,
- cd.categories_name
- from
- " . TABLE_CATEGORIES . " c
- join " . TABLE_CATEGORIES_DESCRIPTION . " cd
- on cd.categories_id = c.categories_id
- where
- c.parent_id = '0'
- and cd.language_id='" . (int) $languages_id . "'
- order by
- sort_order,
- cd.categories_name";
- $res = tep_db_query($query);
- while ($row = tep_db_fetch_array($res)) {
- $node = new category_node(
- $row['categories_id'], //id
- $row['categories_name'], // name
- $row['categories_id'], // cpath, of the top categories === the category id
- 0, //parent
- 0, //depth
- array() // no children
- );
- $this->menu[] = $node;
- $this->allNodesById[$row['categories_id']] = $node;
- }
- //iterate over the top nodes and load their direct children (categories of depth 1)
- reset($this->menu);
- foreach ($this->menu as $parent_node) {
- $parent_id = $parent_node->id;
- $this->addChildren($parent_id, $parent_id, 1);
- }
- //handle cPath, accomodate the potential of this cPath being in a depth higher than 1
- if (tep_not_null($cPath)) {
- reset($cPath_array);
- while (list($key, $parent_id) = each($cPath_array)) {
- //skip if children already populated
- if (!empty($this->allNodesById[$parent_id]->children)) {
- continue;
- }
- //discover the depth
- $depth = $key + 1;
- $this->addChildren($parent_id, $this->allNodesById[$parent_id]->cpath, $depth);
- }
- }
- //construct the div of the categories meny accordion here.
- $data = '<div id="categoriesMenu">' . PHP_EOL;
- $data .= $this->makeAccordion() . PHP_EOL;
- $data .= '</div>' . PHP_EOL;
- return $data;
- }
- /**
- * Adds children to parent nodes.
- * @global type $languages_id
- * @param type $parent_id
- * @param type $parent_cpath
- * @param type $depth
- */
- function addChildren($parent_id, $parent_cpath, $depth) {
- global $languages_id;
- $query =
- "select
- c.categories_id,
- cd.categories_name
- from
- " . TABLE_CATEGORIES . " c
- join " . TABLE_CATEGORIES_DESCRIPTION . " cd
- on (c.categories_id = cd.categories_id)
- where
- c.parent_id = '" . (int) $parent_id . "'
- and cd.language_id = '" . (int) $languages_id . "'
- order by
- sort_order,
- cd.categories_name";
- $res = tep_db_query($query);
- while ($row = tep_db_fetch_array($res)) {
- $child = new category_node(
- $row['categories_id'], //id
- $row['categories_name'], // name
- $parent_cpath . '_' . $row['categories_id'], // cpath, parentId_childId
- $parent_id, //parent
- $depth, //depth
- array() // no children
- );
- $this->allNodesById[$parent_id]->children[] = $child; // append this child to its parent
- $this->allNodesById[$row['categories_id']] = $child; /// add it to the general pool
- }
- }
- function execute() {
- global $SID, $oscTemplate;
- if ((USE_CACHE == 'true') && empty($SID)) {
- $output = tep_cache_categories_accordion_box();
- } else {
- $output = $this->getData();
- }
- $oscTemplate->addBlock($output, $this->group);
- }
- function isEnabled() {
- return $this->enabled;
- }
- function check() {
- return defined('MODULE_BOXES_CATEGORIES_ACCORDION_STATUS');
- }
- function install() {
- 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())");
- 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())");
- 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())");
- 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())");
- 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())");
- }
- function remove() {
- tep_db_query("delete from " . TABLE_CONFIGURATION . " where configuration_key in ('" . implode("', '", $this->keys()) . "')");
- }
- function keys() {
- return array(
- 'MODULE_BOXES_CATEGORIES_ACCORDION_STATUS',
- 'MODULE_BOXES_CATEGORIES_ACCORDION_CONTENT_PLACEMENT',
- 'MODULE_BOXES_CATEGORIES_ACCORDION_SORT_ORDER',
- 'MODULE_BOXES_CATEGORIES_ACCORDION_ICON_SELECTED',
- 'MODULE_BOXES_CATEGORIES_ACCORDION_ICON'
- );
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement