frames view server pagination WIP

This commit is contained in:
Andrew Bauer 2020-11-14 14:19:28 -06:00
parent f88d721ae3
commit 9aa6fea6a3
3 changed files with 99 additions and 49 deletions

View File

@ -14,6 +14,8 @@ if ( empty($_REQUEST['task']) ) {
// query is the only supported task at the moment
} else if ( $_REQUEST['task'] != 'query' ) {
$message = 'Unrecognised task '.$_REQUEST['task'];
} else {
$task = $_REQUEST['task'];
}
if ( empty($_REQUEST['eid']) ) {
@ -54,7 +56,7 @@ if ( isset($_REQUEST['offset']) ) {
$order = (isset($_REQUEST['order']) and (strtolower($_REQUEST['order']) == 'asc')) ? 'ASC' : 'DESC';
// Limit specifies the number of rows to return
$limit = 100;
$limit = 0;
if ( isset($_REQUEST['limit']) ) {
if ( ( !is_int($_REQUEST['limit']) and !ctype_digit($_REQUEST['limit']) ) ) {
ZM\Error('Invalid value for limit: ' . $_REQUEST['limit']);
@ -84,8 +86,12 @@ ajaxResponse($data);
function queryRequest($eid, $search, $advsearch, $sort, $offset, $order, $limit) {
// The table we want our data from
$table = 'Frames';
$data = array(
'total' => 0,
'totalNotFiltered' => 0,
'rows' => array(),
'updated' => preg_match('/%/', DATE_FMT_CONSOLE_LONG) ? strftime(DATE_FMT_CONSOLE_LONG) : date(DATE_FMT_CONSOLE_LONG)
);
// The names of the dB columns in the events table we are interested in
$columns = array('EventId', 'FrameId', 'Type', 'TimeStamp', 'Delta', 'Score');
@ -99,56 +105,64 @@ function queryRequest($eid, $search, $advsearch, $sort, $offset, $order, $limit)
$Monitor = $Event->Monitor();
$values = array();
$likes = array();
$where = 'EventId ='.$eid;
$where = 'WHERE EventId = '.$eid;
// 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) ) {
$sql = 'SELECT * FROM `Frames` '.$where.' ORDER BY '.$sort.' '.$order;
foreach ( $advsearch as $col=>$text ) {
if ( !in_array($col, array_merge($columns, $col_alt)) ) {
ZM\Error("'$col' is not a searchable column name");
continue;
//ZM\Debug('Calling the following sql query: ' .$sql);
$unfiltered_rows = array();
$frame_ids = array();
require_once('includes/Frame.php');
foreach ( dbFetchAll($sql, NULL, $values) as $row ) {
$frame = new ZM\Frame($row);
$frame_ids[] = $frame->Id();
$unfiltered_rows[] = $row;
}
ZM\Debug('Have ' . count($unfiltered_rows) . ' frames matching base filter.');
$filtered_rows = null;
require_once('includes/Filter.php');
if ( count($advsearch) or $search != '' ) {
$search_filter = new ZM\Filter();
$search_filter = $search_filter->addTerm(array('cnj'=>'and', 'attr'=>'Id', 'op'=>'IN', 'val'=>$frame_ids));
// 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) ) {
$terms = array();
foreach ( $advsearch as $col=>$text ) {
$terms[] = array('cnj'=>'and', 'attr'=>$col, 'op'=>'LIKE', 'val'=>$text);
} # end foreach col in advsearch
$terms[0]['obr'] = 1;
$terms[count($terms)-1]['cbr'] = 1;
$search_filter->addTerms($terms);
} else if ( $search != '' ) {
$search = '%' .$search. '%';
$terms = array();
foreach ( $columns as $col ) {
$terms[] = array('cnj'=>'or', 'attr'=>$col, 'op'=>'LIKE', 'val'=>$search);
}
// Don't use wildcards on advanced search
//$text = '%' .$text. '%';
array_push($likes, $col.' LIKE ?');
array_push($query['values'], $text);
}
$wherevalues = $query['values'];
$where = ' WHERE (' .implode(' OR ', $likes). ')';
$terms[0]['obr'] = 1;
$terms[0]['cnj'] = 'and';
$terms[count($terms)-1]['cbr'] = 1;
$search_filter = $search_filter->addTerms($terms, array('obr'=>1, 'cbr'=>1, 'op'=>'OR'));
} # end if search
} else if ( $search != '' ) {
$search = '%' .$search. '%';
foreach ( $columns as $col ) {
array_push($likes, $col.' LIKE ?');
array_push($query['values'], $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);
$data['totalNotFiltered'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table, 'Total');
if ( $search != '' || count($advsearch) ) {
$data['total'] = dbFetchOne('SELECT count(*) AS Total FROM ' .$table.$where , 'Total', $wherevalues);
$sql = 'SELECT * FROM `Frames` WHERE '.$search_filter->sql().' ORDER BY ' .$sort. ' ' .$order;
$filtered_rows = dbFetchAll($sql);
ZM\Debug('Have ' . count($filtered_rows) . ' frames matching search filter.');
} else {
$data['total'] = $data['totalNotFiltered'];
}
$filtered_rows = $unfiltered_rows;
} # end if search_filter->terms() > 1
$returned_rows = array();
$results = dbFetchAll($query['sql'], NULL, $query['values']);
if ( !$results ) {
return $data;
}
foreach ( $results as $row ) {
foreach ( array_slice($filtered_rows, $offset, $limit) as $row ) {
if ( ZM_WEB_LIST_THUMBS ) {
$base_img_src = '?view=image&fid=' .$row['FrameId'];
$ratio_factor = $Monitor->ViewHeight() / $Monitor->ViewWidth();
$ratio_factor = $Monitor->ViewHeight() / $Monitor->ViewWidth();
$thmb_width = ZM_WEB_LIST_THUMB_WIDTH ? 'width='.ZM_WEB_LIST_THUMB_WIDTH : '';
$thmb_height = 'height="'.( ZM_WEB_LIST_THUMB_HEIGHT ? ZM_WEB_LIST_THUMB_HEIGHT : ZM_WEB_LIST_THUMB_WIDTH*$ratio_factor ) .'"';
$thmb_fn = 'filename=' .$Event->MonitorId(). '_' .$row['EventId']. '_' .$row['FrameId']. '.jpg';
@ -156,10 +170,20 @@ function queryRequest($eid, $search, $advsearch, $sort, $offset, $order, $limit)
$full_img_src = join('&', array_filter(array($base_img_src, $thmb_fn)));
$frame_src = '?view=frame&eid=' .$row['EventId']. '&fid=' .$row['FrameId'];
$row['imgHtml'] = '<td class="colThumbnail zoom"><img src="' .$img_src. '" '.$thmb_width. ' ' .$thmb_height. 'img_src="' .$img_src. '" full_img_src="' .$full_img_src. '"></td>'.PHP_EOL;
$row['Thumbnail'] = '<img src="' .$img_src. '" '.$thmb_width. ' ' .$thmb_height. 'img_src="' .$img_src. '" full_img_src="' .$full_img_src. '">';
}
$returned_rows[] = $row;
}
} # end foreach row matching search
$data['rows'] = $returned_rows;
# totalNotFiltered must equal total, except when either search bar has been used
$data['totalNotFiltered'] = count($unfiltered_rows);
if ( $search != '' || count($advsearch) ) {
$data['total'] = count($filtered_rows);
} else {
$data['total'] = $data['totalNotFiltered'];
}
return $data;
}

View File

@ -1,3 +1,6 @@
var backBtn = $j('#backBtn');
var table = $j('#framesTable');
// Called by bootstrap-table to retrieve zm frame data
function ajaxRequest(params) {
if ( params.data && params.data.filter ) {
@ -8,6 +11,8 @@ function ajaxRequest(params) {
.done(function(data) {
var rows = processRows(data.rows);
// rearrange the result into what bootstrap-table expects
console.log('Total: '+data.total);
console.log('TotalnotFiltered: '+data.totalNotFiltered);
params.success({total: data.total, totalNotFiltered: data.totalNotFiltered, rows: rows});
})
.fail(logAjaxFail);
@ -16,6 +21,7 @@ function ajaxRequest(params) {
function processRows(rows) {
$j.each(rows, function(ndx, row) {
// WIP: process each row here
// VERIFY: Might not need to do anything here for the frames table
});
return rows;
}
@ -56,9 +62,10 @@ function detailFormatter(index, row, $detail) {
})
.fail(logAjaxFail);
}
function initPage() {
var backBtn = $j('#backBtn');
var table = $j('#framesTable');
// Remove the thumbnail column from the DOM if thumbnails are off globally
if ( !WEB_LIST_THUMBS ) $j('th[data-field="Thumbnail"]').remove();
// Init the bootstrap-table
table.bootstrapTable({icons: icons});
@ -93,6 +100,24 @@ function initPage() {
evt.preventDefault();
window.location.reload(true);
});
// Update table links each time after new data is loaded
table.on('post-body.bs.table', function(data) {
var type_ndx = $j('#framesTable tr th').filter(function() {
return $j(this).text().trim() == 'Type';
}).index();
$j('#framesTable tr').each(function(ndx, row) {
var row = $j(row);
var type = row.find('td').eq(type_ndx).text().trim();
row.addClass(type.toLowerCase());
});
var thumb_ndx = $j('#framesTable tr th').filter(function() {
return $j(this).text().trim() == 'Thumbnail';
}).index();
table.find("tr td:nth-child(" + (thumb_ndx+1) + ")").addClass('colThumbnail zoom');
});
}
$j(document).ready(function() {

View File

@ -1 +1,2 @@
var eid = <?php echo validInt($_REQUEST['eid']) ?>;
var WEB_LIST_THUMBS = <?php echo ZM_WEB_LIST_THUMBS?'true':'false' ?>;