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 "<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 );
50 my $addr = PciIds::Address::new( $locId );
51 if( defined( $actHist ) ) {
52 print "<tr class='item'>";
54 print "<tr class='unnamedItem'>";
56 print "<td><a href='/read/".$addr->get()."'>".encode( $addr->pretty() )."</a><td>".safeEncode( $actName )."<td>".safeEncode( $actNote )."<td>".safeEncode( $actDisc )."<td>".mailEncode( $actUser );
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";
66 print "<td class='empty'>";
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";
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 );
76 print "<td><input type='checkbox' name='appr-$hiscnt' value='appr-$hist'>";
78 print "<td><input type='radio' name='loc-$cnt-sel' value='$hist'>";
80 print "<td class='empty'>";
82 print "<td><input type='checkbox' name='del-$hiscnt' value='del-$hist'>";
83 print "<input type='hidden' name='owner-$hist' value='$lastId'>\n";
86 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
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";
91 print "<p>No pending items.\n";
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 );
103 return notLoggedComplaint( $req, $args, $auth );
107 sub markAllChecked( $$$$ ) {
108 my( $tables, $itemNum, $deleted, $authid ) = @_;
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" );
120 sub submitAdminForm( $$$$ ) {
121 my( $req, $args, $tables, $auth ) = @_;
122 my $authid = $auth->{'authid'};
123 if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
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-//;
133 $tables->deleteHistory( $del );
134 tulog( $authid, "Discussion deleted $del" );
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 '' ) {
151 $errors .= "$loc - You need to provide name if you provide note\n";
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" );
167 next if( $action eq 'ignore' );
168 if( $action eq 'keep' ) {
169 markAllChecked( $tables, $i, \%deleted, $authid );
170 } elsif( $action eq 'delete' ) {
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 );
183 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
185 return notLoggedComplaint( $req, $args, $auth );