menu: tweak this a little more

findNodeById() is not needed as findNodeByPath() does the same.
It's also not used anywhere else so just merge both.

On the system end make a getItem() which makes the code a little
cleaner and reduces callers of findNodeByPath() to one.  ;)
This commit is contained in:
Franco Fichtner 2026-02-13 09:05:23 +01:00
parent beed8f1000
commit ee040be318
2 changed files with 25 additions and 20 deletions

View file

@ -431,21 +431,6 @@ class MenuItem
return $result;
}
/**
* find node by id/tag name, ignore case.
* @param $id id / tagname
* @return null|MenuItem
*/
public function findNodeById($id)
{
foreach ($this->children as $key => &$node) {
if ($node->isVisible() && strtolower($node->getId()) == strtolower($id)) {
return $node;
}
}
return null;
}
/**
* find node by id/tag path, ignore case.
* @param $id id / tagname path
@ -455,11 +440,21 @@ class MenuItem
{
$node = $this;
foreach (explode('.', $path) as $key) {
$node = $node->findNodeById($key);
if ($node == null) {
foreach (explode('.', $path) as $id) {
$found = null;
foreach ($node->children as &$_node) {
if ($_node->isVisible() && !strcasecmp($_node->getId(), $id)) {
$found = $_node;
break;
}
}
if (is_null($found)) {
return null;
}
$node = $found;
}
return $node;

View file

@ -76,6 +76,16 @@ class MenuSystem
return $menuXml;
}
/**
* get menu item from existing root
* @param string $root xpath expression
* @return null|MenuItem
*/
public function getItem($root)
{
return $this->root->findNodeByPath($root);
}
/**
* append menu item to existing root
* @param string $root xpath expression
@ -85,7 +95,7 @@ class MenuSystem
*/
public function appendItem($root, $id, $properties)
{
return $this->root->findNodeByPath($root)?->append($id, $properties);
return $this->getItem($root)?->append($id, $properties);
}
/**
@ -331,7 +341,7 @@ class MenuSystem
]);
$iftargets['fw'] = array_merge(['FloatingRules' => gettext('Floating')], $iftargets['fw']);
} elseif ($has_mvc_fw) {
$this->root->findNodeByPath('Firewall.Rule')?->setVisibleName(gettext('Rules'));
$this->getItem('Firewall.Rule')?->setVisibleName(gettext('Rules'));
}
$ordid = 1;
foreach ($iftargets['fw'] as $key => $descr) {