]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Admin.pm
Tooltips with name in admin interface
[pciids.git] / PciIds / Html / Admin.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
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.
8 #
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
12 #
13 #       GNU General Public License for more details.
14 #
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
18
19 package PciIds::Html::Admin;
20 use strict;
21 use warnings;
22 use PciIds::Users;
23 use PciIds::Html::Util;
24 use PciIds::Html::Users;
25 use PciIds::Html::Forms;
26 use PciIds::Notifications;
27 use PciIds::Address;
28 use PciIds::Log;
29 use PciIds::Startup;
30 use Apache2::Const qw(:common :http);
31
32 sub safeEncode( $ ) {
33         my( $text ) = @_;
34         return '' unless defined $text;
35         $text = encode( $text );
36         $text =~ s/\n/<br>/s;
37         return $text;
38 }
39
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>";
45 }
46
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>";
52         print "<td>";
53         if( defined $name ) {
54                 if( $name eq '' ) {
55                         print "<span class='name'>Deletion request<br></span>";
56                 } else {
57                         print "<span class='name'>Name: ".encode( $name )."<br></span>";
58                 }
59         }
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
64 }
65
66 sub genNewForm( $$ ) {
67         my( $num, $values ) = @_;
68         print "<tr class='newhistory'><td>";
69         if( @{$values} ) {
70                 print "<select id='ans-$num' name='ans-$num' onchange='answer( \"$num\" );'><option value='0'>Stock answers</option>";
71                 my $i = 0;
72                 foreach( @{$values} ) {
73                         $i ++;
74                         my( $name ) = @{$_};
75                         print "<option value='$i'>$name</option>";
76                 }
77                 print "</select>\n";
78         } else {
79                 print "No stock answers";
80         }
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";
84 }
85
86 sub loadStock() {
87         my @stock;
88         if( open ANS, "$directory/cf/answers" ) {
89                 my( $name, $text, $lines );
90                 while( defined( $name = <ANS> ) ) {
91                         $lines = '';
92                         while( defined( $text = <ANS> ) ) {
93                                 chomp $text;
94                                 last if $text eq '';
95                                 $lines .= "\n" if $lines ne '';
96                                 $lines .= $text;
97                         }
98                         push @stock, [ encode( $name ), $lines ];
99                 }
100                 close ANS;
101         } else {
102                 print STDERR "Could not find stock answers.\n";
103         }
104         return \@stock;
105 }
106
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";
121         my $lastId;
122         my $started = 0;
123         my $cnt = 0;
124         my $hiscnt = 0;
125         my $subcnt;
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";
129         if( @{$stock} ) {
130                 print "function answers() {\nreturn new Array( \"\" ";
131                 foreach( @{$stock} ) {
132                         my( $x, $text ) = @{$_};
133                         $text =~ s/"/\\"/g;
134                         $text =~ s/\n/\\n/g;
135                         print ', "'.$text.'"';
136                 }
137                 print ");\n}\n";
138         }
139         print '
140 function answer( id ) {
141         var ans, index;
142         ans = answers();
143         index = document.getElementById( "ans-" + id ).value;
144         document.getElementById( "disc-" + id ).value = ans[index];
145 }
146 ';
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 ) );
153                         $lastId = $locId;
154                         if( $started ) {
155                                 genNewForm( $cnt, $stock );
156                                 print "</table>\n";
157                         } else {
158                                 $started = 1;
159                         }
160                         my $addr = PciIds::Address::new( $locId );
161                         if( defined( $actHist ) ) {
162                                 print "<table class='item'>\n";
163                         } else {
164                                 print "<table class='unnamedItem'>\n";
165                         }
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 );
171                         $subcnt = 0;
172                         $cnt ++;
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 );
178                         } else {
179                                 print "<tr class='main-history'><td class='empty' colspan='2'><td><input type='radio' name='loc-$cnt-sel' value='seen'>";
180                         }
181                 }
182                 $hiscnt ++;
183                 $subcnt ++;
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'>";
187         }
188         print "<input type='hidden' name='subcnt-$cnt' value='$subcnt'>\n" if( defined( $subcnt ) );
189         if( $started ) {
190                 genNewForm( $cnt, $stock );
191                 print "</table>\n";
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";
195         } else {
196                 print "<p>No pending items.\n";
197         }
198         print "</form>\n";
199         genHtmlFooter( 1, $req, $args );
200         return OK;
201 }
202
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 );
207         } else {
208                 return notLoggedComplaint( $req, $args, $auth );
209         }
210 }
211
212 my $errors;
213
214 sub appendError( $ ) {
215         if( $errors eq '' ) {
216                 $errors = "<p>".shift;
217         } else {
218                 $errors .= "<br>".shift;
219         }
220 }
221
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' );
228                 my $ans;
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 );
232                 $errors = '';
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 '' );
237                 }
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 '' );
241                 }
242                 # Check for collisions
243                 my %collision;
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};
251                         }
252                 }
253                 #Do the deletes
254                 my %modified;
255                 foreach my $del ( keys %deleted ) {
256                         $tables->deleteHistory( $del );
257                         $modified{getFormValue( "owner-$del", '' )} = 1;
258                         tulog( $authid, "Discussion deleted $del" );
259                 }
260                 #Handle the items
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() );
269                                 next;
270                         }
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;
279                         }
280                         $discussion = undef if defined $discussion && $discussion eq '';
281                         my $delete = 0;
282                         if( getFormValue( "loc-$i-softdel", '' ) =~ /^del$/ ) {
283                                 $delete = 1;
284                                 $name = undef;
285                                 $note = undef;
286                         }
287                         if( defined $note && !defined $name ) {
288                                 appendError( "You must specify name if you set note at item ".$addr->pretty() );
289                                 next;
290                         }
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 ) );
299                                 $action = 1;
300                                 notify( $tables, $addr->get(), $histId, defined $name ? 1 : 0, 1 );
301                         }
302                         if( defined $select && select ne '' ) {
303                                 $tables->setMainHistory( $addr->get(), $select );
304                                 tulog( $authid, "Item main history changed ".$addr->get()." $select" );
305                                 $action = 1;
306                                 notify( $tables, $addr->get(), $select, 2, 2 );
307                         }
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" );
316                                 }
317                         }
318                 }
319                 return genNewAdminForm( $req, $args, $tables, $errors, $auth );
320         } else {
321                 return notLoggedComplaint( $req, $args, $auth );
322         }
323 }
324
325 1;