2 # Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License as published by
6 # he Free Software Foundation; either version 2 of the License, or
7 # (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 package PciIds::Html::Admin;
23 use PciIds::Html::Util;
24 use PciIds::Html::Users;
25 use PciIds::Html::Forms;
26 use PciIds::Notifications;
30 use Apache2::Const qw(:common :http);
34 return '' unless defined $text;
35 $text = encode( $text );
40 sub mailEncode( $$ ) {
41 my( $email, $user ) = @_;
42 return '' unless defined $email;
43 ( $user ) = $email =~ /^(.*)@/ unless( defined $user );
44 return "<a href='mailto:$email'>".encode( $user )."</a>";
47 sub genHist( $$$$$$$$$$$ ) {
48 my( $class, $email, $login, $time, $name, $note, $disc, $selname, $selvalue, $delname, $delvalue ) = @_;
49 print "<tr class='$class'><td>";
50 print "<span class='author'>".mailEncode( $email, $login )."<br></span>" if( defined $email );
51 print "<span class='time'>".safeEncode( $time )."</span>";
55 print "<span class='name'>Deletion request<br></span>";
57 print "<span class='name'>Name: ".encode( $name )."<br></span>";
60 print "<span class='note'>Note: ".encode( $note )."<br></span>" if defined $note && $note ne '';
61 print safeEncode( $disc );
62 print "<td class='selects'><input type='radio' name='$selname' value='$selvalue'>\n";
63 print "<td class='deletes'><input type='checkbox' name='$delname' value='$delvalue'>\n" if defined $delname
66 sub genNewForm( $$ ) {
67 my( $num, $values ) = @_;
68 print "<tr class='newhistory'><td>";
70 print "<select id='ans-$num' name='ans-$num' onchange='answer( \"$num\" );'><option value='0'>Stock answers</option>";
72 foreach( @{$values} ) {
75 print "<option value='$i'>$name</option>";
79 print "No stock answers";
81 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";
82 print "<textarea id='disc-$num' name='disc-$num'></textarea>\n";
83 print "<td><td class='deletes'><input type='checkbox' name='loc-$num-softdel' value='del'>\n";
88 if( open ANS, "$directory/cf/answers" ) {
89 my( $name, $text, $lines );
90 while( defined( $name = <ANS> ) ) {
92 while( defined( $text = <ANS> ) ) {
95 $lines .= "\n" if $lines ne '';
98 push @stock, [ encode( $name ), $lines ];
102 print STDERR "Could not find stock answers.\n";
107 sub genNewAdminForm( $$$$$ ) {
108 my( $req, $args, $tables, $error, $auth ) = @_;
109 my $address = PciIds::Address::new( $req->uri() );
110 my $prefix = $address->get();
111 my $limit = delete $args->{'limit'};
112 $prefix = '' if( $args->{'global'} );
113 my $caption = 'Administration '.( $args->{'global'} ? '(Global)' : '('.encode( $address->pretty() ).')' );
114 genHtmlHead( $req, $caption, undef );
115 my $glob = delete $args->{'global'};
116 my $moreButt = 'admin?limit='.( $limit ? int( $limit * 2 ) : 160 ).buildExcept( 'action', $args );
117 $moreButt .= '?global='.$glob if defined $glob;
118 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' ] ], [ [ 'Log out', 'logout' ] ] );
119 print "<div class='error'>$error</div>\n" if( defined $error );
120 print "<form name='admin' id='admin' class='admin' method='POST' action=''>\n";
126 my $stock = loadStock();
127 print "<input type='hidden' name='jscript-active' id='jscript-active' value='0'>";#Trick to find out if user has JScript
128 print "<script type='text/javascript'><!--\n document.getElementById(\"jscript-active\").value = \"1\"; \n";
130 print "function answers() {\nreturn new Array( \"\" ";
131 foreach( @{$stock} ) {
132 my( $x, $text ) = @{$_};
135 print ', "'.$text.'"';
140 function answer( id ) {
143 index = document.getElementById( "ans-" + id ).value;
144 document.getElementById( "disc-" + id ).value = ans[index];
147 print "// --></script>";
148 foreach( @{$tables->adminDump( $prefix, $limit )} ) {
149 my( $locId, $actName, $actNote, $actHist, $actEmail, $actLogin, $actDisc, $actTime,
150 $hist, $disc, $name, $note, $email, $login, $time ) = @{$_};
151 if( !defined( $lastId ) || ( $lastId ne $locId ) ) {
152 last if( $hiscnt > ( defined $limit ? $limit : 80 ) );
155 genNewForm( $cnt, $stock );
160 my $addr = PciIds::Address::new( $locId );
161 if( defined( $actHist ) ) {
162 print "<table class='item'>\n";
164 print "<table class='unnamedItem'>\n";
166 print "<col class='author'><col class='main'><col class='controls' span='2'>\n";
167 print "<tr class='label'><p>\n";
168 print "<td class='path' colspan='2'>";
169 genPathBare( $req, $addr, 1, 0, $tables );
170 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
173 print "<td class='selects'><input type='radio' name='loc-$cnt-sel' value='curr' checked='checked'>";
174 print "<td class='deletes'><input type='checkbox' name='loc-$cnt-del' value='del'>" if hasRight( $auth->{'accrights'}, 'prune' ) || ( !defined $actHist && !$tables->hasChildren( $addr->get() ) );
175 print "<input type='hidden' name='loc-$cnt' value='$locId'>";
176 if( defined $actHist ) {
177 genHist( 'main-history', $actEmail, $actLogin, $actTime, $actName, $actNote, $actDisc, "loc-$cnt-sel", 'seen', undef, undef );
179 print "<tr class='main-history'><td class='empty' colspan='2'><td><input type='radio' name='loc-$cnt-sel' value='seen'>";
184 genHist( 'unseen-history', $email, $login, $time, $name, $note, $disc, "loc-$cnt-sel", $hist, "del-$hiscnt", "del-$hist" );
185 print "<input type='hidden' name='owner-$hist' value='$locId'>";
186 print "<input type='hidden' name='his-$cnt-$subcnt' value='$hist'>";
188 print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
190 genNewForm( $cnt, $stock );
192 print "<p><input type='submit' name='submit' value='Submit'>\n";
193 print "<input type='hidden' name='loc-$cnt-subcnt' value='$subcnt'>" if( $subcnt );
194 print "<input type='hidden' name='max-cnt' value='$cnt'><input type='hidden' name='max-hiscnt' value='$hiscnt'>\n";
196 print "<p>No pending items.\n";
199 genHtmlFooter( 1, $req, $args );
203 sub adminForm( $$$$ ) {
204 my( $req, $args, $tables, $auth ) = @_;
205 if( defined( $auth->{'authid'} ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
206 return genNewAdminForm( $req, $args, $tables, undef, $auth );
208 return notLoggedComplaint( $req, $args, $auth );
214 sub appendError( $ ) {
215 if( $errors eq '' ) {
216 $errors = "<p>".shift;
218 $errors .= "<br>".shift;
222 sub submitAdminForm( $$$$ ) {
223 my( $req, $args, $tables, $auth ) = @_;
224 my $authid = $auth->{'authid'};
225 if( defined( $authid ) && hasRight( $auth->{'accrights'}, 'validate' ) ) {
226 my( %deleted, %approved );
227 my $hasJscript = getFormValue( 'jscript-active', '0' );
229 $ans = loadStock() unless $hasJscript;#If there is jscript, no need to translate
230 my $maxcnt = getFormValue( 'max-cnt', 0 );
231 my $maxhiscnt = getFormValue( 'max-hiscnt', 0 );
233 # Scan for deleted histories
234 for( my $i = 1; $i <= $maxhiscnt; $i ++ ) {
235 my( $del ) = getFormValue( "del-$i", '' ) =~ /^del-(\d+)$/;
236 $deleted{$del} = 1 if( defined $del && $del ne '' );
238 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
239 my( $sel ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
240 $approved{$sel} = 1 if( defined $sel && $sel ne '' );
242 # Check for collisions
244 foreach my $id ( keys %deleted ) {
245 if( $approved{$id} ) {
246 my $owner = getFormValue( "owner-$id", '' );
247 appendError( "You can not approve and delete history at the same time, not modifying item ".PciIds::Address::new( $owner )->pretty() );
248 $collision{$owner} = $_;
249 delete $deleted{$id};
250 delete $approved{$id};
255 foreach my $del ( keys %deleted ) {
256 $tables->deleteHistory( $del );
257 $modified{getFormValue( "owner-$del", '' )} = 1;
258 tulog( $authid, "Discussion deleted $del" );
261 for( my $i = 1; $i <= $maxcnt; $i ++ ) {
262 my $addr = PciIds::Address::new( getFormValue( "loc-$i", '' ) );
263 next if $collision{$addr->get()};
264 next unless defined $addr;
265 my $del = getFormValue( "loc-$i-del", '' );
266 if( defined $del && $del eq 'del' && ( hasRight( $auth->{'accrights'}, 'prune' ) || ( !$tables->hasChildren( $addr->get() ) && !$tables->hasMain( $addr->get() ) ) ) ) {
267 $tables->deleteItem( $addr->get() );
268 tulog( $authid, "Item deleted (recursive) ".$addr->get() );
271 my $name = getFormValue( "name-$i", undef );
272 $name = undef if defined $name && $name eq '';
273 my $note = getFormValue( "note-$i", undef );
274 $note = undef if defined $note && $note eq '';
275 my $discussion = getFormValue( "disc-$i", '' );
276 unless( $hasJscript ) {#Modified by the stock answers, is no jscript at user
277 my $index = getFormValue( "ans-$i", '0' );
278 $discussion = $ans->[$index-1]->[1] if $index;
280 $discussion = undef if defined $discussion && $discussion eq '';
282 if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
287 if( defined $note && !defined $name ) {
288 appendError( "You must specify name if you set note at item ".$addr->pretty() );
291 my $action = $modified{$addr->get()};
292 $action = 1 if getFormValue( "loc-$i-sel", '' ) eq 'seen';
293 my( $select ) = getFormValue( "loc-$i-sel", '' ) =~ /^(\d+)$/;
294 if( defined $name || defined $discussion || $delete ) {
295 my $histId = $tables->submitHistory( { 'name' => $name, 'note' => $note, 'text' => $discussion, 'delete' => $delete }, $auth, $addr );
296 $tables->markChecked( $histId );
297 $select = $histId if defined $name || $delete;
298 tulog( $authid, "Discussion submited (admin) $histId ".$addr->get()." ".logEscape( $name )." ".logEscape( $note )." ".logEscape( $discussion ) );
300 notify( $tables, $addr->get(), $histId, defined $name ? 1 : 0, 1 );
302 if( defined $select && select ne '' ) {
303 $tables->setMainHistory( $addr->get(), $select );
304 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
306 notify( $tables, $addr->get(), $select, 2, 2 );
308 if( $action ) {#Approve anything in this item
309 my $subcnt = getFormValue( "loc-$i-subcnt", 0 );
310 for( my $j = 1; $j <= $subcnt; $j ++ ) {
311 my( $id ) = getFormValue( "his-$i-$j", '' ) =~ /^(\d+)$/;
312 next unless defined $id;
313 next if $deleted{$id};
314 $tables->markChecked( $id );
315 tulog( $authid, "Discussion checked $id" );
319 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
321 return notLoggedComplaint( $req, $args, $auth );