dae6ce542da1968c34bc4fef9e50f9b3b5c14214
[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
23 use MAPS;
24 use MAPSWeb;
25
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
33 my ($userid, $prev, $total, $last);
34
35 my $table_name = 'searchresults';
36
37 sub MakeButtons {
38   my $prev_button = $prev >= 0 ?
39     a ({-href => "search.cgi?str=$str;next=$prev"},
40       "<img src=/maps/images/previous.gif border=0 alt=Previous align=middle>") : "";
41   my $next_button = ($next + $lines) < $total ?
42     a {-href => "search.cgi?str=$str;next=" . ($next + $lines)},
43       "<img src=/maps/images/next.gif border=0 alt=Next align=middle>" : "";
44
45   my $buttons = $prev_button;
46
47   $buttons = $buttons .
48     submit ({-name    => "action",
49              -value   => "Whitelist",
50              -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
51     submit ({-name    => "action",
52              -value   => "Blacklist",
53              -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
54     submit ({-name    => "action",
55              -value   => "Nulllist",
56              -onClick => "return CheckAtLeast1Checked (document.detail);"}) .
57     submit ({-name    => "action",
58              -value   => "Reset",
59              -onClick => "return ClearAll (document.detail);"});
60
61   return $buttons . $next_button;
62 } # MakeButtons
63
64 sub HighlightSearchStr {
65   $_ = shift;
66
67   my $highlighted_str = font {-class => "found"}, $str;
68
69   s/$str/<font class=\"found\">$&<\/font>/gi;
70
71   return $_;
72 } # HighlightSearchStr
73
74 sub Body {
75   my @emails = SearchEmails(
76     userid => $userid,
77     search => $str,
78   );
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   for my $rec (@emails) {
107     my $display_sender = HighlightSearchStr $rec->{sender};
108
109     $rec->{subject} //= '&lt;Unspecified&gt;';
110     $rec->{subject} = HighlightSearchStr $rec->{subject};
111
112     $next++;
113
114     print Tr [
115       td {-class => "tableleftdata",
116           -align => "center"},
117          (checkbox {-name  => "action$next",
118                     -label => ""}),
119           hidden ({-name   => "email$next",
120          -default  => $rec->{sender}}),
121       td {-class   => "sender"}, 
122           a {-href => "mailto:$rec->{sender}"}, $display_sender,
123       td {-class   => "subject"},
124           a {-href => "display.cgi?sender=$rec->{sender}"}, $rec->{subject},
125       td {-class   => "dateright",
126           -width   => "115"}, SQLDatetime2UnixDatetime $rec->{date},
127     ];
128   } # foreach
129   print end_table;
130
131   return;
132 } # Body
133
134 # Main
135 my @scripts = ("ListActions.js");
136
137 $userid = Heading (
138   "getcookie",
139   "",
140   "Search Results",
141   "Search Results for \"$str\"",
142   "",
143   $table_name,
144   @scripts
145 );
146
147 $userid //= $ENV{USER};
148
149 SetContext $userid;
150 NavigationBar $userid;
151
152 DisplayError "No search string specified" if !defined $str;
153
154 if (!$lines) {
155   my %options = GetUserOptions $userid;
156   $lines = $options{"Page"};
157 } # if
158
159 $total = CountEmail(
160   userid     => $userid,
161   additional => "(subject like '%$str%' or sender like '%$str%')",
162 );
163
164 DisplayError "Nothing matching!" if $total eq 0;
165
166 $next //= 0;
167 $last = $next + $lines < $total ? $next + $lines : $total;
168
169 if (($next - $lines) > 0) {
170   $prev = $next - $lines;
171 } else {
172   $prev = $next eq 0 ? -1 : 0;
173 } # if
174
175 Body;
176
177 Footing $table_name;
178
179 exit;