Skip to content
Merged
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
22 changes: 22 additions & 0 deletions css/common.css
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,28 @@
color: black !important;
}

/* Enabled/Disabled status pills on the Panel Library page */
.intropage_status_pill {
display: inline-block;
min-width: 72px;
padding: 2px 10px;
border-radius: 10px;
font-size: 0.85em;
font-weight: bold;
line-height: 1.5;
text-align: center;
}

.intropage_status_pill.enabled {
background: rgba(148,179,107,1);
color: #000;
}

.intropage_status_pill.disabled {
background: #cc0000;
color: #fff;
}

#intropage_main .fa,
#overlay .fa {
padding-right: 3px;
Expand Down
24 changes: 20 additions & 4 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2503,10 +2503,26 @@ function intropage_addpanel_select(int $dashboard_id): void {
ON pda.id = pd.panel_id
WHERE pd.user_id IN (0, ?)
AND pd.dashboard_id = ?
)
ORDER BY pd.name',
)',
[$_SESSION['sess_user_id'], $dashboard_id]);

// Prefix each option label with the panel's source ("Core - " /
// "<Plugin> - "), matching the Panel Library list, and HTML-escape it once
// here so every option below (including the "(no permission)" entries)
// renders the plugin-derived name safely without re-escaping per branch.
// Then order by the prefixed label, rather than with ORDER BY pd.name, so
// the menu stays grouped by source and reads as sorted even though the
// displayed label no longer matches the unprefixed database name.
foreach ($add_panels as &$panel) {
$panel['name'] = html_escape(intropage_prefix_panel_name($panel['panel_id'], (string) $panel['name']));
}

unset($panel);

usort($add_panels, function ($a, $b) {
return strnatcasecmp($a['name'], $b['name']);
});

// display always, even if it is empty
print "<select id='intropage_addpanel'>";
print '<option value="0">' . __('Panels ...', 'intropage') . '</option>';
Expand All @@ -2528,15 +2544,15 @@ function intropage_addpanel_select(int $dashboard_id): void {
if ($uniqid > 0) {
if ($allowed) {
if ($enabled) {
print "<option value='" . $uniqid . "'>" . html_escape($panel['name']) . '</option>';
print "<option value='" . $uniqid . "'>" . $panel['name'] . '</option>';
}
} else {
print "<option value='addpanel_" . $uniqid . "' disabled='disabled'>" . __('%s (no permission)', $panel['name'], 'intropage') . '</option>';
}
} else {
if ($allowed) {
if ($enabled) {
print "<option value='" . $panel['panel_id'] . "'>" . html_escape($panel['name']) . '</option>';
print "<option value='" . $panel['panel_id'] . "'>" . $panel['name'] . '</option>';
}
} else {
print "<option value='addpanel_" . $uniqid . "' disabled='disabled'>" . __('%s (no permission)', $panel['name'], 'intropage') . '</option>';
Expand Down
80 changes: 28 additions & 52 deletions intropage_panels.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,75 +249,51 @@ function intropage_panels(): void {

html_start_box('', '100%', '', '3', 'center', '');

$display_text = [
'name' => [
'display' => __('Panel Name', 'intropage'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The display name of this panel.', 'intropage'),
],
'plugin' => [
'display' => __('Source', 'intropage'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The plugin that provides this panel. Intropage\'s own panels show as Core.', 'intropage'),
],
'class' => [
'display' => __('Category', 'intropage'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The panel category.', 'intropage'),
],
'level' => [
'display' => __('Level', 'intropage'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('System panels are shared by all users; User panels are per-user.', 'intropage'),
],
'requires' => [
'display' => __('Requires', 'intropage'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('Plugins that must be enabled for this panel to render.', 'intropage'),
],
'refresh' => [
'display' => __('Refresh', 'intropage'),
'align' => 'right',
'sort' => 'ASC',
'tip' => __('The panel data collection interval.', 'intropage'),
],
'update_func' => [
'display' => __('Update Function', 'intropage'),
'align' => 'left',
'sort' => 'ASC',
'tip' => __('The function the poller calls to collect this panel\'s data.', 'intropage'),
],
'enabled' => [
'display' => __('Status', 'intropage'),
'align' => 'right',
'sort' => 'DESC',
'tip' => __('Whether the panel is enabled.', 'intropage'),
],
// Column definitions in display order. Each entry is [display, tip] and
// defaults to a left-aligned ascending sort; a column that differs
// overrides the alignment (3rd element) and sort direction (4th).
$columns = [
'name' => [__('Panel Name', 'intropage'), __('The display name of this panel.', 'intropage')],
'plugin' => [__('Source', 'intropage'), __('The plugin that provides this panel. Intropage\'s own panels show as Core.', 'intropage')],
'enabled' => [__('Status', 'intropage'), __('Whether the panel is enabled.', 'intropage'), 'center', 'DESC'],
'class' => [__('Category', 'intropage'), __('The panel category.', 'intropage')],
'level' => [__('Level', 'intropage'), __('System panels are shared by all users; User panels are per-user.', 'intropage')],
'requires' => [__('Requires', 'intropage'), __('Plugins that must be enabled for this panel to render.', 'intropage')],
'update_func' => [__('Update Function', 'intropage'), __('The function the poller calls to collect this panel\'s data.', 'intropage')],
'refresh' => [__('Refresh', 'intropage'), __('The panel data collection interval.', 'intropage'), 'right'],
];

$display_text = [];

foreach ($columns as $field => $column) {
$display_text[$field] = [
'display' => $column[0],
'align' => $column[2] ?? 'left',
'sort' => $column[3] ?? 'ASC',
'tip' => $column[1],
];
}

html_header_sort_checkbox($display_text, get_request_var('sort_column'), get_request_var('sort_direction'), false);

if (cacti_sizeof($panel_list)) {
foreach ($panel_list as $panel) {
$source = ($panel['plugin'] === 'intropage') ? __('Core', 'intropage') : ucwords(str_replace(['_', '-'], ' ', $panel['plugin']));
$level = ((int) $panel['level'] === 0) ? __('System', 'intropage') : __('User', 'intropage');
$status = ((int) $panel['enabled'] === 1) ? __('Enabled', 'intropage') : __('Disabled', 'intropage');
$enabled = ((int) $panel['enabled'] === 1);
$status = $enabled ? __('Enabled', 'intropage') : __('Disabled', 'intropage');
$status_pill = '<span class="intropage_status_pill ' . ($enabled ? 'enabled' : 'disabled') . '">' . html_escape($status) . '</span>';

form_alternate_row('line' . $panel['panel_id'], true, ((int) $panel['enabled'] !== 1));

form_selectable_cell(filter_value(intropage_prefix_panel_name($panel['panel_id'], $panel['name']), get_request_var('filter')), $panel['panel_id']);
form_selectable_cell(html_escape($source), $panel['panel_id']);
form_selectable_cell($status_pill, $panel['panel_id'], '', 'center');
form_selectable_cell(html_escape($panel['class']), $panel['panel_id']);
form_selectable_cell($level, $panel['panel_id']);
form_selectable_cell(html_escape($panel['requires'] != '' ? $panel['requires'] : __('None', 'intropage')), $panel['panel_id']);
form_selectable_cell(intropage_readable_interval((float) $panel['refresh'], 0, false), $panel['panel_id'], '', 'right');
form_selectable_cell(html_escape($panel['update_func']), $panel['panel_id']);
form_selectable_cell($status, $panel['panel_id'], '', 'right');
form_selectable_cell(intropage_readable_interval((float) $panel['refresh'], 0, false), $panel['panel_id'], '', 'right');
form_checkbox_cell($panel['name'], $panel['panel_id']);

form_end_row();
Expand Down
186 changes: 186 additions & 0 deletions tests/Unit/IntropageAddPanelSelectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2026 The Cacti Group |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
*/

/*
* The add-panel dropdown (intropage_addpanel_select()) must label each option
* with the panel's source prefix ("Core - " / "<Plugin> - "), the same way the
* Panel Library list does, and order the options by that prefixed label so the
* menu reads as sorted and grouped by source.
*
* The plugin's db_* helpers are the unit-bootstrap stubs; reads are answered by
* the router built in intropage_addpanel_fixture(). User ids are unique per
* test because is_panel_allowed() caches the resolved permissions per user.
*/

require_once __DIR__ . '/../../includes/functions.php';

/**
* Scripts the queries intropage_addpanel_select() (and the helpers it calls)
* make for one test.
*
* @param array $panels Rows the panel-definition query returns (each
* ['panel_id' => ..., 'name' => ...]).
* @param array $uniqids panel_id => per-user panel-data id ('' when none).
* @param bool $sourced Whether a registration row maps each panel to a source.
* @param array $perms panel_id => 'on' permissions stored for the user.
*/
function intropage_addpanel_fixture(array $panels, array $uniqids, bool $sourced, array $perms): void {
$GLOBALS['__test_enabled_plugins'] = ['intropage' => true];
$GLOBALS['__test_tables'] = ['plugin_intropage_panel_registration' => $sourced];

$GLOBALS['__test_query'] = function ($kind, $sql, $params) use ($panels, $uniqids, $sourced, $perms) {
if ($kind === 'assoc') {
if (strpos($sql, 'DISTINCT') !== false && strpos($sql, 'plugin_intropage_panel_definition') !== false) {
return $panels;
}

if ($sourced && strpos($sql, 'SELECT panel_id, plugin') !== false
&& strpos($sql, 'plugin_intropage_panel_registration') !== false) {
return array_map(static fn ($p) => ['panel_id' => $p['panel_id'], 'plugin' => 'intropage'], $panels);
}

return [];
}

if ($kind === 'cell') {
if (strpos($sql, 'FROM plugin_intropage_panel_data') !== false && strpos($sql, 'SELECT id') !== false) {
return $uniqids[$params[1]] ?? '';
}

if (strpos($sql, 'plugin_intropage_user_auth') !== false) {
return json_encode($perms);
}

return '';
}

return [];
};
}

function intropage_addpanel_render(int $dashboard_id = 1): string {
ob_start();
intropage_addpanel_select($dashboard_id);

return (string) ob_get_clean();
}

beforeEach(function () {
$GLOBALS['__test_db_calls'] = [];
$_SESSION['dashboard_id'] = 1;

// Rebuild the statically cached panel library and drop the per-request
// source/disabled caches so each test starts from a clean slate.
$GLOBALS['intropage_panel_library_stale'] = true;
unset($GLOBALS['intropage_panel_sources'], $GLOBALS['intropage_disabled_panels']);
});

afterEach(function () {
unset($GLOBALS['__test_query'], $GLOBALS['__test_tables'], $GLOBALS['__test_enabled_plugins']);
});

describe('intropage_addpanel_select', function () {
it('prefixes an option with its registered source', function () {
$_SESSION['sess_user_id'] = 3101;

// 'alert_host' is a built-in (Core) panel with no plugin requirement, so
// is_panel_enabled() reports it enabled from the rebuilt library.
intropage_addpanel_fixture(
[['panel_id' => 'alert_host', 'name' => 'Host alerts']],
['alert_host' => '7'],
true,
['alert_host' => 'on']
);

$html = intropage_addpanel_render();

expect($html)->toContain("<option value='7'>Core - Host alerts</option>");
});

it('leaves the label unchanged when the panel has no registered source', function () {
$_SESSION['sess_user_id'] = 3102;

intropage_addpanel_fixture(
[['panel_id' => 'alert_host', 'name' => 'Host alerts']],
['alert_host' => '7'],
false,
['alert_host' => 'on']
);

$html = intropage_addpanel_render();

expect($html)->toContain("<option value='7'>Host alerts</option>");
expect($html)->not->toContain('Core - ');
});

it('offers a panel with no data row yet by its panel id', function () {
$_SESSION['sess_user_id'] = 3106;

intropage_addpanel_fixture(
[['panel_id' => 'alert_host', 'name' => 'Host alerts']],
['alert_host' => ''],
true,
['alert_host' => 'on']
);

$html = intropage_addpanel_render();

expect($html)->toContain("<option value='alert_host'>Core - Host alerts</option>");
});

it('orders options by the prefixed label, not the database name', function () {
$_SESSION['sess_user_id'] = 3103;

// Fed Host-before-Database; the prefixed-label sort must flip them so
// "Core - Database Checks" precedes "Core - Host alerts".
intropage_addpanel_fixture(
[
['panel_id' => 'alert_host', 'name' => 'Host alerts'],
['panel_id' => 'analyse_db', 'name' => 'Database Checks'],
],
['alert_host' => '5', 'analyse_db' => '6'],
true,
['alert_host' => 'on', 'analyse_db' => 'on']
);

$html = intropage_addpanel_render();

expect(strpos($html, 'Core - Database Checks'))->toBeLessThan(strpos($html, 'Core - Host alerts'));
});

it('marks a panel the user may not see as disabled (existing row)', function () {
$_SESSION['sess_user_id'] = 3104;

intropage_addpanel_fixture(
[['panel_id' => 'alert_host', 'name' => 'Host alerts']],
['alert_host' => '9'],
true,
[]
);

$html = intropage_addpanel_render();

expect($html)->toContain("<option value='addpanel_9' disabled='disabled'>");
});

it('marks a panel the user may not see as disabled (no row yet)', function () {
$_SESSION['sess_user_id'] = 3105;

intropage_addpanel_fixture(
[['panel_id' => 'alert_host', 'name' => 'Host alerts']],
['alert_host' => ''],
true,
[]
);

$html = intropage_addpanel_render();

expect($html)->toContain("<option value='addpanel_' disabled='disabled'>");
});
});
8 changes: 7 additions & 1 deletion tests/e2e/cacti-theme.spec.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ for (const { theme, palette } of themes) {
`<div class="cactiTableTitleRow"><div class="cactiTableTitle">Panel Details</div>` +
`<div class="cactiTableButton"><i id="bubble-${color}" class="color_${color}_bubble">●</i></div></div>` +
`<div${palette ? ' class="grid_item"' : theme === 'paper-plane' ? ' class="panel_data"' : ''}><table class="cactiTable"><tr class="${row}"><td><span id="square-${color}" class="inpa_sq color_${color}"></span>Device status</td></tr></table></div>`
).join('') + '</div>');
).join('') + `<span id="pill-enabled" class="intropage_status_pill enabled">Enabled</span><span id="pill-disabled" class="intropage_status_pill disabled">Disabled</span>` + '</div>');
await preparePalette(page, palette);
for (const file of [resolve(core, `include/themes/${theme}/main.css`), resolve(root, 'css/common.css'), resolve(root, `css/${theme}.css`)]) {
await installCss(page, file);
Expand All @@ -141,6 +141,12 @@ for (const { theme, palette } of themes) {
expect(indicators).toHaveLength(8);
if (palette) expect(new Set(indicators.filter(item => item.id.startsWith('bubble-')).map(item => item.foreground)).size, 'distinct severity colors').toBe(4);
for (const indicator of indicators) expect(indicator.ratio, indicator.id).toBeGreaterThanOrEqual(3);
// The Enabled/Disabled status pills carry their own opaque colour, so
// their label must stay legible against it on every theme.
for (const pill of await page.locator('.intropage_status_pill').all()) {
const contrast = await pill.evaluate(elementContrast);
expect(contrast.ratio, contrast.id).toBeGreaterThanOrEqual(4.5);
}
});
}
}
Loading