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::Notifications;
22 use PciIds::Html::Util;
23 use PciIds::Html::Forms;
24 use PciIds::Html::Users;
27 use Apache2::Const qw(:common :http);
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'} );
38 foreach( @{$notifs} ) {
39 my( $location, $recursive ) = @{$_};
40 if( ( substr( $uri, 0, length $location ) eq $location ) && $recursive && ( length $location < length $uri ) ) {
42 print "<div class='navigation'><h2>Item already covered by</h2><ul>\n";
45 print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
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";
54 genRadios( [ [ 'None', '3' ], [ 'Main comment & new sub-item', '2' ], [ 'Suggestion', '1' ], [ 'Discussion', '0' ] ], 'notification', ( defined $data->{'notification'} ) ? $data->{'notification'} : '3' );
55 print "<h2>Notification way</h2>\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";
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";
66 print "</ul></div>\n";
68 genHtmlFooter( 1, $req, $args );
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() ) );
78 return notLoggedComplaint( $req, $args, $auth );
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 );
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 ); },
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 );
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" );
107 checkConf( [ 'default_uri' ] );