]> mj.ucw.cz Git - pciids.git/blobdiff - PciIds/Html/Admin.pm
Keep the current ssl mode in links
[pciids.git] / PciIds / Html / Admin.pm
index 3d8e438fc86818f3a71b61b5af280663081453b3..258df13195ecc58a617b1f6944608dc3fc0ab63e 100644 (file)
@@ -1,3 +1,21 @@
+#      PciIds web database
+#      Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
+#
+#      This program is free software; you can redistribute it and/or modify
+#      it under the terms of the GNU General Public License as published by
+#      he Free Software Foundation; either version 2 of the License, or
+#      (at your option) any later version.
+#
+#      This program is distributed in the hope that it will be useful,
+#      but WITHOUT ANY WARRANTY; without even the implied warranty of
+#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#
+#      GNU General Public License for more details.
+#
+#      You should have received a copy of the GNU General Public License
+#      along with this program; if not, write to the Free Software
+#      Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 package PciIds::Html::Admin;
 use strict;
 use warnings;
@@ -8,6 +26,7 @@ use PciIds::Html::Forms;
 use PciIds::Notifications;
 use PciIds::Address;
 use PciIds::Log;
+use PciIds::Startup;
 use Apache2::Const qw(:common :http);
 
 sub safeEncode( $ ) {
@@ -16,20 +35,85 @@ sub safeEncode( $ ) {
        return '';
 }
 
-sub mailEncode( $ ) {
-       my( $email ) = @_;
+sub mailEncode( $$ ) {
+       my( $email, $user ) = @_;
        return '' unless defined $email;
-       return "<a href='mailto:$email'>".encode( $email )."</a>";
+       ( $user ) = $email =~ /^(.*)@/ unless( defined $user );
+       return "<a href='mailto:$email'>".encode( $user )."</a>";
+}
+
+sub genHist( $$$$$$$$$$$ ) {
+       my( $class, $email, $login, $time, $name, $note, $disc, $selname, $selvalue, $delname, $delvalue ) = @_;
+       print "<tr class='$class'><td>";
+       print "<span class='author'>".mailEncode( $email, $login )."<br></span>" if( defined $email );
+       print "<span class='time'>".safeEncode( $time )."</span>";
+       print "<td>";
+       if( defined $name ) {
+               if( $name eq '' ) {
+                       print "<span class='name'>Deletion request<br></span>";
+               } else {
+                       print "<span class='name'>Name: ".encode( $name )."<br></span>";
+               }
+       }
+       print "<span class='note'>Note: ".encode( $note )."<br></span>" if defined $note && $note ne '';
+       print safeEncode( $disc );
+       print "<td class='selects'><input type='radio' name='$selname' value='$selvalue'>\n";
+       print "<td class='deletes'><input type='checkbox' name='$delname' value='$delvalue'>\n" if defined $delname
+}
+
+sub genNewForm( $$ ) {
+       my( $num, $values ) = @_;
+       print "<tr class='newhistory'><td>";
+       if( @{$values} ) {
+               print "<select id='ans-$num' name='ans-$num' onchange='answer( \"$num\" );'><option value='0'>Stock answers</option>";
+               my $i = 0;
+               foreach( @{$values} ) {
+                       $i ++;
+                       my( $name ) = @{$_};
+                       print "<option value='$i'>$name</option>";
+               }
+               print "</select>\n";
+       } else {
+               print "No stock answers";
+       }
+       print "<td><span class='newname'>Name: <input type='text' name='name-$num'></span><span class='newnote'>Note: <input type='text' name='note-$num'></span><br>\n";
+       print "<textarea id='disc-$num' name='disc-$num'></textarea>\n";
+       print "<td><td class='deletes'><input type='checkbox' name='loc-$num-softdel' value='del'>\n";
+}
+
+sub loadStock() {
+       my @stock;
+       if( open ANS, "$directory/answers" ) {
+               my( $name, $text, $lines );
+               while( defined( $name = <ANS> ) ) {
+                       $lines = '';
+                       while( defined( $text = <ANS> ) ) {
+                               chomp $text;
+                               last if $text eq '';
+                               $lines .= "\n" if $lines ne '';
+                               $lines .= $text;
+                       }
+                       push @stock, [ encode( $name ), $lines ];
+               }
+               close ANS;
+       } else {
+               print STDERR "Could not find stock answers.\n";
+       }
+       return \@stock;
 }
 
 sub genNewAdminForm( $$$$$ ) {
        my( $req, $args, $tables, $error, $auth ) = @_;
        my $address = PciIds::Address::new( $req->uri() );
        my $prefix = $address->get();
+       my $limit = delete $args->{'limit'};
        $prefix = '' if( $args->{'global'} );
        my $caption = 'Administration '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
        genHtmlHead( $req, $caption, undef );
-       genCustomHead( $req, $args, $address, $caption, [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), [ 'Help', 'help', 'admin' ], [ '', 'jump' ] ], [ [ 'Log out', 'logout' ] ] );
+       my $glob = delete $args->{'global'};
+       my $moreButt = 'admin?limit='.( $limit ? int( $limit * 2 ) : 160 ).buildExcept( 'action', $args );
+       $moreButt .= '?global='.$glob if defined $glob;
+       genCustomHead( $req, $args, $address, $caption, [ $address->canAddItem() ? [ 'Add item', 'newitem' ] : (), $address->canDiscuss() ? [ 'Discuss', 'newhistory' ] : (), $glob ? [ 'Local', 'admin'.buildExcept( 'action', $args ) ] : [ 'Global', 'admin?global=1'.buildExcept( 'action', $args ) ], [ 'More items', $moreButt ], [ 'Help', 'help', 'admin' ], [ '', 'jump' ] ], [ [ 'Log out', 'logout' ] ] );
        print "<div class='error'>$error</div>\n" if( defined $error );
        print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
        my $lastId;
@@ -37,59 +121,74 @@ sub genNewAdminForm( $$$$$ ) {
        my $cnt = 0;
        my $hiscnt = 0;
        my $subcnt;
-       print "<p><input type='checkbox' name='default-seen' value='default-seen' checked='checked'> Any action approves all discussion\n";
-       print "<table class='admin'>\n";
-       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";
-       print "<tr class='head'><th>ID<th>Name<th>Note<th>Discussion<th>Author<th>Ok<th>Sel<th>Del\n";
-       foreach( @{$tables->adminDump( $prefix )} ) {
-               my( $locId, $actName, $actNote, $actHist, $actUser, $actDisc,
-                       $hist, $disc, $name, $note, $user ) = @{$_};
+       my $stock = loadStock();
+       print "<input type='hidden' name='jscript-active' id='jscript-active' value='0'>";#Trick to find out if user has JScript
+       print "<script type='text/javascript'><!--\n document.getElementById(\"jscript-active\").value = \"1\"; \n";
+       if( @{$stock} ) {
+               print "function answers() {\nreturn new Array( \"\" ";
+               foreach( @{$stock} ) {
+                       my( $x, $text ) = @{$_};
+                       $text =~ s/"/\\"/g;
+                       $text =~ s/\n/\\n/g;
+                       print ', "'.$text.'"';
+               }
+               print ");\n}\n";
+       }
+       print '
+function answer( id ) {
+       var ans, index;
+       ans = answers();
+       index = document.getElementById( "ans-" + id ).value;
+       document.getElementById( "disc-" + id ).value = ans[index];
+}
+';
+       print "// --></script>";
+       foreach( @{$tables->adminDump( $prefix, $limit )} ) {
+               my( $locId, $actName, $actNote, $actHist, $actEmail, $actLogin, $actDisc, $actTime,
+                       $hist, $disc, $name, $note, $email, $login, $time ) = @{$_};
                if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
-                       last if( $hiscnt > 80 );
+                       last if( $hiscnt > ( defined $limit ? $limit : 80 ) );
                        $lastId = $locId;
-                       $started = 1;
+                       if( $started ) {
+                               genNewForm( $cnt, $stock );
+                               print "</table>\n";
+                       } else {
+                               $started = 1;
+                       }
                        my $addr = PciIds::Address::new( $locId );
                        if( defined( $actHist ) ) {
-                               print "<tr class='item'>";
+                               print "<table class='item'>\n";
                        } else {
-                               print "<tr class='unnamedItem'>";
+                               print "<table class='unnamedItem'>\n";
                        }
-                       print "<td><a href='/read/".$addr->get()."'>".encode( $addr->pretty() )."</a><td>".safeEncode( $actName )."<td>".safeEncode( $actNote )."<td>".safeEncode( $actDisc )."<td>".mailEncode( $actUser );
-
+                       print "<col class='author'><col class='main'><col class='controls' span='2'>\n";
+                       print "<tr class='label'><p>\n";
+                       print "<td class='path' colspan='2'>";
+                       genPathBare( $req, $addr, 1, 0 );
                        print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
                        $subcnt = 0;
-                       $cnt++;
-                       print "<input type='hidden' name='loc-$cnt' value='".$addr->get()."'>\n";
-                       print "<td class='empty'>";
-                       print "<td><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
-                       if( hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) ) ) {
-                               print "<td><input type='checkbox' name='loc-$cnt-del' value='del'>\n";
+                       $cnt ++;
+                       print "<td class='selects'><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
+                       print "<td class='deletes'><input type='checkbox' name='loc-$cnt-del' value='del'>" if hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) );
+                       print "<input type='hidden' name='loc-$cnt' value='$locId'>";
+                       if( defined $actHist ) {
+                               genHist( 'main-history', $actEmail, $actLogin, $actTime, $actName, $actNote, $actDisc, "loc-$cnt-sel", 'seen', undef, undef );
                        } else {
-                               print "<td class='empty'>";
+                               print "<tr class='main-history'><td class='empty' colspan='2'><td><input type='radio' name='loc-$cnt-sel' value='seen'>";
                        }
-                       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";
-                       print "<td colspan='3'>";
-                       genPathBare( $req, $addr, 0, 0 );
-                       print "<td><input type='checkbox' name='loc-$cnt-softdel' value='del'>\n";
                }
-               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 );
                $hiscnt ++;
                $subcnt ++;
+               genHist( 'unseen-history', $email, $login, $time, $name, $note, $disc, "loc-$cnt-sel", $hist, "del-$hiscnt", "del-$hist" );
+               print "<input type='hidden' name='owner-$hist' value='$locId'>";
                print "<input type='hidden' name='his-$cnt-$subcnt' value='$hist'>";
-               print "<td><input type='checkbox' name='appr-$hiscnt' value='appr-$hist'>";
-               if( defined $name ) {
-                       print "<td><input type='radio' name='loc-$cnt-sel' value='$hist'>";
-               } else {
-                       print "<td class='empty'>";
-               }
-               print "<td><input type='checkbox' name='del-$hiscnt' value='del-$hist'>";
-               print "<input type='hidden' name='owner-$hist' value='$lastId'>\n";
        }
-       print "</table>\n";
        print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
        if( $started ) {
-               print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
+               genNewForm( $cnt, $stock );
+               print "</table>\n";
                print "<p><input type='submit' name='submit' value='Submit'>\n";
+               print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
                print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
        } else {
                print "<p>No pending items.\n";
@@ -123,15 +222,16 @@ sub submitAdminForm( $$$$ ) {
        my $authid = $auth->{'authid'};
        if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
                my( %deleted, %approved );
+               my $hasJscript = getFormValue( 'jscript-active', '0' );
+               my $ans;
+               $ans = loadStock() unless $hasJscript;#If there is jscript, no need to translate
                my $maxcnt = getFormValue( 'max-cnt', 0 );
                my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
                $errors = '';
-               # Scan for approved and deleted items
+               # Scan for deleted histories
                for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
                        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+)$/;
@@ -148,19 +248,14 @@ sub submitAdminForm( $$$$ ) {
                                delete $approved{$id};
                        }
                }
-               #Do the deletes and approves
+               #Do the deletes
+               my %modified;
                foreach my $del ( keys %deleted ) {
                        $tables->deleteHistory( $del );
-                       #TODO notify
+                       $modified{getFormValue( "owner-$del", '' )} = 1;
                        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 $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) );
                        next if $collision{$addr->get()};
@@ -168,7 +263,6 @@ sub submitAdminForm( $$$$ ) {
                        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;
                        }
@@ -177,6 +271,10 @@ sub submitAdminForm( $$$$ ) {
                        my $note = getFormValue( "note-$i", undef );
                        $note = undef if defined $note && $note eq '';
                        my $discussion = getFormValue( "disc-$i", '' );
+                       unless( $hasJscript ) {#Modified by the stock answers, is no jscript at user
+                               my $index = getFormValue( "ans-$i", '0' );
+                               $discussion = $ans->[$index-1]->[1] if $index;
+                       }
                        $discussion = undef if defined $discussion && $discussion eq '';
                        my $delete = 0;
                        if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
@@ -188,28 +286,29 @@ sub submitAdminForm( $$$$ ) {
                                appendError( "You must specify name if you set note at item ".$addr->pretty() );
                                next;
                        }
+                       my $action = $modified{$addr->get()};
+                       $action = 1 if getFormValue( "loc-$i-sel", '' ) eq 'seen';
                        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 );
                                $select = $histId if defined $name || $delete;
                                tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
                                $action = 1;
-                               #TODO notify
+                               notify( $tables, $addr->get(), $histId, defined $name ? 1 : 0, 1 );
                        }
                        if( defined $select && select ne '' ) {
                                $tables->setMainHistory( $addr->get(), $select );
                                tulog( $authid, "Item main history changed ".$addr->get()." $select" );
                                $action = 1;
-                               #TODO Notify
+                               notify( $tables, $addr->get(), $select, 2, 2 );
                        }
-                       if( $action && $defaultSeen ) {#Approve anything in this item
+                       if( $action ) {#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};
+                                       next if $deleted{$id};
                                        $tables->markChecked( $id );
                                        tulog( $authid, "Discussion checked $id" );
                                }