refactor displaySearchGrid and related function to remove multiple calls
[BackupPC.git] / lib / BackupPC / SearchLib.pm
index fd019e7..9c07b06 100644 (file)
@@ -37,9 +37,8 @@ sub epoch_to_iso {
        return $iso;
 }
 
-sub getWhere($) {
-       my ($param)    = @_;
-       my @conditions;
+sub dates_from_form($) {
+       my $param = shift || return;
 
        sub mk_epoch_date($$) {
                my ($name,$suffix) = @_;
@@ -57,32 +56,37 @@ sub getWhere($) {
                return $dt->epoch || 'NULL';
        }
 
-       my $backup_from = mk_epoch_date('search_backup', 'from');
+       return (
+               mk_epoch_date('search_backup', 'from'),
+               mk_epoch_date('search_backup', 'to'),
+               mk_epoch_date('search', 'from'),
+               mk_epoch_date('search', 'to'),
+       );
+}
+
+
+sub getWhere($) {
+       my $param = shift || return;
+
+       my ($backup_from, $backup_to, $files_from, $files_to) = dates_from_form($param);
+
+       my @conditions;
        push @conditions, qq{ backups.date >= $backup_from } if ($backup_from);
-       my $backup_to = mk_epoch_date('search_backup', 'to');
        push @conditions, qq{ backups.date <= $backup_to } if ($backup_to);
-
-       my $files_from = mk_epoch_date('search', 'from');
        push @conditions, qq{ files.date >= $files_from } if ($files_from);
-       my $files_to = mk_epoch_date('search', 'to');
        push @conditions, qq{ files.date <= $files_to } if ($files_to);
 
        print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:" . join(" | ",@conditions);
-    
-       push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'});
 
+       push( @conditions, ' files.shareid = ' . $param->{'search_share'} ) if ($param->{'search_share'});
        push (@conditions, " upper(files.path) LIKE upper('%".$param->{'search_filename'}."%')") if ($param->{'search_filename'});
 
-       return (
-               join(" and ", @conditions),
-               $files_from, $files_to,
-               $backup_from, $backup_to
-       );
+       return join(" and ", @conditions);
 }
 
 
 sub getFiles($$) {
-       my ($where, $offset) = @_;
+       my ($param, $offset) = @_;
 
        my $dbh = DBI->connect($dsn, $db_user, "", { RaiseError => 1, AutoCommit => 1 } );
 
@@ -113,6 +117,7 @@ sub getFiles($$) {
        };
 
        my $sql_where;
+       my $where = getWhere($param);
        $sql_where = " WHERE ". $where if ($where);
 
        my $sql_order = qq{
@@ -289,13 +294,17 @@ EOF3
        return $retHTML;
 }      
 
-sub displayGrid($$$$) {
-       my ($where, $addForm, $offset, $hilite) = @_;
+sub displayGrid($$) {
+       my ($param, $addForm) = @_;
+
+       my $offset = $param->{'offset'};
+       my $hilite = $param->{'search_filename'};
+
        my $retHTML = "";
  
        my $start_t = time();
 
-       my ($results, $files) = getFiles($where, $offset);
+       my ($results, $files) = getFiles($param, $offset);
 
        my $dur_t = time() - $start_t;
        my $dur = sprintf("%0.4fs", $dur_t);