display limits in search, easier to understand filter generator (and correct
authordpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Mon, 11 Jul 2005 14:32:40 +0000 (14:32 +0000)
committerdpavlin <dpavlin@8392b6e1-25fa-0310-8288-cc32f8e212ea>
Mon, 11 Jul 2005 14:32:40 +0000 (14:32 +0000)
one -- doesn't require users to enter dates as 01 01 2001)

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

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

index 605c1d1..05817f3 100644 (file)
@@ -98,7 +98,17 @@ $subtitle
        my $result;
        my $rows = "";
 
-       my $grid = BackupPC::SearchLib::displayGrid( BackupPC::SearchLib::getWhere(\%In), 1, $In{'offset'}, $In{'search_filename'});
+       my ($where, $from_f, $to_f, $from_b, $to_b) = BackupPC::SearchLib::getWhere(\%In);
+
+       my $q = $In{'search_filename'};
+
+       $cont .= "<small>";
+       $cont .= "Filename filter: $q</br>" if ($q);
+       $cont .= "Files date limit: $from_f - $to_f</br>" if ($from_f && $to_f);
+       $cont .= "Backup date limit: $from_b - $to_b</br>" if ($from_b && $to_b);
+       $cont .= "</small>";
+
+       my $grid = BackupPC::SearchLib::displayGrid( $where, 1, $In{'offset'}, $q);
        $cont .= $grid;
     }
     Header( eval("qq{$Lang->{Search_archive}}"), "", 1, "", $cont );
index 5ef0f8a..8334075 100644 (file)
@@ -25,98 +25,44 @@ sub getUnits() {
 }
 
 sub getWhere($) {
-    my ($param)    = @_;
-    my $retSQL     = "";
-    my @conditions = ();
-    my $cond;
+       my ($param)    = @_;
+       my @conditions;
 
-    
-   
-    
-    if ( defined( $param->{'search_backup_day_from'} ) && $param->{'search_backup_day_from'} ne "") {
-        push( @conditions,
-           ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) >= "'
-              . $param->{'search_backup_day_from'} ."\"");
-    }
-    if ( defined( $param->{'search_backup_day_to'} ) && $param->{'search_backup_day_to'} ne "") {
-        push( @conditions,
-           ' strftime("%d", datetime(backups.date, "unixepoch","localtime")) <= "'
-              . $param->{'search_backup_day_from'}  ."\"");
-    }
-    if ( defined( $param->{'search_backup_month_from'} ) && $param->{'search_backup_month_from'} ne "") {
-        push( @conditions,
-           ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) >= "'
-              . $param->{'search_backup_month_from'}  ."\"");
-    }
-    if ( defined( $param->{'search_backup_month_to'} ) && $param->{'search_backup_month_to'} ne "") {
-        push( @conditions,
-           ' strftime("%m", datetime(backups.date, "unixepoch","localtime")) <= "'
-              . $param->{'search_backup_month_to'}  ."\"");
-    }
-    if ( defined( $param->{'search_backup_year_from'} ) && $param->{'search_backup_year_from'} ne "") {
-        push( @conditions,
-           ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) >= "'
-              . $param->{'search_backup_year_from'}  ."\"");
-    }
-    if ( defined( $param->{'search_backup_year_to'} ) && $param->{'search_backup_year_to'} ne "") {
-        push( @conditions,
-           ' strftime("%Y", datetime(backups.date, "unixepoch","localtime")) <= "'
-              . $param->{'search_backup_year_to'}  ."\"");
-    }
+       sub mk_iso_date($$) {
+               my ($name,$suffix) = @_;
 
-    if ( defined( $param->{'search_day_from'} )   && $param->{'search_day_from'} ne "" ) {
-        push( @conditions,
-            ' strftime("%d", datetime(files.date, "unixepoch","localtime")) >= "'
-              . $param->{'search_day_from'}  ."\"");
-    }
-    if ( defined( $param->{'search_month_from'} ) && $param->{'search_month_from'} ne "") {
-        push( @conditions,
-            ' strftime("%m", datetime(files.date, "unixepoch","localtime")) >= "'
-              . $param->{'search_month_from'}  ."\"");
-    }
-    if ( defined( $param->{'search_year_from'} ) && $param->{'search_year_from'} ne "") {
-        push( @conditions,
-            ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) >= "'
-              . $param->{'search_year_from'}  ."\"");
-    }
-    if ( defined( $param->{'search_day_to'} )   && $param->{'search_day_to'} ne "" ) {
-        push( @conditions,
-            ' strftime("%d", datetime(files.date, "unixepoch","localtime")) <= "'
-              . $param->{'search_day_to'}  ."\"");
-    }
-    if ( defined( $param->{'search_month_to'} ) && $param->{'search_month_to'} ne "" ) {
-        push( @conditions,
-            ' strftime("%m", datetime(files.date, "unixepoch","localtime")) <= "'
-              . $param->{'search_month_to'} ."\"" );
-    }
-    if ( defined( $param->{'search_year_to'} )&& $param->{'search_year_to'} ne "" )  {
-        push( @conditions,
-            ' strftime("%Y", datetime(files.date, "unixepoch","localtime")) <= "'
-              . $param->{'search_year_to'} ."\"");
-    }
+               my $yyyy = $param->{ $name . '_year_' . $suffix} || return;
+               my $mm .= $param->{ $name . '_month_' . $suffix} ||
+                       ( $suffix eq 'from' ? 1 : 12);
+               my $dd .= $param->{ $name . '_day_' . $suffix} ||
+                       ( $suffix eq 'from' ? 1 : 31);
+               return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd);
+       }
 
-    if ( defined( $param->{'search_host'} ) && $param->{'search_host'} ne "") {
-      push( @conditions, ' backups.hostID = ' . $param->{'search_host'} );
-    }
+       my $backup_from = mk_iso_date('search_backup', 'from');
+       push @conditions, qq{ date(backups.date, 'unixepoch','localtime') >= '$backup_from' } if ($backup_from);
+       my $backup_to = mk_iso_date('search_backup', 'to');
+       push @conditions, qq{ date(backups.date, 'unixepoch','localtime') <= '$backup_to' } if ($backup_to);
 
-    if ( defined ($param->{'search_filename'}) && $param->{'search_filename'} ne "") {
-       push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'");
-       }
-    
-    $retSQL = "";
-    foreach $cond(@conditions)
-      {
-         if ($retSQL ne "")
-           {
-               $retSQL .= " AND ";
-           }
-         $retSQL .= $cond;
-      }       
+       my $files_from = mk_iso_date('search', 'from');
+       push @conditions, qq{ date(files.date, 'unixepoch','localtime') >= '$files_from' } if ($files_from);
+       my $files_to = mk_iso_date('search', 'to');
+       push @conditions, qq{ date(files.date, 'unixepoch','localtime') <= '$files_to' } if ($files_to);
 
+       print STDERR "backup: $backup_from - $backup_to files: $files_from - $files_to cond:",join(" | ",@conditions);
     
-    return $retSQL;
+       push( @conditions, ' backups.hostID = ' . $param->{'search_host'} ) if ($param->{'search_host'});
+
+       push (@conditions, " files.name LIKE '".$param->{'search_filename'}."%'") if ($param->{'search_filename'});
+
+       return (
+               join(" and ", @conditions),
+               $files_from, $files_to,
+               $backup_from, $backup_to
+       );
 }
 
+
 sub getFiles($$)
   {
       my ($where, $offset) = @_;
@@ -141,6 +87,7 @@ sub getFiles($$)
                FROM files 
                        INNER JOIN shares       ON files.shareID=shares.ID
                        INNER JOIN hosts        ON hosts.ID = shares.hostID
+                       INNER JOIN backups      ON backups.num = files.backupNum
                        LEFT  JOIN dvds         ON dvds.ID = files.dvdid
          };