]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Admin.pm
Use empty URLs for POST forms
[pciids.git] / PciIds / Html / Admin.pm
1 package PciIds::Html::Admin;
2 use strict;
3 use warnings;
4 use PciIds::Users;
5 use PciIds::Html::Util;
6 use PciIds::Html::Users;
7 use PciIds::Html::Forms;
8 use PciIds::Notifications;
9 use PciIds::Log;
10 use Apache2::Const qw(:common :http);
11
12 sub genNewAdminForm( $$$$ ) {
13         my( $req, $args, $tables, $error ) = @_;
14         genHtmlHead( $req, 'Administration ‒ pending events', undef );
15         print "<h1>Administration ‒ pending events</h1>\n";
16         print "<div class='error'>".$error."</div>\n" if( defined $error );
17         print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
18         my $lastId;
19         my $started = 0;
20         my $cnt = 0;
21         my $hiscnt = 0;
22         my $subcnt;
23         foreach( @{$tables->adminDump()} ) {
24                 my( $locId, $actName, $actDescription, $actCom, $actUser, $actText,
25                         $com, $text, $name, $description, $user ) = @{$_};
26                 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
27                         $lastId = $locId;
28                         print "</div>\n" if( $started );
29                         $started = 1;
30                         print "<div class='".( defined( $actCom ) ? 'item' : 'unnamedItem' )."'>\n";
31                         my $addr = PciIds::Address::new( $locId );
32                         print "<h3><a href='/read/".$addr->get()."/'>".encode( $addr->pretty() )."</a></h3>\n";
33                         print htmlDiv( 'name', '<p>'.encode( $actName ) ) if( defined( $actName ) );
34                         print htmlDiv( 'description', '<p>'.encode( $actDescription ) ) if( defined( $actDescription ) );
35                         print '<p>'.encode( $actText ) if( defined( $actText ) );
36                         print '<p><a class="navigation" href="/read/'.$addr->parent()->get().'/">'.encode( $addr->parent()->pretty() )."</a>" if( defined( $addr->parent() ) );
37                         print htmlDiv( 'author', '<p>'.encode( $actUser ) ) if( defined( $actUser ) );
38                         print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
39                         $subcnt = 0;
40                         $cnt++;
41                         print "<input type='hidden' name='loc-$cnt' value='".$addr->get()."'>\n";
42                         print "<p><input type='radio' name='action-$cnt' value='ignore' checked='checked'> I will decide later.\n";
43                         if( defined( $actCom ) ) {
44                                 print "<br><input type='radio' name='action-$cnt' value='keep'> Keep current name.\n";
45                         }
46                         print "<br><input type='radio' name='action-$cnt' value='delete'> Delete item.\n";
47                         print "<br>Add comment:\n";
48                         print "<br><table>\n";
49                         print "<tr><td>Set name:<td><input type='text' name='name-$cnt' maxlength='200'>\n";
50                         print "<tr><td>Set description:<td><input type='text' name='description-$cnt' maxlength='1024'>\n";
51                         print "<tr><td>Text:<td><textarea name='text-$cnt' rows='2'></textarea>\n";
52                         print "</table>\n";
53                 }
54                 print "<div class='unseen-comment'>\n";
55                 print "<p class='name'>".encode( $name ) if( defined( $name ) );
56                 print "<p class='description'>".encode( $description ) if( defined( $description ) );
57                 print '<p>'.encode( $text ) if( defined( $text ) );
58                 print "<p class='author'>".encode( $user ) if( defined( $user ) );
59                 print "<p><input type='radio' name='action-$cnt' value='set-$com'> Use this one.\n" if( defined( $name ) && ( $name ne "" ) );
60                 $hiscnt ++;
61                 print "<br><input type='checkbox' name='delete-$hiscnt' value='delete-$com'> Delete comment.\n";
62                 print "</div>\n";
63                 $subcnt ++;
64                 print "<input type='hidden' name='sub-$cnt-$subcnt' value='$com'>\n";
65         }
66         print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
67         if( $started ) {
68                 print "</div>\n" if( $started );
69                 print "<p><input type='submit' name='submit' value='Submit'>\n";
70                 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
71         } else {
72                 print "<p>No pending comments.\n";
73         }
74         print "</form>\n";
75         genHtmlTail();
76         return OK;
77 }
78
79 sub adminForm( $$$$ ) {
80         my( $req, $args, $tables, $auth ) = @_;
81         if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
82                 return genNewAdminForm( $req, $args, $tables, undef );
83         } else {
84                 return notLoggedComplaint( $req, $args, $auth );
85         }
86 }
87
88 sub markAllChecked( $$$$ ) {
89         my( $tables, $itemNum, $deleted, $authid ) = @_;
90         my $i;
91         my $subcnt = getFormValue( "subcnt-$itemNum", 0 );
92         for( $i = 1; $i <= $subcnt; ++ $i ) {
93                 my $id = getFormValue( "sub-$itemNum-$i", undef );
94                 next unless( defined( $id ) );
95                 next if( $deleted->{$id} );#Do not update this one, already deleted
96                 $tables->markChecked( $id );
97                 tulog( $authid, "Comment checked $id" );
98         }
99 }
100
101 sub submitAdminForm( $$$$ ) {
102         my( $req, $args, $tables, $auth ) = @_;
103         my $authid = $auth->{'authid'};
104         if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
105                 my $errors = '';
106                 my %deleted;
107                 my $maxcnt = getFormValue( 'max-cnt', 0 );
108                 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
109                 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
110                         my $del = getFormValue( "delete-$i", "" );
111                         $del =~ s/^delete-//;
112                         if( $del ne '' ) {
113                                 $deleted{$del} = 1;
114                                 $tables->deleteHistory( $del );
115                                 tulog( $authid, "Comment deleted $del" );
116                         }
117                 }
118                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
119                         my $action = getFormValue( "action-$i", 'ignore' );
120                         my $loc = getFormValue( "loc-$i", undef );
121                         next unless( defined( $loc ) );
122                         my( $text, $name, $description ) = (
123                                 getFormValue( "text-$i", undef ),
124                                 getFormValue( "name-$i", undef ),
125                                 getFormValue( "description-$i", undef ) );
126                         if( defined( $description ) && ( $description ne '' ) && ( !defined( $name ) || ( length $name < 3 ) ) ) {
127                                 if( $errors eq '' ) {
128                                         $errors = '<p>';
129                                 } else {
130                                         $errors .= '<br>';
131                                 }
132                                 $errors .= "$loc - You need to provide name if you provide description\n";
133                                 next;
134                         }
135                         if( ( defined( $name ) && ( length $name >= 3 ) ) || ( defined( $text ) && ( $text ne '' ) ) ) { #Submited comment
136                                 my $addr = PciIds::Address::new( $loc );
137                                 my $comId = $tables->submitComment( { 'name' => $name, 'description' => $description, 'explanation' => $text }, $auth, $addr );
138                                 my $main = defined $name && ( $name ne '' );
139                                 notify( $tables, $addr->get(), $comId, $main ? 2 : 0, $main ? 2 : 1 );
140                                 $tables->markChecked( $comId );
141                                 tulog( $authid, "Comment created (admin) $comId $loc ".logEscape( $name )." ".logEscape( $description )." ".logEscape( $text ) );
142                                 if( defined( $name ) && ( length $name >= 3 ) ) {
143                                         $tables->setMainComment( $loc, $comId );
144                                         tulog( $authid, "Item main comment changed $loc $comId" );
145                                         $action = 'keep';
146                                 }
147                         }
148                         next if( $action eq 'ignore' );
149                         if( $action eq 'keep' ) {
150                                 markAllChecked( $tables, $i, \%deleted, $authid );
151                         } elsif( $action eq 'delete' ) {
152                                 eval {
153                                         $tables->deleteItem( $loc );
154                                         tulog( $authid, "Item deleted (recursive) $loc" );
155                                 } #Ignore if it was already deleted by superitem
156                         } elsif( my( $setId ) = ( $action =~ /set-(.*)/ ) ) {
157                                 next if( $deleted{$setId} );
158                                 $tables->setMainComment( $loc, $setId );
159                                 notify( $tables, $loc, $setId, 2, 2 );
160                                 tulog( $authid, "Item main comment changed $loc $setId" );
161                                 markAllChecked( $tables, $i, \%deleted, $authid );
162                         }
163                 }
164                 return genNewAdminForm( $req, $args, $tables, $errors );
165         } else {
166                 return notLoggedComplaint( $req, $args, $auth );
167         }
168 }
169
170 1;