]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Notifications.pm
Tooltips with name in admin interface
[pciids.git] / PciIds / Html / Notifications.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::Notifications;
20 use strict;
21 use warnings;
22 use PciIds::Html::Util;
23 use PciIds::Html::Forms;
24 use PciIds::Html::Users;
25 use PciIds::Address;
26 use PciIds::Config;
27 use Apache2::Const qw(:common :http);
28
29 sub genNotifForm( $$$$$$ ) {
30         my( $req, $args, $tables, $auth, $error, $data ) = @_;
31         my $addr = PciIds::Address::new( $req->uri() );
32         genHtmlHead( $req, 'Notifications', undef );
33         genCustomHead( $req, $args, $addr, "Notifications", [ $addr->canAddItem() ? [ 'New item', 'newitem' ] : (), $addr->canDiscuss ? [ 'Discuss', 'newhistory' ] : (), [ 'Help', 'help', 'notifications' ] ], [ logItem( $auth ), [ 'Profile', 'profile' ] ] );
34         print "<div class='error'>$error</div>\n" if( defined $error );
35         my $uri = $addr->get();
36         my $notifs = $tables->notificationsUser( $auth->{'authid'} );
37         my $started;
38         foreach( @{$notifs} ) {
39                 my( $location, $recursive ) = @{$_};
40                 if( ( substr( $uri, 0, length $location ) eq $location ) && $recursive && ( length $location < length $uri ) ) {
41                         unless( $started ) {
42                                 print "<div class='navigation'><h2>Item already covered by</h2><ul>\n";
43                                 $started = 1;
44                         }
45                         print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
46                 }
47         }
48         print "</ul></div>\n" if( $started );
49         print "<form name='notifications' id='notifications' method='POST' action=''>\n";
50         print "<h2>Effect range</h2>\n";
51         print "<p><input type='checkbox' value='recursive' name='recursive'".( $data->{'recursive'} ? " checked='checked'" : "" )."> Recursive\n";
52         print "<h2>Notification level</h2>\n";
53         print "<p>\n";
54         genRadios( [ [ 'None', '3' ], [ 'Main comment &amp; new sub-item', '2' ], [ 'Suggestion', '1' ], [ 'Discussion', '0' ] ], 'notification', ( defined $data->{'notification'} ) ? $data->{'notification'} : '3' );
55         print "<h2>Notification way</h2>\n";
56         print "<p>\n";
57         genRadios( [ [ 'Email', '0' ], [ 'Xmpp', '1' ], [ 'Both', '2' ] ], 'way', ( defined $data->{'way'} ) ? $data->{'way'} : '0' );
58         print "<p><input type='submit' value='Submit' name='submit'>\n";
59         print "</form>\n";
60         if( @{$notifs} ) {
61                 print "<div class='navigation'><h2>All notifications</h2><ul>\n";
62                 foreach( @{$notifs} ) {
63                         my( $location ) = @{$_};
64                         print "<li><a href='/mods/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
65                 }
66                 print "</ul></div>\n";
67         }
68         genHtmlFooter( 1, $req, $args );
69         return OK;
70 }
71
72 sub notifForm( $$$$ ) {
73         my( $req, $args, $tables, $auth ) = @_;
74         return HTTPRedirect( $req, '/mods/'.$config{'default_uri'}.'?action=notifications' ) if $req->uri() =~ /^(\/(read|mods))?\/?$/;
75         if( defined $auth->{'authid'} ) {
76                 return genNotifForm( $req, $args, $tables, $auth, undef, $tables->getNotifData( $auth->{'authid'}, PciIds::Address::new( $req->uri() )->get() ) );
77         } else {
78                 return notLoggedComplaint( $req, $args, $auth );
79         }
80 }
81
82 sub range( $$$ ) {
83         my( $value, $name, $max ) = @_;
84         return ( "Invalid number in $name", 0 ) if $value !~ /\d+/;
85         return ( "Invalid range in $name", 0 ) if ( $value < 0 ) || ( $value > $max );
86         return undef;
87 }
88
89 sub notifFormSubmit( $$$$ ) {
90         my( $req, $args, $tables, $auth ) = @_;
91         return notLoggedComplaint( $req, $args, $auth ) unless defined $auth->{'authid'};
92         my( $data, $error ) = getForm( {
93                 'notification' => sub { return range( shift, "notification", 3 ); },
94                 'way' => sub { return range( shift, "way", 2 ); },
95                 'recursive' => sub {
96                         my $value = shift;
97                         return ( undef, 1 ) if ( defined $value ) && ( $value eq 'recursive' );
98                         return ( undef, 0 ) if ( !defined $value ) || ( $value eq '' );
99                         return ( 'Invalid value in recursive', 0 );
100                 }
101         }, [] );
102         return genNotifForm( $req, $args, $tables, $auth, $error, $data ) if defined $error;
103         $tables->submitNotification( $auth->{'authid'}, PciIds::Address::new( $req->uri() )->get(), $data );
104         return HTTPRedirect( $req, setAddrPrefix( $req->uri(), 'read' ).buildExcept( 'action', $args )."?action=list" );
105 }
106
107 checkConf( [ 'default_uri' ] );
108
109 1;