1 package PciIds::Html::Admin;
5 use PciIds::Html::Util;
6 use PciIds::Html::Users;
7 use PciIds::Html::Forms;
8 use PciIds::Notifications;
10 use Apache2::Const qw(:common :http);
12 sub genNewAdminForm( $$$$ ) {
13 my( $req, $args, $tables, $error ) = @_;
14 genHtmlHead( $req, 'Administration ‒ pending events', undef );
15 print "<h1>Administration ‒ pending events</h1>\n";
16 print "<div class='error'>".$error."</div>\n" if( defined $error );
17 print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
23 foreach( @{$tables->adminDump()} ) {
24 my( $locId, $actName, $actNote, $actHist, $actUser, $actDisc,
25 $hist, $disc, $name, $note, $user ) = @{$_};
26 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
28 print "</div>\n" if( $started );
30 print "<div class='".( defined( $actHist ) ? 'item' : 'unnamedItem' )."'>\n";
31 my $addr = PciIds::Address::new( $locId );
32 print "<h3><a href='/read/".$addr->get()."/'>".encode( $addr->pretty() )."</a></h3>\n";
33 print htmlDiv( 'name', '<p>'.encode( $actName ) ) if( defined( $actName ) );
34 print htmlDiv( 'note', '<p>'.encode( $actNote ) ) if( defined( $actNote ) );
35 print '<p>'.encode( $actDisc ) if( defined( $actDisc ) );
36 print '<p><a class="navigation" href="/read/'.$addr->parent()->get().'/">'.encode( $addr->parent()->pretty() )."</a>" if( defined( $addr->parent() ) );
37 print htmlDiv( 'author', '<p>'.encode( $actUser ) ) if( defined( $actUser ) );
38 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
41 print "<input type='hidden' name='loc-$cnt' value='".$addr->get()."'>\n";
42 print "<p><input type='radio' name='action-$cnt' value='ignore' checked='checked'> I will decide later.\n";
43 if( defined( $actHist ) ) {
44 print "<br><input type='radio' name='action-$cnt' value='keep'> Keep current name.\n";
46 print "<br><input type='radio' name='action-$cnt' value='delete'> Delete item.\n";
47 print "<br>Add discussion:\n";
48 print "<br><table>\n";
49 print "<tr><td>Set name:<td><input type='text' name='name-$cnt' maxlength='200'>\n";
50 print "<tr><td>Set note:<td><input type='text' name='note-$cnt' maxlength='1024'>\n";
51 print "<tr><td>Discussion:<td><textarea name='discussion-$cnt' rows='2'></textarea>\n";
54 print "<div class='unseen-history'>\n";
55 print "<p class='name'>".encode( $name ) if( defined( $name ) );
56 print "<p class='note'>".encode( $note ) if( defined( $note ) );
57 print '<p>'.encode( $disc ) if( defined( $disc ) );
58 print "<p class='author'>".encode( $user ) if( defined( $user ) );
59 print "<p><input type='radio' name='action-$cnt' value='set-$hist'> Use this one.\n" if( defined( $name ) && ( $name ne "" ) );
61 print "<br><input type='checkbox' name='delete-$hiscnt' value='delete-$hist'> Delete history.\n";
64 print "<input type='hidden' name='sub-$cnt-$subcnt' value='$hist'>\n";
66 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
68 print "</div>\n" if( $started );
69 print "<p><input type='submit' name='submit' value='Submit'>\n";
70 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
72 print "<p>No pending items.\n";
79 sub adminForm( $$$$ ) {
80 my( $req, $args, $tables, $auth ) = @_;
81 if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
82 return genNewAdminForm( $req, $args, $tables, undef );
84 return notLoggedComplaint( $req, $args, $auth );
88 sub markAllChecked( $$$$ ) {
89 my( $tables, $itemNum, $deleted, $authid ) = @_;
91 my $subcnt = getFormValue( "subcnt-$itemNum", 0 );
92 for( $i = 1; $i <= $subcnt; ++ $i ) {
93 my $id = getFormValue( "sub-$itemNum-$i", undef );
94 next unless( defined( $id ) );
95 next if( $deleted->{$id} );#Do not update this one, already deleted
96 $tables->markChecked( $id );
97 tulog( $authid, "Discussion checked $id" );
101 sub submitAdminForm( $$$$ ) {
102 my( $req, $args, $tables, $auth ) = @_;
103 my $authid = $auth->{'authid'};
104 if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
107 my $maxcnt = getFormValue( 'max-cnt', 0 );
108 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
109 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
110 my $del = getFormValue( "delete-$i", "" );
111 $del =~ s/^delete-//;
114 $tables->deleteHistory( $del );
115 tulog( $authid, "Discussion deleted $del" );
118 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
119 my $action = getFormValue( "action-$i", 'ignore' );
120 my $loc = getFormValue( "loc-$i", undef );
121 next unless( defined( $loc ) );
122 my( $discussion, $name, $note ) = (
123 getFormValue( "discussion-$i", undef ),
124 getFormValue( "name-$i", undef ),
125 getFormValue( "note-$i", undef ) );
126 if( defined( $note ) && ( $note ne '' ) && ( !defined( $name ) || ( length $name < 3 ) ) ) {
127 if( $errors eq '' ) {
132 $errors .= "$loc - You need to provide name if you provide note\n";
135 if( ( defined( $name ) && ( length $name >= 3 ) ) || ( defined( $discussion ) && ( $discussion ne '' ) ) ) { #Submited comment
136 my $addr = PciIds::Address::new( $loc );
137 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion }, $auth, $addr );
138 my $main = defined $name && ( $name ne '' );
139 notify( $tables, $addr->get(), $histId, $main ? 2 : 0, $main ? 2 : 1 );
140 $tables->markChecked( $histId );
141 tulog( $authid, "Discussion submited (admin) $histId $loc ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
142 if( defined( $name ) && ( length $name >= 3 ) ) {
143 $tables->setMainHistory( $loc, $histId );
144 tulog( $authid, "Item main history changed $loc $histId" );
148 next if( $action eq 'ignore' );
149 if( $action eq 'keep' ) {
150 markAllChecked( $tables, $i, \%deleted, $authid );
151 } elsif( $action eq 'delete' ) {
153 $tables->deleteItem( $loc );
154 tulog( $authid, "Item deleted (recursive) $loc" );
155 } #Ignore if it was already deleted by superitem
156 } elsif( my( $setId ) = ( $action =~ /set-(.*)/ ) ) {
157 next if( $deleted{$setId} );
158 $tables->setMainHistory( $loc, $setId );
159 notify( $tables, $loc, $setId, 2, 2 );
160 tulog( $authid, "Item main history changed $loc $setId" );
161 markAllChecked( $tables, $i, \%deleted, $authid );
164 return genNewAdminForm( $req, $args, $tables, $errors );
166 return notLoggedComplaint( $req, $args, $auth );