From e4a1bacbbf03219bd038b0b2db5857e66158ca20 Mon Sep 17 00:00:00 2001 From: Andrew Bauer Date: Thu, 24 Sep 2020 11:05:34 -0500 Subject: [PATCH] get adv search working in newlog view --- web/ajax/newlog.php | 46 +++++++++++++++++++--------- web/skins/classic/views/js/newlog.js | 11 +++---- 2 files changed, 36 insertions(+), 21 deletions(-) diff --git a/web/ajax/newlog.php b/web/ajax/newlog.php index f97a6b649..9e6bd9585 100644 --- a/web/ajax/newlog.php +++ b/web/ajax/newlog.php @@ -21,10 +21,11 @@ $columns = array('TimeKey', 'Component', 'ServerId', 'Pid', 'Code', 'Message', ' $col_alt = array('DateTime', 'Server'); // Search contains a user entered string to search on -$search = ''; -if ( isset($_REQUEST['search']) ) { - $search = $_REQUEST['search']; -} +$search = isset($_REQUEST['search']) ? $_REQUEST['search'] : ''; + +// Advanced search contains an array of "column name" => "search text" pairs +// Bootstrap table sends json_ecoded array, which we must decode +$advsearch = isset($_REQUEST['filter']) ? json_decode($_REQUEST['filter'], JSON_OBJECT_AS_ARRAY) : array(); // Sort specifies the name of the column to sort on $sort = 'TimeKey'; @@ -65,7 +66,26 @@ $data = array(); $query = array(); $query['values'] = array(); $likes = array(); -if ( $search != '' ) { +$where = ''; +// There are two search bars in the log view, normal and advanced +// Making an exuctive decision to ignore the normal search, when advanced search is in use +// Alternatively we could try to do both +if ( count($advsearch) ) { + + foreach ( $advsearch as $col=>$text ) { + if ( !in_array($col, array_merge($columns, $col_alt)) ) { + ZM\Error("'$col' is not a sortable column name"); + continue; + } + $text = '%' .$text. '%'; + array_push($likes, $col.' LIKE ?'); + array_push($query['values'], $text); + } + $wherevalues = $query['values']; + $where = ' WHERE (' .implode(' OR ', $likes). ')'; + +} else if ( $search != '' ) { + $search = '%' .$search. '%'; foreach ( $columns as $col ) { array_push($likes, $col.' LIKE ?'); @@ -73,20 +93,18 @@ if ( $search != '' ) { } $wherevalues = $query['values']; $where = ' WHERE (' .implode(' OR ', $likes). ')'; - $query['sql'] = 'SELECT ' .$col_str. ' FROM `' .$table. '` ' .$where. ' ORDER BY ' .$sort. ' ' .$order. ' LIMIT ?, ?'; - array_push($query['values'], $offset, $limit); -} else { - $query['sql'] = 'SELECT ' .$col_str. ' FROM `' .$table. '` ORDER BY ' .$sort. ' ' .$order. ' LIMIT ?, ?'; - $query['values'] = array($offset, $limit); -} +} + +$query['sql'] = 'SELECT ' .$col_str. ' FROM `' .$table. '` ' .$where. ' ORDER BY ' .$sort. ' ' .$order. ' LIMIT ?, ?'; +array_push($query['values'], $offset, $limit); //ZM\Warning('Calling the following sql query: ' .$query['sql']); $data['totalNotFiltered'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table, 'Total'); -if ( $search == '' ) { - $data['total'] = $data['totalNotFiltered']; -} else { +if ( $search != '' || count($advsearch) ) { $data['total'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table.$where , 'Total', $wherevalues); +} else { + $data['total'] = $data['totalNotFiltered']; } if ( !$Servers ) diff --git a/web/skins/classic/views/js/newlog.js b/web/skins/classic/views/js/newlog.js index 80cc8e82c..4896644d0 100644 --- a/web/skins/classic/views/js/newlog.js +++ b/web/skins/classic/views/js/newlog.js @@ -28,14 +28,11 @@ var params = // Called by bootstrap-table to retrieve zm log data function ajaxRequest(params) { $j.getJSON(thisUrl + '?view=request&request=newlog&task=query', params.data) - .done(function(res) { - //console.log('total: ' + res.total); - //console.log('totalNotFiltered: ' + res.totalNotFiltered); - console.log(JSON.stringify(params)); + .done(function(data) { + //console.log('Ajax parameters: ' + JSON.stringify(params)); // rearrange the result into what bootstrap-table expects - var data = {total: res.total, totalNotFiltered: res.totalNotFiltered, rows: res.rows}; - params.success(data); - updateHeaderStats(res); + params.success({total: data.total, totalNotFiltered: data.totalNotFiltered, rows: data.rows}); + updateHeaderStats(data); }) .fail(logAjaxFail); }