refactor displaySearchGrid and related function to remove multiple calls
authordpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Sun, 28 Aug 2005 10:14:48 +0000 (10:14 +0000)
committerdpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Sun, 28 Aug 2005 10:14:48 +0000 (10:14 +0000)
from BackupPC::CGI::SearchArchives to BackupPC::SearchLib.

git-svn-id: svn+ssh://llin/home/dpavlin/private/svn/BackupPC/trunk@83 8392b6e1-25fa-0310-8288-cc32f8e212ea

lib/BackupPC/CGI/SearchArchives.pm
lib/BackupPC/SearchLib.pm

index 9f50298..a097215 100644 (file)
@@ -67,24 +67,15 @@ sub action() {
 
        if ( !defined($In{search_results}) ) {
                $html .= eval(q{ ${h2("Search criteria")}});
+
                $html .= $form;
        } else {
                $html .= eval(q{ ${h2("Search results")}});
+
                my $fif = new HTML::FillInForm;
                $html .= $fif->fill(scalarref => \$form, fdat => \%In);
 
-               my ($where, $from_f, $to_f, $from_b, $to_b) = BackupPC::SearchLib::getWhere(\%In);
-
-               my $q = $In{'search_filename'};
-
-               # DEBUG
-               #$html .= "<small>";
-               #$html .= "Filename filter: $q</br>" if ($q);
-               #$html .= "Files date limit: $from_f - $to_f</br>" if ($from_f || $to_f);
-               #$html .= "Backup date limit: $from_b - $to_b</br>" if ($from_b || $to_b);
-               #$html .= "</small>";
-
-               $html .= BackupPC::SearchLib::displayGrid( $where, 1, $In{'offset'}, $q);
+               $html .= BackupPC::SearchLib::displayGrid( \%In, 1 );
        }
 
        Header( eval("qq{$Lang->{Search_archive}}"), "", 1, "", $html );
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);