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