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;
19 sub mailEncode( $$ ) {
20 my( $email, $user ) = @_;
21 return '' unless defined $email;
22 ( $user ) = $email =~ /^(.*)@/ unless( defined $user );
23 return "<a href='mailto:$email'>".encode( $user )."</a>";
26 sub genHist( $$$$$$$$$$$ ) {
27 my( $class, $email, $login, $time, $name, $note, $disc, $selname, $selvalue, $delname, $delvalue ) = @_;
28 print "<tr class='$class'><td>";
29 print "<span class='author'>".mailEncode( $email, $login )."<br></span>" if( defined $email );
30 print "<span class='time'>".safeEncode( $time )."</span>";
34 print "<span class='name'>Deletion request<br></span>";
36 print "<span class='name'>Name: ".encode( $name )."<br></span>";
39 print "<span class='note'>Note: ".encode( $note )."<br></span>" if defined $note && $note ne '';
40 print safeEncode( $disc );
41 print "<td class='selects'><input type='radio' name='$selname' 'value='$selvalue'>\n";
42 print "<td class='deletes'><input type='checkbox' name='$delname' value='$delvalue'>\n" if defined $delname
47 print "<tr class='newhistory'><td>TODO combo";
48 print "<td><span class='newname'>Name: <input type='text' name='name-$num'></span><span class='newnote'>Note: <input type='note-$num'></span><br>\n";
49 print "<textarea name='disc-$num'></textarea>\n";
50 print "<td><td class='deletes'><input type='checkbox' name='loc-$num-softdel' value='del'>\n";
53 sub genNewAdminForm( $$$$$ ) {
54 my( $req, $args, $tables, $error, $auth ) = @_;
55 my $address = PciIds::Address::new( $req->uri() );
56 my $prefix = $address->get();
57 $prefix = '' if( $args->{'global'} );
58 my $caption = 'Administration '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
59 genHtmlHead( $req, $caption, undef );
60 my $glob = delete $args->{'global'};
61 genCustomHead( $req, $args, $address, $caption, [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), $glob ? [ 'Local', 'admin' ] : [ 'Global', 'admin?global=1' ], [ 'Help', 'help', 'admin' ], [ '', 'jump' ] ], [ [ 'Log out', 'logout' ] ] );
62 print "<div class='error'>$error</div>\n" if( defined $error );
63 print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
69 foreach( @{$tables->adminDump( $prefix )} ) {
70 my( $locId, $actName, $actNote, $actHist, $actEmail, $actLogin, $actDisc, $actTime,
71 $hist, $disc, $name, $note, $email, $login, $time ) = @{$_};
72 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
73 last if( $hiscnt > 80 );
81 my $addr = PciIds::Address::new( $locId );
82 if( defined( $actHist ) ) {
83 print "<table class='item'>\n";
85 print "<table class='unnamedItem'>\n";
87 print "<col class='author'><col class='main'><col class='controls' span='2'>\n";
88 print "<tr class='label'><p>\n";
89 print "<td class='path' colspan='2'>";
90 genPathBare( $req, $addr, 0, 0, 0 );
91 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
94 print "<td class='selects'><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
95 print "<td class='deletes'><input type='checkbox' name='loc-$cnt-del' value='del'>" if hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) );
96 genHist( 'main-history', $actEmail, $actLogin, $actTime, $actName, $actNote, $actDisc, "loc-$cnt-sel", 'seen', undef, undef ) if( defined $actHist );
100 genHist( 'unseen-history', $email, $login, $time, $name, $note, $disc, "loc-$cnt-sel", $hist, "del-$hiscnt", "del-$hist" );
102 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
106 print "<p><input type='submit' name='submit' value='Submit'>\n";
107 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
108 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
110 print "<p>No pending items.\n";
117 sub adminForm( $$$$ ) {
118 my( $req, $args, $tables, $auth ) = @_;
119 if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
120 return genNewAdminForm( $req, $args, $tables, undef, $auth );
122 return notLoggedComplaint( $req, $args, $auth );
128 sub appendError( $ ) {
129 if( $errors eq '' ) {
130 $errors = "<p>".shift;
132 $errors .= "<br>".shift;
136 sub submitAdminForm( $$$$ ) {
137 my( $req, $args, $tables, $auth ) = @_;
138 my $authid = $auth->{'authid'};
139 if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
140 my( %deleted, %approved );
141 my $maxcnt = getFormValue( 'max-cnt', 0 );
142 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
144 # Scan for approved and deleted items
145 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
146 my( $del ) = getFormValue( "del-$i", '' ) =~ /^del-(\d+)$/;
147 $deleted{$del} = 1 if( defined $del && $del ne '' );
148 my( $appr ) = getFormValue( "appr-$i", '' ) =~ /^appr-(\d+)$/;
149 $approved{$appr} = 1 if( defined $appr && $appr ne '' );
151 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
152 my( $sel ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
153 $approved{$sel} = 1 if( defined $sel && $sel ne '' );
155 # Check for collisions
157 foreach my $id ( keys %deleted ) {
158 if( $approved{$id} ) {
159 my $owner = getFormValue( "owner-$id", '' );
160 appendError( "You can not approve and delete history at the same time, not modifying item ".PciIds::Address::new( $owner )->pretty() );
161 $collision{$owner} = $_;
162 delete $deleted{$id};
163 delete $approved{$id};
166 #Do the deletes and approves
167 foreach my $del ( keys %deleted ) {
168 $tables->deleteHistory( $del );
169 tulog( $authid, "Discussion deleted $del" );
171 foreach my $appr ( keys %approved ) {
172 $tables->markChecked( $appr );
173 tulog( $authid, "Discussion checked $appr" );
176 my $defaultSeen = getFormValue( 'default-seen', '' ) =~ /^default-seen$/;
177 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
178 my $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) );
179 next if $collision{$addr->get()};
180 next unless defined $addr;
181 my $del = getFormValue( "loc-$i-del", '' );
182 if( defined $del && $del eq 'del' && ( hasRight( $auth->{'accrights'}, 'prune' ) || ( !$tables->hasChildren( $addr->get() ) && !$tables->hasMain( $addr->get() ) ) ) ) {
183 $tables->deleteItem( $addr->get() );
184 tulog( $authid, "Item deleted (recursive) ".$addr->get() );
187 my $name = getFormValue( "name-$i", undef );
188 $name = undef if defined $name && $name eq '';
189 my $note = getFormValue( "note-$i", undef );
190 $note = undef if defined $note && $note eq '';
191 my $discussion = getFormValue( "disc-$i", '' );
192 $discussion = undef if defined $discussion && $discussion eq '';
194 if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
199 if( defined $note && !defined $name ) {
200 appendError( "You must specify name if you set note at item ".$addr->pretty() );
203 my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
205 if( defined $name || defined $discussion || $delete ) {
206 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion, 'delete' => $delete }, $auth, $addr );
207 $tables->markChecked( $histId );
208 $select = $histId if defined $name || $delete;
209 tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
211 notify( $tables, $addr->get(), $histId, defined $name ? 1 : 0, 1 );
213 if( defined $select && select ne '' ) {
214 $tables->setMainHistory( $addr->get(), $select );
215 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
217 notify( $tables, $addr->get(), $select, 2, 2 );
219 if( $action && $defaultSeen ) {#Approve anything in this item
220 my $subcnt = getFormValue( "loc-$i-subcnt", 0 );
221 for( my $j = 1; $j <= $subcnt; $j ++ ) {
222 my( $id ) = getFormValue( "his-$i-$j", '' ) =~ /^(\d+)$/;
223 next unless defined $id;
224 next if $approved{$id} || $deleted{$id};
225 $tables->markChecked( $id );
226 tulog( $authid, "Discussion checked $id" );
230 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
232 return notLoggedComplaint( $req, $args, $auth );