]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Admin.pm
New admin interface
[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 "<table class='admin'>\n";
41         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";
42         print "<tr class='head'><th>ID<th>Name<th>Note<th>Discussion<th>Author<th>Ok<th>Sel<th>Del\n";
43         foreach( @{$tables->adminDump( $prefix )} ) {
44                 my( $locId, $actName, $actNote, $actHist, $actUser, $actDisc,
45                         $hist, $disc, $name, $note, $user ) = @{$_};
46                 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
47                         last if( $hiscnt > 80 );
48                         $lastId = $locId;
49                         $started = 1;
50                         my $addr = PciIds::Address::new( $locId );
51                         if( defined( $actHist ) ) {
52                                 print "<tr class='item'>";
53                         } else {
54                                 print "<tr class='unnamedItem'>";
55                         }
56                         print "<td><a href='/read/".$addr->get()."'>".encode( $addr->pretty() )."</a><td>".safeEncode( $actName )."<td>".safeEncode( $actNote )."<td>".safeEncode( $actDisc )."<td>".mailEncode( $actUser );
57
58                         $subcnt = 0;
59                         $cnt++;
60                         print "<input type='hidden' name='loc-$cnt' value='".$addr->get()."'>\n";
61                         print "<td class='empty'>";
62                         print "<td><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
63                         if( hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) ) ) {
64                                 print "<td><input type='checkbox' name='loc-$cnt-del' value='del'>\n";
65                         } else {
66                                 print "<td class='empty'>";
67                         }
68                         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";
69                         print "<td colspan='3'>";
70                         genPathBare( $req, $addr, 0, 0 );
71                         print "<td><input type='checkbox' name='loc-$cnt-softdel'>\n";
72                 }
73                 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 );
74                 $hiscnt ++;
75                 $subcnt ++;
76                 print "<td><input type='checkbox' name='appr-$hiscnt' value='appr-$hist'>";
77                 if( defined $name ) {
78                         print "<td><input type='radio' name='loc-$cnt-sel' value='$hist'>";
79                 } else {
80                         print "<td class='empty'>";
81                 }
82                 print "<td><input type='checkbox' name='del-$hiscnt' value='del-$hist'>";
83                 print "<input type='hidden' name='owner-$hist' value='$lastId'>\n";
84         }
85         print "</table>\n";
86         print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
87         if( $started ) {
88                 print "<p><input type='submit' name='submit' value='Submit'>\n";
89                 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
90         } else {
91                 print "<p>No pending items.\n";
92         }
93         print "</form>\n";
94         genHtmlTail();
95         return OK;
96 }
97
98 sub adminForm( $$$$ ) {
99         my( $req, $args, $tables, $auth ) = @_;
100         if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
101                 return genNewAdminForm( $req, $args, $tables, undef, $auth );
102         } else {
103                 return notLoggedComplaint( $req, $args, $auth );
104         }
105 }
106
107 sub markAllChecked( $$$$ ) {
108         my( $tables, $itemNum, $deleted, $authid ) = @_;
109         my $i;
110         my $subcnt = getFormValue( "subcnt-$itemNum", 0 );
111         for( $i = 1; $i <= $subcnt; ++ $i ) {
112                 my $id = getFormValue( "sub-$itemNum-$i", undef );
113                 next unless( defined( $id ) );
114                 next if( $deleted->{$id} );#Do not update this one, already deleted
115                 $tables->markChecked( $id );
116                 tulog( $authid, "Discussion checked $id" );
117         }
118 }
119
120 sub submitAdminForm( $$$$ ) {
121         my( $req, $args, $tables, $auth ) = @_;
122         my $authid = $auth->{'authid'};
123         if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
124                 my $errors = '';
125                 my %deleted;
126                 my $maxcnt = getFormValue( 'max-cnt', 0 );
127                 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
128                 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
129                         my $del = getFormValue( "delete-$i", "" );
130                         $del =~ s/^delete-//;
131                         if( $del ne '' ) {
132                                 $deleted{$del} = 1;
133                                 $tables->deleteHistory( $del );
134                                 tulog( $authid, "Discussion deleted $del" );
135                         }
136                 }
137                 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
138                         my $action = getFormValue( "action-$i", 'ignore' );
139                         my $loc = getFormValue( "loc-$i", undef );
140                         next unless( defined( $loc ) );
141                         my( $discussion, $name, $note ) = (
142                                 getFormValue( "discussion-$i", undef ),
143                                 getFormValue( "name-$i", undef ),
144                                 getFormValue( "note-$i", undef ) );
145                         if( defined( $note ) && ( $note ne '' ) && ( !defined( $name ) || ( length $name < 3 ) ) ) {
146                                 if( $errors eq '' ) {
147                                         $errors = '<p>';
148                                 } else {
149                                         $errors .= '<br>';
150                                 }
151                                 $errors .= "$loc - You need to provide name if you provide note\n";
152                                 next;
153                         }
154                         if( ( defined( $name ) && ( length $name >= 3 ) ) || ( defined( $discussion ) && ( $discussion ne '' ) ) ) { #Submited comment
155                                 my $addr = PciIds::Address::new( $loc );
156                                 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion }, $auth, $addr );
157                                 my $main = defined $name && ( $name ne '' );
158                                 notify( $tables, $addr->get(), $histId, $main ? 2 : 0, $main ? 2 : 1 );
159                                 $tables->markChecked( $histId );
160                                 tulog( $authid, "Discussion submited (admin) $histId $loc ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
161                                 if( defined( $name ) && ( length $name >= 3 ) ) {
162                                         $tables->setMainHistory( $loc, $histId );
163                                         tulog( $authid, "Item main history changed $loc $histId" );
164                                         $action = 'keep';
165                                 }
166                         }
167                         next if( $action eq 'ignore' );
168                         if( $action eq 'keep' ) {
169                                 markAllChecked( $tables, $i, \%deleted, $authid );
170                         } elsif( $action eq 'delete' ) {
171                                 eval {
172                                         $tables->deleteItem( $loc );
173                                         tulog( $authid, "Item deleted (recursive) $loc" );
174                                 } #Ignore if it was already deleted by superitem
175                         } elsif( my( $setId ) = ( $action =~ /set-(.*)/ ) ) {
176                                 next if( $deleted{$setId} );
177                                 $tables->setMainHistory( $loc, $setId );
178                                 notify( $tables, $loc, $setId, 2, 2 );
179                                 tulog( $authid, "Item main history changed $loc $setId" );
180                                 markAllChecked( $tables, $i, \%deleted, $authid );
181                         }
182                 }
183                 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
184         } else {
185                 return notLoggedComplaint( $req, $args, $auth );
186         }
187 }
188
189 1;