From ab9838298442a82d1156c17adfe866afc67a3367 Mon Sep 17 00:00:00 2001 From: Jokob-sk Date: Fri, 20 Jan 2023 23:44:33 +1100 Subject: [PATCH] Devices Columns 0.3 --- front/devices.php | 131 ++++++++++++++----------- front/js/helper_objects.js | 1 - front/maintenance.php | 28 +++++- front/php/server/devices.php | 100 +++++++++---------- front/php/server/util.php | 18 ++++ front/php/templates/language/en_us.php | 2 +- 6 files changed, 168 insertions(+), 112 deletions(-) delete mode 100644 front/js/helper_objects.js diff --git a/front/devices.php b/front/devices.php index 72c5ab55..0f79c467 100755 --- a/front/devices.php +++ b/front/devices.php @@ -150,21 +150,7 @@
- - - - - - - - - - - - - - - + @@ -206,7 +192,8 @@ var parTableOrder = 'Front_Devices_Order'; var tableRows = 10; var tableOrder = [[3,'desc'], [0,'asc']]; - var tableColumnShow = [0,1,2,3,4,5,6,7,8,9,10,12,13] + var tableColumnVisible = [0,1,2,3,4,5,6,7,8,9,10,12,13] + var tableColumnOrder = [0,1,2,3,4,5,6,7,8,9,10,12,13] var tableColumnAll = [0,1,2,3,4,5,6,7,8,9,10,12,13] // Read parameters & Initialize components @@ -215,41 +202,72 @@ // ----------------------------------------------------------------------------- function main () { - - // get parameter value - $.get('php/server/parameters.php?action=get&expireMinutes=525600&defaultValue=[0,1,2,3,4,5,6,7,8,9,10,11,12,13]¶meter=Front_Devices_Columns', function(data) { + + // get visible columns + $.get('php/server/parameters.php?action=get&expireMinutes=525600&defaultValue=[0,1,2,3,4,5,6,7,8,9,10,11,12,13]¶meter=Front_Devices_Columns_Visible', function(data) { - tableColumnShow = numberArrayFromString(data); + tableColumnVisible = numberArrayFromString(data); - console.log(tableColumnShow); + // get the custom order specified by the user + $.get('php/server/parameters.php?action=get&expireMinutes=525600&defaultValue=[0,1,2,3,4,5,6,7,8,9,10,11,12,13]¶meter=Front_Devices_Columns_Order', function(data) { + + tableColumnOrder = numberArrayFromString(data); - // get parameter value - $.get('php/server/parameters.php?action=get&defaultValue=50¶meter='+ parTableRows, function(data) { - var result = JSON.parse(data); + //initialize the table headers in the correct order + var headersDefaultOrder = [ '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '', + '']; - result = parseInt(result, 10) - - if (Number.isInteger (result) ) { - tableRows = result; + html = ''; + + for(index = 0; index < tableColumnOrder.length; index++) + { + html += ''; } + $('#tableDevices tr').html(html); + + + // get parameter value - $.get('php/server/parameters.php?action=get&defaultValue=[[3,"desc"],[0,"asc"]]¶meter='+ parTableOrder, function(data) { + $.get('php/server/parameters.php?action=get&defaultValue=50¶meter='+ parTableRows, function(data) { var result = JSON.parse(data); - result = JSON.parse(result); - + result = parseInt(result, 10) - if (Array.isArray (result) ) { - tableOrder = result; + if (Number.isInteger (result) ) { + tableRows = result; } - // Initialize components with parameters - initializeDatatable(); + // get parameter value + $.get('php/server/parameters.php?action=get&defaultValue=[[3,"desc"],[0,"asc"]]¶meter='+ parTableOrder, function(data) { + var result = JSON.parse(data); + result = JSON.parse(result); - // query data - getDevicesTotals(); - getDevicesList (deviceStatus); + + + if (Array.isArray (result) ) { + tableOrder = result; + } + + // Initialize components with parameters + initializeDatatable(); + + // query data + getDevicesTotals(); + getDevicesList (deviceStatus); + }); }); }); }); @@ -260,24 +278,31 @@ var tableColumnHide = []; function mapIndx(oldIndex) { - newIndex = oldIndex; - return newIndex; + for(i=0;i'+ cellData +''); + $(td).html (''+ cellData +''); } }, // Icon @@ -322,8 +347,7 @@ function initializeDatatable () { $(td).html (''); } } }, - // Favorite - // {targets: [3], + // Favorite {targets: [mapIndx(4)], 'createdCell': function (td, cellData, rowData, row, col) { if (cellData == 1){ @@ -333,15 +357,13 @@ function initializeDatatable () { } } }, - // Dates - // {targets: [5, 6], + // Dates {targets: [mapIndx(6), mapIndx(7)], 'createdCell': function (td, cellData, rowData, row, col) { $(td).html (translateHTMLcodes (cellData)); } }, - // Random MAC - // {targets: [8], + // Random MAC {targets: [mapIndx(9)], 'createdCell': function (td, cellData, rowData, row, col) { if (cellData == 1){ @@ -351,8 +373,7 @@ function initializeDatatable () { } } }, - // Status color - // {targets: [9], + // Status color {targets: [mapIndx(10)], 'createdCell': function (td, cellData, rowData, row, col) { switch (cellData) { diff --git a/front/js/helper_objects.js b/front/js/helper_objects.js deleted file mode 100644 index ffa85a93..00000000 --- a/front/js/helper_objects.js +++ /dev/null @@ -1 +0,0 @@ -var deviceColumns = {'Icon':4, 'Owner':5} \ No newline at end of file diff --git a/front/maintenance.php b/front/maintenance.php index 1b548656..c4234965 100755 --- a/front/maintenance.php +++ b/front/maintenance.php @@ -761,14 +761,36 @@ function scrollDown() // -------------------------------------------------------- function saveSelectedColumns () { - $.get('php/server/parameters.php?action=set&expireMinutes=525600&value=['+ $('#columnsSelect').val().toString() +']¶meter=Front_Devices_Columns', function(data) { - showMessage(data) + $.get('php/server/parameters.php?action=set&expireMinutes=525600&value=['+ $('#columnsSelect').val().toString() +']¶meter=Front_Devices_Columns_Visible', function(data) { + // save full order of all columns to simplify mapping later on + + colDisplayed = $('#columnsSelect').val(); + + colDefaultOrder = ['0','1','2','3','4','5','6','7','8','9','10','12','13']; + + colNewOrder = colDisplayed; + + for(i = 0; i < colDefaultOrder.length; i++) + { + if(!colDisplayed.includes(colDefaultOrder[i])) + { + colNewOrder.push(colDefaultOrder[i]) + } + } + + console.log(colNewOrder); + + $.get('php/server/parameters.php?action=set&expireMinutes=525600&value=['+ colNewOrder.toString() +']¶meter=Front_Devices_Columns_Order', function(data) { + + showMessage(data); + + }); }); } // -------------------------------------------------------- function initializeSelectedColumns () { - $.get('php/server/parameters.php?action=get&expireMinutes=525600&defaultValue=[0,1,2,3,4,5,6,7,8,9,10,12,13]¶meter=Front_Devices_Columns', function(data) { + $.get('php/server/parameters.php?action=get&expireMinutes=525600&defaultValue=[0,1,2,3,4,5,6,7,8,9,10,12,13]¶meter=Front_Devices_Columns_Visible', function(data) { tableColumnShow = numberArrayFromString(data); diff --git a/front/php/server/devices.php b/front/php/server/devices.php index d6798fea..1f13c5ac 100755 --- a/front/php/server/devices.php +++ b/front/php/server/devices.php @@ -569,50 +569,39 @@ function getDevicesTotals() { function getDevicesList() { global $db; - + // This object is used to map from the old order ( second parameter, first number) to the 3rd parameter (Second number (here initialized to -1)) $columnOrderMapping = array( - array("dev_Name", 0, -1), // 2 - array("dev_Owner", 1, -1), // 5 - array("dev_DeviceType", 2, -1), // 6 - array("dev_Icon", 3, -1), // 0 - array("dev_Favorite", 4, -1), // 7 - array("dev_Group", 5, -1), // 8 - array("dev_FirstConnection", 6, -1), // 9 - array("dev_LastConnection", 7, -1), // 10 - array("dev_LastIP", 8, -1), // 4 - array("dev_MAC", 9, -1), // 11 - array("dev_Status", 10, -1), // 1 - array("dev_MAC_full", 11, -1), // 3 - array("dev_LastIP_orderable", 12, -1), // 12 - array("rowid", 13, -1) // 13 + array("dev_Name", 0, -1), + array("dev_Owner", 1, -1), + array("dev_DeviceType", 2, -1), + array("dev_Icon", 3, -1), + array("dev_Favorite", 4, -1), + array("dev_Group", 5, -1), + array("dev_FirstConnection", 6, -1), + array("dev_LastConnection", 7, -1), + array("dev_LastIP", 8, -1), + array("dev_MAC", 9, -1), + array("dev_Status", 10, -1), + array("dev_MAC_full", 11, -1), + array("dev_LastIP_orderable", 12, -1), + array("rowid", 13, -1) ); // get device columns order - $sql = 'SELECT par_Value FROM Parameters where par_ID = "Front_Devices_Columns"'; + $sql = 'SELECT par_Value FROM Parameters where par_ID = "Front_Devices_Columns_Order"'; $result = $db->query($sql); $row = $result -> fetchArray (SQLITE3_NUM); if($row != NULL && count($row) == 1) { - $displayedColumns = createArray($row[0]); + // ordered columns setting from the maintenance page + $orderedColumns = createArray($row[0]); - // init ordered columns - $index = 0; - foreach ($displayedColumns as $columnIndex) { - - $columnOrderMapping[$columnIndex][2] = $index; - - $index = $index + 1; + // init ordered columns + for($i = 0; $i < count($orderedColumns); $i++) { + $columnOrderMapping[$i][2] = $orderedColumns[$i]; } - - foreach ($columnOrderMapping as $mapping) { - if($mapping[2] == -1) - { - $mapping[2] = $index; - $index = $index + 1; - } - } - } + } // SQL $condition = getDeviceCondition ($_REQUEST['status']); @@ -625,29 +614,36 @@ function getDevicesList() { END AS dev_Status FROM Devices '. $condition; $result = $db->query($sql); - - - + // arrays of rows $tableData = array(); while ($row = $result -> fetchArray (SQLITE3_ASSOC)) { - $tableData['data'][] = array ($row['dev_Name'], - $row['dev_Owner'], - $row['dev_DeviceType'], - $row['dev_Icon'], - $row['dev_Favorite'], - $row['dev_Group'], - formatDate ($row['dev_FirstConnection']), - formatDate ($row['dev_LastConnection']), - $row['dev_LastIP'], - ( in_array($row['dev_MAC'][1], array("2","6","A","E","a","e")) ? 1 : 0), - $row['dev_Status'], - $row['dev_MAC'], // MAC (hidden) - formatIPlong ($row['dev_LastIP']), // IP orderable - $row['rowid'] // Rowid (hidden) - - ); + $defaultOrder = array ($row['dev_Name'], + $row['dev_Owner'], + $row['dev_DeviceType'], + $row['dev_Icon'], + $row['dev_Favorite'], + $row['dev_Group'], + formatDate ($row['dev_FirstConnection']), + formatDate ($row['dev_LastConnection']), + $row['dev_LastIP'], + ( in_array($row['dev_MAC'][1], array("2","6","A","E","a","e")) ? 1 : 0), + $row['dev_Status'], + $row['dev_MAC'], // MAC (hidden) + formatIPlong ($row['dev_LastIP']), // IP orderable + $row['rowid'] // Rowid (hidden) + ); + + $newOrder = array(); + + + for($index = 0; $index < count($columnOrderMapping); $index++) + { + array_push($newOrder, $defaultOrder[$columnOrderMapping[$index][2]]); + } + + $tableData['data'][] = $newOrder; } // Control no rows diff --git a/front/php/server/util.php b/front/php/server/util.php index 520bcc80..8ebdbb68 100755 --- a/front/php/server/util.php +++ b/front/php/server/util.php @@ -79,6 +79,24 @@ function createArray($input){ return $options; } +// ------------------------------------------------------------------------------------------- +// For debugging - Print arrays +function printArray ($array) { + echo '['; + foreach ($array as $val) + { + if(is_array($val)) + { + echo '
'; + printArray($val); + } else + { + echo $val.', '; + } + } + echo ']
'; +} + // ------------------------------------------------------------------------------------------- function formatDate ($date1) { return date_format (new DateTime ($date1) , 'Y-m-d H:i'); diff --git a/front/php/templates/language/en_us.php b/front/php/templates/language/en_us.php index d205b501..09a68d44 100755 --- a/front/php/templates/language/en_us.php +++ b/front/php/templates/language/en_us.php @@ -155,7 +155,7 @@ $lang['en_us'] = array( 'DevDetail_Shortcut_DownAlerts' => 'Down Alerts', 'DevDetail_Tab_Details' => ' Details', 'DevDetail_Tab_Nmap' => ' Nmap', -'DevDetail_Tab_Sessions' => ' Sessions', +'DevDetail_Tab_Sessions' => ' Sessions', 'DevDetail_Tab_Presence' => ' Presence', 'DevDetail_Tab_Events' => ' Events', 'DevDetail_Tab_Pholus' => ' Pholus',
' + headersDefaultOrder[tableColumnOrder[index]] + '