Why do I need to save a menu in database ?
In my work, I made a business web site with cake-php 3.9.X , Postgresql database and Bootstrap.
The goal of this tool is to display reports (generated automatically in pdf with Businness Object).Every report has a specific name.If click on an item of my menu, I need severeal paremeters in order to display the correct report like name, date, enable or disable...
Here is an example of a menu :
I've met a problem when I wanted to add a submenu, I need to write to much codes :
<?php
$ssdomain='INFORMATION';
if($selectedSSDomain == $ssdomain) {
$ssdomainLabel = "<span class='navbar-custom'>CONTACT</span>";
} else {
$ssdomainLabel = "CONTACT";
}
echo "<form method='post' action='index.php' id='form_contact'>";
echo "<input type='hidden' name='domain' value='Information'/>";
echo "<input type='hidden' name='ssdomain' value='CONTACT'/>";
echo "<input type='hidden' name='startWithReport' value='key_1'/>";
if (isset($_POST['chooseReportDate'])) {
$tmpDate = $_POST['chooseReportDate'];
echo "<input type='hidden' name='chooseReportDate' value=\"$tmpDate\"/>";
}
if(isset($_POST['select_org']) ) {
$code = $_POST['select_org'];
echo "<input type='hidden' name='select_org' value=\"$code\"/>";
}
echo "<a class='dropdown-item' onclick=\"document.getElementById('form_contact').submit(); return false;\" >$ssdomainLabel</a>";
echo "</form>";
In my business, it happens quite often (every 6 month) and I need to remember all parameters !That's why I decided to save my menu in database. Now, it's really easy to add a new menu !
Table Menu
First, we need to add all your menu information in a table of our database as text, position, enable...
Here is the Menu table :
CREATE TABLE yuzu.menu (
id int primary key ,
id_parent int,
shortkey varchar(50) NOT NULL,
name varchar(50) NOT NULL,
position int,
grey bool
);
- id : menu id
- id_parent : To indicate that the menu is a submenu
- shortkey : Use to identify the choosen menu
- position : We need to order menu.
HomeController
In my controller, I retreived the Menu table and stored it in a session.
Display auto generated menu
I wrote some code to display the menu automatically. To achieved that, I defined one method : function displayMenu($menus){}
Here is the code of two methods :
function getSortedMenu($menus, $currentIdParent) {
$mainMenus = array();
// Sort Menu
foreach ($menus as $arr) {
$submenus = $arr[0];
$idParent = $submenus['id_parent'];
if ($idParent == $currentIdParent) {
$position = $submenus['position'];
$mainMenus[$position]=$submenus;
}
}
ksort($mainMenus);
return $mainMenus;
}
function displayMenu($menus) {
$mainMenus = getSortedMenu($menus, 0);
foreach($mainMenus as $levelOnes) {
echo "<li class=\"nav-item dropdown\">";
$shortkey = "dropdown_".$levelOnes['shortkey'];
echo "<a class=\"nav-link dropdown-toggle text-menu\" id='$shortkey' data-toggle=\"dropdown\" href='#'>".$levelOnes['name']."</a>";
echo "<div class='dropdown-menu' aria-labelledby='$shortkey'>";
foreach ($menus as $arr) {
$menu = $arr[0];
if($menu['id_parent']==$levelOnes['id']) {
$shortkey = "form_".$menu['shortkey'];
echo "<form method='post' action='/home' id='".$shortkey."'>";
$disabled="";
if ($menu['grey']) {
$disabled='disabled';
}
echo "<input type='hidden' name='menu_selected_id' value='".$menu['id']."'/>";
echo "<a class='dropdown-item $disabled text-menu' href='#'>" . $menu['name'] . "</a>";
echo "</form>";
}
}
echo "</div>";
echo "</li>";
}
}
?>
This code used bootstrap and will certainly be improved.I think it's often a good way to auto-generated things. I hope it will give you some idea for your source code.
Notes :
- We have only two levels because I didn't need a level three of my menu.
- The next step is to manage authorization !
Versions :
Postgresql 9.6
Bootstrap 4.2.1