Fixed bug in search.cgi
[clearscm.git] / maps / bin / search.cgi
index 2584f39..6a8e656 100755 (executable)
@@ -2,8 +2,8 @@
 ################################################################################
 #
 # File:         $RCSfile: search.cgi,v $
-# Revision:    $Revision: 1.1 $
-# Description: Search by sender and subject
+# Revision:     $Revision: 1.1 $
+# Description:  Search by sender and subject
 # Author:       Andrew@DeFaria.com
 # Created:      Mon Jan 16 20:25:32 PST 2006
 # Modified:     $Date: 2013/06/12 14:05:47 $
@@ -16,24 +16,25 @@ use strict;
 use warnings;
 
 use FindBin;
-$0 = $FindBin::Script;
+local $0 = $FindBin::Script;
 
-use lib $FindBin::Bin;
+use lib "$FindBin::Bin/../lib";
+use lib "$FindBin::Bin/../../lib";
 
+use DateUtils;
 use MAPS;
 use MAPSWeb;
-use MAPSUtil;
+
 use CGI qw (:standard *table start_Tr start_td start_div end_Tr end_td end_div);
 use CGI::Carp "fatalsToBrowser";
 
-my $str                = param ("str");
-my $next        = param ("next");
-my $lines      = param ("lines");
-my $userid;
-my $prev;
-my $total;
-my $last;
-my $table_name = "searchresults";
+my $str   = param('str');
+my $next  = param('next');
+my $lines = param('lines');
+
+my ($userid, $prev, $total, $last);
+
+my $table_name = 'searchresults';
 
 sub MakeButtons {
   my $prev_button = $prev >= 0 ?
@@ -46,18 +47,18 @@ sub MakeButtons {
   my $buttons = $prev_button;
 
   $buttons = $buttons .
-    submit ({-name     => "action",
-            -value     => "Whitelist Marked",
-                    -onClick   => "return CheckAtLeast1Checked (document.detail);"}) .
-    submit ({-name     => "action",
-            -value     => "Blacklist Marked",
-                    -onClick   => "return CheckAtLeast1Checked (document.detail);"}) .
-    submit ({-name     => "action",
-            -value     => "Nulllist Marked",
-                    -onClick   => "return CheckAtLeast1Checked (document.detail);"}) .
-    submit ({-name     => "action",
-            -value     => "Reset Marks",
-            -onClick   => "return ClearAll (document.detail);"});
+    submit ({-name    => "action",
+             -value   => "Whitelist",
+             -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
+    submit ({-name    => "action",
+             -value   => "Blacklist",
+             -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
+    submit ({-name    => "action",
+             -value   => "Nulllist",
+             -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
+    submit ({-name    => "action",
+             -value   => "Reset",
+             -onClick => "return ClearAll (document.detail);"});
 
   return $buttons . $next_button;
 } # MakeButtons
@@ -73,63 +74,63 @@ sub HighlightSearchStr {
 } # HighlightSearchStr
 
 sub Body {
-  my @emails;
-
-  @emails = SearchEmails $userid, $str;
+  my @emails = SearchEmails(
+    userid => $userid,
+    search => $str,
+  );
 
   my $current = $next + 1;
 
   print div {-align => "center"}, b (
     "(" . $current . "-" . $last . " of " . $total . ")");
   print start_form {
-    -method    => "post",
-    -action    => "processaction.cgi",
-    -name      => "detail"
+    -method => "post",
+    -action => "processaction.cgi",
+    -name   => "detail"
   };
   my $buttons = MakeButtons;
-  print div {-align    => "center",
-            -class     => "toolbar"}, $buttons;
-  print start_table ({-align           => "center",
-                     -id               => $table_name,
-                     -border           => 0,
-                     -cellspacing      => 0,
-                     -cellpadding      => 0,
-                     -width            => "100%"}) . "\n";
+  print div {-align => "center",
+             -class => "toolbar"}, $buttons;
+  print start_table ({-align       => "center",
+                      -id          => $table_name,
+                      -border      => 0,
+                      -cellspacing => 0,
+                      -cellpadding => 0,
+                      -width       => "100%"}) . "\n";
   print
     Tr [
       th {-class => "tableleftend"},
-      th {-class => "tableheader"},    "Sender",
-      th {-class => "tableheader"},    "Subject",
-      th {-class => "tablerightend"},  "Date"
+      th {-class => "tableheader"},   "Sender",
+      th {-class => "tableheader"},   "Subject",
+      th {-class => "tablerightend"}, "Date"
     ];
 
-  foreach (@emails) {
-    my $sender  = shift @{$_};
-    my $subject = shift @{$_};
-    my $date    = shift @{$_};
+  for my $rec (@emails) {
+    my $display_sender = HighlightSearchStr $rec->{sender};
 
-    my $display_sender  = HighlightSearchStr $sender;
-    $subject = HighlightSearchStr $subject;
-    $subject = $subject eq "" ? "<Unspecified>" : $subject;
+    $rec->{subject} //= '<Unspecified>';
+    $rec->{subject} = HighlightSearchStr $rec->{subject};
 
     $next++;
 
     print Tr [
       td {-class => "tableleftdata",
-         -align => "center"},
-       (checkbox {-name        => "action$next",
-                  -label       => ""}),
-        hidden ({-name         => "email$next",
-                -default       => $sender}),
-      td {-class => "sender"}, 
-       a {-href => "mailto:$sender"}, $display_sender,
-      td {-class => "subject"},
-       a {-href => "display.cgi?sender=$sender"}, $subject,
-      td {-class => "dateright",
-         -width => "115"},             SQLDatetime2UnixDatetime $date
+          -align => "center"},
+         (checkbox {-name  => "action$next",
+                    -label => ""}),
+          hidden ({-name   => "email$next",
+         -default  => $rec->{sender}}),
+      td {-class   => "sender"}, 
+          a {-href => "mailto:$rec->{sender}"}, $display_sender,
+      td {-class   => "subject"},
+          a {-href => "display.cgi?sender=$rec->{sender}"}, $rec->{subject},
+      td {-class   => "dateright",
+          -width   => "115"}, SQLDatetime2UnixDatetime $rec->{timestamp},
     ];
   } # foreach
   print end_table;
+
+  return;
 } # Body
 
 # Main
@@ -145,22 +146,26 @@ $userid = Heading (
   @scripts
 );
 
+$userid //= $ENV{USER};
+
 SetContext $userid;
 NavigationBar $userid;
 
 DisplayError "No search string specified" if !defined $str;
 
-if (!defined $lines) {
+if (!$lines) {
   my %options = GetUserOptions $userid;
   $lines = $options{"Page"};
 } # if
 
-$total = MAPSDB::count "email",
-  "userid = \"$userid\" and (subject like \"%$str%\" or sender like \"%$str%\")";
+$total = CountEmail(
+  userid     => $userid,
+  additional => "(subject like '%$str%' or sender like '%$str%')",
+);
 
 DisplayError "Nothing matching!" if $total eq 0;
 
-$next = !defined $next ? 0 : $next;
+$next //= 0;
 $last = $next + $lines < $total ? $next + $lines : $total;
 
 if (($next - $lines) > 0) {