Skip to content

Commit

Permalink
Separate category tree display, data, and structure
Browse files Browse the repository at this point in the history
The current category tree class mixes several things.  It creates a
tree structure to hold the category data.  It loads and exposes the
data.  And it has specific displays of the data.

This separates into multiple classes.  A generic tree structure with
tree operations.  A category tree implementation of that, which loads
and exposes the data.  A display class.  And a controller class for
the display.

This also simplifies accesses of the tree structure.  Both because
some could be simpler and through the addition of a parent_of data
structure.  This saves iterating to find a category in the tree.
We can simply look up the location of any category.  And we can
find its ancestors through iteration of a simple lookup.

The separation of data and display requires modification of uses of
the category tree.  In particular, the categories box.
  • Loading branch information
ecartz committed Dec 28, 2020
1 parent 6a31474 commit ed7a5c8
Show file tree
Hide file tree
Showing 6 changed files with 406 additions and 370 deletions.
82 changes: 32 additions & 50 deletions includes/modules/boxes/bm_categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,48 @@
Released under the GNU General Public License
*/

class bm_categories {
var $code = 'bm_categories';
var $group = 'boxes';
var $title;
var $description;
var $sort_order;
var $enabled = false;
class bm_categories extends abstract_block_module {

function __construct() {
$this->title = MODULE_BOXES_CATEGORIES_TITLE;
$this->description = MODULE_BOXES_CATEGORIES_DESCRIPTION;
const CONFIG_KEY_BASE = 'MODULE_BOXES_CATEGORIES_';

if ( defined('MODULE_BOXES_CATEGORIES_STATUS') ) {
$this->sort_order = MODULE_BOXES_CATEGORIES_SORT_ORDER;
$this->enabled = (MODULE_BOXES_CATEGORIES_STATUS == 'True');
function execute() {
$display = new tree_display(Guarantor::ensure_global('category_tree'));

$this->group = ((MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT == 'Left Column') ? 'boxes_column_left' : 'boxes_column_right');
}
}
$display->setPath($GLOBALS['cPath'], '<strong>', '</strong>');
$display->setMaximumLevel((int)MODULE_BOXES_CATEGORIES_MAX_LEVEL);

function execute() {
global $oscTemplate, $cPath;

$max_nesting_level = (int)MODULE_BOXES_CATEGORIES_MAX_LEVEL;
$display->setChildString('', '');

$OSCOM_CategoryTree = new category_tree();
$OSCOM_CategoryTree->setCategoryPath($cPath, '<strong>', '</strong>');
$OSCOM_CategoryTree->setMaximumLevel($max_nesting_level);

$OSCOM_CategoryTree->setParentGroupString('<div class="list-group list-group-flush">', '</div>', false);
$OSCOM_CategoryTree->setChildString('', '');
$OSCOM_CategoryTree->setSpacerString('<i class="fas fa-angle-right ml-2 mr-1 text-muted"></i>', 1);

$category_tree = $OSCOM_CategoryTree->getTree();

$tpl_data = ['group' => $this->group, 'file' => __FILE__];
include 'includes/modules/block_template.php';
}

function isEnabled() {
return $this->enabled;
}

function check() {
return defined('MODULE_BOXES_CATEGORIES_STATUS');
protected function get_parameters() {
return [
'MODULE_BOXES_CATEGORIES_STATUS' => [
'title' => 'Enable Categories Module',
'value' => 'True',
'desc' => 'Do you want to add the module to your shop?',
'set_func' => "tep_cfg_select_option(['True', 'False'], ",
],
'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT' => [
'title' => 'Content Placement',
'value' => 'Left Column',
'desc' => 'Should the module be loaded in the left or right column?',
'set_func' => "tep_cfg_select_option(['Left Column', 'Right Column'], ",
],
'MODULE_BOXES_CATEGORIES_MAX_LEVEL' => [
'title' => 'Maximum Level of Nesting',
'value' => '1',
'desc' => 'If you increase this number, subcategories will show in the module output.',
],
'MODULE_BOXES_CATEGORIES_SORT_ORDER' => [
'title' => 'Sort Order',
'value' => '0',
'desc' => 'Sort order of display. Lowest is displayed first.',
],
];
}

function install() {
tep_db_query("insert into 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_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 configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Content Placement', 'MODULE_BOXES_CATEGORIES_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 configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Maximum Level of Nesting', 'MODULE_BOXES_CATEGORIES_MAX_LEVEL', '1', 'If you increase this number, subcategories will show in the module output.', '6', '3', now())");
tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_BOXES_CATEGORIES_SORT_ORDER', '0', 'Sort order of display. Lowest is displayed first.', '6', '4', now())");
}

function remove() {
tep_db_query("delete from configuration where configuration_key in ('" . implode("', '", $this->keys()) . "')");
}

function keys() {
return array('MODULE_BOXES_CATEGORIES_STATUS', 'MODULE_BOXES_CATEGORIES_CONTENT_PLACEMENT', 'MODULE_BOXES_CATEGORIES_MAX_LEVEL', 'MODULE_BOXES_CATEGORIES_SORT_ORDER');
}
}

34 changes: 16 additions & 18 deletions includes/modules/boxes/templates/tpl_bm_categories.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
<div class="card mb-2 bm-categories">
<div class="card-header">
<?php echo MODULE_BOXES_CATEGORIES_BOX_TITLE; ?>
</div>
<div class="list-group list-group-flush">
<?php echo $category_tree; ?>
</div>
</div>

<?php
/*
Copyright (c) 2018, G Burton
All rights reserved.
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
$Id$
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
osCommerce, Open Source E-Commerce Solutions
http://www.oscommerce.com
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
Copyright (c) 2020 osCommerce
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Released under the GNU General Public License
*/

$display->setParentGroupString('<div class="list-group list-group-flush">', '</div>', false);
$display->setSpacerString('<i class="fas fa-angle-right ml-2 mr-1 text-muted"></i>', 1);
?>
<div class="card mb-2 bm-categories">
<div class="card-header">
<?= MODULE_BOXES_CATEGORIES_BOX_TITLE ?>
</div>
<div class="list-group list-group-flush">
<?= $display ?>
</div>
</div>
Loading

0 comments on commit ed7a5c8

Please sign in to comment.