diff --git a/app/Http/Controllers/Table/TnmsneController.php b/app/Http/Controllers/Table/TnmsneController.php new file mode 100644 index 0000000000..97a8d60b51 --- /dev/null +++ b/app/Http/Controllers/Table/TnmsneController.php @@ -0,0 +1,110 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2023 Tony Murray + * @author Tony Murray + */ + +namespace App\Http\Controllers\Table; + +use App\Models\TnmsneInfo; +use Illuminate\Http\Request; + +class TnmsneController extends TableController +{ + protected function rules() + { + return [ + 'device_id' => 'nullable|integer', + ]; + } + + protected function sortFields($request) + { + return [ + 'neName', + 'neLocation', + 'neType', + 'neOpMode', + 'neAlarm', + 'neOpState', + ]; + } + + protected function searchFields(Request $request) + { + return [ + 'neName', + 'neLocation', + 'neType', + 'neOpMode', + 'neAlarm', + 'neOpState', + ]; + } + + protected function filterFields(Request $request) + { + return ['device_id']; + } + + /** + * @inheritDoc + */ + protected function baseQuery(Request $request) + { + return TnmsneInfo::query(); + } + + /** + * @param TnmsneInfo $tnmsne + * @return array + */ + public function formatItem($tnmsne) + { + $neOp = $tnmsne->neOpMode == 'operation' + ? 'operation' + : '' . htmlspecialchars($tnmsne->neOpMode) . ''; + + $opState = $tnmsne->neOpState == 'enabled' + ? 'enabled' + : '' . htmlspecialchars($tnmsne->neOpState) . ''; + + return [ + 'neName' => htmlspecialchars($tnmsne->neName), + 'neLocation' => htmlspecialchars($tnmsne->neLocation), + 'neType' => htmlspecialchars($tnmsne->neType), + 'neOpMode' => $neOp, + 'neAlarm' => $this->getAlarmLabel($tnmsne->neAlarm), + 'neOpState' => $opState, + ]; + } + + private function getAlarmLabel(string $neAlarm): string + { + return match ($neAlarm) { + 'cleared' => 'cleared', + 'warning' => 'warning', + 'minor', 'major', 'critical', 'indeterminate' => '' . htmlspecialchars($neAlarm) . '', + default => '' . htmlspecialchars($neAlarm) . '', + }; + } +} diff --git a/includes/common.php b/includes/common.php index aada736a6e..2a41ce6c68 100644 --- a/includes/common.php +++ b/includes/common.php @@ -445,13 +445,6 @@ function is_customoid_graph($type, $subtype) return false; } // is_customoid_graph -function search_phrase_column($c) -{ - global $searchPhrase; - - return "$c LIKE '%$searchPhrase%'"; -} // search_phrase_column - /** * Parse location field for coordinates * diff --git a/includes/html/pages/device/tnmsne.inc.php b/includes/html/pages/device/tnmsne.inc.php index afbafcdadd..ba16d9d3a0 100644 --- a/includes/html/pages/device/tnmsne.inc.php +++ b/includes/html/pages/device/tnmsne.inc.php @@ -27,11 +27,10 @@ $pagetitle[] = 'Hardware'; post: function() { return { - id: "tnmsneinfo", device_id: '', }; }, - url: "ajax_table.php", + url: "", formatters: { }, templates: { diff --git a/includes/html/table/tnmsneinfo.inc.php b/includes/html/table/tnmsneinfo.inc.php deleted file mode 100644 index be79ff488a..0000000000 --- a/includes/html/table/tnmsneinfo.inc.php +++ /dev/null @@ -1,110 +0,0 @@ - - * - * by Paul Gear - * based on code by Søren Friis Rosiak - * in commit 054bf3ae209f34a2c3bc8968300722004903df1b - * - * 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 3 of the License, or (at your - * option) any later version. Please see LICENSE.txt at the top level of - * the source code distribution for details. - */ - -$columns = [ - 'neName', - 'neLocation', - 'neType', - 'neOpMode', - 'neAlarm', - 'neOpState', -]; - -if (isset($vars['device_id'])) { - $params = [ - $vars['device_id'], - ]; - $sql = 'SELECT `neName`,`neLocation`,`neType`,`neOpMode`,`neAlarm`,`neOpState` FROM `tnmsneinfo`'; - $wheresql = ' WHERE `device_id` = ?'; - $sortcolumns = 3; - $count_sql = 'SELECT COUNT(id) FROM `tnmsneinfo`' . $wheresql; - - // all columns are searchable - search across them - if (isset($searchPhrase) && ! empty($searchPhrase)) { - $searchsql = implode(' OR ', array_map('search_phrase_column', $columns)); - $wheresql .= " AND ( $searchsql )"; - } - $sql .= $wheresql; - - // get total - $total = dbFetchCell($count_sql, $params); - if (empty($total)) { - $total = 0; - } - - // set up default sort - if (! isset($sort) || empty($sort)) { - $sort = implode(', ', array_slice($columns, 0, $sortcolumns)); - } - $sql .= " ORDER BY $sort"; - - // select only the required rows - if (isset($current)) { - $limit_low = (($current * $rowCount) - $rowCount); - $limit_high = $rowCount; - } - if ($rowCount != -1) { - $sql .= " LIMIT $limit_low,$limit_high"; - } - - // load data from database into response array - $response = []; - foreach (dbFetchRows($sql, $params) as $tnmsne) { - if ($tnmsne['neOpMode'] == 'operation') { - $neop = 'operation'; - } else { - $neop = '' . $tnmsne['neOpMode'] . ''; - } - switch ($tnmsne['neAlarm']) { - case 'cleared': - $alarm = 'cleared'; - break; - case 'warning': - $alarm = 'warning'; - break; - case 'minor': - case 'major': - case 'critical': - case 'indeterminate': - $alarm = '' . $tnmsne['neAlarm'] . ''; - break; - default: - $alarm = '' . $tnmsne['neAlarm'] . ''; - } - if ($tnmsne['neOpState'] == 'enabled') { - $opstate = 'enabled'; - } else { - $opstate = '' . $tnmsne['neOpState'] . ''; - } - $response[] = [ - 'neName' => $tnmsne['neName'], - 'neLocation' => $tnmsne['neLocation'], - 'neType' => $tnmsne['neType'], - 'neOpMode' => $neop, - 'neAlarm' => $alarm, - 'neOpState' => $opstate, - ]; - } - - $output = [ - 'current' => $current, - 'rowCount' => $rowCount, - 'rows' => $response, - 'total' => $total, - ]; - echo json_encode($output, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); -} diff --git a/routes/web.php b/routes/web.php index 4985aa19ed..a957686485 100644 --- a/routes/web.php +++ b/routes/web.php @@ -201,6 +201,7 @@ Route::middleware(['auth'])->group(function () { Route::post('ports', 'PortsController')->name('table.ports'); Route::post('routes', 'RoutesTablesController'); Route::post('syslog', 'SyslogController'); + Route::post('tnmsne', 'TnmsneController')->name('table.tnmsne'); Route::post('vminfo', 'VminfoController'); });