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