]> mj.ucw.cz Git - pciids.git/blob - PciIds/Html/Notifications.pm
Place menu on the sides of title
[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 "<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'} );
21         my $started;
22         foreach( @{$notifs} ) {
23                 my( $location, $recursive ) = @{$_};
24                 if( ( substr( $uri, 0, length $location ) eq $location ) && $recursive && ( length $location < length $uri ) ) {
25                         unless( $started ) {
26                                 print "<div class='navigation'><h2>Item already covered by</h2><ul>\n";
27                                 $started = 1;
28                         }
29                         print "<li><a href='/$location".buildArgs( $args )."'>".PciIds::Address::new( $location )->pretty()."</a>\n";
30                 }
31         }
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";
37         print "<p>\n";
38         genRadios( [ [ 'None', '3' ], [ 'Main comment &amp; new sub-item', '2' ], [ 'Suggestion', '1' ], [ 'Discussion', '0' ] ], 'notification', ( defined $data->{'notification'} ) ? $data->{'notification'} : '3' );
39         print "<h2>Notification way</h2>\n";
40         print "<p>\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";
43         print "</form>\n";
44         if( @{$notifs} ) {
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";
49                 }
50                 print "</ul></div>\n";
51         }
52         genHtmlTail();
53         return OK;
54 }
55
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() ) );
60         } else {
61                 return notLoggedComplaint( $req, $args, $auth );
62         }
63 }
64
65 sub range( $$$ ) {
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 );
69         return undef;
70 }
71
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 ); },
78                 'recursive' => sub {
79                         my $value = shift;
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 );
83                 }
84         }, [] );
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" );
88 }
89
90 1;