Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions flowview_filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
include('./include/auth.php');
include_once($config['base_path'] . '/plugins/flowview/setup.php');
include_once($config['base_path'] . '/plugins/flowview/functions.php');
include_once($config['base_path'] . '/plugins/flowview/ui_helpers.php');
include_once($config['base_path'] . '/lib/time.php');
include_once($config['base_path'] . '/lib/timespan_settings.php');

Expand All @@ -52,21 +53,11 @@
sort_filter();
break;
case 'edit':
if (!isset_request_var('embed')) {
top_header();
}

edit_filter();

if (!isset_request_var('embed')) {
bottom_footer();
}
flowview_filters_render_with_layout('edit_filter', true);

break;
default:
top_header();
show_filters();
bottom_footer();
flowview_filters_render_with_layout('show_filters');
break;
}

Expand Down Expand Up @@ -399,4 +390,3 @@ function clearFilter() {

form_end();
}

85 changes: 85 additions & 0 deletions tests/test_layout_wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<?php

/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
+-------------------------------------------------------------------------+
*/

require_once __DIR__ . '/../ui_helpers.php';

$events = [];
$request_vars = [];

function top_header() {
global $events;
$events[] = 'top';
}

function bottom_footer() {
global $events;
$events[] = 'bottom';
}

function isset_request_var($key) {
global $request_vars;
return isset($request_vars[$key]);
}

function test_renderer() {
global $events;
$events[] = 'render';
}

function assert_same($expected, $actual, $message) {
if ($expected !== $actual) {
fwrite(STDERR, $message . PHP_EOL);
fwrite(STDERR, 'Expected: ' . json_encode($expected) . PHP_EOL);
fwrite(STDERR, 'Actual: ' . json_encode($actual) . PHP_EOL);
exit(1);
}
}

function assert_regex($pattern, $subject, $message) {
if (!preg_match($pattern, $subject)) {
fwrite(STDERR, $message . PHP_EOL);
exit(1);
}
}

flowview_filters_render_with_layout('test_renderer');
assert_same(['top', 'render', 'bottom'], $events, 'Default wrapper should render with layout.');

$events = [];
$request_vars['embed'] = 1;
flowview_filters_render_with_layout('test_renderer', true);
assert_same(['render'], $events, 'Embed-aware wrapper should skip layout when embed is set.');

$events = [];
flowview_filters_render_with_layout('test_renderer', false);
assert_same(['top', 'render', 'bottom'], $events, 'Non-embed wrapper should keep layout even when embed is set.');

$source = file_get_contents(__DIR__ . '/../flowview_filters.php');
if ($source === false) {
fwrite(STDERR, 'Unable to read flowview_filters.php' . PHP_EOL);
exit(1);
}

assert_regex(
"/flowview_filters_render_with_layout\\(\\s*'edit_filter'\\s*,\\s*true\\s*\\)\\s*;/",
$source,
'Expected edit action to use shared layout helper.'
);

assert_regex(
"/flowview_filters_render_with_layout\\(\\s*'show_filters'\\s*\\)\\s*;/",
$source,
'Expected default action to use shared layout helper.'
);

echo "OK\n";
39 changes: 39 additions & 0 deletions ui_helpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDTool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
Comment on lines +9 to +22
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 9f37b1d: expanded ui_helpers.php to the standard full Cacti header block used by this plugin.

*/

if (!function_exists('flowview_filters_render_with_layout')) {
function flowview_filters_render_with_layout(callable $renderer, $respect_embed = false) {
$should_wrap = (!$respect_embed || !isset_request_var('embed'));

if ($should_wrap) {
top_header();
}

$renderer();

if ($should_wrap) {
bottom_footer();
}
}
}