]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Admin.pm
Administration interface works
[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::Address;
10 use PciIds::Log;
11 use Apache2::Const qw(:common :http);
12
13 sub safeEncode( $ ) {
14         my( $text ) = @_;
15         return encode( $text ) if defined $text;
16         return '';
17 }
18
19 sub mailEncode( $ ) {
20         my( $email ) = @_;
21         return '' unless defined $email;
22         return "<a href='mailto:$email'>".encode( $email )."</a>";
23 }
24
25 sub genNewAdminForm( $$$$$ ) {
26         my( $req, $args, $tables, $error, $auth ) = @_;
27         my $address = PciIds::Address::new( $req->uri() );
28         my $prefix = $address->get();
29         $prefix = '' if( $args->{'global'} );
30         my $caption = 'Administration '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
31         genHtmlHead( $req, $caption, undef );
32         genCustomHead( $req, $args, $address, $caption, [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Help', 'help', 'admin' ], [ '', 'jump' ] ], [ [ 'Log out', 'logout' ] ] );
33         print "<div class='error'>$error</div>\n" if( defined $error );
34         print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
35         my $lastId;
36         my $started = 0;
37         my $cnt = 0;
38         my $hiscnt = 0;
39         my $subcnt;
40         print "<p><input type='checkbox' name='default-seen' value='default-seen' checked='checked'> Any action approves all discussion\n";
41         print "<table class='admin'>\n";
42         print "<col class='id-col'><col class='name-col'><col class='note-col'><col class='disc-col'><col class='auth-col'><col class='control-col' span='3'>\n";
43         print "<tr class='head'><th>ID<th>Name<th>Note<th>Discussion<th>Author<th>Ok<th>Sel<th>Del\n";
44         foreach( @{$tables->adminDump( $prefix )} ) {
45                 my( $locId, $actName, $actNote, $actHist, $actUser, $actDisc,
46                         $hist, $disc, $name, $note, $user ) = @{$_};
47                 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
48                         last if( $hiscnt > 80 );
49                         $lastId = $locId;
50                         $started = 1;
51                         my $addr = PciIds::Address::new( $locId );
52                         if( defined( $actHist ) ) {
53                                 print "<tr class='item'>";
54                         } else {
55                                 print "<tr class='unnamedItem'>";
56                         }
57                         print "<td><a href='/read/".$addr->get()."'>".encode( $addr->pretty() )."</a><td>".safeEncode( $actName )."<td>".safeEncode( $actNote )."<td>".safeEncode( $actDisc )."<td>".mailEncode( $actUser );
58
59                         print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
60                         $subcnt = 0;
61                         $cnt++;
62                         print "<input type='hidden' name='loc-$cnt' value='".$addr->get()."'>\n";
63                         print "<td class='empty'>";
64                         print "<td><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
65                         if( hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) ) ) {
66                                 print "<td><input type='checkbox' name='loc-$cnt-del' value='del'>\n";
67                         } else {
68                                 print "<td class='empty'>";
69                         }
70                         print "<tr class='new'><td>New:<td><input type='text' name='name-$cnt' class='text'><td><input type='text' name='note-$cnt' class='text'><td><textarea name='disc-$cnt'></textarea>\n";
71                         print "<td colspan='3'>";
72                         genPathBare( $req, $addr, 0, 0 );
73                         print "<td><input type='checkbox' name='loc-$cnt-softdel' value='del'>\n";
74                 }
75                 print "<tr class='unseen-history'><td class='empty'><td>".safeEncode( ( defined $name && $name eq '' ) ? 'Deletion request' : $name )."<td>".safeEncode( $note )."<td>".safeEncode( $disc )."<td>".mailEncode( $user );
76                 $hiscnt ++;
77                 $subcnt ++;
78                 print "<input type='hidden' name='his-$cnt-$subcnt' value='$hist'>";
79                 print "<td><input type='checkbox' name='appr-$hiscnt' value='appr-$hist'>";
80                 if( defined $name ) {
81                         print "<td><input type='radio' name='loc-$cnt-sel' value='$hist'>";
82                 } else {
83                         print "<td class='empty'>";
84                 }
85                 print "<td><input type='checkbox' name='del-$hiscnt' value='del-$hist'>";
86                 print "<input type='hidden' name='owner-$hist' value='$lastId'>\n";
87         }
88         print "</table>\n";
89         print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
90         if( $started ) {
91                 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
92                 print "<p><input type='submit' name='submit' value='Submit'>\n";
93                 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
94         } else {
95                 print "<p>No pending items.\n";
96         }
97         print "</form>\n";
98         genHtmlTail();
99         return OK;
100 }
101
102 sub adminForm( $$$$ ) {
103         my( $req, $args, $tables, $auth ) = @_;
104         if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
105                 return genNewAdminForm( $req, $args, $tables, undef, $auth );
106         } else {
107                 return notLoggedComplaint( $req, $args, $auth );
108         }
109 }
110
111 my $errors;
112
113 sub appendError( $ ) {
114         if( $errors eq '' ) {
115                 $errors = "<p>".shift;
116         } else {
117                 $errors .= "<br>".shift;
118         }
119 }
120
121 sub submitAdminForm( $$$$ ) {
122         my( $req, $args, $tables, $auth ) = @_;
123         my $authid = $auth->{'authid'};
124         if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
125                 my( %deleted, %approved );
126                 my $maxcnt = getFormValue( 'max-cnt', 0 );
127                 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
128                 $errors = '';
129                 # Scan for approved and deleted items
130                 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
131                         my( $del ) = getFormValue( "del-$i", '' ) =~ /^del-(\d+)$/;
132                         $deleted{$del} = 1 if( defined $del && $del ne '' );
133                         my( $appr ) = getFormValue( "appr-$i", '' ) =~ /^appr-(\d+)$/;
134                         $approved{$appr} = 1 if( defined $appr && $appr ne '' );
135                 }
136                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
137                         my( $sel ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
138                         $approved{$sel} = 1 if( defined $sel && $sel ne '' );
139                 }
140                 # Check for collisions
141                 my %collision;
142                 foreach my $id ( keys %deleted ) {
143                         if( $approved{$id} ) {
144                                 my $owner = getFormValue( "owner-$id", '' );
145                                 appendError( "You can not approve and delete history at the same time, not modifying item ".PciIds::Address::new( $owner )->pretty() );
146                                 $collision{$owner} = $_;
147                                 delete $deleted{$id};
148                                 delete $approved{$id};
149                         }
150                 }
151                 #Do the deletes and approves
152                 foreach my $del ( keys %deleted ) {
153                         $tables->deleteHistory( $del );
154                         #TODO notify
155                         tulog( $authid, "Discussion deleted $del" );
156                 }
157                 foreach my $appr ( keys %approved ) {
158                         $tables->markChecked( $appr );
159                         #TODO notify
160                         tulog( $authid, "Discussion checked $appr" );
161                 }
162                 #Handle the items
163                 my $defaultSeen = getFormValue( 'default-seen', '' ) =~ /^default-seen$/;
164                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
165                         my $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) );
166                         next if $collision{$addr->get()};
167                         next unless defined $addr;
168                         my $del = getFormValue( "loc-$i-del", '' );
169                         if( defined $del && $del eq 'del' && ( hasRight( $auth->{'accrights'}, 'prune' ) || ( !$tables->hasChildren( $addr->get() ) && !$tables->hasMain( $addr->get() ) ) ) ) {
170                                 $tables->deleteItem( $addr->get() );
171                                 #TODO notify
172                                 tulog( $authid, "Item deleted (recursive) ".$addr->get() );
173                                 next;
174                         }
175                         my $name = getFormValue( "name-$i", undef );
176                         $name = undef if defined $name && $name eq '';
177                         my $note = getFormValue( "note-$i", undef );
178                         $note = undef if defined $note && $note eq '';
179                         my $discussion = getFormValue( "disc-$i", '' );
180                         $discussion = undef if defined $discussion && $discussion eq '';
181                         my $delete = 0;
182                         if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
183                                 $delete = 1;
184                                 $name = undef;
185                                 $note = undef;
186                         }
187                         if( defined $note && !defined $name ) {
188                                 appendError( "You must specify name if you set note at item ".$addr->pretty() );
189                                 next;
190                         }
191                         my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
192                         my $action = 0;
193                         if( defined $name || defined $discussion || $delete ) {
194                                 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion, 'delete' => $delete }, $auth, $addr );
195                                 $tables->markChecked( $histId );
196                                 $select = $histId if defined $name || $delete;
197                                 tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
198                                 $action = 1;
199                                 #TODO notify
200                         }
201                         if( defined $select && select ne '' ) {
202                                 $tables->setMainHistory( $addr->get(), $select );
203                                 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
204                                 $action = 1;
205                                 #TODO Notify
206                         }
207                         if( $action && $defaultSeen ) {#Approve anything in this item
208                                 my $subcnt = getFormValue( "loc-$i-subcnt", 0 );
209                                 for( my $j = 1;  $j <= $subcnt; $j ++ ) {
210                                         my( $id ) = getFormValue( "his-$i-$j", '' ) =~ /^(\d+)$/;
211                                         next unless defined $id;
212                                         next if $approved{$id} || $deleted{$id};
213                                         $tables->markChecked( $id );
214                                         tulog( $authid, "Discussion checked $id" );
215                                 }
216                         }
217                 }
218                 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
219         } else {
220                 return notLoggedComplaint( $req, $args, $auth );
221         }
222 }
223
224 1;