3.8 KiB
Build a Filterable Plugin Page
Build a page where a user can search four sample devices, sort the matches, and move between pages without losing the search. The example stores its own inventory in MariaDB. Everyone with the example realm can see those four records. To display real Cacti devices, also constrain the query to devices the current user is authorized to see.
Install the example
Use a disposable Cacti installation. Extract
workflowdemo version 1 into the Cacti
plugins/ directory. From the application root run:
php cli/plugin_manage.php --plugin=workflowdemo --install --enable
In the Cacti database, insert the following fixture once on a fresh install.
INSERT INTO plugin_workflowdemo_devices (id, name)
VALUES (1, 'edge-a'), (2, 'edge-b'), (3, 'edge-c'), (4, 'core-a');
Grant a test user View Workflow Example in User Management. Sign in as
that user and open plugins/workflowdemo/workflowdemo_devices.php under
your Cacti URL. The first page lists core-a and edge-a.
Render the filter for your branch
Both branches ship lib/html_filter.php. Their CactiTableFilter
constructors differ. On develop, the fourth argument is the session key.
On 1.2.x the fourth argument is the form width and the fifth is the session
key. The rendering methods are render() and filter_render(),
respectively.
The downloadable plugin loads include/auth.php and renders a labeled GET
form on 1.2.x. It submits the field named filter to the same page. Escape
the current value when putting it back in the search box. Submitting the
form starts a new search on page one.
<form method="get" action="workflowdemo_devices.php">
<label>Search <input name="filter" type="search"></label>
<button type="submit">Go</button>
</form>
The downloadable example also includes the development branch's filter renderer. Both forms feed the count and list queries below.
Keep search, sort, and paging together
The count and list queries use the same predicate. With no search text,
LOCATE('', name) matches every row. A search containing a literal
percent sign or underscore searches for that character.
$count = (int) db_fetch_cell_prepared(
'SELECT COUNT(*) FROM plugin_workflowdemo_devices WHERE LOCATE(?, name) > 0',
[$term]
);
The page maps two accepted directions to fixed SQL fragments. It orders by name and then by ID so equal names have a stable order. It limits the page number to the available range before computing the offset.
$order = $direction === 'desc' ? 'name DESC, id DESC' : 'name ASC, id ASC';
$last = max(1, (int) ceil($count / 2));
$page = min($last, max(1, $page));
$offset = ($page - 1) * 2;
$rows = db_fetch_assoc_prepared(
"SELECT name FROM plugin_workflowdemo_devices
WHERE LOCATE(?, name) > 0 ORDER BY $order LIMIT 2 OFFSET $offset",
[$term]
);
Use http_build_query() to carry the search and direction into each link.
Escape the URL for the HTML attribute and escape each displayed name. A new
search or sort starts at page one. An empty result has its own message.
Verify the visible result
Search for edge and press Enter. Click Name to reverse the sort. The first two rows should now be edge-c and edge-b. Click Next. Only edge-a should remain. Searching for absent should show No matching devices. The screenshots show the second page of the same search on the two tested branches.
Run the browser test to repeat these checks and verify access with a second account.
Copyright (c) 2004-2026 The Cacti Group

