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