From d8d36960220afc6ea41b26c98c9cb3aba5a05bab Mon Sep 17 00:00:00 2001 From: Michal Vaner Date: Mon, 1 Sep 2008 23:11:23 +0200 Subject: [PATCH] Administration interface works It still needs to send notifications --- PciIds/DBQ.pm | 12 +++- PciIds/Html/Admin.pm | 151 ++++++++++++++++++++++++++----------------- PciIds/Html/List.pm | 2 +- rights | 1 + 4 files changed, 104 insertions(+), 62 deletions(-) diff --git a/PciIds/DBQ.pm b/PciIds/DBQ.pm index 20941c0..c6de46c 100644 --- a/PciIds/DBQ.pm +++ b/PciIds/DBQ.pm @@ -95,17 +95,23 @@ sub new( $ ) { 'time' => 'SELECT NOW()', 'searchname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? ORDER BY l.id', 'searchlocalname' => 'SELECT l.id, l.name, p.name FROM locations AS l JOIN locations AS p ON l.parent = p.id WHERE l.name LIKE ? AND l.id LIKE ? ORDER BY l.id', - 'hasChildren' => 'SELECT 1 FROM locations WHERE parent = ?' + 'hasChildren' => 'SELECT DISTINCT 1 FROM locations WHERE parent = ?', + 'hasMain' => 'SELECT DISTINCT 1 FROM locations WHERE id = ? AND mainhistory IS NOT NULL' } ); } -my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 ); - sub hasChildren( $$ ) { my( $self, $parent ) = @_; return scalar @{$self->query( 'hasChildren', [ $parent ] )}; } +sub hasMain( $$ ) { + my( $self, $id ) = @_; + return scalar @{$self->query( 'hasMain', [ $id ] )}; +} + +my %sorts = ( 'id' => 1, 'rid' => 1, 'name' => 1, 'rname' => 1 ); + sub nodes( $$$$ ) { my( $self, $parent, $args, $restrict ) = @_; my $q = 'id'; diff --git a/PciIds/Html/Admin.pm b/PciIds/Html/Admin.pm index 1307b55..3d8e438 100644 --- a/PciIds/Html/Admin.pm +++ b/PciIds/Html/Admin.pm @@ -37,6 +37,7 @@ sub genNewAdminForm( $$$$$ ) { my $cnt = 0; my $hiscnt = 0; my $subcnt; + print "

Any action approves all discussion\n"; print "\n"; print "\n"; print "
IDNameNoteDiscussionAuthorOkSelDel\n"; @@ -55,6 +56,7 @@ sub genNewAdminForm( $$$$$ ) { } print "".encode( $addr->pretty() )."".safeEncode( $actName )."".safeEncode( $actNote )."".safeEncode( $actDisc )."".mailEncode( $actUser ); + print "" if( $subcnt ); $subcnt = 0; $cnt++; print "\n"; @@ -68,11 +70,12 @@ sub genNewAdminForm( $$$$$ ) { print "
New:\n"; print ""; genPathBare( $req, $addr, 0, 0 ); - print "\n"; + print "\n"; } print "
".safeEncode( ( defined $name && $name eq '' ) ? 'Deletion request' : $name )."".safeEncode( $note )."".safeEncode( $disc )."".mailEncode( $user ); $hiscnt ++; $subcnt ++; + print ""; print ""; if( defined $name ) { print ""; @@ -85,6 +88,7 @@ sub genNewAdminForm( $$$$$ ) { print "
\n"; print "\n" if( defined( $subcnt ) ); if( $started ) { + print "" if( $subcnt ); print "

\n"; print "\n"; } else { @@ -104,16 +108,13 @@ sub adminForm( $$$$ ) { } } -sub markAllChecked( $$$$ ) { - my( $tables, $itemNum, $deleted, $authid ) = @_; - my $i; - my $subcnt = getFormValue( "subcnt-$itemNum", 0 ); - for( $i = 1; $i <= $subcnt; ++ $i ) { - my $id = getFormValue( "sub-$itemNum-$i", undef ); - next unless( defined( $id ) ); - next if( $deleted->{$id} );#Do not update this one, already deleted - $tables->markChecked( $id ); - tulog( $authid, "Discussion checked $id" ); +my $errors; + +sub appendError( $ ) { + if( $errors eq '' ) { + $errors = "

".shift; + } else { + $errors .= "
".shift; } } @@ -121,63 +122,97 @@ sub submitAdminForm( $$$$ ) { my( $req, $args, $tables, $auth ) = @_; my $authid = $auth->{'authid'}; if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) { - my $errors = ''; - my %deleted; + my( %deleted, %approved ); my $maxcnt = getFormValue( 'max-cnt', 0 ); my $maxhiscnt = getFormValue( 'max-hiscnt', 0 ); + $errors = ''; + # Scan for approved and deleted items for( my $i = 1; $i <= $maxhiscnt; $i ++ ) { - my $del = getFormValue( "delete-$i", "" ); - $del =~ s/^delete-//; - if( $del ne '' ) { - $deleted{$del} = 1; - $tables->deleteHistory( $del ); - tulog( $authid, "Discussion deleted $del" ); + my( $del ) = getFormValue( "del-$i", '' ) =~ /^del-(\d+)$/; + $deleted{$del} = 1 if( defined $del && $del ne '' ); + my( $appr ) = getFormValue( "appr-$i", '' ) =~ /^appr-(\d+)$/; + $approved{$appr} = 1 if( defined $appr && $appr ne '' ); + } + for( my $i = 1; $i <= $maxcnt; $i ++ ) { + my( $sel ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/; + $approved{$sel} = 1 if( defined $sel && $sel ne '' ); + } + # Check for collisions + my %collision; + foreach my $id ( keys %deleted ) { + if( $approved{$id} ) { + my $owner = getFormValue( "owner-$id", '' ); + appendError( "You can not approve and delete history at the same time, not modifying item ".PciIds::Address::new( $owner )->pretty() ); + $collision{$owner} = $_; + delete $deleted{$id}; + delete $approved{$id}; } } + #Do the deletes and approves + foreach my $del ( keys %deleted ) { + $tables->deleteHistory( $del ); + #TODO notify + tulog( $authid, "Discussion deleted $del" ); + } + foreach my $appr ( keys %approved ) { + $tables->markChecked( $appr ); + #TODO notify + tulog( $authid, "Discussion checked $appr" ); + } + #Handle the items + my $defaultSeen = getFormValue( 'default-seen', '' ) =~ /^default-seen$/; for( my $i = 1; $i <= $maxcnt; $i ++ ) { - my $action = getFormValue( "action-$i", 'ignore' ); - my $loc = getFormValue( "loc-$i", undef ); - next unless( defined( $loc ) ); - my( $discussion, $name, $note ) = ( - getFormValue( "discussion-$i", undef ), - getFormValue( "name-$i", undef ), - getFormValue( "note-$i", undef ) ); - if( defined( $note ) && ( $note ne '' ) && ( !defined( $name ) || ( length $name < 3 ) ) ) { - if( $errors eq '' ) { - $errors = '

'; - } else { - $errors .= '
'; - } - $errors .= "$loc - You need to provide name if you provide note\n"; + my $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) ); + next if $collision{$addr->get()}; + next unless defined $addr; + my $del = getFormValue( "loc-$i-del", '' ); + if( defined $del && $del eq 'del' && ( hasRight( $auth->{'accrights'}, 'prune' ) || ( !$tables->hasChildren( $addr->get() ) && !$tables->hasMain( $addr->get() ) ) ) ) { + $tables->deleteItem( $addr->get() ); + #TODO notify + tulog( $authid, "Item deleted (recursive) ".$addr->get() ); + next; + } + my $name = getFormValue( "name-$i", undef ); + $name = undef if defined $name && $name eq ''; + my $note = getFormValue( "note-$i", undef ); + $note = undef if defined $note && $note eq ''; + my $discussion = getFormValue( "disc-$i", '' ); + $discussion = undef if defined $discussion && $discussion eq ''; + my $delete = 0; + if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) { + $delete = 1; + $name = undef; + $note = undef; + } + if( defined $note && !defined $name ) { + appendError( "You must specify name if you set note at item ".$addr->pretty() ); next; } - if( ( defined( $name ) && ( length $name >= 3 ) ) || ( defined( $discussion ) && ( $discussion ne '' ) ) ) { #Submited comment - my $addr = PciIds::Address::new( $loc ); - my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion }, $auth, $addr ); - my $main = defined $name && ( $name ne '' ); - notify( $tables, $addr->get(), $histId, $main ? 2 : 0, $main ? 2 : 1 ); + my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/; + my $action = 0; + if( defined $name || defined $discussion || $delete ) { + my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion, 'delete' => $delete }, $auth, $addr ); $tables->markChecked( $histId ); - tulog( $authid, "Discussion submited (admin) $histId $loc ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) ); - if( defined( $name ) && ( length $name >= 3 ) ) { - $tables->setMainHistory( $loc, $histId ); - tulog( $authid, "Item main history changed $loc $histId" ); - $action = 'keep'; - } + $select = $histId if defined $name || $delete; + tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) ); + $action = 1; + #TODO notify + } + if( defined $select && select ne '' ) { + $tables->setMainHistory( $addr->get(), $select ); + tulog( $authid, "Item main history changed ".$addr->get()." $select" ); + $action = 1; + #TODO Notify } - next if( $action eq 'ignore' ); - if( $action eq 'keep' ) { - markAllChecked( $tables, $i, \%deleted, $authid ); - } elsif( $action eq 'delete' ) { - eval { - $tables->deleteItem( $loc ); - tulog( $authid, "Item deleted (recursive) $loc" ); - } #Ignore if it was already deleted by superitem - } elsif( my( $setId ) = ( $action =~ /set-(.*)/ ) ) { - next if( $deleted{$setId} ); - $tables->setMainHistory( $loc, $setId ); - notify( $tables, $loc, $setId, 2, 2 ); - tulog( $authid, "Item main history changed $loc $setId" ); - markAllChecked( $tables, $i, \%deleted, $authid ); + if( $action && $defaultSeen ) {#Approve anything in this item + my $subcnt = getFormValue( "loc-$i-subcnt", 0 ); + for( my $j = 1; $j <= $subcnt; $j ++ ) { + my( $id ) = getFormValue( "his-$i-$j", '' ) =~ /^(\d+)$/; + next unless defined $id; + next if $approved{$id} || $deleted{$id}; + $tables->markChecked( $id ); + tulog( $authid, "Discussion checked $id" ); + } } } return genNewAdminForm( $req, $args, $tables, $errors, $auth ); diff --git a/PciIds/Html/List.pm b/PciIds/Html/List.pm index 911e9f8..73815c1 100644 --- a/PciIds/Html/List.pm +++ b/PciIds/Html/List.pm @@ -34,7 +34,7 @@ sub list( $$$$ ) { print '

'.encode( $id ).'

'; print htmlDiv( 'name', '

Name: '.encode( $name ) ); } - print htmlDiv( 'note', '

Note: '.encode( $note ) ) if defined( $note ); + print htmlDiv( 'note', '

Note: '.encode( $note ) ) if defined $note && $note ne ''; my $diss = 0; my $history; foreach $history ( @{$tables->history( $address->get() )} ) { diff --git a/rights b/rights index cd7fd40..a1cd64a 100644 --- a/rights +++ b/rights @@ -1,2 +1,3 @@ 1 validate 2 listUsers +3 prune -- 2.39.2