From 2ab1aa601c7d8220a3f7c9b021c14d719f1578b2 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Wed, 20 Apr 2022 14:10:23 +0200 Subject: [PATCH] Service tree view with local scope parents --- pandora_console/general/header.php | 2 +- .../include/class/TreeService.class.php | 2 + pandora_console/include/functions.php | 50 +++++++++++++++++++ .../include/javascript/tree/TreeController.js | 3 +- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/pandora_console/general/header.php b/pandora_console/general/header.php index 9b0333c55a..5e2ad24ce2 100644 --- a/pandora_console/general/header.php +++ b/pandora_console/general/header.php @@ -954,7 +954,7 @@ echo sprintf('
', $menuTypeClass); $("a.autorefresh").click (function () { $("a.autorefresh_txt").toggle (); - $("#combo_refr").toggle (); + $("#combo_refr").toggle(); $("select#ref").change (function () { href = $("a.autorefresh").attr ("href"); diff --git a/pandora_console/include/class/TreeService.class.php b/pandora_console/include/class/TreeService.class.php index ebca4562a6..e69862a9fd 100644 --- a/pandora_console/include/class/TreeService.class.php +++ b/pandora_console/include/class/TreeService.class.php @@ -523,6 +523,8 @@ class TreeService extends Tree continue 2; } + $tmp['parents'] = $item->service()->getAncestors(); + $tmp['title'] = join('/', $tmp['parents']); $tmp['id'] = (int) $item->service()->id(); $tmp['name'] = $item->service()->name(); $tmp['alias'] = $item->service()->name(); diff --git a/pandora_console/include/functions.php b/pandora_console/include/functions.php index ca0c9dad7d..c1b6697022 100644 --- a/pandora_console/include/functions.php +++ b/pandora_console/include/functions.php @@ -5986,6 +5986,56 @@ function send_test_email( } +/** + * Return array of ancestors of item, given array. + * + * @param integer $item From index. + * @param array $data Array data. + * @param string $key Pivot key (identifies the parent). + * @param string|null $extract Extract certain column or index. + * @param array $visited Cycle detection. + * + * @return array Array of ancestors. + */ +function get_ancestors( + int $item, + array $data, + string $key, + ?string $extract=null, + array &$visited=[] +) :array { + if (isset($visited[$item]) === true) { + return []; + } + + $visited[$item] = 1; + + if (isset($data[$item]) === false) { + return []; + } + + if (isset($data[$item][$key]) === false) { + if ($extract !== null) { + return [$data[$item][$extract]]; + } + + return [$item]; + } + + if ($extract !== null) { + return array_merge( + get_ancestors($data[$item][$key], $data, $key, $extract, $visited), + [$data[$item][$extract]] + ); + } + + return array_merge( + get_ancestors($data[$item][$key], $data, $key, $extract, $visited), + [$item] + ); +} + + if (function_exists('str_contains') === false) { diff --git a/pandora_console/include/javascript/tree/TreeController.js b/pandora_console/include/javascript/tree/TreeController.js index 7f22c6e9d0..a09be1880d 100644 --- a/pandora_console/include/javascript/tree/TreeController.js +++ b/pandora_console/include/javascript/tree/TreeController.js @@ -848,6 +848,7 @@ var TreeController = { break; case "services": + console.log(element); if ( typeof element.statusImageHTML != "undefined" && element.statusImageHTML.length > 0 @@ -861,7 +862,7 @@ var TreeController = { '' +
                 element.name +
                 ' ';