diff --git a/web/skins/classic/views/events.php b/web/skins/classic/views/events.php index a70fa8d19..c8e033fc9 100644 --- a/web/skins/classic/views/events.php +++ b/web/skins/classic/views/events.php @@ -1,331 +1,213 @@ "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 = 'StartTime'; +if ( isset($_REQUEST['sort']) ) { + $sort = $_REQUEST['sort']; } -$filter = isset($_REQUEST['filter_id']) ? new ZM\Filter($_REQUEST['filter_id']) : new ZM\Filter(); -if ( isset($_REQUEST['filter'])) { - $filter->set($_REQUEST['filter']); -} - -parseSort(); - -$filterQuery = $filter->querystring(); -ZM\Debug('Filter '.print_r($filter, true)); - -if ( $filter->sql() ) { - $eventsSql .= ' AND ('.$filter->sql().')'; -} else { - ZM\Warning('No filters'); - exit; -} -$eventsSql .= ' ORDER BY '.$sortColumn.' '.$sortOrder; -if ( $sortColumn != 'E.Id' ) $eventsSql .= ',E.Id '.$sortOrder; - -$page = isset($_REQUEST['page']) ? validInt($_REQUEST['page']) : 0; -$limit = isset($_REQUEST['limit']) ? validInt($_REQUEST['limit']) : $filter['limit']; - -if ( $_POST ) { - // I think this is basically so that a refresh doesn't repost - ZM\Debug('Redirecting to ' . $_SERVER['REQUEST_URI']); - header('Location: ?view=' . $view.htmlspecialchars_decode($filterQuery).htmlspecialchars_decode($sortQuery).$limitQuery.'&page='.$page); - exit(); -} - -$failed = !$filter->test_pre_sql_conditions(); -if ( $failed ) { - ZM\Debug('Pre conditions failed, not doing sql'); -} - -$results = $failed ? null : dbQuery($eventsSql); - -$nEvents = $results ? $results->rowCount() : 0; -if ( ! $results ) { - global $error_message; - $error_message = dbError($eventsSql); -} -ZM\Debug("Pre conditions succeeded sql return $nEvents events"); - -if ( !empty($limit) && ($nEvents > $limit) ) { - $nEvents = $limit; -} -$pages = (int)ceil($nEvents/ZM_WEB_EVENTS_PER_PAGE); -#Debug("Page $page Limit $limit #vents: $nEvents pages: $pages "); -if ( !empty($page) ) { - if ( $page < 0 ) - $page = 1; - else if ( $pages and ( $page > $pages ) ) - $page = $pages; - - $limitStart = (($page-1)*ZM_WEB_EVENTS_PER_PAGE); - if ( empty($limit) ) { - $limitAmount = ZM_WEB_EVENTS_PER_PAGE; +// Offset specifies the starting row to return, used for pagination +$offset = 0; +if ( isset($_REQUEST['offset']) ) { + if ( ( !is_int($_REQUEST['offset']) and !ctype_digit($_REQUEST['offset']) ) ) { + ZM\Error('Invalid value for offset: ' . $_REQUEST['offset']); } else { - $limitLeft = $limit - $limitStart; - $limitAmount = ($limitLeft>ZM_WEB_EVENTS_PER_PAGE)?ZM_WEB_EVENTS_PER_PAGE:$limitLeft; + $offset = $_REQUEST['offset']; } - $eventsSql .= ' LIMIT '.$limitStart.', '.$limitAmount; -} else if ( !empty($limit) ) { - $eventsSql .= ' LIMIT 0, '.$limit; } -$maxShortcuts = 5; +// Order specifies the sort direction, either asc or desc +$order = (isset($_REQUEST['order']) and (strtolower($_REQUEST['order']) == 'asc')) ? 'ASC' : 'DESC'; -$focusWindow = true; - -$storage_areas = ZM\Storage::find(); -$StorageById = array(); -foreach ( $storage_areas as $S ) { - $StorageById[$S->Id()] = $S; +// Limit specifies the number of rows to return +$limit = 100; +if ( isset($_REQUEST['limit']) ) { + if ( ( !is_int($_REQUEST['limit']) and !ctype_digit($_REQUEST['limit']) ) ) { + ZM\Error('Invalid value for limit: ' . $_REQUEST['limit']); + } else { + $limit = $_REQUEST['limit']; + } } -xhtmlHeaders(__FILE__, translate('Events')); -getBodyTopHTML(); +// +// MAIN LOOP +// -?> - -
- -
- - - - - - - - - - - -
+switch ( $task ) { + case 'archive' : + case 'unarchive' : + foreach ( $eids as $eid ) archiveRequest($task, $eid); + break; + case 'delete' : + foreach ( $eids as $eid ) $data[] = deleteRequest($eid); + break; + case 'query' : + $data = queryRequest($search, $advsearch, $sort, $offset, $order, $limit); + break; + default : + ZM\Fatal("Unrecognised task '$task'"); +} // end switch task - -
- - - - - - - - - - - - - - - - - - - - 1 ) { -?> - - - - - - - - - -test_post_sql_conditions($event) ) { - $event->remove_from_cache(); - continue; - } - $events[] = $event; - if ( $limit and (count($events) >= $limit) ) { - break; - } - ZM\Debug("Have " . count($events) . " events, limit $limit"); +function archiveRequest($task, $eid) { + $archiveVal = ($task == 'archive') ? 1 : 0; + dbQuery( + 'UPDATE Events SET Archived = ? WHERE Id = ?', + array($archiveVal, $eid) + ); +} + +function deleteRequest($eid) { + $message = array(); + $event = new ZM\Event($eid); + if ( !$event->Id() ) { + $message[] = array($eid=>'Event not found.'); + } else if ( $event->Archived() ) { + $message[] = array($eid=>'Event is archived, cannot delete it.'); + } else { + $event->delete(); } - foreach ( $events as $event ) { + + return $message; +} - $scale = max(reScale(SCALE_BASE, $event->DefaultScale(), ZM_WEB_DEFAULT_SCALE), SCALE_BASE); -?> - Archived() ? ' class="archived"' : '' ?>> - - +function queryRequest($search, $advsearch, $sort, $offset, $order, $limit) { + // Put server pagination code here + // The table we want our data from + $table = 'Events'; - + // The names of the dB columns in the log table we are interested in + $columns = array('Id', 'MonitorId', 'StorageId', 'Name', 'Cause', 'StartTime', 'EndTime', 'Length', 'Frames', 'AlarmFrames', 'TotScore', 'AvgScore', 'MaxScore', 'Archived', 'Emailed', 'Notes', 'DiskSpace'); - - - - - - - - - - - - - - 1 ) { -?> - - -DiskSpace(); -?> - -'; - $imgSrc = $event->getThumbnailSrc(array(),'&'); - $streamSrc = $event->getStreamSrc(array( - 'mode'=>'jpeg', 'scale'=>$scale, 'maxfps'=>ZM_WEB_VIDEO_MAXFPS, 'replay'=>'single', 'rate'=>'400'), '&'); - $imgHtml = ''. validHtmlStr('Event '.$event->Id()) .''; - echo ''.$imgHtml.''; - echo ''; - } // end if ZM_WEB_LIST_THUMBS -?> - - - - - - - -1 ) { -?> - -$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 ?'); + 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); + + ZM\Warning('Calling the following sql query: ' .$query['sql']); + + $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); + } else { + $data['total'] = $data['totalNotFiltered']; + } + + $storage_areas = ZM\Storage::find(); + $StorageById = array(); + foreach ( $storage_areas as $S ) { + $StorageById[$S->Id()] = $S; + } + + $monitor_names = ZM\Monitor::find(); + $MonitorById = array(); + foreach ( $monitor_names as $S ) { + $MonitorById[$S->Id()] = $S; + } + + $rows = array(); + foreach ( dbFetchAll($query['sql'], NULL, $query['values']) as $row ) { + $event = new ZM\Event($row['Id']); + $scale = intval(5*100*ZM_WEB_LIST_THUMB_WIDTH / $event->Width()); + $imgSrc = $event->getThumbnailSrc(array(),'&'); + $streamSrc = $event->getStreamSrc(array( + 'mode'=>'jpeg', 'scale'=>$scale, 'maxfps'=>ZM_WEB_VIDEO_MAXFPS, 'replay'=>'single', 'rate'=>'400'), '&'); + + // Modify the row data as needed + $row['imgHtml'] = '' .validHtmlStr('Event ' .$event->Id()). ''; + $row['Name'] = validHtmlStr($row['Name']); + $row['Archived'] = $row['Archived'] ? translate('Yes') : translate('No'); + $row['Emailed'] = $row['Emailed'] ? translate('Yes') : translate('No'); + $row['Monitor'] = ( $row['MonitorId'] and isset($MonitorById[$row['MonitorId']]) ) ? $MonitorById[$row['MonitorId']]->Name() : ''; + $row['Cause'] = validHtmlStr($row['Cause']); + $row['StartTime'] = strftime(STRF_FMT_DATETIME_SHORTER, strtotime($row['StartTime'])); + $row['EndTime'] = strftime(STRF_FMT_DATETIME_SHORTER, strtotime($row['StartTime'])); + $row['Length'] = gmdate('H:i:s', $row['Length'] ); + $row['Storage'] = ( $row['StorageId'] and isset($StorageById[$row['StorageId']]) ) ? $StorageById[$row['StorageId']]->Name() : 'Default'; + $row['Notes'] = htmlspecialchars($row['Notes']); + $row['DiskSpace'] = human_filesize($row['DiskSpace']); + $rows[] = $row; + } + $data['rows'] = $rows; + $data['updated'] = preg_match('/%/', DATE_FMT_CONSOLE_LONG) ? strftime(DATE_FMT_CONSOLE_LONG) : date(DATE_FMT_CONSOLE_LONG); + + return $data; } ?> - - - - - - - - - -
-
-