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);
13 sub genNewAdminForm( $$$$ ) {
14 my( $req, $args, $tables, $error ) = @_;
15 my $address = PciIds::Address::new( $req->uri() );
16 my $prefix = $address->get();
17 $prefix = '' if( $args->{'global'} );
18 my $caption = 'Administration ‒ pending events '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
19 genHtmlHead( $req, $caption, undef );
20 genCustomHead( $req, $args, $address, $caption, [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Help', 'help', 'admin' ], [ '', 'jump' ] ], [ [ 'Log out', 'logout' ] ] );
21 print "<div class='error'>$error</div>\n" if( defined $error );
22 print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
28 foreach( @{$tables->adminDump( $prefix )} ) {
29 my( $locId, $actName, $actNote, $actHist, $actUser, $actDisc,
30 $hist, $disc, $name, $note, $user ) = @{$_};
31 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
33 print "</div>\n" if( $started );
35 print "<div class='".( defined( $actHist ) ? 'item' : 'unnamedItem' )."'>\n";
36 my $addr = PciIds::Address::new( $locId );
37 print "<h3><a href='/read/".$addr->get()."/'>".encode( $addr->pretty() )."</a></h3>\n";
38 print htmlDiv( 'name', '<p>'.encode( $actName ) ) if( defined( $actName ) );
39 print htmlDiv( 'note', '<p>'.encode( $actNote ) ) if( defined( $actNote ) );
40 print '<p>'.encode( $actDisc ) if( defined( $actDisc ) );
41 print '<p><a class="navigation" href="/read/'.$addr->parent()->get().'/">'.encode( $addr->parent()->pretty() )."</a>" if( defined( $addr->parent() ) );
42 print htmlDiv( 'author', '<p>'.encode( $actUser ) ) if( defined( $actUser ) );
43 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
46 print "<input type='hidden' name='loc-$cnt' value='".$addr->get()."'>\n";
47 print "<p><input type='radio' name='action-$cnt' value='ignore' checked='checked'> I will decide later.\n";
48 if( defined( $actHist ) ) {
49 print "<br><input type='radio' name='action-$cnt' value='keep'> Keep current name.\n";
51 print "<br><input type='radio' name='action-$cnt' value='delete'> Delete item.\n";
52 print "<br>Add discussion:\n";
53 print "<br><table>\n";
54 print "<tr><td>Set name:<td><input type='text' name='name-$cnt' maxlength='200'>\n";
55 print "<tr><td>Set note:<td><input type='text' name='note-$cnt' maxlength='1024'>\n";
56 print "<tr><td>Discussion:<td><textarea name='discussion-$cnt' rows='2'></textarea>\n";
59 print "<div class='unseen-history'>\n";
60 print "<p class='name'>".encode( $name ) if( defined( $name ) );
61 print "<p class='note'>".encode( $note ) if( defined( $note ) );
62 print '<p>'.encode( $disc ) if( defined( $disc ) );
63 print "<p class='author'>".encode( $user ) if( defined( $user ) );
64 print "<p><input type='radio' name='action-$cnt' value='set-$hist'> Use this one.\n" if( defined( $name ) && ( $name ne "" ) );
66 print "<br><input type='checkbox' name='delete-$hiscnt' value='delete-$hist'> Delete history.\n";
69 print "<input type='hidden' name='sub-$cnt-$subcnt' value='$hist'>\n";
71 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
73 print "</div>\n" if( $started );
74 print "<p><input type='submit' name='submit' value='Submit'>\n";
75 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
77 print "<p>No pending items.\n";
84 sub adminForm( $$$$ ) {
85 my( $req, $args, $tables, $auth ) = @_;
86 if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
87 return genNewAdminForm( $req, $args, $tables, undef );
89 return notLoggedComplaint( $req, $args, $auth );
93 sub markAllChecked( $$$$ ) {
94 my( $tables, $itemNum, $deleted, $authid ) = @_;
96 my $subcnt = getFormValue( "subcnt-$itemNum", 0 );
97 for( $i = 1; $i <= $subcnt; ++ $i ) {
98 my $id = getFormValue( "sub-$itemNum-$i", undef );
99 next unless( defined( $id ) );
100 next if( $deleted->{$id} );#Do not update this one, already deleted
101 $tables->markChecked( $id );
102 tulog( $authid, "Discussion checked $id" );
106 sub submitAdminForm( $$$$ ) {
107 my( $req, $args, $tables, $auth ) = @_;
108 my $authid = $auth->{'authid'};
109 if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
112 my $maxcnt = getFormValue( 'max-cnt', 0 );
113 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
114 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
115 my $del = getFormValue( "delete-$i", "" );
116 $del =~ s/^delete-//;
119 $tables->deleteHistory( $del );
120 tulog( $authid, "Discussion deleted $del" );
123 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
124 my $action = getFormValue( "action-$i", 'ignore' );
125 my $loc = getFormValue( "loc-$i", undef );
126 next unless( defined( $loc ) );
127 my( $discussion, $name, $note ) = (
128 getFormValue( "discussion-$i", undef ),
129 getFormValue( "name-$i", undef ),
130 getFormValue( "note-$i", undef ) );
131 if( defined( $note ) && ( $note ne '' ) && ( !defined( $name ) || ( length $name < 3 ) ) ) {
132 if( $errors eq '' ) {
137 $errors .= "$loc - You need to provide name if you provide note\n";
140 if( ( defined( $name ) && ( length $name >= 3 ) ) || ( defined( $discussion ) && ( $discussion ne '' ) ) ) { #Submited comment
141 my $addr = PciIds::Address::new( $loc );
142 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion }, $auth, $addr );
143 my $main = defined $name && ( $name ne '' );
144 notify( $tables, $addr->get(), $histId, $main ? 2 : 0, $main ? 2 : 1 );
145 $tables->markChecked( $histId );
146 tulog( $authid, "Discussion submited (admin) $histId $loc ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
147 if( defined( $name ) && ( length $name >= 3 ) ) {
148 $tables->setMainHistory( $loc, $histId );
149 tulog( $authid, "Item main history changed $loc $histId" );
153 next if( $action eq 'ignore' );
154 if( $action eq 'keep' ) {
155 markAllChecked( $tables, $i, \%deleted, $authid );
156 } elsif( $action eq 'delete' ) {
158 $tables->deleteItem( $loc );
159 tulog( $authid, "Item deleted (recursive) $loc" );
160 } #Ignore if it was already deleted by superitem
161 } elsif( my( $setId ) = ( $action =~ /set-(.*)/ ) ) {
162 next if( $deleted{$setId} );
163 $tables->setMainHistory( $loc, $setId );
164 notify( $tables, $loc, $setId, 2, 2 );
165 tulog( $authid, "Item main history changed $loc $setId" );
166 markAllChecked( $tables, $i, \%deleted, $authid );
169 return genNewAdminForm( $req, $args, $tables, $errors );
171 return notLoggedComplaint( $req, $args, $auth );