1 package PciIds::Html::Admin;
5 use PciIds::Html::Util;
6 use PciIds::Html::Users;
7 use PciIds::Html::Forms;
8 use PciIds::Notifications;
11 use Apache2::Const qw(:common :http);
15 return encode( $text ) if defined $text;
21 return '' unless defined $email;
22 return "<a href='mailto:$email'>".encode( $email )."</a>";
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";
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 );
51 my $addr = PciIds::Address::new( $locId );
52 if( defined( $actHist ) ) {
53 print "<tr class='item'>";
55 print "<tr class='unnamedItem'>";
57 print "<td><a href='/read/".$addr->get()."'>".encode( $addr->pretty() )."</a><td>".safeEncode( $actName )."<td>".safeEncode( $actNote )."<td>".safeEncode( $actDisc )."<td>".mailEncode( $actUser );
59 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
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";
68 print "<td class='empty'>";
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";
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 );
78 print "<input type='hidden' name='his-$cnt-$subcnt' value='$hist'>";
79 print "<td><input type='checkbox' name='appr-$hiscnt' value='appr-$hist'>";
81 print "<td><input type='radio' name='loc-$cnt-sel' value='$hist'>";
83 print "<td class='empty'>";
85 print "<td><input type='checkbox' name='del-$hiscnt' value='del-$hist'>";
86 print "<input type='hidden' name='owner-$hist' value='$lastId'>\n";
89 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
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";
95 print "<p>No pending items.\n";
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 );
107 return notLoggedComplaint( $req, $args, $auth );
113 sub appendError( $ ) {
114 if( $errors eq '' ) {
115 $errors = "<p>".shift;
117 $errors .= "<br>".shift;
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 );
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 '' );
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 '' );
140 # Check for collisions
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};
151 #Do the deletes and approves
152 foreach my $del ( keys %deleted ) {
153 $tables->deleteHistory( $del );
155 tulog( $authid, "Discussion deleted $del" );
157 foreach my $appr ( keys %approved ) {
158 $tables->markChecked( $appr );
160 tulog( $authid, "Discussion checked $appr" );
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() );
172 tulog( $authid, "Item deleted (recursive) ".$addr->get() );
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 '';
182 if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
187 if( defined $note && !defined $name ) {
188 appendError( "You must specify name if you set note at item ".$addr->pretty() );
191 my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
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 ) );
201 if( defined $select && select ne '' ) {
202 $tables->setMainHistory( $addr->get(), $select );
203 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
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" );
218 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
220 return notLoggedComplaint( $req, $args, $auth );