diff --git a/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm b/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm index 22bcb0062..fe54e5a86 100644 --- a/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm +++ b/scripts/ZoneMinder/lib/ZoneMinder/Filter.pm @@ -43,8 +43,8 @@ our @ISA = qw(Exporter ZoneMinder::Base); # will save memory. our %EXPORT_TAGS = ( 'functions' => [ qw( - ) ] -); + ) ] + ); push( @{$EXPORT_TAGS{all}}, @{$EXPORT_TAGS{$_}} ) foreach keys %EXPORT_TAGS; our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); @@ -88,7 +88,7 @@ sub load { if ( ! $data ) { Error( "Failure to load Filter record for $$self{Id}: Reason: " . $$self{dbh}->errstr ); } else { - Debug( 3, "Loaded Filter $$self{Id}" ); + Debug( 3, "Loaded Filter $$self{Id}" ); } # end if } # end if ! $data if ( $data and %$data ) { @@ -130,6 +130,8 @@ sub find { my $filter = new ZoneMinder::Filter( $$db_filter{Id}, $db_filter ); push @results, $filter; } # end while + $sth->finish(); + return @results; } @@ -176,78 +178,74 @@ sub Sql { my $self = $_[0]; if ( ! $$self{Sql} ) { my $filter_expr = ZoneMinder::General::jsonDecode( $self->{Query} ); - my $sql = "SELECT E.Id, - E.MonitorId, + my $sql = "SELECT E.*, + unix_timestamp(E.StartTime) as Time, M.Name as MonitorName, M.DefaultRate, - M.DefaultScale, - E.Name, - E.Cause, - E.Notes, - E.StartTime, - unix_timestamp(E.StartTime) as Time, - E.Length, - E.Frames, - E.AlarmFrames, - E.TotScore, - E.AvgScore, - E.MaxScore, - E.Archived, - E.Videoed, - E.Uploaded, - E.Emailed, - E.Messaged, - E.Executed + M.DefaultScale FROM Events as E INNER JOIN Monitors as M on M.Id = E.MonitorId "; $self->{Sql} = ''; if ( $filter_expr->{terms} ) { - for ( my $i = 0; $i < @{$filter_expr->{terms}}; $i++ ) { - if ( exists($filter_expr->{terms}[$i]->{cnj}) ) { - $self->{Sql} .= " ".$filter_expr->{terms}[$i]->{cnj}." "; + foreach my $term ( @{$filter_expr->{terms}} ) { + + if ( exists($term->{cnj}) ) { + $self->{Sql} .= " ".$term->{cnj}." "; } - if ( exists($filter_expr->{terms}[$i]->{obr}) ) { - $self->{Sql} .= " ".str_repeat( "(", $filter_expr->{terms}[$i]->{obr} )." "; + if ( exists($term->{obr}) ) { + $self->{Sql} .= " ".str_repeat( "(", $term->{obr} )." "; } - my $value = $filter_expr->{terms}[$i]->{val}; + my $value = $term->{val}; my @value_list; - if ( $filter_expr->{terms}[$i]->{attr} ) { - if ( $filter_expr->{terms}[$i]->{attr} =~ /^Monitor/ ) { - my ( $temp_attr_name ) = $filter_expr->{terms}[$i]->{attr} =~ /^Monitor(.+)$/; + if ( $term->{attr} ) { + if ( $term->{attr} =~ /^Monitor/ ) { + my ( $temp_attr_name ) = $term->{attr} =~ /^Monitor(.+)$/; $self->{Sql} .= "M.".$temp_attr_name; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DateTime' ) { + } elsif ( $term->{attr} =~ /^Server/ ) { + $self->{Sql} .= "M.".$term->{attr}; + } elsif ( $term->{attr} eq 'DateTime' ) { $self->{Sql} .= "E.StartTime"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Date' ) { + } elsif ( $term->{attr} eq 'Date' ) { $self->{Sql} .= "to_days( E.StartTime )"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Time' ) { + } elsif ( $term->{attr} eq 'Time' ) { $self->{Sql} .= "extract( hour_second from E.StartTime )"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Weekday' ) { + } elsif ( $term->{attr} eq 'Weekday' ) { $self->{Sql} .= "weekday( E.StartTime )"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DiskPercent' ) { + } elsif ( $term->{attr} eq 'DiskPercent' ) { $self->{Sql} .= "zmDiskPercent"; $self->{HasDiskPercent} = !undef; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DiskBlocks' ) { + } elsif ( $term->{attr} eq 'DiskBlocks' ) { $self->{Sql} .= "zmDiskBlocks"; $self->{HasDiskBlocks} = !undef; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'SystemLoad' ) { + } elsif ( $term->{attr} eq 'SystemLoad' ) { $self->{Sql} .= "zmSystemLoad"; $self->{HasSystemLoad} = !undef; } else { - $self->{Sql} .= "E.".$filter_expr->{terms}[$i]->{attr}; + $self->{Sql} .= "E.".$term->{attr}; } ( my $stripped_value = $value ) =~ s/^["\']+?(.+)["\']+?$/$1/; foreach my $temp_value ( split( /["'\s]*?,["'\s]*?/, $stripped_value ) ) { - if ( $filter_expr->{terms}[$i]->{attr} =~ /^Monitor/ ) { + if ( $term->{attr} =~ /^Monitor/ ) { $value = "'$temp_value'"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Name' - || $filter_expr->{terms}[$i]->{attr} eq 'Cause' - || $filter_expr->{terms}[$i]->{attr} eq 'Notes' + } elsif ( $term->{attr} eq 'ServerId' ) { + if ( $temp_value eq 'ZM_SERVER_ID' ) { + $value = "'$Config{ZM_SERVER_ID}'"; + # This gets used later, I forget for what + $$self{Server} = new ZoneMinder::Server( $Config{ZM_SERVER_ID} ); + } else { + $value = "'$temp_value'"; + # This gets used later, I forget for what + $$self{Server} = new ZoneMinder::Server( $temp_value ); + } + } elsif ( $term->{attr} eq 'Name' + || $term->{attr} eq 'Cause' + || $term->{attr} eq 'Notes' ) { $value = "'$temp_value'"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'DateTime' ) { + } elsif ( $term->{attr} eq 'DateTime' ) { $value = DateTimeToSQL( $temp_value ); if ( !$value ) { Error( "Error parsing date/time '$temp_value', " @@ -255,7 +253,7 @@ sub Sql { return; } $value = "'$value'"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Date' ) { + } elsif ( $term->{attr} eq 'Date' ) { $value = DateTimeToSQL( $temp_value ); if ( !$value ) { Error( "Error parsing date/time '$temp_value', " @@ -263,7 +261,7 @@ sub Sql { return; } $value = "to_days( '$value' )"; - } elsif ( $filter_expr->{terms}[$i]->{attr} eq 'Time' ) { + } elsif ( $term->{attr} eq 'Time' ) { $value = DateTimeToSQL( $temp_value ); if ( !$value ) { Error( "Error parsing date/time '$temp_value', " @@ -277,21 +275,21 @@ sub Sql { push( @value_list, $value ); } # end foreach temp_value } # end if has an attr - if ( $filter_expr->{terms}[$i]->{op} ) { - if ( $filter_expr->{terms}[$i]->{op} eq '=~' ) { + if ( $term->{op} ) { + if ( $term->{op} eq '=~' ) { $self->{Sql} .= " regexp $value"; - } elsif ( $filter_expr->{terms}[$i]->{op} eq '!~' ) { + } elsif ( $term->{op} eq '!~' ) { $self->{Sql} .= " not regexp $value"; - } elsif ( $filter_expr->{terms}[$i]->{op} eq '=[]' ) { + } elsif ( $term->{op} eq '=[]' ) { $self->{Sql} .= " in (".join( ",", @value_list ).")"; - } elsif ( $filter_expr->{terms}[$i]->{op} eq '!~' ) { + } elsif ( $term->{op} eq '!~' ) { $self->{Sql} .= " not in (".join( ",", @value_list ).")"; } else { - $self->{Sql} .= " ".$filter_expr->{terms}[$i]->{op}." $value"; + $self->{Sql} .= " ".$term->{op}." $value"; } } # end if has an operator - if ( exists($filter_expr->{terms}[$i]->{cbr}) ) { - $self->{Sql} .= " ".str_repeat( ")", $filter_expr->{terms}[$i]->{cbr} )." "; + if ( exists($term->{cbr}) ) { + $self->{Sql} .= " ".str_repeat( ")", $term->{cbr} )." "; } } # end foreach term } # end if terms @@ -308,22 +306,22 @@ sub Sql { } my @auto_terms; if ( $self->{AutoArchive} ) { - push( @auto_terms, "E.Archived = 0" ) + push @auto_terms, "E.Archived = 0"; } if ( $self->{AutoVideo} ) { - push( @auto_terms, "E.Videoed = 0" ) + push @auto_terms, "E.Videoed = 0"; } if ( $self->{AutoUpload} ) { - push( @auto_terms, "E.Uploaded = 0" ) + push @auto_terms, "E.Uploaded = 0"; } if ( $self->{AutoEmail} ) { - push( @auto_terms, "E.Emailed = 0" ) + push @auto_terms, "E.Emailed = 0"; } if ( $self->{AutoMessage} ) { - push( @auto_terms, "E.Messaged = 0" ) + push @auto_terms, "E.Messaged = 0"; } if ( $self->{AutoExecute} ) { - push( @auto_terms, "E.Executed = 0" ) + push @auto_terms, "E.Executed = 0"; } if ( @auto_terms ) { $sql .= " and ( ".join( " or ", @auto_terms )." )"; diff --git a/web/includes/functions.php b/web/includes/functions.php index 53a1c23fd..20def7487 100644 --- a/web/includes/functions.php +++ b/web/includes/functions.php @@ -1221,6 +1221,9 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&' ) { case 'MonitorName': $filter['sql'] .= 'M.'.preg_replace( '/^Monitor/', '', $filter['terms'][$i]['attr'] ); break; + case 'ServerId': + $filter['sql'] .= 'M.ServerId'; + break; case 'DateTime': $filter['sql'] .= "E.StartTime"; break; @@ -1266,6 +1269,13 @@ function parseFilter( &$filter, $saveToSession=false, $querySep='&' ) { case 'Notes': $value = dbEscape($value); break; + case 'ServerId': + if ( $value == 'ZM_SERVER_ID' ) { + $value = ZM_SERVER_ID; + } else { + $value = dbEscape($value); + } + break; case 'DateTime': $value = "'".strftime( STRF_FMT_DATETIME_DB, strtotime( $value ) )."'"; break; diff --git a/web/lang/en_gb.php b/web/lang/en_gb.php index 906548063..dfc0b130a 100644 --- a/web/lang/en_gb.php +++ b/web/lang/en_gb.php @@ -125,8 +125,7 @@ $SLANG = array( 'AttrMaxScore' => 'Max. Score', 'AttrMonitorId' => 'Monitor Id', 'AttrMonitorName' => 'Monitor Name', - 'AttrServerId' => 'Server Id', - 'AttrServerName' => 'Server Name', + 'AttrServer' => 'Server', 'AttrName' => 'Name', 'AttrNotes' => 'Notes', 'AttrSystemLoad' => 'System Load', diff --git a/web/skins/classic/views/filter.php b/web/skins/classic/views/filter.php index 390e94ee1..43b651ea9 100644 --- a/web/skins/classic/views/filter.php +++ b/web/skins/classic/views/filter.php @@ -97,8 +97,7 @@ $attrTypes = array( 'DiskPercent' => translate('AttrDiskPercent'), 'DiskBlocks' => translate('AttrDiskBlocks'), 'SystemLoad' => translate('AttrSystemLoad'), - 'ServerId' => translate('AttrServerId'), - 'ServerName' => translate('AttrServerName'), + 'ServerId' => translate('AttrServer'), ); $opTypes = array( '=' => translate('OpEq'), @@ -239,6 +238,18 @@ for ( $i = 0; isset($_REQUEST['filter']) && $i < count($_REQUEST['filter']['term ?> + + +