The SB Menu class is a PHP class that allows for the creation, modification, saving, loading, and display of a menu tree without the need for a database. This class is designed to be simple, efficient, and easily customizable. It uses a flatfile approach, meaning that the menu structure is stored in a text file rather than a database.
'label' (user provided) and 'id' (generated) are default fields in the base class.
Class Methods:
save($filename) - saves the menu tree to a file
load($filename) - loads the menu tree from a file
getNodeByField($field, $value) - returns a reference to the node with a field that matches a value
applyCallbackToNode($id, $callback) - applies a callback to a node with a specific id
applyCallbackToNodeByField($field, $value, $callback) - applies a callback to a node with a specific field that matches a value
Examples:
class.sb_menu.test.directory.php // an example showing a directory and subdirectories
class.sb_menu.test.simple.php // a simple multilevel collapsible flexible and responsive accordion menu
EXAMPLE USAGE:
EXAMPLE USAGE:
EXAMPLE USAGE:
<?php
// include the class
include_once("class.sb_menu_1.0.php");
// example creation... (look at class.sb_menu.test.simple.php for extended class needed to render)
class SimpleTree_Extended extends Tree { ... extended code ( render(), renderNode(), etc ) ... }
// create extended class of Tree and throw in a render and renderNode to customize display based on dynamic data fields
// we pass the name we want for the main root node. In the example, we skip rendering this root node.
$menu = new SimpleTree_Extended(new TreeNode('Main Menu')); // .. or the extended class to seperate the render() method
// now add some menus...
// when menu is created, a root node is created and assigned to $menu->root. This is our main level.
$last_menu = $menu->root->addChild(new TreeNode('Submenu 1'));
$last_menu = $menu->root->addChild(new TreeNode('Submenu 2'));
// another level
$next_level = $last_menu->addChild(new TreeNode('Item 2.1'));
// example adding more data for our extended class to use with a custom render() method
$next_level->data['url'] = "https://www.google.com"; // handled in extended class render
$next_level->data['target'] = "_blank"; // handled in extended class render
// # use a callback to run some code on a specific node via its id (id and label are default fields in base class)
$menu->applyCallbackToNode($s2_id, function($node) {
$node->data['label'] = 'New Label';
$new_submenu = new TreeNode('New Submenu');
$node->addChild($new_submenu);
});
// example usage of applyCallbackToNodeByField($field, $value, $callback)
// (this example changes the label of the node with label 'Submenu 1' to 'New Label')
$menu->applyCallbackToNodeByField('label', 'Submenu 1', function($node) {
$node->data['label'] = 'New Label';
});
// get the reference to the node by a field for modification. (only returns first match)
// example usage of getNodeByField($key, $value)
$s2 = $menu->getNodeByField('label', 'Submenu 2');
$s2_id = $s2->data['id'];
# save (ez) (make sure directory or file is writeable)
$menu->save('menu-simple.txt');
# load (pz)
$menu2 = $menu->load('menu-simple.txt');
// render (display the menu :: see example Tree extended class in class.sb_menu.test.simple.php )
echo $menu->render('sbmenu'); // render defined in extended class
?>
|