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