Fixed search.cgi
[clearscm.git] / maps / bin / search.cgi
1 #!/usr/bin/perl
2 ################################################################################
3 #
4 # File:         $RCSfile: search.cgi,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  Search by sender and subject
7 # Author:       Andrew@DeFaria.com
8 # Created:      Mon Jan 16 20:25:32 PST 2006
9 # Modified:     $Date: 2013/06/12 14:05:47 $
10 # Language:     perl
11 #
12 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
13 #
14 ################################################################################
15 use strict;
16 use warnings;
17
18 use FindBin;
19 $0 = $FindBin::Script;
20
21 use lib "$FindBin::Bin/../lib";
22
23 use MAPS;
24 use MAPSWeb;
25 use MAPSUtil;
26 use CGI qw (:standard *table start_Tr start_td start_div end_Tr end_td end_div);
27 use CGI::Carp "fatalsToBrowser";
28
29 my $str   = param('str');
30 my $next  = param('next');
31 my $lines = param('lines');
32 my $userid;
33 my $prev;
34 my $total;
35 my $last;
36 my $table_name = 'searchresults';
37
38 sub MakeButtons {
39   my $prev_button = $prev >= 0 ?
40     a ({-href => "search.cgi?str=$str;next=$prev"},
41       "<img src=/maps/images/previous.gif border=0 alt=Previous align=middle>") : "";
42   my $next_button = ($next + $lines) < $total ?
43     a {-href => "search.cgi?str=$str;next=" . ($next + $lines)},
44       "<img src=/maps/images/next.gif border=0 alt=Next align=middle>" : "";
45
46   my $buttons = $prev_button;
47
48   $buttons = $buttons .
49     submit ({-name    => "action",
50              -value   => "Whitelist",
51              -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
52     submit ({-name    => "action",
53              -value   => "Blacklist",
54              -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
55     submit ({-name    => "action",
56              -value   => "Nulllist",
57              -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
58     submit ({-name    => "action",
59              -value   => "Reset",
60              -onClick => "return ClearAll (document.detail);"});
61
62   return $buttons . $next_button;
63 } # MakeButtons
64
65 sub HighlightSearchStr {
66   $_ = shift;
67
68   my $highlighted_str = font {-class => "found"}, $str;
69
70   s/$str/<font class=\"found\">$&<\/font>/gi;
71
72   return $_;
73 } # HighlightSearchStr
74
75 sub Body {
76   my @emails;
77
78   @emails = SearchEmails $userid, $str;
79
80   my $current = $next + 1;
81
82   print div {-align => "center"}, b (
83     "(" . $current . "-" . $last . " of " . $total . ")");
84   print start_form {
85     -method => "post",
86     -action => "processaction.cgi",
87     -name   => "detail"
88   };
89   my $buttons = MakeButtons;
90   print div {-align => "center",
91              -class => "toolbar"}, $buttons;
92   print start_table ({-align       => "center",
93                       -id          => $table_name,
94                       -border      => 0,
95                       -cellspacing => 0,
96                       -cellpadding => 0,
97                       -width       => "100%"}) . "\n";
98   print
99     Tr [
100       th {-class => "tableleftend"},
101       th {-class => "tableheader"},   "Sender",
102       th {-class => "tableheader"},   "Subject",
103       th {-class => "tablerightend"}, "Date"
104     ];
105
106   foreach (@emails) {
107     my $sender  = shift @{$_};
108     my $subject = shift @{$_};
109     my $date    = shift @{$_};
110
111     my $display_sender  = HighlightSearchStr $sender;
112     $subject = HighlightSearchStr $subject;
113     $subject = $subject eq "" ? "&lt;Unspecified&gt;" : $subject;
114
115     $next++;
116
117     print Tr [
118       td {-class => "tableleftdata",
119           -align => "center"},
120          (checkbox {-name  => "action$next",
121                     -label => ""}),
122           hidden ({-name   => "email$next",
123          -default  => $sender}),
124       td {-class   => "sender"}, 
125           a {-href => "mailto:$sender"}, $display_sender,
126       td {-class   => "subject"},
127           a {-href => "display.cgi?sender=$sender"}, $subject,
128       td {-class   => "dateright",
129           -width   => "115"}, SQLDatetime2UnixDatetime $date
130     ];
131   } # foreach
132   print end_table;
133 } # Body
134
135 # Main
136 my @scripts = ("ListActions.js");
137
138 $userid = Heading (
139   "getcookie",
140   "",
141   "Search Results",
142   "Search Results for \"$str\"",
143   "",
144   $table_name,
145   @scripts
146 );
147
148 SetContext $userid;
149 NavigationBar $userid;
150
151 DisplayError "No search string specified" if !defined $str;
152
153 if (!$lines) {
154   my %options = GetUserOptions $userid;
155   $lines = $options{"Page"};
156 } # if
157
158 $total = count "email",
159   "userid = \"$userid\" and (subject like \"%$str%\" or sender like \"%$str%\")";
160
161 DisplayError "Nothing matching!" if $total eq 0;
162
163 $next = !defined $next ? 0 : $next;
164 $last = $next + $lines < $total ? $next + $lines : $total;
165
166 if (($next - $lines) > 0) {
167   $prev = $next - $lines;
168 } else {
169   $prev = $next eq 0 ? -1 : 0;
170 } # if
171
172 Body;
173
174 Footing $table_name;
175
176 exit;