fix: map filter sort fields to query columns in Filter::Events refs #6033

This commit is contained in:
Jash committed 2026-11-16 00:42:29 +05:30
1 parent 7b00682d40
commit 21e22c1ea4
2 files changed
+68 -1

No files matched your search

+46
View File
@@ -0,0 +1,46 @@
<?php
// Filter::Events() must turn the filter's sort field into a column of its
// Events/Monitors/Tags query. The filter editor offers MonitorName and Tags,
// which are not Events columns, so passing them through unchanged made the
// query fail ("Unknown column 'MonitorName' in 'ORDER BY'") and the report
// view showed no events.
// Run: php tests/php/test_filter_events_sort.php
$src = file_get_contents(__DIR__.'/../../web/includes/Filter.php');
function extract_method($src, $name) {
$sig = strpos($src, 'function '.$name.'(');
if ($sig === false) return null;
$brace = strpos($src, '{', $sig);
$depth = 0;
for ($i = $brace; $i < strlen($src); $i++) {
if ($src[$i] === '{') $depth++;
elseif ($src[$i] === '}') {
$depth--;
if ($depth === 0) return substr($src, $sig, $i - $sig + 1);
}
}
return null;
}
$fail = 0;
function check($label, $ok) {
global $fail;
echo ($ok ? ' ok ' : ' FAIL ').$label."\n";
if (!$ok) $fail++;
}
$method = extract_method($src, 'events_sort_sql');
check('Filter has events_sort_sql()', $method !== null);
if ($method === null) exit(1);
eval(preg_replace('/^function events_sort_sql\(/', 'function events_sort_sql(', $method));
check('MonitorName sorts by the monitor name', events_sort_sql('MonitorName') === 'M.Name');
check('Tags sorts by the event\'s tag names', strpos(events_sort_sql('Tags'), 'GROUP_CONCAT') !== false);
check('Name is the event name, not ambiguous', events_sort_sql('Name') === 'E.Name');
check('plain columns are qualified', events_sort_sql('StartDateTime') === 'E.StartDateTime');
check('NULLs-last form keeps its shape',
events_sort_sql('EndDateTime IS NOT NULL, EndDateTime') === 'E.EndDateTime IS NOT NULL, E.EndDateTime');
check('no sort field gives no sort', events_sort_sql('') === '');
exit($fail ? 1 : 0);
+22 -1
View File
@@ -855,6 +855,26 @@ class Filter extends ZM_Object {
return $this;
}
// The sort field names a filter attribute; map it to a column of the Events() query,
// as ZoneMinder::Filter does in Perl. MonitorName and Tags are not Events columns.
public static function events_sort_sql($sort_field) {
if (!$sort_field) return '';
$parts = array();
foreach (explode(',', $sort_field) as $part) {
$parts[] = preg_replace_callback('/^\s*([A-Za-z][A-Za-z0-9_]*)/', function($m) {
switch ($m[1]) {
case 'MonitorName': return 'M.Name';
case 'Tags': return '(SELECT GROUP_CONCAT(Name) FROM Tags WHERE Id IN (SELECT TagId FROM Events_Tags WHERE Events_Tags.EventId=E.Id))';
case 'StartTime': return 'E.StartDateTime';
case 'EndTime': return 'E.EndDateTime';
case 'Secs': return 'E.Length';
default: return 'E.'.$m[1];
}
}, trim($part));
}
return implode(', ', $parts);
}
function Events() {
$events = array();
if (!$this->test_pre_sql_conditions()) {
@@ -864,7 +884,8 @@ class Filter extends ZM_Object {
$where = $this->sql() ? ' WHERE ('.$this->sql().')' : '';
// $where = ' WHERE ( T.Name = "Bird" )';
$sort = $this->sort_field() ? $this->sort_field() .' '.($this->sort_asc() ? 'ASC' : 'DESC') : '';
$sort_sql = self::events_sort_sql($this->sort_field());
$sort = $sort_sql ? $sort_sql.' '.($this->sort_asc() ? 'ASC' : 'DESC') : '';
$col_str = '
E.*,