Initial commit
[clearscm.git] / maps / bin / processaction.cgi
1 #!/usr/bin/perl
2 #################################################################################
3 #
4 # File:         $RCSfile: processaction.cgi,v $
5 # Revision:     $Revision: 1.1 $
6 # Description:  Process the action
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 $0 = $FindBin::Script;
20
21 use lib $FindBin::Bin;
22
23 use MAPS;
24 use MAPSWeb;
25
26 use CGI qw (:standard *table start_Tr end_Tr);
27 use CGI::Carp 'fatalsToBrowser';
28
29 my $type        = param 'type';
30 my $action      = param 'action';
31 my $next        = param 'next';
32 my $userid      = cookie 'MAPSUser';
33 my $lines;
34 my $total;
35 my $table_name  = 'list';
36
37 my @scripts = ('ListActions.js');
38
39 sub ReturnSequenceNbrs {
40   my @names = param;
41   my @sequence_nbrs;
42
43   Debug "Entered ReturnSequenceNbrs";
44
45   foreach (@names) {
46     if (/action(\d+)/) {
47       push @sequence_nbrs, $1;
48     } # if
49   } # foreach
50
51   Debug "Returning sequence nbrs " . join ' ', @sequence_nbrs;
52
53   return @sequence_nbrs;
54 } # ReturnSequenceNbrs
55
56 sub DeleteEntries {
57   my ($type) = @_;
58
59   my @sequence_nbrs = ReturnSequenceNbrs;
60
61   my $count;
62
63   foreach (@sequence_nbrs) {
64     $count += DeleteList $type, $_;
65   } # foreach
66
67   if ($count eq 0) {
68     DisplayError 'Nothing to delete!';
69   } else {
70     ResequenceList $userid, $type;
71
72     if ($count eq 1) {
73       print redirect ("/maps/php/list.php?type=$type&next=$next&message=Deleted entry");
74     } else {
75       print redirect ("/maps/php/list.php?type=$type&next=$next&message=Deleted $count entries");
76     } # if
77   } # if
78
79   return $count;
80 } # DeleteEntries
81
82 sub PrintInputLine ($$$$$) {
83   my ($nextseq, $email_nbr, $leftclass, $dataclass, $rightclass) = @_;
84
85   my $email     = '';
86   my $pattern   = '';
87   my $domain    = '';
88
89   if (defined $email_nbr && $email_nbr ne '') {
90     $email = param "email$email_nbr";
91     if ($email && $email ne '') {
92       ($pattern, $domain) = split /\@/, $email;
93     } # if
94   } # if
95
96   print Tr [
97     td {-class                  => $leftclass,
98         -align                  => 'center'}, "$nextseq",
99     td {-class                  => $dataclass,
100         -align                  => 'right'},
101       (textfield {-class        => 'inputfield',
102                   -style        => 'width:100%',
103                   -align        => 'right',
104                   -size         => 25,
105                   -maxlength    => '255',
106                   -name         => "pattern$nextseq",
107                   -value        => $pattern}),
108     td {-class                  => $dataclass,
109         -align                  => 'center'}, '@',
110     td {-class                  => $dataclass},
111       (textfield {-class        => 'inputfield',
112                   -style        => 'width:100%',
113                   -align        => 'left',
114                   -size         => 25,
115                   -maxlength    => '255',
116                   -name         => "domain$nextseq",
117                   -value        => $domain}),
118     td {-class                  => $rightclass},
119       (textfield {-class        => 'inputfield',
120                   -style        => 'width:100%',
121                   -align        => 'left',
122                   -size         => 25,
123                   -maxlength    => '255',
124                   -name         => "comment$nextseq",
125                   -value        => ''}),
126   ];
127 } # PrintInputLine
128
129 sub AddNewEntry {
130   my ($type, @selected) = @_;
131
132   # First display the last page and add the appropriate number of
133   # empty, editable entries (possibly filled in) for the user to add
134   # the new entry
135   my $selected = @selected;
136   my $nextseq = MAPSDB::GetNextSequenceNo $userid, $type;
137   my $next = ($nextseq - $lines) + $selected - 1;
138
139   $next = 0
140     if $next < 0;
141
142   my $Type = ucfirst $type;
143
144   Heading (
145     'getcookie',
146     '',
147     "Add to $Type List",
148     "Add to $Type List",
149     '',
150     $table_name,
151     @scripts
152   );
153
154   NavigationBar $userid;
155
156   # Now display table and new entry
157   print start_form {
158     -method     => 'post',
159     -action     => 'add2' . $type . 'list.cgi',
160     -name       => 'list'
161   };
162
163   print start_table {-align             => 'center',
164                      -id                => $table_name,
165                      -border            => 0,
166                      -cellspacing       => 0,
167                      -cellpadding       => 4,
168                      -width             => '100%'};
169   print Tr [
170     th {-class  => 'tableleftend'},     'Seq',
171     th {-class  => 'tableheader'},      'Username',
172     th {-class  => 'tableheader'},      '@',
173     th {-class  => 'tableheader'},      'Domain',
174     th {-class  => 'tablerightend'},    'Comments'
175   ];
176
177   my @list = ReturnList $type, $next, $lines;
178   my %record;
179   my $i = 1;
180
181   foreach (@list) {
182     $i++;
183
184     %record = %{$_};
185
186     # Normalize fields
187     my $sequence = $record{sequence};
188     my $pattern  = $record{pattern} ? $record{pattern} : '&nbsp;';
189     my $domain   = $record{domain}  ? $record{domain}  : '&nbsp;';
190     my $comment  = $record{comment} ? $record{comment} : '&nbsp;';
191
192     print Tr [
193       td {-class        => 'tableleftdata',
194           -align        => 'center'}, $sequence,
195       td {-class        => 'tabledata',
196           -align        => 'right'}, $pattern,
197       td {-class        => 'tabledata',
198           -align        => 'center'}, '@',
199       td {-class        => 'tabledata',
200           -align        => 'left'}, $domain,
201       td {-class        => 'tablerightdata',
202           -align        => 'left'}, $comment
203     ];
204   } # foreach
205
206   # Now the input line(s)
207   if (@selected eq 0) {
208     PrintInputLine $nextseq, undef, 'tablebottomleft', 'tablebottomdata',
209                                     'tablebottomright';
210   } else {
211     foreach (@selected) {
212       my $leftclass  = $i == $lines ? 'tablebottomleft'  : 'tableleftdata';
213       my $dataclass  = $i == $lines ? 'tablebottomdata'  : 'tabledata';
214       my $rightclass = $i == $lines ? 'tablebottomright' : 'tablerightdata';
215       $i++;
216       PrintInputLine $nextseq++, $_, $leftclass, $dataclass, $rightclass;
217     } # foreach
218   } # for
219
220   print end_table;
221   print br,
222     '<center>',
223       submit ({-name    => 'update',
224                -value   => 'Update',
225                -onClick => 'return CheckEntry (document.list);'}),
226       submit ({-name    => 'Reset',
227                -value   => 'Reset',
228                -onClick => 'history.back(); return false'}),
229     '</center>';
230   print end_form;
231 } # AddNewEntry
232
233 sub ModifyEntries {
234   my ($type) = @_;
235
236   my @selected = ReturnSequenceNbrs;
237
238   my $Type = ucfirst $type;
239
240   Heading (
241     'getcookie',
242     '',
243     "Modify $Type List",
244     "Modify $Type List",
245     '',
246     $table_name,
247     @scripts
248   );
249
250   NavigationBar $userid;
251
252   # Redisplay the page but open up the lines that are getting modified
253   print start_form {
254     -method     => 'post',
255     -action     => 'modifyentries.cgi',
256     -name       => 'list'
257   };
258
259   # Print some hidden fields to pass along
260   print
261     hidden ({-name      => 'type',
262              -default   => $type}),
263     hidden ({-name      => 'next',
264              -default   => $next});
265
266   print start_table {-align             => 'center',
267                      -id                => $table_name,
268                      -border            => 0,
269                      -cellspacing       => 0,
270                      -cellpadding       => 4,
271                      -width             => '100%'};
272   print Tr [
273     th {-class  => 'tableleftend'},     'Seq',
274     th {-class  => 'tableheader'},      'Username',
275     th {-class  => 'tableheader'},      '@',
276     th {-class  => 'tableheader'},      'Domain',
277     th {-class  => 'tablerightend'},    'Comments'
278   ];
279
280   my @list = ReturnList $type, $next, $lines;
281   my %record;
282   my $s = 0;
283   my $i = 1;
284
285   foreach (@list) {
286     %record = %{$_};
287
288     my $sequence = $record{sequence};
289     my $leftclass  = ($i eq $lines || $sequence eq $total) ?
290       'tablebottomleft'  : 'tableleftdata';
291     my $dataclass  = ($i eq $lines || $sequence eq $total) ?
292       'tablebottomdata'  : 'tabledata';
293     my $rightclass = ($i eq $lines || $sequence eq $total) ?
294       'tablebottomright' : 'tablerightdata';
295
296     $i++;
297
298     print start_Tr,
299       td {-class        => $leftclass,
300           -align        => 'center'}, $record{sequence};
301
302     if ($record{sequence} eq $selected[$s]) {
303       $s++;
304       # Normalize fields
305       my $pattern = $record{pattern} ? $record{pattern} : '';
306       my $domain  = $record{domain}  ? $record{domain}  : '';
307       my $comment = $record{comment} ? $record{comment} : '';
308
309       print
310         td {-class                      => $dataclass,
311             -align                      => 'right'},
312           (textfield {-class            => 'inputfield',
313                       -style            => 'width:100%',
314                       -align            => 'right',
315                       -size             => 25,
316                       -maxlength        => '255',
317                       -name             => "pattern$sequence",
318                       -value            => $pattern}),
319         td {-class                      => $dataclass,
320             -align                      => 'center'}, '@',
321         td {-class                      => $dataclass},
322           (textfield {-class            => 'inputfield',
323                       -style            => 'width:100%',
324                       -align            => 'left',
325                       -size             => 25,
326                       -maxlength        => '255',
327                       -name             => "domain$sequence",
328                       -value            => $domain}),
329         td {-class                      => $rightclass},
330            (textfield {-class           => 'inputfield',
331                        -style           => 'width:100%',
332                        -align           => 'left',
333                        -size            => 25,
334                        -maxlength       => '255',
335                        -name            => "comment$sequence",
336                        -value           => $comment});
337     } else {
338       # Put in '&nbsp;' for undefined fields
339       my $pattern = $record{pattern} ? $record{pattern} : '&nbsp;';
340       my $domain  = $record{domain}  ? $record{domain}  : '&nbsp;';
341       my $comment = $record{comment} ? $record{comment} : '&nbsp;';
342
343       print
344         td {-class => $dataclass,
345             -align => 'right'}, $pattern,
346         td {-class => $dataclass,
347             -align => 'center'}, '@',
348         td {-class => $dataclass,
349             -align => 'left'}, $domain,
350         td {-class => $rightclass,
351             -align => 'left'}, $comment;
352     } # if
353
354     print end_Tr;
355   } # foreach
356
357   print end_table;
358   print br,
359     '<center>',
360       submit ({-name    => 'update',
361                -value   => 'Update',
362                -onClick => 'return CheckEntry (document.list);'}),
363       submit ({-name    => 'Reset',
364                -value   => 'Reset',
365                -onClick => 'history.back(); return false'}),
366     '</center>';
367   print end_form;
368 } # ModifyEntries
369
370 sub WhitelistMarked {
371   AddNewEntry 'white', ReturnSequenceNbrs;
372 } # WhitelistMarked
373
374 sub BlacklistMarked {
375   AddNewEntry 'black', ReturnSequenceNbrs;
376 } # BlacklistMarked
377
378 sub NulllistMarked {
379   AddNewEntry 'null', ReturnSequenceNbrs;
380 } # NulllistMarked
381
382 # Main
383 $userid ||= $ENV{USER};
384
385 SetContext $userid;
386
387 my %options = GetUserOptions $userid;
388
389 $lines = $options{'Page'};
390
391 $total = MAPSDB::count 'list', "userid = \"$userid\" and type = \"$type\""
392   if $type;
393
394 if ($action eq 'Add New Entry') {
395   AddNewEntry $type;
396 } elsif ($action eq 'Delete Marked') {
397   DeleteEntries $type;
398 } elsif ($action eq 'Modify Marked') {
399   ModifyEntries $type;
400 } elsif ($action eq 'Whitelist Marked') {
401   WhitelistMarked;
402 } elsif ($action eq 'Blacklist Marked') {
403   BlacklistMarked;
404 } elsif ($action eq 'Nulllist Marked') {
405   NulllistMarked;
406 } else {
407   Heading (
408     'getcookie',
409     '',
410     "Unknown action ($action)",
411     "Unknown action ($action)"
412   );
413
414   NavigationBar $userid;
415   DisplayError "Unknown action encountered ($action)";
416 } # if
417
418 Footing $table_name;
419
420 exit;