WIP: Created new way to do the menus

This commit is contained in:
jose.gonzalez@pandorafms.com 2022-10-20 09:43:02 +02:00
parent 4c74f1bee7
commit f2ebd96cc8
6 changed files with 696 additions and 41 deletions

View File

@ -32,10 +32,11 @@ require_once 'include/functions_menu.php';
check_login();
$access_console_node = !is_reporting_console_node();
$access_console_node = is_reporting_console_node() === false;
$menu_godmode = [];
$menu_godmode['class'] = 'godmode';
$menuGodmode = [];
if ($access_console_node === true) {
enterprise_include('godmode/menu.php');
}

View File

@ -0,0 +1,126 @@
<?php
require_once 'include/class/MenuItem.class.php';
/**
* Global Menu class.
*/
class Menu
{
// Check ACL
/*
if ((bool) check_acl($config['id_user'], 0, '$item->ACL_el_que_sea') === true) {
haz cosas aqui
}
*/
public function __construct(
private string $name,
private array $items=[],
private array $menu=[]
) {
}
/**
* Create Item function.
*
* @param MenuItem $menuItem Item for print.
*
* @return array.
*/
public function generateItem(MenuItem $menuItem)
{
// Start with empty element.
$item = [];
if ($menuItem->canBeDisplayed() === true) {
// Handle of information.
if (empty($menuItem->getUrl()) === false) {
$urlPath = $menuItem->getUrl();
} else {
$urlPath = ui_get_full_url(
$menuItem->getSec().'/'.$menuItem->getSec2().'/'.$menuItem->getParameters()
);
}
// Creation of the line.
$item[] = '<li>';
// Create the link if is neccesary.
if (empty($urlPath) === false) {
$item[] = sprintf(
'<a href="%s">%s</a>',
$urlPath,
$menuItem->getText()
);
}
// Check if this item has submenu. If is the case, create it.
if ($menuItem->hasSubmenu() === true) {
$item[] = '<ul>';
$itemSubMenu = $menuItem->getSubmenu();
foreach ($itemSubMenu as $subMenu) {
$item[] = $this->generateItem($subMenu);
}
$item[] = '</ul>';
}
$item[] = '</li>';
}
return $item;
}
/**
* Generate the menu.
*
* @return void.
*/
public function generateMenu()
{
$output = [];
/*
Estructura
<ul>
<li>level0.item1</li>
<li>level0.item2</li>
<ul>
<li>level1.item1</li>
<li>level1.item2</li>
</ul>
<li>level0.item3</li>
</ul>
*/
$output[] = '<ul>';
foreach ($this->items as $menuItem) {
// If the item must be displayed.
$this->generateItem($menuItem);
}
$output[] = '</ul>';
$this->menu[] = $output;
}
/**
* Prints the menu.
*
* @return void.
*/
public function printMenu()
{
if (empty($this->menu) === false) {
foreach ($this->menu as $element) {
echo $element."\n";
}
}
}
}

View File

@ -0,0 +1,431 @@
<?php
/**
* Menu Item class.
*/
class MenuItem
{
/**
* Constructor
*
* @param string $text Caption for show.
* @param string $url Raw URL.
* @param string $sec URL sec.
* @param string $sec2 URL sec2.
* @param string $parameters Added parameters to builded url.
* @param string $id Unique identificator.
* @param string $icon Icon.
* @param string $class Class of his own.
* @param array $submenu Submenus.
* @param integer $refr Refr.
* @param array $acl ACLs affected.
* @param array $activationToken Config Token for active this item.
* @param boolean $display True if must be displayed.
* @param boolean $enabled True if must be Enabled.
*
* @return MenuItem.
*/
public function __construct(
private string $text='',
private string $url='',
private string $sec='',
private string $sec2='',
private string $parameters='',
private string $id='',
private string $icon='',
private string $class='',
private array $submenu=[],
private int $refr=0,
private array $acl=[],
private array $activationToken=[],
private bool $display=true,
private bool $enabled=true,
) {
if (empty($this->id) === true && empty($this->text) === false) {
$this->id = str_replace(' ', '_', $text);
}
return $this;
}
/**
* Set Text. The caption of the option.
*
* @param string $text Text.
*
* @return void.
*/
public function setText(string $text)
{
$this->text = $text;
}
/**
* Get Text
*
* @return string.
*/
public function getText()
{
return $this->text;
}
/**
* Set URL. Raw URL avoid sec and sec2 information.
*
* @param string $url Url.
*
* @return void.
*/
public function setUrl(string $url)
{
$this->url = $url;
}
/**
* Get URL
*
* @return string.
*/
public function getUrl()
{
return $this->url;
}
/**
* Set sec
*
* @param string $sec Sec.
*
* @return void.
*/
public function setSec(string $sec)
{
$this->sec = $sec;
}
/**
* Get sec
*
* @return string.
*/
public function getSec()
{
return $this->sec;
}
/**
* Set sec2
*
* @param string $sec2 Sec2.
*
* @return void.
*/
public function setSec2(string $sec2)
{
$this->sec2 = $sec2;
}
/**
* Get sec2
*
* @return string.
*/
public function getSec2()
{
return $this->sec2;
}
/**
* Set parameters. Added parameters for builded url (sec + sec2).
*
* @param string $parameters Parameters.
*
* @return void.
*/
public function setParameters(string $parameters)
{
$this->parameters = $parameters;
}
/**
* Get parameters
*
* @return string.
*/
public function getParameters()
{
return $this->parameters;
}
/**
* Set id. This is useful for identify the option selected.
*
* @param string $id Id.
*
* @return void.
*/
public function setId(string $id)
{
$this->id = $id;
}
/**
* Get id
*
* @return string.
*/
public function getId()
{
return $this->id;
}
/**
* Set icon. Must be relative path.
*
* @param string $icon Icon.
*
* @return void.
*/
public function setIcon(string $icon)
{
$this->icon = $icon;
}
/**
* Get icon
*
* @return string.
*/
public function getIcon()
{
return $this->icon;
}
/**
* Set class.
*
* @param string $class Class.
*
* @return void.
*/
public function setClass(string $class)
{
$this->class = $class;
}
/**
* Get class
*
* @return string.
*/
public function getClass()
{
return $this->class;
}
/**
* Set submenu. Array with options under this selection.
*
* @param array $submenu Submenu.
*
* @return void.
*/
public function setSubmenu(array $submenu)
{
$this->submenu = $submenu;
}
/**
* Get Submenu
*
* @return array.
*/
public function getSubmenu()
{
return $this->submenu;
}
/**
* Set ACLs. Attach only allowed ACLs in an array.
*
* @param array $acl ACL.
*
* @return void.
*/
public function setACL(array $acl)
{
$this->acl = $acl;
}
/**
* Get ACLs
*
* @return array.
*/
public function getACL()
{
return $this->acl;
}
/**
* Set activation token.
*
* @param array $activationToken ACL.
*
* @return void.
*/
public function setActivationToken(array $activationToken)
{
$this->activationToken = $activationToken;
}
/**
* Get activation token.
*
* @return array.
*/
public function getActivationToken()
{
return $this->activationToken;
}
/**
* Set refr
*
* @param integer $refr Refr.
*
* @return void.
*/
public function setRefr(int $refr)
{
$this->refr = $refr;
}
/**
* Get Refr
*
* @return integer.
*/
public function getRefr()
{
return $this->refr;
}
/**
* Set display. The item will be exists if this value is true.
*
* @param boolean $display Display.
*
* @return void.
*/
public function setDisplay(bool $display)
{
$this->display = $display;
}
/**
* Get Display
*
* @return boolean.
*/
public function getDisplay()
{
return $this->display;
}
/**
* Set enabled
*
* @param boolean $enabled Enabled.
*
* @return void.
*/
public function setEnabled(bool $enabled)
{
$this->enabled = $enabled;
}
/**
* Get enabled
*
* @return boolean.
*/
public function getEnabled()
{
return $this->enabled;
}
/**
* Returns true if this object have submenu.
*
* @return boolean.
*/
public function hasSubmenu()
{
return empty($this->getSubmenu()) === false;
}
/**
* Returns true if this object can be displayed.
*
* @return boolean.
*/
public function canBeDisplayed()
{
// Global config.
global $config;
$response = true;
if ($this->getDisplay() === false) {
// Display value is false.
$response = false;
} else if (empty($this->getACL()) === false) {
// Check all the ACLs.
$acls = $this->getACL();
foreach ($acls as $acl) {
$response = check_acl($config['id_user'], 0, $acl);
// In false case, end the check.
if ($response === false) {
break;
}
}
}
return $response;
}
}

View File

@ -53,12 +53,12 @@ function menu_print_menu(&$menu)
$sec = (string) get_parameter('sec');
$sec2 = (string) get_parameter('sec2');
if ($sec2 == 'operation/agentes/ver_agente') {
if ($sec2 === 'operation/agentes/ver_agente') {
$sec2 = 'godmode/agentes/configurar_agente';
} else if ($sec2 == 'godmode/servers/discovery') {
} else if ($sec2 === 'godmode/servers/discovery') {
$wiz = (string) get_parameter('wiz');
$sec2 = 'godmode/servers/discovery&wiz='.$wiz;
} else if ($sec2 == 'godmode/groups/group_list') {
} else if ($sec2 === 'godmode/groups/group_list') {
$tab = (string) get_parameter('tab');
if ($tab === 'credbox') {
$sec2 = 'godmode/groups/group_list&tab='.$tab;
@ -70,58 +70,39 @@ function menu_print_menu(&$menu)
$menu_selected = false;
$allsec2 = explode('sec2=', $_SERVER['REQUEST_URI']);
if (isset($allsec2[1])) {
if (isset($allsec2[1]) === true) {
$allsec2 = $allsec2[1];
} else {
$allsec2 = $sec2;
}
// Open list of menu.
echo '<ul'.(isset($menu['class']) ? ' class="'.$menu['class'].'"' : '').'>';
echo '<ul'.((isset($menu['class']) === true) ? ' class="'.$menu['class'].'"' : '').'>';
// Use $config because a global var is required because normal
// and godmode menu are painted separately.
if (!isset($config['count_main_menu'])) {
if (isset($config['count_main_menu']) === false) {
$config['count_main_menu'] = 0;
}
foreach ($menu as $mainsec => $main) {
$extensionInMenuParameter = (string) get_parameter('extension_in_menu', '');
$extensionInMenuParameter = (string) get_parameter('extension_in_menu');
$showSubsection = true;
if ($extensionInMenuParameter != '') {
if ($extensionInMenuParameter == $mainsec) {
$showSubsection = true;
} else {
$showSubsection = false;
}
if (empty($extensionInMenuParameter) === false) {
$showSubsection = ($extensionInMenuParameter === $mainsec);
}
if ($mainsec == 'class') {
if ($mainsec === 'class') {
continue;
}
// ~ if (enterprise_hook ('enterprise_acl', array ($config['id_user'], $mainsec)) == false)
// ~ continue;
if (! isset($main['id'])) {
$id = 'menu_'.(++$idcounter);
} else {
$id = $main['id'];
}
$id = (isset($main['id']) === false) ? 'menu_'.(++$idcounter) : $main['id'];
$submenu = false;
if ($menuTypeClass === 'classic') {
$classes = [
'menu_icon',
'no_hidden_menu',
];
} else {
$classes = [
'menu_icon',
'menu_icon_collapsed',
];
}
$classes = [
'menu_icon',
($menuTypeClass === 'classic') ? 'no_hidden_menu' : 'menu_icon_collapsed',
];
if (isset($main['sub']) === true) {
$classes[] = '';
@ -132,11 +113,11 @@ function menu_print_menu(&$menu)
$main['refr'] = 0;
}
if (($sec == $mainsec) && ($showSubsection)) {
if (($sec === $mainsec) && ((bool) $showSubsection === true)) {
$classes[] = '';
} else {
$classes[] = '';
if ($extensionInMenuParameter == $mainsec) {
if ($extensionInMenuParameter === $mainsec) {
$classes[] = '';
}
}

View File

@ -0,0 +1,6 @@
.operation li,
.godmode li {
display: flex;
justify-content: flex-start;
align-items: center;
}

View File

@ -19,12 +19,122 @@ use PandoraFMS\Dashboard\Manager;
require_once 'include/functions_menu.php';
require_once $config['homedir'].'/include/functions_visual_map.php';
require_once 'include/class/MenuItem.class.php';
enterprise_include('operation/menu.php');
$menu_operation = [];
$menu_operation['class'] = 'operation';
$menuOperation = [];
$subMenuMonitoring = [];
$subMenuMonitoringViews = [];
// L0. Monitoring.
$menuOperation['monitoring'] = new MenuItem(__('Monitoring'));
$menuOperation['monitoring']->setIcon('icon');
$menuOperation['monitoring']->setClass('menu');
$menuOperation['monitoring']->setACL(['AR']);
$monitoringItems = [];
// L1. Views.
$monitoringItems['views'] = new MenuItem(__('Views'));
$monitoringItems['views']->setIcon('icono');
$monitoringItems['views']->setClass('submenu');
$monitoringViewsItems = [];
// L2. Tactical view.
$monitoringViewsItems['tacticalView'] = new MenuItem(__('Tactical view'));
$monitoringViewsItems['tacticalView']->setSec('view');
$monitoringViewsItems['tacticalView']->setSec2('operation/agentes/tactical');
$monitoringViewsItems['tacticalView']->setClass('submenu');
// L2. Group View.
$monitoringViewsItems['groupView'] = new MenuItem(__('Group view'));
$monitoringViewsItems['groupView']->setSec('view');
$monitoringViewsItems['groupView']->setSec2('operation/agentes/group_view');
$monitoringViewsItems['groupView']->setClass('submenu');
// L2. Tree View.
$monitoringViewsItems['treeView'] = new MenuItem(__('Tree view'));
$monitoringViewsItems['treeView']->setSec('view');
$monitoringViewsItems['treeView']->setSec2('operation/tree');
$monitoringViewsItems['treeView']->setClass('submenu');
// L2. Monitor detail.
$monitoringViewsItems['monitorDetail'] = new MenuItem(__('Monitor detail'));
$monitoringViewsItems['monitorDetail']->setSec('view');
$monitoringViewsItems['monitorDetail']->setSec2('operation/agentes/status_monitor');
$monitoringViewsItems['monitorDetail']->setClass('submenu');
// L2. Interface view.
$monitoringViewsItems['interfaceView'] = new MenuItem(__('Interface View'));
$monitoringViewsItems['interfaceView']->setSec('view');
$monitoringViewsItems['interfaceView']->setSec2('operation/agentes/interface_view');
$monitoringViewsItems['interfaceView']->setClass('submenu');
// L2. Enterprise Tag view.
$idTagView = 'tagView';
$monitoringViewsItems[$idTagView] = enterprise_hook('tag_view_submenu', $idTagView);
// L2. Alert detail view.
$monitoringViewsItems['alertDetail'] = new MenuItem(__('Alert Detail'));
$monitoringViewsItems['alertDetail']->setSec('view');
$monitoringViewsItems['alertDetail']->setSec2('operation/agentes/alerts_status');
$monitoringViewsItems['alertDetail']->setClass('submenu');
// L2. Heatmap view.
$monitoringViewsItems['heatmapView'] = new MenuItem(__('Heatmap view'));
$monitoringViewsItems['heatmapView']->setSec('view');
$monitoringViewsItems['heatmapView']->setSec2('operation/heatmap');
$monitoringViewsItems['heatmapView']->setClass('submenu');
$monitoringItems['views']->setSubmenu($monitoringViewsItems);
// L1. Inventory.
$monitoringItems['inventory'] = new MenuItem(__('Inventory'));
$monitoringItems['inventory']->setSec('estado');
$monitoringItems['inventory']->setSec2('enterprise/operation/inventory/inventory');
$monitoringItems['inventory']->setClass('submenu');
// L1. Network.
$monitoringItems['network'] = new MenuItem();
$monitoringItems['network']->setDisplay((bool) $config['activate_netflow'] === true);
$monitoringItems['network']->setText(__('Network'));
// L2. Netflow explorer.
$monitoringNetworkItems['netflowExplorer'] = new MenuItem();
$monitoringNetworkItems['netflowExplorer']->setText(__('Netflow Explorer'));
$monitoringNetworkItems['netflowExplorer']->setSec('network_traffic');
$monitoringNetworkItems['netflowExplorer']->setSec2('operation/netflow/netflow_explorer');
// L2. Netflow Live view.
$monitoringNetworkItems['netflowLiveView'] = new MenuItem();
$monitoringNetworkItems['netflowLiveView']->setText(__('Netflow Live View'));
$monitoringNetworkItems['netflowLiveView']->setSec('network_traffic');
$monitoringNetworkItems['netflowLiveView']->setSec2('operation/netflow/nf_live_view');
// L2. Network usage map.
$monitoringNetworkItems['networkUsageMap'] = new MenuItem();
$monitoringNetworkItems['networkUsageMap']->setText(__('Network usage map'));
$monitoringNetworkItems['networkUsageMap']->setSec('network_traffic');
$monitoringNetworkItems['networkUsageMap']->setSec2('operation/network/network_usage_map');
$monitoringItems['network']->setSubmenu($monitoringNetworkItems);
$menuOperation['monitoring']->setSubmenu($monitoringItems);
// L0. Topology Maps.
$menuOperation['topologyMaps'] = new MenuItem(__('Topology Maps'));
$menuOperation['topologyMaps']->setIcon('icon');
$menuOperation['topologyMaps']->setClass('menu');
$menuOperation['topologyMaps']->setSubmenu($topologyMapsItems);
$access_console_node = !is_reporting_console_node();
if ($access_console_node === true) {
// Agent read, Server read.
@ -74,8 +184,8 @@ if ($access_console_node === true) {
$sub['view']['sub2'] = $sub2;
enterprise_hook('inventory_menu');
if ($config['activate_netflow']) {
/*
if ($config['activate_netflow']) {
$sub['network_traffic'] = [
'text' => __('Network'),
'id' => 'Network',
@ -112,8 +222,8 @@ if ($access_console_node === true) {
);
$sub['network_traffic']['sub2'] = $netflow_sub;
}
}
*/
if ($config['log_collector'] == 1) {
enterprise_hook('log_collector_menu');
}