1 package PciIds::Html::Notifications;
4 use PciIds::Html::Util;
5 use PciIds::Html::Forms;
6 use PciIds::Html::Users;
8 use Apache2::Const qw(:common :http);
10 sub genNotifForm( $$$$$$ ) {
11 my( $req, $args, $tables, $auth, $error, $data ) = @_;
12 my $addr = PciIds::Address::new( $req->uri() );
13 genHtmlHead( $req, $addr->pretty().' - notifications', undef );
14 print "<div class='top'>\n";
15 print "<h1>".$addr->pretty()." - notifications</h1>\n";
16 genLocMenu( $req, $args, [ logItem( $auth ), $addr->canAddItem() ? [ 'New item', 'newitem' ] : (), $addr->canDiscuss ? [ 'Discuss', 'newhistory' ] : (), [ 'Profile', 'profile' ], [ 'Help', 'help', 'notifications' ] ] );
17 print "<div class='clear'></div></div>\n";
18 print "<div class='error'>$error</div>\n" if( defined $error );
19 my $uri = $addr->get();
20 my $notifs = $tables->notificationsUser( $auth->{'authid'} );
22 foreach( @{$notifs} ) {
23 my( $location, $recursive ) = @{$_};
24 if( ( substr( $uri, 0, length $location ) eq $location ) && $recursive && ( length $location < length $uri ) ) {
26 print "<div class='navigation'><h2>Item already covered by</h2><ul>\n";
29 print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
32 print "</ul></div>\n" if( $started );
33 print "<form name='notifications' id='notifications' method='POST' action=''>\n";
34 print "<h2>Effect range</h2>\n";
35 print "<p><input type='checkbox' value='recursive' name='recursive'".( $data->{'recursive'} ? " checked='checked'" : "" )."> Recursive\n";
36 print "<h2>Notification level</h2>\n";
38 genRadios( [ [ 'None', '3' ], [ 'Main comment & new sub-item', '2' ], [ 'Suggestion', '1' ], [ 'Discussion', '0' ] ], 'notification', ( defined $data->{'notification'} ) ? $data->{'notification'} : '3' );
39 print "<h2>Notification way</h2>\n";
41 genRadios( [ [ 'Email', '0' ], [ 'Xmpp', '1' ], [ 'Both', '2' ] ], 'way', ( defined $data->{'way'} ) ? $data->{'way'} : '0' );
42 print "<p><input type='submit' value='Submit' name='submit'>\n";
45 print "<div class='navigation'><h2>All notifications</h2><ul>\n";
46 foreach( @{$notifs} ) {
47 my( $location ) = @{$_};
48 print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
50 print "</ul></div>\n";
56 sub notifForm( $$$$ ) {
57 my( $req, $args, $tables, $auth ) = @_;
58 if( defined $auth->{'authid'} ) {
59 return genNotifForm( $req, $args, $tables, $auth, undef, $tables->getNotifData( $auth->{'authid'}, PciIds::Address::new( $req->uri() )->get() ) );
61 return notLoggedComplaint( $req, $args, $auth );
66 my( $value, $name, $max ) = @_;
67 return ( "Invalid number in $name", 0 ) if $value !~ /\d+/;
68 return ( "Invalid range in $name", 0 ) if ( $value < 0 ) || ( $value > $max );
72 sub notifFormSubmit( $$$$ ) {
73 my( $req, $args, $tables, $auth ) = @_;
74 return notLoggedComplaint( $req, $args, $auth ) unless defined $auth->{'authid'};
75 my( $data, $error ) = getForm( {
76 'notification' => sub { return range( shift, "notification", 3 ); },
77 'way' => sub { return range( shift, "way", 2 ); },
80 return ( undef, 1 ) if ( defined $value ) && ( $value eq 'recursive' );
81 return ( undef, 0 ) if ( !defined $value ) || ( $value eq '' );
82 return ( 'Invalid value in recursive', 0 );
85 return genNotifForm( $req, $args, $tables, $auth, $error, $data ) if defined $error;
86 $tables->submitNotification( $auth->{'authid'}, PciIds::Address::new( $req->uri() )->get(), $data );
87 return HTTPRedirect( $req, setAddrPrefix( $req->uri(), 'read' ).buildExcept( 'action', $args )."?action=list" );