Update Filter object, specifying fields and using parent's find methods

This commit is contained in:
Isaac Connor 2020-05-07 10:59:06 -04:00
parent 58fae48736
commit 4bd0cca4b8
1 changed files with 36 additions and 42 deletions

View File

@ -49,49 +49,34 @@ use ZoneMinder::Database qw(:all);
require ZoneMinder::Storage;
require ZoneMinder::Server;
sub Name {
if ( @_ > 1 ) {
$_[0]{Name} = $_[1];
}
return $_[0]{Name};
} # end sub Path
use vars qw/ $table $primary_key %fields /;
$table = 'Users';
$primary_key = 'Id';
sub find {
shift if $_[0] eq 'ZoneMinder::Filter';
my %sql_filters = @_;
my $sql = 'SELECT * FROM Filters';
my @sql_filters;
my @sql_values;
if ( exists $sql_filters{Name} ) {
push @sql_filters , ' Name = ? ';
push @sql_values, $sql_filters{Name};
}
$sql .= ' WHERE ' . join(' AND ', @sql_filters ) if @sql_filters;
$sql .= ' LIMIT ' . $sql_filters{limit} if $sql_filters{limit};
my $sth = $ZoneMinder::Database::dbh->prepare_cached($sql)
or Fatal("Can't prepare '$sql': ".$ZoneMinder::Database::dbh->errstr());
my $res = $sth->execute(@sql_values)
or Fatal("Can't execute '$sql': ".$sth->errstr());
my @results;
while( my $db_filter = $sth->fetchrow_hashref() ) {
my $filter = new ZoneMinder::Filter($$db_filter{Id}, $db_filter);
push @results, $filter;
} # end while
$sth->finish();
return @results;
}
sub find_one {
my @results = find(@_);
return $results[0] if @results;
}
%fields = map { $_ => $_ } qw(
Id
Name
Query_json
AutoArchive
AutoVideo
AutoUpload
AutoEmail
EmailTo
EmailSubject
EmailBody
AutoMessage
AutoExecute
AutoExecuteCmd
AutoDelete
AutoMove
AutoMoveTo
AutoCopy
AutoCopyTo
UpdateDiskSpace
UserId
Background
Concurrent
);
sub Execute {
my $self = $_[0];
@ -458,6 +443,15 @@ sub DateTimeToSQL {
return POSIX::strftime('%Y-%m-%d %H:%M:%S', localtime($dt_val));
}
sub User {
my $self = shift;
$$self{User} = shift if @_;
if ( ! $$self{User} and $$self{UserId} ) {
$$self{User} = ZoneMinder::User->find_one(Id=>$$self{UserId});
}
return $$self{User};
}
1;
__END__
# Below is stub documentation for your module. You'd better edit it!