5dd9d23488797bfdfb113e465d1d2a6e0e3a3818
[clearscm.git] / maps / lib / MAPSWeb.pm
1 #################################################################################
2 #
3 # File:         $RCSfile: MAPSWeb.pm,v $
4 # Revision:     $Revision: 1.1 $
5 # Description:  Routines for generating portions of MAPSWeb
6 # Author:       Andrew@DeFaria.com
7 # Created:      Fri Nov 29 14:17:21  2002
8 # Modified:     $Date: 2013/06/12 14:05:47 $
9 # Language:     perl
10 #
11 # (c) Copyright 2000-2006, Andrew@DeFaria.com, all rights reserved.
12 #
13 ################################################################################
14 package MAPSWeb;
15
16 use strict;
17 use warnings;
18
19 use base qw(Exporter);
20
21 use DateUtils;
22
23 use MAPS;
24 use MAPSLog;
25
26 use CGI qw(:standard *table start_Tr end_Tr start_div end_div);
27
28 our @EXPORT = qw(
29   DebugWeb
30   DisplayError
31   Footing
32   Heading
33   NavigationBar
34 );
35
36 sub getquickstats(%) {
37   my (%params) = @_;
38
39   my %dates = GetStats(
40     userid => $params{userid},
41     days   => 1,
42     date   => $params{date},
43   );
44
45   my $date = $params{date};
46
47   for (@MAPSLog::Types) {
48     $dates{$date}{processed} += $dates{$date}{$_};
49   } # for
50
51   return %dates;
52 } # getquickstats
53
54 sub displayquickstats($) {
55   my ($userid) = @_;
56
57   # Quick stats are today only.
58   my $today = Today2SQLDatetime;
59   my $time  = substr $today, 11;
60   my $date  = substr $today, 0, 10;
61   my %dates = getquickstats(
62     userid => $userid,
63     date   => $date
64   );
65
66   print start_div {-class => 'quickstats'};
67   print h4 {-class    => 'todaysactivity',
68             -align    => 'center'},
69     'Today\'s Activity';
70   print p {-align     => 'center'},
71     b ('as of ' . FormatTime($time));
72   print start_table {
73     -align       => 'center',
74     -border      => 0,
75     -cellspacing => 0,
76     -cellpadding => 2};
77   print start_Tr {-align => 'right'};
78   print
79     td {-class => 'smalllabel',
80         -align => 'right'},
81       'Processed';
82   print
83     td {-class => 'smallnumber',
84         -align => 'right'},
85       $dates{$date}{'processed'};
86   print
87     td {-class => 'smallnumber',
88         -align => 'right'},
89       'n/a';
90   print end_Tr;
91
92   for (@MAPSLog::Types) {
93     print start_Tr {-align => 'right'};
94
95     my $value = $dates{$date}{$_};
96     my $percent;
97     if ($_ eq 'mailloop' || $_ eq 'registered') {
98       $percent = 'n/a';
99     } else {
100       $percent = $dates{$date}{processed} == 0 ?
101         0 : $dates{$date}{$_} / $dates{$date}{processed} * 100;
102       $percent = sprintf '%5.1f%s', $percent, '%';
103     } # if
104     my $stat = $value == 0 ?
105       0 : a {-href => "detail.cgi?type=$_;date=$date"}, $value;
106     print
107       td {-class => 'smalllabel'}, ucfirst ($_);
108     print
109       td {-class => 'smallnumber'}, $stat;
110     print
111       td {-class => 'smallnumber'}, $percent;
112     print end_Tr;
113   } # foreach
114   print end_table;
115   print end_div;
116
117   return;
118 } # displayquickstats
119
120 sub Footing(;$) {
121   my ($table_name) = @_;
122
123   # General footing (copyright). Note we calculate the current year
124   # so that the copyright automatically extends itself.
125   my $year = substr((scalar (localtime)), 20, 4);
126
127   print start_div {-class => "copyright"};
128   print "Copyright © 2001-$year - All rights reserved";
129   print br (
130     a ({-href => 'https://defaria.com'},
131       'Andrew DeFaria'),
132     a ({-href => 'mailto:Andrew@DeFaria.com'},
133       '<Andrew@DeFaria.com>'));
134   print end_div;
135
136   print end_div; # This div ends "content" which was started in Heading
137   print "<script language='JavaScript1.2'>AdjustTableWidth (\"$table_name\");</script>"
138     if $table_name;
139   print end_html;
140
141   return;
142 } # Footing
143
144 sub DebugWeb($) {
145   my ($msg) = @_;
146
147   print br, font({ -class => 'error' }, 'DEBUG: '), $msg;
148
149   return;
150 } # Debug
151
152 sub DisplayError($) {
153   my ($errmsg) = @_;
154
155   print h3({
156     -class => 'error',
157     -align => 'center'},
158     'ERROR: ' . $errmsg
159   );
160
161   Footing;
162
163   exit 1;
164 } # DisplayError
165
166 # This subroutine puts out the header for web pages. It is called by
167 # various cgi scripts thus has a few parameters.
168 sub Heading($$$$;$$@) {
169   my ($action,          # One of getcookie, setcookie, unsetcookie
170       $userid,          # User id (if setting a cookie)
171       $title,           # Title string
172       $h1,              # H1 header
173       $h2,              # H2 header (optional)
174       $table_name,      # Name of table in page, if any
175       @scripts) = @_;   # Array of JavaScript scripts to include
176
177   my @java_scripts;
178   my $cookie;
179
180   # Since CheckAddress appears on all pages (well except for the login
181   # page) include it by default along with MAPSUtils.js
182   push @java_scripts, [
183     {-language => 'JavaScript1.2',
184      -src      => '/maps/JavaScript/MAPSUtils.js'},
185     {-language => 'JavaScript1.2',
186      -src      => '/maps/JavaScript/CheckAddress.js'}
187   ];
188
189   # Add on any additional JavaScripts that the caller wants. Note the
190   # odd single element array of hashes but that's what CGI requires!
191   # Build up scripts from array
192   for (@scripts) {
193     push @{$java_scripts[0]},
194       {-language => 'JavaScript1.2',
195        -src      => "/maps/JavaScript/$_"}
196   } # foreach
197
198   # Since Heading is called from various scripts we sometimes need to
199   # set a cookie, other times delete a cookie but most times return the
200   # cookie.
201   if ($action eq 'getcookie') {
202     # Get userid from cookie
203     $userid = cookie ('MAPSUser');
204   } elsif ($action eq 'setcookie') {
205     $cookie = cookie (
206        -name    => 'MAPSUser',
207        -value   => $userid,
208        -expires => '+1y',
209        -path    => '/maps'
210     );
211   } elsif ($action eq 'unsetcookie') {
212     $cookie = cookie (
213        -name    => 'MAPSUser',
214        -value   => '',
215        -expires => '-1d',
216        -path    => '/maps'
217     );
218   } # if
219
220   print header(
221     -title  => $title,
222     -cookie => $cookie
223   );
224
225   if ($table_name) {
226     print start_html(
227       -title    => $title,
228       -author   => 'Andrew\@DeFaria.com',
229       -style    => {-src    => '/maps/css/MAPSStyle.css'},
230       -onResize => "AdjustTableWidth (\"$table_name\");",
231       -head     => [
232       Link({-rel  => 'icon',
233         -href => '/maps/MAPS.png',
234         -type => 'image/png'}),
235       Link({-rel  => 'shortcut icon',
236         -href => '/maps/favicon.ico'})
237       ],
238       -script    => @java_scripts);
239   } else {
240     print start_html(
241       -title  => $title,
242       -author => 'Andrew\@DeFaria.com',
243       -style  => {-src    => '/maps/css/MAPSStyle.css'},
244       -head   => [
245       Link({-rel  => 'icon',
246         -href => '/maps/MAPS.png',
247         -type => 'image/png'}),
248       Link({-rel  => 'shortcut icon',
249         -href => '/maps/favicon.ico'})],
250         -script    => @java_scripts);
251   } # if
252
253   print start_div {class => 'heading'};
254   print h2 {
255     -align => 'center',
256     -class => 'header'}, $h1;
257
258   if (defined $h2 && $h2 ne '') {
259     print h3 {
260       -align => 'center',
261       -class => 'header'}, $h2;
262   } # if
263   print end_div;
264
265   # Start body content
266   print start_div {-class => 'content'};
267
268   return $userid
269 } # Heading
270
271 sub NavigationBar($) {
272   my ($userid) = @_;
273
274   print start_div {-id => 'leftbar'};
275
276   unless ($userid) {
277     print h2({-align => 'center'}, font({-color => 'white'}, "MAPS $MAPS::Version"));
278     print div ({-class => 'username'}, 'Welcome to MAPS');
279     print div ({-class => 'menu'},
280       (a {-href => '/maps/doc/'},
281         'What is MAPS?<br>'),
282       (a {-href => '/maps/doc/SPAM.html'},
283         'What is SPAM?<br>'),
284       (a {-href => '/maps/doc/Requirements.html'},
285         'Requirements<br>'),
286       (a {-href => '/maps/SignupForm.html'},
287         'Signup<br>'),
288       (a {-href => '/maps/doc/Using.html'},
289         'Using MAPS<br>'),
290       (a {-href => '/maps/doc/'},
291         'Help<br>'),
292     );
293   } else {
294     print h2({-align => 'center'}, font({-color => 'white'}, "MAPS $MAPS::Version"));
295     print div ({-class => 'username'}, 'Welcome '. ucfirst $userid);
296
297     print div ({-class => 'menu'},
298       (a {-href => '/maps/'},
299         'Home<br>'),
300       (a {-href => '/maps/bin/stats.cgi'},
301         'Statistics<br>'),
302       (a {-href => '/maps/bin/editprofile.cgi'},
303         'Profile<br>'),
304       (a {-href => '/maps/php/Reports.php'},
305         'Reports<br>'),
306       (a {-href => '/maps/php/list.php?type=white'},
307         'White<br>'),
308       (a {-href => '/maps/php/list.php?type=black'},
309         'Black<br>'),
310       (a {-href => '/maps/php/list.php?type=null'},
311         'Null<br>'),
312       (a {-href => '/maps/doc/'},
313         'Help<br>'),
314       (a {-href => '/maps/adm/'},
315         'MAPS<br>'),
316       (a {-href => '/maps/?logout=yes'},
317         'Logout'),
318     );
319
320     displayquickstats($userid);
321
322     print start_div {-class => 'search'};
323     print start_form {-method => 'get',
324                       -action => '/maps/bin/search.cgi',
325                       -name   => 'search'};
326     print 'Search Sender/Subject',
327       textfield {-class     => 'searchfield',
328                  -id        => 'searchfield',
329                  -name      => 'str',
330                  -size      => 20,
331                  -maxlength => 255,
332                  -value     => '',
333                  -onclick   => "document.search.str.value = '';"};
334     print end_form;
335     print end_div;
336
337     print start_div {-class => 'search'};
338     print start_form {-method => 'post',
339                 -action   => 'javascript://',
340                 -name     => 'address',
341                 -onsubmit => 'checkaddress(this);'};
342     print 'Check Email Address',
343       textfield {-class     => 'searchfield',
344                  -id        => 'searchfield',
345                  -name      => 'email',
346                  -size      => 20,
347                  -maxlength => 255,
348                  -value     => '',
349                  -onclick   => "document.address.email.value = '';"};
350     print p "";
351     print end_form;
352     print end_div;
353   } # if
354
355   print end_div;
356
357   return;
358 } # NavigationBar
359
360 1;