]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Notifications.pm
32cbbed68218a6717ba3c65733dd03c43bf246dc
[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         genCustomHead( $req, $args, $addr, $addr->pretty()." - notifications", [ $addr->canAddItem() ? [ 'New item', 'newitem' ] : (), $addr->canDiscuss ? [ 'Discuss', 'newhistory' ] : (), [ 'Help', 'help', 'notifications' ], [ '', 'jump' ] ], [ logItem( $auth ), [ 'Profile', 'profile' ] ] );
15         print "<div class='error'>$error</div>\n" if( defined $error );
16         my $uri = $addr->get();
17         my $notifs = $tables->notificationsUser( $auth->{'authid'} );
18         my $started;
19         foreach( @{$notifs} ) {
20                 my( $location, $recursive ) = @{$_};
21                 if( ( substr( $uri, 0, length $location ) eq $location ) && $recursive && ( length $location < length $uri ) ) {
22                         unless( $started ) {
23                                 print "<div class='navigation'><h2>Item already covered by</h2><ul>\n";
24                                 $started = 1;
25                         }
26                         print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
27                 }
28         }
29         print "</ul></div>\n" if( $started );
30         print "<form name='notifications' id='notifications' method='POST' action=''>\n";
31         print "<h2>Effect range</h2>\n";
32         print "<p><input type='checkbox' value='recursive' name='recursive'".( $data->{'recursive'} ? " checked='checked'" : "" )."> Recursive\n";
33         print "<h2>Notification level</h2>\n";
34         print "<p>\n";
35         genRadios( [ [ 'None', '3' ], [ 'Main comment &amp; new sub-item', '2' ], [ 'Suggestion', '1' ], [ 'Discussion', '0' ] ], 'notification', ( defined $data->{'notification'} ) ? $data->{'notification'} : '3' );
36         print "<h2>Notification way</h2>\n";
37         print "<p>\n";
38         genRadios( [ [ 'Email', '0' ], [ 'Xmpp', '1' ], [ 'Both', '2' ] ], 'way', ( defined $data->{'way'} ) ? $data->{'way'} : '0' );
39         print "<p><input type='submit' value='Submit' name='submit'>\n";
40         print "</form>\n";
41         if( @{$notifs} ) {
42                 print "<div class='navigation'><h2>All notifications</h2><ul>\n";
43                 foreach( @{$notifs} ) {
44                         my( $location ) = @{$_};
45                         print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
46                 }
47                 print "</ul></div>\n";
48         }
49         genHtmlTail();
50         return OK;
51 }
52
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() ) );
57         } else {
58                 return notLoggedComplaint( $req, $args, $auth );
59         }
60 }
61
62 sub range( $$$ ) {
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 );
66         return undef;
67 }
68
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 ); },
75                 'recursive' => sub {
76                         my $value = shift;
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 );
80                 }
81         }, [] );
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" );
85 }
86
87 1;