Core Boxx – Dynamic Menu Module

This is a simple dynamic menu module for Core Boxx – If you want to dynamically create and update menus like a CMS. Simply plug this one into Core Boxx, and the foundations are done. Minus the HTML/CSS/Javascript, that is.

 

TABLE OF CONTENTS

Download & Notes Quick Reference

 

 

DOWNLOAD & NOTES

First, here are the download links and a quick “setup guide” for the impatient folks who don’t have the patience to read through everything.

 

DOWNLOAD LINKS

Download Core Boxx Menu Module | GitHub | Source Forge

 

LICENSE

Core Boxx is released under the MIT License. You are free to use it for your own personal and commercial projects, modify it as you see fit. On the condition that there the software is provided “as-is”. There are no warranties provided and “no strings attached”. Code Boxx and the authors are not liable for any claims, damages, or liabilities.

 

 

INSTALLATION & REQUIREMENTS

  • Download and set up the “main Core Boxx” if you have not already done so.
  • Then, create your database and import lib/SQL-menu.sql… Remember to delete this file afterward, that’s all.

 

MORE DEVELOPMENT NOTES

This module will work “as-it-is”. Just save your menu items in the database and use get() to retrieve them. But for better performance, it is recommended to:

  • Complete toHTML() in LIB-Menu.php – Generate a flat HTML file for the dynamic menu.
  • Change save() in LIB-Menu.php to run toHTML() at the end.
  • Include the generated HTML menu – It is much faster to load a flat HTML file than to generate from the database every time.
  • Protect the API calls in API-menu.php – Do a check if the users have permission to access.

 

QUICK REFERENCE

This section is a quick walkthrough of the general module structures.

 

MENU DATABASE TABLES

Menu
Field Description
menu_id Menu ID, primary key.
menu_name Name of the menu. E.G. Main menu, side-bar menu, footer menu, etc…
menu_desc A short description of the menu.
Menu Items
Field Description
menu_id Menu ID, foreign and primary key.
item_id Menu item ID, primary key. Take note – This is not auto-increment. You need to manually specify this, and also use this as the menu item sort order.
parent_id For nested menu items – The parent item ID. Leave as NULL if root level.
item_label The label of the menu item.
item_link Where the menu item links to.
item_target Optional. This should either be _BLANK or _SELF if you want to set.

 

 

MENU LIBRARY FUNCTIONS

save($name, $desc, $id)

Add a new menu, or update an existing menu.

  • $name String, the menu name.
  • $desc String, short menu description.
  • $id Int, pass in the menu ID if you want to update an existing menu.
echo $_CORE->Menu->save(
  "Main Menu", "Top navigation bar"
) ? "OK" : $_CORE->error ;
saveItems($id, $items)

Save/update menu items.

  • $id Int, the menu ID.
  • $items Array, the menu items.
echo $_CORE->Menu->saveItems(1, [
  // ITEM ID, PARENT ID, LABEL, LINK, TARGET
  // START WITH ROOT!
  [1, null, "Blog", "blog/", null],
  [2, null, "Products", "products/", null],
  [3, null, "Reviews", "reviews/", null],
  // SECOND LEVEL
  [4, 1, "Tech", "blog/tech/", "_SELF"],
  [5, 1, "Hacks", "blog/hacks/", "_BLANK"]
  // WE CAN GO DEEPER, BUT SHALL STOP AT 2 LEVELS
  // [6, 5, "Windows", "blog/hacks/windows", null]
  // [7, 5, "Mac", "blog/hacks/mac", null]
]) ? "OK" : $_CORE->error ;
del($id)

Delete an entire menu and items.

  • $id Int, the menu ID
echo $_CORE->Menu->del(1) ? "OK" : $_CORE->error ;
get($id)

Get the specified menu.

  • $id Int, the menu ID
$menu = $_CORE->Menu->get(1);
count($search)

Get the total number of menu entries.

  • $search String, optional search for menu name or description.
$entries = $_CORE->Menu->count("main");
getAll($search, $page)

Get all or search for menu.

  • $search String, optional search for menu name or description.
  • $page Int, optional current page number.
$menu = $_CORE->Menu->getAll("main", 3);
getItems($id)

Get all menu items for the specified menu.

  • $id Int, the menu ID
$items = $_CORE->Menu->get(1);
toHTML($id)

This is an incomplete support function for save() – Generates a static HTML file for the menu, to speed up the performance of your website. I.E. Directly load the menu HTML instead of generating from the database every time.

  • $id Int, the menu ID

 

 

MENU API FUNCTIONS

Accessible at http://yoursite.com/api/menu/REQUEST/. These are pretty much a replica of the above library functions, except in REST API format. Feel free to delete api/API-menu.php if you don’t intend to integrate an API.

api/menu/save

Add or update the menu.

  • $_POST['name'] – String, menu name.
  • $_POST['desc'] – String, menu description
  • $_POST['id'] – Int, optional menu ID. Pass in to update instead of add.
api/menu/saveItems

Save menu items.

  • $_POST['id'] – Int, menu ID.
  • $_POST['items'] – JSON encoded string, menu items. The format is the same as above in the function.
api/menu/del

Delete a menu.

  • $_POST['id'] – Int, menu ID.
api/menu/get

Get a menu.

  • $_POST['id'] Int, menu ID.
api/menu/getAll

Get or search for all menus.

  • $_POST['search'] – String, menu name or description.
api/menu/getItems

Get all items for the specified menu.

  • $_POST['id'] Int, menu ID.

Leave a Comment

Your email address will not be published. Required fields are marked *