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;
26 use Apache2::Const qw(:common :http);
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'} );
37 foreach( @{$notifs} ) {
38 my( $location, $recursive ) = @{$_};
39 if( ( substr( $uri, 0, length $location ) eq $location ) && $recursive && ( length $location < length $uri ) ) {
41 print "<div class='navigation'><h2>Item already covered by</h2><ul>\n";
44 print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
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";
53 genRadios( [ [ 'None', '3' ], [ 'Main comment & new sub-item', '2' ], [ 'Suggestion', '1' ], [ 'Discussion', '0' ] ], 'notification', ( defined $data->{'notification'} ) ? $data->{'notification'} : '3' );
54 print "<h2>Notification way</h2>\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";
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";
65 print "</ul></div>\n";
67 genHtmlFooter( 1, $req, $args );
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() ) );
76 return notLoggedComplaint( $req, $args, $auth );
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 );
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 ); },
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 );
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" );