The sc_appmenu_add_item macro dynamically adds items to the menu application initialized by the sc_appmenu_create macro.
It is important to note that to create a dynamic menu using macros, the application must not contain any items created in the Menu Items interface.
Syntax// Initializes the dynamic menu. This call is required before using sc_appmenu_add_item
sc_appmenu_create("menu_app_name");
// Adds an item to the already initialized dynamic menu
sc_appmenu_add_item(
"menu_app_name",
"id_item",
"id_item_parent",
"label",
"application",
"parameters",
"icon",
"hint",
"target",
"megamenu"
);
Parameters
Parameter |
Required |
Accepts Empty |
Description and example |
menu_app_name |
Yes |
No |
Receives the name of the menu application where the items will be created. Accepts fixed text in double quotes, local variable, or global variable.
Example: • "menu" • $local_variable • [global_var] |
id_item |
Yes |
No |
Defines the ID of the created item. This parameter accepts fixed text in double quotes or variable.
Example: • "item_01" • "item_" . $array[0] |
id_item_parent |
Yes |
Yes |
Should be informed only if the item is a subitem. In this case, use the ID of the parent item.
Example: • "item_01" • "item_" . $array[0] |
label |
Yes |
No |
Receives the name of the item that will be displayed in the application. The value can be provided as fixed text, variable, or lang.
Example: • "Item name" • $itemName • {lang_item_name} |
application_name |
Conditional¹ |
Yes |
Defines the application to be opened when the item is clicked. Can be left blank if the item contains subitems.
¹ Can be left blank if the item is a group (parent item), without a click action to open an application. |
parameters |
No |
Yes |
String of parameters that will be passed when the item is triggered. These parameters must be retrieved as global variables.
Example: • param1="value";param2=$variable;param3={field} |
hint |
No |
Yes |
Text that will be displayed when hovering over the item. Can be fixed text, variable, or lang.
Example: • "Item name" • $itemName • {lang_item_name} |
icon |
No |
Yes |
Defines the Font Awesome icon (or image) for the menu item. The value must be enclosed in double quotes.
Example: • "fa fa-user" |
target |
No |
Yes |
Defines how the link will be opened.
Example: • "_self" – Same window • "_blank" – New tab • "_parent" – Opens in the parent window context |
megamenu |
No |
Yes |
Defines whether the item will be displayed as a mega menu. To function correctly: – The item must be a top-level menu (parent), i.e., id_item_parent must be blank. – The item must contain at least two levels of subitems.
Example: • "S" – Item will be shown as a mega menu • "" – Item will be shown as a standard menu |
Examples
1. Static menu with direct values sc_appmenu_create("menu");
// Main fixed item
sc_appmenu_add_item("menu", "item_0", "", "Home", "app_home", "", "fa fa-home", "Home page", "_self", "");
// Group: Records
sc_appmenu_add_item("menu", "item_1", "", "Records", "", "", "fa fa-folder", "", "", "");
sc_appmenu_add_item("menu", "item_2", "item_1", "Clients", "app_clients", "", "fa fa-users", "", "_self", "");
sc_appmenu_add_item("menu", "item_3", "item_1", "Suppliers", "app_suppliers", "", "fa fa-truck", "", "_self", "");
sc_appmenu_add_item("menu", "item_4", "item_1", "Products", "app_products", "", "fa fa-box", "", "_self", "");
// Group: Reports
sc_appmenu_add_item("menu", "item_5", "", "Reports", "", "", "fa fa-file", "", "", "");
sc_appmenu_add_item("menu", "item_6", "item_5", "Billing", "app_rel_billing", "", "fa fa-chart-line", "", "_self", "");
sc_appmenu_add_item("menu", "item_7", "item_5", "Stock", "app_rel_stock", "", "fa fa-warehouse", "", "_self", "");
sc_appmenu_add_item("menu", "item_8", "item_5", "Clients", "app_rel_clients", "", "fa fa-address-book", "", "_self", "");
2. Using array and foreach sc_appmenu_create("menu");
// Home
sc_appmenu_add_item("menu", "item_0", "", "Home", "app_home", "", "fa fa-home", "Home page", "_self", "");
$groups = array(
array(
"id" => "item_1",
"label" => "Records",
"icon" => "fa fa-folder",
"items" => array(
array("label" => "Clients", "app" => "app_clients", "icon" => "fa fa-users"),
array("label" => "Suppliers", "app" => "app_suppliers", "icon" => "fa fa-truck"),
array("label" => "Products", "app" => "app_products", "icon" => "fa fa-box")
)
),
array(
"id" => "item_5",
"label" => "Reports",
"icon" => "fa fa-file",
"items" => array(
array("label" => "Billing", "app" => "app_rel_billing", "icon" => "fa fa-chart-line"),
array("label" => "Stock", "app" => "app_rel_stock", "icon" => "fa fa-warehouse"),
array("label" => "Clients", "app" => "app_rel_clients", "icon" => "fa fa-address-book")
)
)
);
$id_counter = 2;
foreach ($groups as $group) {
sc_appmenu_add_item("menu", $group["id"], "", $group["label"], "", "", $group["icon"], "", "", "");
foreach ($group["items"] as $item) {
$id = "item_" . $id_counter++;
sc_appmenu_add_item("menu", $id, $group["id"], $item["label"], $item["app"], "", $item["icon"], "", "_self", "");
}
}
3. Using foreach with database lookup sc_appmenu_create("menu");
// Home
sc_appmenu_add_item("menu", "item_0", "", "Home", "app_home", "", "fa fa-home", "Home page", "_self", "");
// Query with JOIN
sc_lookup(menu_data, "
SELECT
g.id AS group_id,
g.name AS group_name,
i.id AS item_id,
i.name AS item_name,
i.app AS app,
i.icon AS icon
FROM groups g
JOIN items i ON i.group_id = g.id
WHERE g.active = 'Y' AND i.active = 'Y'
ORDER BY g.id, i.id
");
$created_groups = array();
if (isset({menu_data[0][0]})) {
foreach ({menu_data} as $row) {
$group_id = "item_" . $row[0];
$group_name = $row[1];
$item_id = "item_" . $row[2];
$item_name = $row[3];
$item_app = $row[4];
$item_icon = $row[5];
if (!in_array($group_id, $created_groups)) {
sc_appmenu_add_item("menu", $group_id, "", $group_name, "", "", "fa fa-folder", "", "", "");
$created_groups[] = $group_id;
}
sc_appmenu_add_item("menu", $item_id, $group_id, $item_name, $item_app, "", $item_icon, "", "_self", "");
}
}
4. Mega menu example sc_appmenu_create("menu");
// Mega menu root
sc_appmenu_add_item("menu", "item_0", "", "Management", "", "", "fa fa-sitemap", "", "", "S");
// Mega menu groups
$groups = array(
array(
"id" => "item_1",
"label" => "Records",
"items" => array("Clients", "Products", "Users")
),
array(
"id" => "item_2",
"label" => "Finance",
"items" => array("Accounts Payable", "Revenue", "Billing")
),
array(
"id" => "item_3",
"label" => "Reports",
"items" => array("Sales", "Stock", "Clients")
)
);
$id_counter = 4;
foreach ($groups as $group) {
sc_appmenu_add_item("menu", $group["id"], "item_0", $group["label"], "", "", "", "", "", "");
foreach ($group["items"] as $label) {
$id = "item_" . $id_counter++;
$app = "app_" . strtolower(str_replace(" ", "_", $label));
sc_appmenu_add_item("menu", $id, $group["id"], $label, $app, "", "", "", "_self", "");
}
}
|