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 "<h1>".$addr->pretty()." - notifications</h1>\n";
15 print "<div class='error'>$error</div>\n" if( defined $error );
16 my $uri = $addr->get();
17 my $notifs = $tables->notificationsUser( $auth->{'authid'} );
19 foreach( @{$notifs} ) {
20 my( $location, $recursive ) = @{$_};
21 if( ( substr( $uri, 0, length $location ) eq $location ) && $recursive && ( length $location < length $uri ) ) {
23 print "<div class='navigation'><h2>Item already covered by</h2><ul>\n";
26 print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
29 print "</ul></div>\n" if( $started );
30 print "<form name='notifications' id='notifications' method='POST' action=''>\n";
31 print "<p><input type='checkbox' value='recursive' name='recursive'".( $data->{'recursive'} ? " checked='checked'" : "" )."> Recursive\n";
32 print "<h3>Notification level</h3>\n";
34 genRadios( [ [ 'None', '3' ], [ 'Main comment & new subitem', '2' ], [ 'Description', '1' ], [ 'Comment', '0' ] ], 'notification', ( defined $data->{'notification'} ) ? $data->{'notification'} : '3' );
35 print "<h3>Notification way</h3>\n";
37 genRadios( [ [ 'Email', '0' ], [ 'Xmpp', '1' ], [ 'Both', '2' ] ], 'way', ( defined $data->{'way'} ) ? $data->{'way'} : '0' );
38 print "<p><input type='submit' value='Submit' name='submit'>\n";
41 print "<div class='navigation'><h3>All notifications</h3><ul>\n";
42 foreach( @{$notifs} ) {
43 my( $location ) = @{$_};
44 print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
46 print "</ul></div>\n";
48 print "<a class='navigation' href='".setAddrPrefix( $req->uri(), 'read' ).buildExcept( 'action', $args )."?action=list'>Back to browsing</a>\n";
53 sub notifForm( $$$$ ) {
54 my( $req, $args, $tables, $auth ) = @_;
55 if( defined $auth->{'authid'} ) {
56 return genNotifForm( $req, $args, $tables, $auth, undef, $tables->getNotifData( $auth->{'authid'}, PciIds::Address::new( $req->uri() )->get() ) );
58 return notLoggedComplaint( $req, $args, $auth );
63 my( $value, $name, $max ) = @_;
64 return ( "Invalid number in $name", 0 ) if $value !~ /\d+/;
65 return ( "Invalid range in $name", 0 ) if ( $value < 0 ) || ( $value > $max );
69 sub notifFormSubmit( $$$$ ) {
70 my( $req, $args, $tables, $auth ) = @_;
71 return notLoggedComplaint( $req, $args, $auth ) unless defined $auth->{'authid'};
72 my( $data, $error ) = getForm( {
73 'notification' => sub { return range( shift, "notification", 3 ); },
74 'way' => sub { return range( shift, "way", 2 ); },
77 return ( undef, 1 ) if ( defined $value ) && ( $value eq 'recursive' );
78 return ( undef, 0 ) if ( !defined $value ) || ( $value eq '' );
79 return ( 'Invalid value in recursive', 0 );
82 return genNotifForm( $req, $args, $tables, $auth, $error, $data ) if defined $error;
83 $tables->submitNotification( $auth->{'authid'}, PciIds::Address::new( $req->uri() )->get(), $data );
84 return HTTPRedirect( $req, setAddrPrefix( $req->uri(), 'read' ).buildExcept( 'action', $args )."?action=list" );