Added use lib for clearscm
[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 MAPS;
25 use MAPSWeb;
26
27 use CGI qw (:standard *table start_Tr start_td start_div end_Tr end_td end_div);
28 use CGI::Carp "fatalsToBrowser";
29
30 my $str   = param('str');
31 my $next  = param('next');
32 my $lines = param('lines');
33
34 my ($userid, $prev, $total, $last);
35
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 = SearchEmails(
77     userid => $userid,
78     search => $str,
79   );
80
81   my $current = $next + 1;
82
83   print div {-align => "center"}, b (
84     "(" . $current . "-" . $last . " of " . $total . ")");
85   print start_form {
86     -method => "post",
87     -action => "processaction.cgi",
88     -name   => "detail"
89   };
90   my $buttons = MakeButtons;
91   print div {-align => "center",
92              -class => "toolbar"}, $buttons;
93   print start_table ({-align       => "center",
94                       -id          => $table_name,
95                       -border      => 0,
96                       -cellspacing => 0,
97                       -cellpadding => 0,
98                       -width       => "100%"}) . "\n";
99   print
100     Tr [
101       th {-class => "tableleftend"},
102       th {-class => "tableheader"},   "Sender",
103       th {-class => "tableheader"},   "Subject",
104       th {-class => "tablerightend"}, "Date"
105     ];
106
107   for my $rec (@emails) {
108     my $display_sender = HighlightSearchStr $rec->{sender};
109
110     $rec->{subject} //= '&lt;Unspecified&gt;';
111     $rec->{subject} = HighlightSearchStr $rec->{subject};
112
113     $next++;
114
115     print Tr [
116       td {-class => "tableleftdata",
117           -align => "center"},
118          (checkbox {-name  => "action$next",
119                     -label => ""}),
120           hidden ({-name   => "email$next",
121          -default  => $rec->{sender}}),
122       td {-class   => "sender"}, 
123           a {-href => "mailto:$rec->{sender}"}, $display_sender,
124       td {-class   => "subject"},
125           a {-href => "display.cgi?sender=$rec->{sender}"}, $rec->{subject},
126       td {-class   => "dateright",
127           -width   => "115"}, SQLDatetime2UnixDatetime $rec->{date},
128     ];
129   } # foreach
130   print end_table;
131
132   return;
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 $userid //= $ENV{USER};
149
150 SetContext $userid;
151 NavigationBar $userid;
152
153 DisplayError "No search string specified" if !defined $str;
154
155 if (!$lines) {
156   my %options = GetUserOptions $userid;
157   $lines = $options{"Page"};
158 } # if
159
160 $total = CountEmail(
161   userid     => $userid,
162   additional => "(subject like '%$str%' or sender like '%$str%')",
163 );
164
165 DisplayError "Nothing matching!" if $total eq 0;
166
167 $next //= 0;
168 $last = $next + $lines < $total ? $next + $lines : $total;
169
170 if (($next - $lines) > 0) {
171   $prev = $next - $lines;
172 } else {
173   $prev = $next eq 0 ? -1 : 0;
174 } # if
175
176 Body;
177
178 Footing $table_name;
179
180 exit;